Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instance): security group rule read from api and acl database not statefull #892

Merged
merged 5 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scaleway/resource_instance_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func getSecurityGroupRules(ctx context.Context, instanceAPI *instance.API, zone
stateRules[direction] = append(stateRules[direction], securityGroupRuleFlatten(apiRule))
}
}
// There are rule in tfstate not present in api
if len(apiRules[direction]) != len(stateRules[direction]) {
// Truncate stateRules with apiRules length
stateRules[direction] = stateRules[direction][0:len(apiRules[direction])]
}
}

return stateRules[instance.SecurityGroupRuleDirectionInbound], stateRules[instance.SecurityGroupRuleDirectionOutbound], nil
Expand Down
12 changes: 6 additions & 6 deletions scaleway/resource_rdb_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func resourceScalewayRdbACL() *schema.Resource {
Description: "Instance on which the ACL is applied",
},
"acl_rules": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Description: "List of ACL rules to apply",
Elem: &schema.Resource{
Expand Down Expand Up @@ -72,7 +72,7 @@ func resourceScalewayRdbACLCreate(ctx context.Context, d *schema.ResourceData, m
createReq := &rdb.SetInstanceACLRulesRequest{
Region: region,
InstanceID: ID,
Rules: rdbACLExpand(d.Get("acl_rules")),
Rules: rdbACLExpand(d.Get("acl_rules").(*schema.Set)),
}

_, err = rdbAPI.SetInstanceACLRules(createReq, scw.WithContext(ctx))
Expand Down Expand Up @@ -127,7 +127,7 @@ func resourceScalewayRdbACLUpdate(ctx context.Context, d *schema.ResourceData, m
req := &rdb.SetInstanceACLRulesRequest{
Region: region,
InstanceID: ID,
Rules: rdbACLExpand(d.Get("acl_rules")),
Rules: rdbACLExpand(d.Get("acl_rules").(*schema.Set)),
}

_, err = rdbAPI.SetInstanceACLRules(req, scw.WithContext(ctx))
Expand All @@ -145,7 +145,7 @@ func resourceScalewayRdbACLDelete(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}
aclruleips := make([]string, 0)
for _, acl := range rdbACLExpand(d.Get("acl_rules")) {
for _, acl := range rdbACLExpand(d.Get("acl_rules").(*schema.Set)) {
aclruleips = append(aclruleips, acl.IP.String())
}
_, err = rdbAPI.DeleteInstanceACLRules(&rdb.DeleteInstanceACLRulesRequest{
Expand All @@ -161,13 +161,13 @@ func resourceScalewayRdbACLDelete(ctx context.Context, d *schema.ResourceData, m
return nil
}

func rdbACLExpand(data interface{}) []*rdb.ACLRuleRequest {
func rdbACLExpand(data *schema.Set) []*rdb.ACLRuleRequest {
type aclRule struct {
IP string
Description string
}
var res []*rdb.ACLRuleRequest
for _, rule := range data.([]interface{}) {
for _, rule := range data.List() {
r := rule.(map[string]interface{})
res = append(res, &rdb.ACLRuleRequest{
IP: expandIPNet(r["ip"].(string)),
Expand Down
16 changes: 8 additions & 8 deletions scaleway/resource_rdb_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func TestAccScalewayRdbACL_Basic(t *testing.T) {
}
}`, instanceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.ip", "1.2.3.4/32"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.description", "foo"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.ip", "4.5.6.7/32"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.description", "bar"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.ip", "4.5.6.7/32"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.description", "bar"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.ip", "1.2.3.4/32"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.description", "foo"),
),
},
{
Expand All @@ -73,10 +73,10 @@ func TestAccScalewayRdbACL_Basic(t *testing.T) {
}
}`, instanceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.ip", "1.2.3.4/32"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.description", "foo"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.ip", "9.0.0.0/16"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.description", "baz"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.ip", "9.0.0.0/16"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.0.description", "baz"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.ip", "1.2.3.4/32"),
resource.TestCheckResourceAttr("scaleway_rdb_acl.main", "acl_rules.1.description", "foo"),
),
},
},
Expand Down