Skip to content

Commit 8da7dc9

Browse files
authored
fix(vpc): set correct default policy value (#3215)
* fix(vpc): set correct default policy value * fix * update tests
1 parent 13b813e commit 8da7dc9

File tree

5 files changed

+1279
-150
lines changed

5 files changed

+1279
-150
lines changed

docs/resources/vpc_acl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ resource "scaleway_vpc_acl" "acl01" {
3939
The following arguments are supported:
4040

4141
- `vpc_id` - (Required) The VPC ID the ACL belongs to.
42-
- `default_policy` - (Required) The action to take for packets which do not match any rules.
42+
- `default_policy` - (Optional. Defaults to `accept`) The action to take for packets which do not match any rules.
4343
- `is_ipv6` - (Optional) Defines whether this set of ACL rules is for IPv6 (false = IPv4). Each Network ACL can have rules for only one IP type.
4444
- `rules` - (Optional) The list of Network ACL rules.
4545
- `protocol` - (Optional) The protocol to which this rule applies. Default value: ANY.

internal/services/vpc/acl.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func ResourceACL() *schema.Resource {
3131
},
3232
"default_policy": {
3333
Type: schema.TypeString,
34-
Required: true,
34+
Optional: true,
35+
Default: vpc.ActionAccept,
3536
Description: "The action to take for packets which do not match any rules",
3637
ValidateDiagFunc: verify.ValidateEnum[vpc.Action](),
3738
},
@@ -43,7 +44,7 @@ func ResourceACL() *schema.Resource {
4344
},
4445
"rules": {
4546
Type: schema.TypeList,
46-
Required: true,
47+
Optional: true,
4748
Description: "The list of Network ACL rules",
4849
Elem: &schema.Resource{
4950
Schema: map[string]*schema.Schema{
@@ -201,7 +202,7 @@ func ResourceVPCACLDelete(ctx context.Context, d *schema.ResourceData, m any) di
201202
_, err = vpcAPI.SetACL(&vpc.SetACLRequest{
202203
VpcID: locality.ExpandID(ID),
203204
Region: region,
204-
DefaultPolicy: "drop",
205+
DefaultPolicy: vpc.ActionAccept,
205206
}, scw.WithContext(ctx))
206207
if err != nil {
207208
return diag.FromErr(err)

internal/services/vpc/acl_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,55 @@ import (
1313
)
1414

1515
func TestAccACL_Basic(t *testing.T) {
16+
tt := acctest.NewTestTools(t)
17+
defer tt.Cleanup()
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() { acctest.PreCheck(t) },
20+
ProviderFactories: tt.ProviderFactories,
21+
CheckDestroy: isACLDestroyed(tt),
22+
Steps: []resource.TestStep{
23+
{
24+
Config: `
25+
resource "scaleway_vpc" "vpc01" {
26+
name = "tf-vpc-acl-basic"
27+
}
28+
29+
resource "scaleway_vpc_acl" "acl01" {
30+
vpc_id = scaleway_vpc.vpc01.id
31+
is_ipv6 = false
32+
}
33+
`,
34+
Check: resource.ComposeTestCheckFunc(
35+
isACLPresent(tt, "scaleway_vpc_acl.acl01"),
36+
resource.TestCheckResourceAttrPair("scaleway_vpc_acl.acl01", "vpc_id", "scaleway_vpc.vpc01", "id"),
37+
resource.TestCheckResourceAttr("scaleway_vpc_acl.acl01", "is_ipv6", "false"),
38+
resource.TestCheckResourceAttr("scaleway_vpc_acl.acl01", "default_policy", "accept"),
39+
),
40+
},
41+
{
42+
Config: `
43+
resource "scaleway_vpc" "vpc01" {
44+
name = "tf-vpc-acl-basic"
45+
}
46+
47+
resource "scaleway_vpc_acl" "acl01" {
48+
vpc_id = scaleway_vpc.vpc01.id
49+
is_ipv6 = false
50+
default_policy = "drop"
51+
}
52+
`,
53+
Check: resource.ComposeTestCheckFunc(
54+
isACLPresent(tt, "scaleway_vpc_acl.acl01"),
55+
resource.TestCheckResourceAttrPair("scaleway_vpc_acl.acl01", "vpc_id", "scaleway_vpc.vpc01", "id"),
56+
resource.TestCheckResourceAttr("scaleway_vpc_acl.acl01", "is_ipv6", "false"),
57+
resource.TestCheckResourceAttr("scaleway_vpc_acl.acl01", "default_policy", "drop"),
58+
),
59+
},
60+
},
61+
})
62+
}
63+
64+
func TestAccACL_WithRules(t *testing.T) {
1665
tt := acctest.NewTestTools(t)
1766
defer tt.Cleanup()
1867
resource.ParallelTest(t, resource.TestCase{
@@ -121,6 +170,16 @@ func TestAccACL_Basic(t *testing.T) {
121170
resource.TestCheckResourceAttr("scaleway_vpc_acl.acl01", "rules.1.action", "accept"),
122171
),
123172
},
173+
{
174+
Config: `
175+
resource "scaleway_vpc" "vpc01" {
176+
name = "tf-vpc-acl"
177+
}
178+
`,
179+
Check: resource.ComposeTestCheckFunc(
180+
testAccCheckACLDefaultPolicy(tt, "scaleway_vpc.vpc01"),
181+
),
182+
},
124183
},
125184
})
126185
}
@@ -178,3 +237,32 @@ func isACLDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
178237
return nil
179238
}
180239
}
240+
241+
func testAccCheckACLDefaultPolicy(tt *acctest.TestTools, n string) resource.TestCheckFunc {
242+
return func(s *terraform.State) error {
243+
rs, ok := s.RootModule().Resources[n]
244+
if !ok {
245+
return fmt.Errorf("resource not found: %s", n)
246+
}
247+
248+
vpcAPI, region, ID, err := vpc.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
249+
if err != nil {
250+
return err
251+
}
252+
253+
acl, err := vpcAPI.GetACL(&vpcSDK.GetACLRequest{
254+
VpcID: ID,
255+
Region: region,
256+
IsIPv6: false,
257+
})
258+
if err != nil {
259+
return err
260+
}
261+
262+
if acl.DefaultPolicy.String() != vpcSDK.ActionAccept.String() {
263+
return fmt.Errorf("expected default_policy to be %s, got %s", vpcSDK.ActionAccept.String(), acl.DefaultPolicy.String())
264+
}
265+
266+
return nil
267+
}
268+
}

0 commit comments

Comments
 (0)