Skip to content

Commit

Permalink
fix(iam_policy): use TypeList for rules (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute committed Aug 5, 2022
1 parent 1cdf057 commit 0c9aad0
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 508 deletions.
35 changes: 3 additions & 32 deletions scaleway/helpers_iam.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package scaleway

import (
"bytes"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
Expand Down Expand Up @@ -53,8 +50,8 @@ func flattenPermissionSetNames(permissions []string) *schema.Set {

func expandPolicyRuleSpecs(d interface{}) []*iam.RuleSpecs {
rules := []*iam.RuleSpecs(nil)
rawRules := d.(*schema.Set)
for _, rawRule := range rawRules.List() {
rawRules := d.([]interface{})
for _, rawRule := range rawRules {
mapRule := rawRule.(map[string]interface{})
rule := &iam.RuleSpecs{
PermissionSetNames: expandPermissionSetNames(mapRule["permission_set_names"]),
Expand All @@ -70,32 +67,6 @@ func expandPolicyRuleSpecs(d interface{}) []*iam.RuleSpecs {
return rules
}

func iamPolicyRuleHash(v interface{}) int {
var buf bytes.Buffer
m, ok := v.(map[string]interface{})

if !ok {
return 0
}

if orgID, hasOrgID := m["organization_id"]; hasOrgID && orgID != nil {
buf.WriteString(fmt.Sprintf("%s-", orgID.(string)))
}
if projIDs, hasProjIDs := m["project_ids"]; hasProjIDs && projIDs != nil {
projIDList := projIDs.([]interface{})
for _, projID := range projIDList {
buf.WriteString(fmt.Sprintf("%s-", projID.(string)))
}
}
if permSet, hasPermSet := m["permission_set_names"]; hasPermSet {
permSetNames := permSet.(*schema.Set)
for _, permName := range permSetNames.List() {
buf.WriteString(fmt.Sprintf("%s-", permName.(string)))
}
}
return StringHashcode(buf.String())
}

func flattenPolicyRules(rules []*iam.Rule) interface{} {
rawRules := []interface{}(nil)
for _, rule := range rules {
Expand All @@ -113,5 +84,5 @@ func flattenPolicyRules(rules []*iam.Rule) interface{} {
}
rawRules = append(rawRules, rawRule)
}
return schema.NewSet(iamPolicyRuleHash, rawRules)
return rawRules
}
3 changes: 1 addition & 2 deletions scaleway/resource_iam_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ func resourceScalewayIamPolicy() *schema.Resource {
ExactlyOneOf: []string{"user_id", "group_id", "application_id"},
},
"rule": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
Description: "Rules of the policy to create",
Set: iamPolicyRuleHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"organization_id": {
Expand Down
188 changes: 94 additions & 94 deletions scaleway/testdata/iam-policy-basic.cassette.yaml

Large diffs are not rendered by default.

0 comments on commit 0c9aad0

Please sign in to comment.