@@ -3,9 +3,11 @@ package scaleway
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "fmt"
6
7
"net"
7
8
"sort"
8
9
10
+ "github.com/hashicorp/terraform-plugin-log/tflog"
9
11
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -120,7 +122,17 @@ func resourceScalewayRdbACLRead(ctx context.Context, d *schema.ResourceData, met
120
122
id := newRegionalID (region , instanceID ).String ()
121
123
d .SetId (id )
122
124
_ = d .Set ("instance_id" , id )
123
- _ = d .Set ("acl_rules" , rdbACLRulesFlatten (res .Rules ))
125
+ if aclRulesRaw , ok := d .GetOk ("acl_rules" ); ok {
126
+ aclRules , mergeErrors := rdbACLRulesFlattenFromSchema (res .Rules , aclRulesRaw .([]interface {}))
127
+ if len (mergeErrors ) > 0 {
128
+ for _ , w := range mergeErrors {
129
+ tflog .Warn (ctx , fmt .Sprintf ("%s" , w ))
130
+ }
131
+ }
132
+ _ = d .Set ("acl_rules" , aclRules )
133
+ } else {
134
+ _ = d .Set ("acl_rules" , rdbACLRulesFlatten (res .Rules ))
135
+ }
124
136
125
137
return nil
126
138
}
@@ -218,6 +230,52 @@ func rdbACLExpand(data []interface{}) ([]*rdb.ACLRuleRequest, error) {
218
230
return res , nil
219
231
}
220
232
233
+ func rdbACLRulesFlattenFromSchema (rules []* rdb.ACLRule , dataFromSchema []interface {}) ([]map [string ]interface {}, []error ) {
234
+ var res []map [string ]interface {}
235
+ var errors []error
236
+ ruleMap := make (map [string ]* rdb.ACLRule )
237
+ for _ , rule := range rules {
238
+ ruleMap [rule .IP .String ()] = rule
239
+ }
240
+
241
+ ruleMapFromSchema := map [string ]struct {}{}
242
+ for _ , ruleFromSchema := range dataFromSchema {
243
+ currentRule := ruleFromSchema .(map [string ]interface {})
244
+ ip , err := expandIPNet (currentRule ["ip" ].(string ))
245
+ if err != nil {
246
+ errors = append (errors , err )
247
+ }
248
+
249
+ aclRule := ruleMap [ip .String ()]
250
+ ruleMapFromSchema [ip .String ()] = struct {}{}
251
+ r := map [string ]interface {}{
252
+ "ip" : aclRule .IP .String (),
253
+ "description" : aclRule .Description ,
254
+ }
255
+ res = append (res , r )
256
+ }
257
+
258
+ return append (res , mergeDiffToSchema (ruleMapFromSchema , ruleMap )... ), errors
259
+ }
260
+
261
+ func mergeDiffToSchema (rulesFromSchema map [string ]struct {}, ruleMap map [string ]* rdb.ACLRule ) []map [string ]interface {} {
262
+ var res []map [string ]interface {}
263
+
264
+ for ruleIP , info := range ruleMap {
265
+ _ , ok := rulesFromSchema [ruleIP ]
266
+ // check if new rule has been added on config
267
+ if ! ok {
268
+ r := map [string ]interface {}{
269
+ "ip" : info .IP .String (),
270
+ "description" : info .Description ,
271
+ }
272
+ res = append (res , r )
273
+ }
274
+ }
275
+
276
+ return res
277
+ }
278
+
221
279
func rdbACLRulesFlatten (rules []* rdb.ACLRule ) []map [string ]interface {} {
222
280
var res []map [string ]interface {}
223
281
for _ , rule := range rules {
@@ -233,6 +291,5 @@ func rdbACLRulesFlatten(rules []*rdb.ACLRule) []map[string]interface{} {
233
291
ipJ , _ , _ := net .ParseCIDR (res [j ]["ip" ].(string ))
234
292
return bytes .Compare (ipI , ipJ ) < 0
235
293
})
236
-
237
294
return res
238
295
}
0 commit comments