Skip to content

Commit

Permalink
SCALRCORE-19768 > Reverse sort array in state upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vang committed Mar 15, 2022
1 parent 224f348 commit b403d95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion scalr/resource_scalr_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
scalr "github.com/scalr/go-scalr"
Expand Down Expand Up @@ -48,7 +49,7 @@ func resourceScalrRole() *schema.Resource {
},

"permissions": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 128,
Expand Down Expand Up @@ -132,7 +133,9 @@ func resourceScalrRoleRead(d *schema.ResourceData, meta interface{}) error {
for _, permission := range role.Permissions {
remotePermissions = append(remotePermissions, permission.ID)
}
sort.Strings(remotePermissions)
}

log.Printf("[DEBUG] remote permissions: %+v", remotePermissions)

d.Set("permissions", remotePermissions)
Expand Down
19 changes: 15 additions & 4 deletions scalr/resource_scalr_role_migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package scalr

import (
"sort"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -28,7 +30,7 @@ func resourceScalrRoleResourceV0() *schema.Resource {
},

"permissions": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 128,
Expand All @@ -40,8 +42,7 @@ func resourceScalrRoleResourceV0() *schema.Resource {

func resourceScalrRoleStateUpgradeV0(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
permissionsSet := make(map[string]bool)
permissions := rawState["permissions"].([]interface{})
for _, perm := range permissions {
for _, perm := range rawState["permissions"].([]interface{}) {
permissionsSet[perm.(string)] = true
}

Expand All @@ -50,8 +51,18 @@ func resourceScalrRoleStateUpgradeV0(rawState map[string]interface{}, meta inter
}

if permissionsSet["global-scope:read"] && permissionsSet["accounts:update"] {
rawState["permissions"] = append(permissions, "accounts:set-quotas")
permissionsSet["accounts:set-quotas"] = true
}

permissions := make([]string, 0)
for perm := range permissionsSet {
permissions = append(permissions, perm)
}

// For some reason array should be reverse sorted to produce correct diff ¯\_(ツ)_/¯
sort.Sort(sort.Reverse(sort.StringSlice(permissions)))

rawState["permissions"] = permissions

return rawState, nil
}

0 comments on commit b403d95

Please sign in to comment.