Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vang committed Mar 23, 2022
1 parent d4f6b15 commit 1f48242
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
17 changes: 16 additions & 1 deletion scalr/resource_scalr_role.go
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"reflect"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -128,6 +129,17 @@ func resourceScalrRoleRead(d *schema.ResourceData, meta interface{}) error {
d.Set("account_id", role.Account.ID)
d.Set("is_system", role.IsSystem)

schemaPermissions := make([]string, 0)
if value, ok := d.GetOk("permissions"); ok {
permissionNames := value.([]interface{})

for _, id := range permissionNames {
schemaPermissions = append(schemaPermissions, id.(string))
}
sort.Strings(schemaPermissions)
}
log.Printf("[DEBUG] schema permissions: %+v", schemaPermissions)

remotePermissions := make([]string, 0)
if len(role.Permissions) != 0 {
for _, permission := range role.Permissions {
Expand All @@ -138,7 +150,10 @@ func resourceScalrRoleRead(d *schema.ResourceData, meta interface{}) error {

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

d.Set("permissions", remotePermissions)
// ignore permission ordering from the remote server
if !reflect.DeepEqual(remotePermissions, schemaPermissions) {
d.Set("permissions", remotePermissions)
}

return nil
}
Expand Down
7 changes: 6 additions & 1 deletion scalr/resource_scalr_role_migrate.go
Expand Up @@ -62,7 +62,12 @@ func resourceScalrRoleStateUpgradeV0(rawState map[string]interface{}, meta inter
// For some reason array should be reverse sorted to produce correct diff ¯\_(ツ)_/¯
sort.Sort(sort.Reverse(sort.StringSlice(permissions)))

rawState["permissions"] = permissions
perms := make([]interface{}, len(permissions))
for i, perm := range permissions {
perms[i] = perm
}

rawState["permissions"] = perms

return rawState, nil
}
24 changes: 12 additions & 12 deletions scalr/resource_scalr_role_migrate_test.go
Expand Up @@ -5,44 +5,44 @@ import (
)

func testResourceScalrRoleStateDataV0() map[string]interface{} {
perms := make([]interface{}, 0)
perms := []interface{}{"accounts:update", "global-scope:read"}
return map[string]interface{}{
"permissions": append(perms, "accounts:update", "global-scope:read"),
"permissions": perms,
}
}

func testResourceScalrRoleStateDataV1() map[string]interface{} {
perms := make([]interface{}, 0)
perms := []interface{}{"global-scope:read", "accounts:update", "accounts:set-quotas"}
return map[string]interface{}{
"permissions": append(perms, "accounts:update", "global-scope:read", "accounts:set-quotas"),
"permissions": perms,
}
}

func testResourceScalrRoleStateDataV0NoGlobalScope() map[string]interface{} {
perms := make([]interface{}, 0)
perms := []interface{}{"accounts:create"}
return map[string]interface{}{
"permissions": append(perms, "accounts:create"),
"permissions": perms,
}
}

func testResourceScalrRoleStateDataV1NoGlobalScope() map[string]interface{} {
perms := make([]interface{}, 0)
perms := []interface{}{"accounts:create"}
return map[string]interface{}{
"permissions": append(perms, "accounts:create"),
"permissions": perms,
}
}

func testResourceScalrRoleStateDataV0ExistingPerm() map[string]interface{} {
perms := make([]interface{}, 0)
perms := []interface{}{"accounts:set-quotas", "global-scope:read"}
return map[string]interface{}{
"permissions": append(perms, "accounts:set-quotas", "global-scope:read"),
"permissions": perms,
}
}

func testResourceScalrRoleStateDataV1ExistingPerm() map[string]interface{} {
perms := make([]interface{}, 0)
perms := []interface{}{"accounts:set-quotas", "global-scope:read"}
return map[string]interface{}{
"permissions": append(perms, "accounts:set-quotas", "global-scope:read"),
"permissions": perms,
}
}

Expand Down

0 comments on commit 1f48242

Please sign in to comment.