Skip to content

Commit

Permalink
SCALRCORE-19198 > Make account_id, environment_id and workspace_id va…
Browse files Browse the repository at this point in the history
…riables computable, fix issue with BC between rc14 and rc15
  • Loading branch information
Artem Vang committed May 21, 2021
1 parent 1f111b9 commit 236ccda
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions scalr/resource_scalr_variable.go
Expand Up @@ -32,18 +32,28 @@ func resourceScalrVariable() *schema.Resource {
},
func(d *schema.ResourceDiff, meta interface{}) error {
// Reject any changes for variable scope
var scopeAttributes = []string{"workspace_id", "environment_id", "account_id"}
var scopeAttributes = []string{"workspace_id", "account_id", "environment_id"}

notChangedFields := 0
scopeIsAlreadySet := false
for _, scope := range scopeAttributes {
old, _ := d.GetChange(scope)
if old.(string) != "" {
scopeIsAlreadySet = true
break
}
}

scopeWasChanged := false
for _, scope := range scopeAttributes {
old, new := d.GetChange(scope)

if old.(string) == "" || old.(string) == new.(string) {
notChangedFields++
if scopeIsAlreadySet && old.(string) != new.(string) {
scopeWasChanged = true
break
}
}

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

Expand Down Expand Up @@ -116,16 +126,19 @@ func resourceScalrVariable() *schema.Resource {
"workspace_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"environment_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"account_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
Expand Down Expand Up @@ -218,9 +231,13 @@ 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("workspace_id", nil)
} else if variable.Account != nil {
}

if variable.Environment != nil {
d.Set("environment_id", variable.Environment.ID)
}

if variable.Account != nil {
d.Set("account_id", variable.Account.ID)
}

Expand All @@ -234,7 +251,6 @@ func resourceScalrVariableRead(d *schema.ResourceData, meta interface{}) error {

func resourceScalrVariableUpdate(d *schema.ResourceData, meta interface{}) error {
scalrClient := meta.(*scalr.Client)

// Create a new options struct.
options := scalr.VariableUpdateOptions{
Key: scalr.String(d.Get("key").(string)),
Expand Down

0 comments on commit 236ccda

Please sign in to comment.