Skip to content

Commit

Permalink
SCALRCORE-18529 > Fix multiscope logic, add tests on update
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vang committed Apr 2, 2021
1 parent 3458e2c commit 67e24fb
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
20 changes: 13 additions & 7 deletions scalr/resource_scalr_variable.go
Expand Up @@ -31,16 +31,22 @@ func resourceScalrVariable() *schema.Resource {
return nil
},
func(d *schema.ResourceDiff, meta interface{}) error {
// Reject any changes for account_id, environment_id or workspace_id
const templateString string = "Error changing '%s' attribute for variable %s: immutable attribute"
var scope_attributes = []string{"workspace_id", "environment_id", "account_id"}
// Reject any changes for variable scope
var scopeAttributes = []string{"workspace_id", "environment_id", "account_id"}

for _, scope := range scope_attributes {
if d.HasChange(scope) {
return fmt.Errorf(templateString, scope, d.Id())
notChangedFields := 0
for _, scope := range scopeAttributes {
old, new := d.GetChange(scope)

if old.(string) == "" || old.(string) == new.(string) {
notChangedFields++
}
}

if notChangedFields < len(scopeAttributes) {
return fmt.Errorf("Error changing scope for variable %s: scope is immutable attribute", d.Id())
}

return nil
},
),
Expand Down Expand Up @@ -213,7 +219,7 @@ func resourceScalrVariableRead(d *schema.ResourceData, meta interface{}) error {
if variable.Workspace != nil {
d.Set("workspace_id", variable.Workspace.ID)
} else if variable.Environment != nil {
d.Set("environment_id", variable.Environment.ID)
d.Set("workspace_id", nil)
} else if variable.Account != nil {
d.Set("account_id", variable.Account.ID)
}
Expand Down
93 changes: 87 additions & 6 deletions scalr/resource_scalr_variable_test.go
Expand Up @@ -79,7 +79,7 @@ func TestAccScalrVariable_scopes(t *testing.T) {

func TestAccScalrVariable_notTerraformOnMultiscope(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
r, _ := regexp.Compile(errVariableMultiOnlyEnv.Error())
r := regexp.MustCompile(errVariableMultiOnlyEnv.Error())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -145,6 +145,16 @@ func TestAccScalrVariable_update(t *testing.T) {
resource.TestCheckResourceAttr(
"scalr_variable.test", "final", "true")),
},

{
Config: testAccScalrVariableOnWorkspaceScopeSensitiveUpdate(rInt),
},

{
Config: testAccScalrVariableOnWorkspaceScopeSensitiveUpdate(rInt + 1),
ExpectError: regexp.MustCompile("Error changing 'key' attribute for variable var-[a-z0-9]+: immutable for sensitive variable"),
PlanOnly: true,
},
},
})
}
Expand Down Expand Up @@ -327,7 +337,7 @@ resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
Expand All @@ -337,8 +347,27 @@ resource scalr_variable test {
key = "var_on_ws_%[1]d"
value = "test"
category = "env"
account_id = "%[2]s"
workspace_id = scalr_workspace.test.id
}`, rInt, defaultAccount)
}

func testAccScalrVariableOnWorkspaceScopeSensitive(rInt int) string {
return fmt.Sprintf(`
resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
}
resource scalr_variable test {
key = "var_on_ws_%[1]d"
value = "test"
category = "env"
sensitive = true
workspace_id = scalr_workspace.test.id
}`, rInt, defaultAccount)
}
Expand All @@ -349,7 +378,7 @@ resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
Expand All @@ -370,7 +399,7 @@ resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
Expand Down Expand Up @@ -413,7 +442,32 @@ resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
}
resource scalr_variable test {
key = "var_on_ws_updated_%[1]d"
value = "updated"
category = "terraform"
hcl = true
force = true
final = true
account_id = "%[2]s"
environment_id = scalr_environment.test.id
workspace_id = scalr_workspace.test.id
}`, rInt, defaultAccount)
}

func testAccScalrVariableOnWorkspaceScopeSensitiveUpdate(rInt int) string {
return fmt.Sprintf(`
resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
Expand All @@ -426,7 +480,34 @@ resource scalr_variable test {
hcl = true
force = true
final = true
sensitive = true
account_id = "%[2]s"
environment_id = scalr_environment.test.id
workspace_id = scalr_workspace.test.id
}`, rInt, defaultAccount)
}

func testAccScalrVariableOnWorkspaceScopeUpdateSensitive(rInt int) string {
return fmt.Sprintf(`
resource scalr_environment test {
name = "test-env-%[1]d"
account_id = "%[2]s"
}
resource scalr_workspace test {
name = "test-ws-%[1]d"
environment_id = scalr_environment.test.id
}
resource scalr_variable test {
key = "var_on_ws_updated_sensitive_%[1]d"
value = "updated"
category = "terraform"
hcl = true
force = true
final = true
account_id = "%[2]s"
sensitive = true
environment_id = scalr_environment.test.id
workspace_id = scalr_workspace.test.id
}`, rInt, defaultAccount)
Expand Down

0 comments on commit 67e24fb

Please sign in to comment.