Skip to content

Commit adb82ac

Browse files
authored
fix(rdb): acl flatten helper (#1438)
1 parent ff4cb46 commit adb82ac

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

scaleway/resource_rdb_acl.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net"
88
"sort"
99

10-
"github.com/hashicorp/terraform-plugin-log/tflog"
10+
"github.com/hashicorp/go-cty/cty"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -125,19 +125,27 @@ func resourceScalewayRdbACLRead(ctx context.Context, d *schema.ResourceData, met
125125
id := newRegionalID(region, instanceID).String()
126126
d.SetId(id)
127127
_ = d.Set("instance_id", id)
128+
129+
diags := diag.Diagnostics{}
130+
128131
if aclRulesRaw, ok := d.GetOk("acl_rules"); ok {
129132
aclRules, mergeErrors := rdbACLRulesFlattenFromSchema(res.Rules, aclRulesRaw.([]interface{}))
130133
if len(mergeErrors) > 0 {
131134
for _, w := range mergeErrors {
132-
tflog.Warn(ctx, fmt.Sprintf("%s", w))
135+
diags = append(diags, diag.Diagnostic{
136+
Severity: diag.Warning,
137+
Summary: "acl_rules does not match server's, updating state",
138+
Detail: w.Error(),
139+
AttributePath: cty.GetAttrPath("acl_rules"),
140+
})
133141
}
134142
}
135143
_ = d.Set("acl_rules", aclRules)
136144
} else {
137145
_ = d.Set("acl_rules", rdbACLRulesFlatten(res.Rules))
138146
}
139147

140-
return nil
148+
return diags
141149
}
142150

143151
func resourceScalewayRdbACLUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@@ -216,16 +224,21 @@ func rdbACLExpand(data []interface{}) ([]*rdb.ACLRuleRequest, error) {
216224
var res []*rdb.ACLRuleRequest
217225
for _, rule := range data {
218226
r := rule.(map[string]interface{})
219-
ip, err := expandIPNet(r["ip"].(string))
220-
if err != nil {
221-
return res, err
227+
228+
ipRaw, ok := r["ip"]
229+
if ok {
230+
aclRule := &rdb.ACLRuleRequest{}
231+
ip, err := expandIPNet(ipRaw.(string))
232+
if err != nil {
233+
return res, err
234+
}
235+
aclRule.IP = ip
236+
if descriptionRaw, descriptionExist := r["description"]; descriptionExist {
237+
aclRule.Description = descriptionRaw.(string)
238+
}
239+
res = append(res, aclRule)
222240
}
223-
res = append(res, &rdb.ACLRuleRequest{
224-
IP: ip,
225-
Description: r["description"].(string),
226-
})
227241
}
228-
229242
sort.Slice(res, func(i, j int) bool {
230243
return bytes.Compare(res[i].IP.IP, res[j].IP.IP) < 0
231244
})
@@ -247,9 +260,14 @@ func rdbACLRulesFlattenFromSchema(rules []*rdb.ACLRule, dataFromSchema []interfa
247260
ip, err := expandIPNet(currentRule["ip"].(string))
248261
if err != nil {
249262
errors = append(errors, err)
263+
continue
250264
}
251265

252-
aclRule := ruleMap[ip.String()]
266+
aclRule, aclRuleExists := ruleMap[ip.String()]
267+
if !aclRuleExists {
268+
errors = append(errors, fmt.Errorf("acl from state does not exist on server (%s)", ip.String()))
269+
continue
270+
}
253271
ruleMapFromSchema[ip.String()] = struct{}{}
254272
r := map[string]interface{}{
255273
"ip": aclRule.IP.String(),

0 commit comments

Comments
 (0)