1
1
package scaleway
2
2
3
3
import (
4
+ "bytes"
5
+ "fmt"
6
+
4
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5
8
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
6
9
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -21,10 +24,20 @@ func expandPermissionSetNames(rawPermissions interface{}) *[]string {
21
24
return & permissions
22
25
}
23
26
27
+ func flattenPermissionSetNames (permissions []string ) * schema.Set {
28
+ rawPermissions := []interface {}(nil )
29
+ for _ , perm := range permissions {
30
+ rawPermissions = append (rawPermissions , perm )
31
+ }
32
+ return schema .NewSet (func (i interface {}) int {
33
+ return StringHashcode (i .(string ))
34
+ }, rawPermissions )
35
+ }
36
+
24
37
func expandPolicyRuleSpecs (d interface {}) []* iam.RuleSpecs {
25
38
rules := []* iam.RuleSpecs (nil )
26
- rawRules := d .([] interface {} )
27
- for _ , rawRule := range rawRules {
39
+ rawRules := d .(* schema. Set )
40
+ for _ , rawRule := range rawRules . List () {
28
41
mapRule := rawRule .(map [string ]interface {})
29
42
rule := & iam.RuleSpecs {
30
43
PermissionSetNames : expandPermissionSetNames (mapRule ["permission_set_names" ]),
@@ -40,6 +53,32 @@ func expandPolicyRuleSpecs(d interface{}) []*iam.RuleSpecs {
40
53
return rules
41
54
}
42
55
56
+ func iamPolicyRuleHash (v interface {}) int {
57
+ var buf bytes.Buffer
58
+ m , ok := v .(map [string ]interface {})
59
+
60
+ if ! ok {
61
+ return 0
62
+ }
63
+
64
+ if orgID , hasOrgID := m ["organization_id" ]; hasOrgID && orgID != nil {
65
+ buf .WriteString (fmt .Sprintf ("%s-" , orgID .(string )))
66
+ }
67
+ if projIDs , hasProjIDs := m ["project_ids" ]; hasProjIDs && projIDs != nil {
68
+ projIDList := projIDs .([]interface {})
69
+ for _ , projID := range projIDList {
70
+ buf .WriteString (fmt .Sprintf ("%s-" , projID .(string )))
71
+ }
72
+ }
73
+ if permSet , hasPermSet := m ["permission_set_names" ]; hasPermSet {
74
+ permSetNames := permSet .(* schema.Set )
75
+ for _ , permName := range permSetNames .List () {
76
+ buf .WriteString (fmt .Sprintf ("%s-" , permName .(string )))
77
+ }
78
+ }
79
+ return StringHashcode (buf .String ())
80
+ }
81
+
43
82
func flattenPolicyRules (rules []* iam.Rule ) interface {} {
44
83
rawRules := []interface {}(nil )
45
84
for _ , rule := range rules {
@@ -53,9 +92,9 @@ func flattenPolicyRules(rules []*iam.Rule) interface{} {
53
92
rawRule ["project_ids" ] = flattenSliceString (* rule .ProjectIDs )
54
93
}
55
94
if rule .PermissionSetNames != nil {
56
- rawRule ["permission_set_names" ] = flattenSliceString (* rule .PermissionSetNames )
95
+ rawRule ["permission_set_names" ] = flattenPermissionSetNames (* rule .PermissionSetNames )
57
96
}
58
97
rawRules = append (rawRules , rawRule )
59
98
}
60
- return rawRules
99
+ return schema . NewSet ( iamPolicyRuleHash , rawRules )
61
100
}
0 commit comments