Skip to content

Commit 0c9aad0

Browse files
fix(iam_policy): use TypeList for rules (#1410)
1 parent 1cdf057 commit 0c9aad0

7 files changed

+478
-508
lines changed

scaleway/helpers_iam.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package scaleway
22

33
import (
4-
"bytes"
5-
"fmt"
6-
74
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
85
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
96
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -53,8 +50,8 @@ func flattenPermissionSetNames(permissions []string) *schema.Set {
5350

5451
func expandPolicyRuleSpecs(d interface{}) []*iam.RuleSpecs {
5552
rules := []*iam.RuleSpecs(nil)
56-
rawRules := d.(*schema.Set)
57-
for _, rawRule := range rawRules.List() {
53+
rawRules := d.([]interface{})
54+
for _, rawRule := range rawRules {
5855
mapRule := rawRule.(map[string]interface{})
5956
rule := &iam.RuleSpecs{
6057
PermissionSetNames: expandPermissionSetNames(mapRule["permission_set_names"]),
@@ -70,32 +67,6 @@ func expandPolicyRuleSpecs(d interface{}) []*iam.RuleSpecs {
7067
return rules
7168
}
7269

73-
func iamPolicyRuleHash(v interface{}) int {
74-
var buf bytes.Buffer
75-
m, ok := v.(map[string]interface{})
76-
77-
if !ok {
78-
return 0
79-
}
80-
81-
if orgID, hasOrgID := m["organization_id"]; hasOrgID && orgID != nil {
82-
buf.WriteString(fmt.Sprintf("%s-", orgID.(string)))
83-
}
84-
if projIDs, hasProjIDs := m["project_ids"]; hasProjIDs && projIDs != nil {
85-
projIDList := projIDs.([]interface{})
86-
for _, projID := range projIDList {
87-
buf.WriteString(fmt.Sprintf("%s-", projID.(string)))
88-
}
89-
}
90-
if permSet, hasPermSet := m["permission_set_names"]; hasPermSet {
91-
permSetNames := permSet.(*schema.Set)
92-
for _, permName := range permSetNames.List() {
93-
buf.WriteString(fmt.Sprintf("%s-", permName.(string)))
94-
}
95-
}
96-
return StringHashcode(buf.String())
97-
}
98-
9970
func flattenPolicyRules(rules []*iam.Rule) interface{} {
10071
rawRules := []interface{}(nil)
10172
for _, rule := range rules {
@@ -113,5 +84,5 @@ func flattenPolicyRules(rules []*iam.Rule) interface{} {
11384
}
11485
rawRules = append(rawRules, rawRule)
11586
}
116-
return schema.NewSet(iamPolicyRuleHash, rawRules)
87+
return rawRules
11788
}

scaleway/resource_iam_policy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ func resourceScalewayIamPolicy() *schema.Resource {
7878
ExactlyOneOf: []string{"user_id", "group_id", "application_id"},
7979
},
8080
"rule": {
81-
Type: schema.TypeSet,
81+
Type: schema.TypeList,
8282
Required: true,
8383
Description: "Rules of the policy to create",
84-
Set: iamPolicyRuleHash,
8584
Elem: &schema.Resource{
8685
Schema: map[string]*schema.Schema{
8786
"organization_id": {

scaleway/testdata/iam-policy-basic.cassette.yaml

Lines changed: 94 additions & 94 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)