1
1
package scaleway
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "net"
7
+ "sort"
5
8
6
9
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -32,7 +35,7 @@ func resourceScalewayRdbACL() *schema.Resource {
32
35
Description : "Instance on which the ACL is applied" ,
33
36
},
34
37
"acl_rules" : {
35
- Type : schema .TypeSet ,
38
+ Type : schema .TypeList ,
36
39
Required : true ,
37
40
Description : "List of ACL rules to apply" ,
38
41
Elem : & schema.Resource {
@@ -70,7 +73,7 @@ func resourceScalewayRdbACLCreate(ctx context.Context, d *schema.ResourceData, m
70
73
return diag .FromErr (err )
71
74
}
72
75
73
- aclRules , err := rdbACLExpand (d .Get ("acl_rules" ).(* schema. Set ))
76
+ aclRules , err := rdbACLExpand (d .Get ("acl_rules" ).([] interface {} ))
74
77
if err != nil {
75
78
return diag .FromErr (err )
76
79
}
@@ -139,7 +142,7 @@ func resourceScalewayRdbACLUpdate(ctx context.Context, d *schema.ResourceData, m
139
142
return diag .FromErr (err )
140
143
}
141
144
142
- aclRules , err := rdbACLExpand (d .Get ("acl_rules" ).(* schema. Set ))
145
+ aclRules , err := rdbACLExpand (d .Get ("acl_rules" ).([] interface {} ))
143
146
if err != nil {
144
147
return diag .FromErr (err )
145
148
}
@@ -164,7 +167,7 @@ func resourceScalewayRdbACLDelete(ctx context.Context, d *schema.ResourceData, m
164
167
return diag .FromErr (err )
165
168
}
166
169
aclRuleIPs := make ([]string , 0 )
167
- aclRules , err := rdbACLExpand (d .Get ("acl_rules" ).(* schema. Set ))
170
+ aclRules , err := rdbACLExpand (d .Get ("acl_rules" ).([] interface {} ))
168
171
if err != nil {
169
172
return diag .FromErr (err )
170
173
}
@@ -182,17 +185,21 @@ func resourceScalewayRdbACLDelete(ctx context.Context, d *schema.ResourceData, m
182
185
InstanceID : instanceID ,
183
186
ACLRuleIPs : aclRuleIPs ,
184
187
}, scw .WithContext (ctx ))
188
+ if err != nil && ! is404Error (err ) {
189
+ return diag .FromErr (err )
190
+ }
185
191
192
+ _ , err = waitForRDBInstance (ctx , rdbAPI , region , instanceID , d .Timeout (schema .TimeoutDelete ))
186
193
if err != nil && ! is404Error (err ) {
187
194
return diag .FromErr (err )
188
195
}
189
196
190
197
return nil
191
198
}
192
199
193
- func rdbACLExpand (data * schema. Set ) ([]* rdb.ACLRuleRequest , error ) {
200
+ func rdbACLExpand (data [] interface {} ) ([]* rdb.ACLRuleRequest , error ) {
194
201
var res []* rdb.ACLRuleRequest
195
- for _ , rule := range data . List () {
202
+ for _ , rule := range data {
196
203
r := rule .(map [string ]interface {})
197
204
ip , err := expandIPNet (r ["ip" ].(string ))
198
205
if err != nil {
@@ -204,6 +211,10 @@ func rdbACLExpand(data *schema.Set) ([]*rdb.ACLRuleRequest, error) {
204
211
})
205
212
}
206
213
214
+ sort .Slice (res , func (i , j int ) bool {
215
+ return bytes .Compare (res [i ].IP .IP , res [j ].IP .IP ) < 0
216
+ })
217
+
207
218
return res , nil
208
219
}
209
220
@@ -216,5 +227,12 @@ func rdbACLRulesFlatten(rules []*rdb.ACLRule) []map[string]interface{} {
216
227
}
217
228
res = append (res , r )
218
229
}
230
+
231
+ sort .Slice (res , func (i , j int ) bool {
232
+ ipI , _ , _ := net .ParseCIDR (res [i ]["ip" ].(string ))
233
+ ipJ , _ , _ := net .ParseCIDR (res [j ]["ip" ].(string ))
234
+ return bytes .Compare (ipI , ipJ ) < 0
235
+ })
236
+
219
237
return res
220
238
}
0 commit comments