Skip to content

Commit c007c86

Browse files
authored
fix: replace create by a set securiry group (#1460)
1 parent 3e4e954 commit c007c86

15 files changed

+4005
-5823
lines changed

scaleway/resource_instance_security_group.go

Lines changed: 22 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -300,110 +300,43 @@ func resourceScalewayInstanceSecurityGroupUpdate(ctx context.Context, d *schema.
300300
}
301301

302302
// updateSecurityGroupeRules handles updating SecurityGroupRules
303-
//
304-
// It works as followed:
305-
// 1. Creates 2 map[direction][]rule: one for rules in state and one for rules in API nolint:gofmt
306-
// 2. For each direction we:
307-
// A) Loop for each rule in state for this direction
308-
// a) Compare with api rule in this direction at the same index
309-
// if different update / if equals do nothing / if no more api rules to compare create new api rule
310-
// B) If there is more rule in the API we remove them
311303
func updateSecurityGroupeRules(ctx context.Context, d *schema.ResourceData, zone scw.Zone, securityGroupID string, instanceAPI *instance.API) error {
312-
apiRules := map[instance.SecurityGroupRuleDirection][]*instance.SecurityGroupRule{
313-
instance.SecurityGroupRuleDirectionInbound: {},
314-
instance.SecurityGroupRuleDirectionOutbound: {},
315-
}
316304
stateRules := map[instance.SecurityGroupRuleDirection][]interface{}{
317305
instance.SecurityGroupRuleDirectionInbound: d.Get("inbound_rule").([]interface{}),
318306
instance.SecurityGroupRuleDirectionOutbound: d.Get("outbound_rule").([]interface{}),
319307
}
320308

321-
// Fill apiRules with data from API
322-
resRules, err := instanceAPI.ListSecurityGroupRules(&instance.ListSecurityGroupRulesRequest{
323-
Zone: zone,
324-
SecurityGroupID: expandID(securityGroupID),
325-
}, scw.WithAllPages(), scw.WithContext(ctx))
326-
if err != nil {
327-
return err
328-
}
329-
sort.Slice(resRules.Rules, func(i, j int) bool {
330-
return resRules.Rules[i].Position < resRules.Rules[j].Position
331-
})
332-
for _, apiRule := range resRules.Rules {
333-
if !apiRule.Editable {
334-
continue
335-
}
336-
apiRules[apiRule.Direction] = append(apiRules[apiRule.Direction], apiRule)
337-
}
338-
339-
// Loop through all directions
309+
setGroupRules := []*instance.SetSecurityGroupRulesRequestRule{}
340310
for direction := range stateRules {
341311
// Loop for all state rules in this direction
342-
for index, rawStateRule := range stateRules[direction] {
312+
for _, rawStateRule := range stateRules[direction] {
343313
stateRule, err := securityGroupRuleExpand(rawStateRule)
344314
if err != nil {
345315
return err
346316
}
347317

348-
// This happen when there is more rule in state than in the api. We create more rule in API.
349-
if index >= len(apiRules[direction]) {
350-
_, err = instanceAPI.CreateSecurityGroupRule(&instance.CreateSecurityGroupRuleRequest{
351-
Zone: zone,
352-
SecurityGroupID: securityGroupID,
353-
Protocol: stateRule.Protocol,
354-
IPRange: stateRule.IPRange,
355-
Action: stateRule.Action,
356-
DestPortTo: stateRule.DestPortTo,
357-
DestPortFrom: stateRule.DestPortFrom,
358-
Direction: direction,
359-
}, scw.WithContext(ctx))
360-
if err != nil {
361-
return err
362-
}
363-
continue
364-
}
365-
366-
// We compare rule stateRule[index] and apiRule[index]. If they are different we update api rule to match state.
367-
apiRule := apiRules[direction][index]
368-
if ok, _ := securityGroupRuleEquals(stateRule, apiRule); !ok {
369-
destPortFrom := stateRule.DestPortFrom
370-
destPortTo := stateRule.DestPortTo
371-
if destPortFrom == nil {
372-
destPortFrom = scw.Uint32Ptr(0)
373-
}
374-
if destPortTo == nil {
375-
destPortTo = scw.Uint32Ptr(0)
376-
}
377-
378-
_, err = instanceAPI.UpdateSecurityGroupRule(&instance.UpdateSecurityGroupRuleRequest{
379-
Zone: zone,
380-
SecurityGroupID: securityGroupID,
381-
SecurityGroupRuleID: apiRule.ID,
382-
Protocol: &stateRule.Protocol,
383-
IPRange: &stateRule.IPRange,
384-
Action: &stateRule.Action,
385-
DestPortTo: destPortTo,
386-
DestPortFrom: destPortFrom,
387-
Direction: &direction,
388-
}, scw.WithContext(ctx))
389-
if err != nil {
390-
return err
391-
}
392-
}
318+
// This happens when there is more rule in state than in the api. We create more rule in API.
319+
setGroupRules = append(setGroupRules, &instance.SetSecurityGroupRulesRequestRule{
320+
Zone: zone,
321+
Protocol: stateRule.Protocol,
322+
IPRange: stateRule.IPRange,
323+
Action: stateRule.Action,
324+
DestPortTo: stateRule.DestPortTo,
325+
DestPortFrom: stateRule.DestPortFrom,
326+
Direction: direction,
327+
})
393328
}
329+
}
394330

395-
// We loop through remaining API rules and delete them as they are no longer in the state.
396-
for index := len(stateRules[direction]); index < len(apiRules[direction]); index++ {
397-
err = instanceAPI.DeleteSecurityGroupRule(&instance.DeleteSecurityGroupRuleRequest{
398-
Zone: zone,
399-
SecurityGroupID: securityGroupID,
400-
SecurityGroupRuleID: apiRules[direction][index].ID,
401-
}, scw.WithContext(ctx))
402-
if err != nil {
403-
return err
404-
}
405-
}
331+
_, err := instanceAPI.SetSecurityGroupRules(&instance.SetSecurityGroupRulesRequest{
332+
SecurityGroupID: securityGroupID,
333+
Zone: zone,
334+
Rules: setGroupRules,
335+
}, scw.WithContext(ctx))
336+
if err != nil {
337+
return err
406338
}
339+
407340
return nil
408341
}
409342

@@ -469,6 +402,7 @@ func securityGroupRuleSchema() *schema.Resource {
469402
Optional: true,
470403
ValidateFunc: validation.IsIPAddress,
471404
Description: "Ip address for this rule (e.g: 1.1.1.1). Only one of ip or ip_range should be provided",
405+
Deprecated: "Ip address is deprecated. Please use ip_range instead",
472406
},
473407
"ip_range": {
474408
Type: schema.TypeString,

scaleway/resource_instance_security_group_rules_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestAccScalewayInstanceSecurityGroupRules_Basic(t *testing.T) {
3232
),
3333
},
3434
{
35-
// We test that we can add some rules and they stay in correct orders
35+
// We test that we can add some rules, and they stay in correct orders
3636
Config: `
3737
resource scaleway_instance_security_group sg01 {
3838
external_rules = true

0 commit comments

Comments
 (0)