Skip to content

Commit

Permalink
SCALRCORE-32273 Provider > Workspace resource > There is no way to al…
Browse files Browse the repository at this point in the history
…ign back Drifted resources with the terraform configuration (#387)

* SCALRCORE-32273 Rename modules

* SCALRCORE-32273 Refactor workspace resource onto Framework

* SCALRCORE-32273 Refactor state migrations

* SCALRCORE-32273 Finish state migrations

* SCALRCORE-32273 Fix linter

* SCALRCORE-32273 Add UpgradeFromSDK test

* SCALRCORE-32273 Fix vcs_repo.trigger_patterns behavior

* SCALRCORE-32273 Update docs
  • Loading branch information
petroprotsakh authored Jan 27, 2025
1 parent d98af3a commit 3099dbd
Show file tree
Hide file tree
Showing 13 changed files with 2,082 additions and 1,725 deletions.
10 changes: 5 additions & 5 deletions docs/resources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ order: 24
---
## Resource: scalr_workspace

Manage the state of workspaces in Scalr. Create, update and destroy.
Manages the state of workspaces in Scalr.

## Example Usage

Expand Down Expand Up @@ -156,15 +156,15 @@ resource "scalr_workspace" "example-b" {
* `always` - runs will be triggered automatically on every upload of the configuration version.
* `never` - configuration versions are uploaded into the workspace, but runs will not be triggered.
- `deletion_protection_enabled` (Boolean) Indicates if the workspace has the protection from an accidental state lost. If enabled and the workspace has resource, the deletion will not be allowed. Default `true`.
- `execution_mode` (String) Which execution mode to use. Valid values are `remote` and `local`. When set to `local`, the workspace will be used for state storage only. Defaults to `remote` (not set, backend default is used).
- `execution_mode` (String) Which execution mode to use. Valid values are `remote` and `local`. When set to `local`, the workspace will be used for state storage only. Defaults to `remote`.
- `force_latest_run` (Boolean) Set (true/false) to configure if latest new run will be automatically raised in priority. Default `false`.
- `hooks` (Block List) Settings for the workspaces custom hooks. (see [below for nested schema](#nestedblock--hooks))
- `iac_platform` (String) The IaC platform to use for this workspace. Valid values are `terraform` and `opentofu`. Defaults to `terraform`.
- `module_version_id` (String) The identifier of a module version in the format `modver-<RANDOM STRING>`. This attribute conflicts with `vcs_provider_id` and `vcs_repo` attributes.
- `operations` (Boolean, Deprecated) Set (true/false) to configure workspace remote execution. When `false` workspace is only used to store state. Defaults to `true`.
- `provider_configuration` (Block Set) Provider configurations used in workspace runs. (see [below for nested schema](#nestedblock--provider_configuration))
- `remote_state_consumers` (Set of String) The list of workspace identifiers that are allowed to access the state of this workspace. Use `["*"]` to share the state with all the workspaces within the environment.
- `run_operation_timeout` (Number) The number of minutes run operation can be executed before termination. Defaults to `0` (not set, backend default is used).
- `remote_state_consumers` (Set of String) The list of workspace identifiers that are allowed to access the state of this workspace. Use `["*"]` to share the state with all the workspaces within the environment (default).
- `run_operation_timeout` (Number) The number of minutes run operation can be executed before termination.
- `ssh_key_id` (String) The identifier of the SSH key to use for the workspace.
- `tag_ids` (Set of String) List of tag IDs associated with the workspace.
- `terraform_version` (String) The version of Terraform to use for this workspace. Defaults to the latest available version.
Expand All @@ -173,7 +173,7 @@ resource "scalr_workspace" "example-b" {
- `type` (String) The type of the Scalr Workspace environment, available options: `production`, `staging`, `testing`, `development`, `unmapped`.
- `var_files` (List of String) A list of paths to the `.tfvars` file(s) to be used as part of the workspace configuration.
- `vcs_provider_id` (String) ID of VCS provider - required if vcs-repo present and vice versa, in the format `vcs-<RANDOM STRING>`.
- `vcs_repo` (Block List, Max: 1) Settings for the workspace's VCS repository. (see [below for nested schema](#nestedblock--vcs_repo))
- `vcs_repo` (Block List) Settings for the workspace's VCS repository. (see [below for nested schema](#nestedblock--vcs_repo))
- `working_directory` (String) A relative path that Terraform will be run in. Defaults to the root of the repository `""`.

### Read-Only
Expand Down
36 changes: 24 additions & 12 deletions internal/provider/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,6 @@ func InterfaceArrToTagRelationArr(arr []interface{}) []*scalr.TagRelation {
return tags
}

func InterfaceArrToWorkspaceRelationArr(arr []interface{}) []*scalr.WorkspaceRelation {
relations := make([]*scalr.WorkspaceRelation, 0)
for _, id := range arr {
strID := id.(string)
if strID == "*" {
continue
}
relations = append(relations, &scalr.WorkspaceRelation{ID: strID})
}
return relations
}

func getDefaultScalrAccountID() (string, bool) {
if v := os.Getenv(defaults.CurrentAccountIDEnvVar); v != "" {
return v, true
Expand Down Expand Up @@ -150,3 +138,27 @@ func scalrAccountIDOptionalDefaultFunc() (interface{}, error) {
func ptr[T any](v T) *T {
return &v
}

// diff returns the added and removed elements between two slices.
func diff[T comparable](old, new []T) (added, removed []T) {
newSet := make(map[T]struct{}, len(new))
for _, v := range new {
newSet[v] = struct{}{}
}

oldSet := make(map[T]struct{}, len(old))
for _, v := range old {
oldSet[v] = struct{}{}
if _, found := newSet[v]; !found {
removed = append(removed, v)
}
}

for _, v := range new {
if _, found := oldSet[v]; !found {
added = append(added, v)
}
}

return
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (p *scalrProvider) Configure(ctx context.Context, req provider.ConfigureReq
func (p *scalrProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
newTagResource,
newWorkspaceResource,
}
}

Expand Down
1 change: 0 additions & 1 deletion internal/provider/provider_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func Provider(v string) *schema.Provider {
"scalr_variable": resourceScalrVariable(),
"scalr_vcs_provider": resourceScalrVcsProvider(),
"scalr_webhook": resourceScalrWebhook(),
"scalr_workspace": resourceScalrWorkspace(),
"scalr_workspace_run_schedule": resourceScalrWorkspaceRunSchedule(),
"scalr_run_schedule_rule": resourceScalrRunScheduleRule(),
"scalr_event_bridge_integration": resourceScalrEventBridgeIntegration(),
Expand Down
Loading

0 comments on commit 3099dbd

Please sign in to comment.