Skip to content

Commit d7b83df

Browse files
authored
fix vpc acl inconsistent when port is ALL (#2135)
* fix vpc acl inconsistent when port is ALL * add changelog
1 parent 06730df commit d7b83df

File tree

3 files changed

+107
-12
lines changed

3 files changed

+107
-12
lines changed

.changelog/2135.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/tencentcloud_vpc_acl: Fix vpc acl entry inconsistent problem while port is `ALL`.
3+
```

tencentcloud/resource_tc_vpc_acl.go

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
"log"
4040
"strings"
4141

42+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
43+
4244
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
4345
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
4446
)
@@ -190,28 +192,77 @@ func resourceTencentCloudVpcACLRead(d *schema.ResourceData, meta interface{}) er
190192
_ = d.Set("name", info.NetworkAclName)
191193
egressList := make([]string, 0, len(info.EgressEntries))
192194
for i := range info.EgressEntries {
193-
if info.EgressEntries[i].Port == nil || *info.EgressEntries[i].Port == "" {
195+
// remove default rule
196+
if CheckIfDefaultRule(info.EgressEntries[i]) {
194197
continue
195198
}
199+
200+
var (
201+
action string
202+
cidrBlock string
203+
port string
204+
protocol string
205+
)
206+
207+
if info.EgressEntries[i].Action != nil {
208+
action = *info.EgressEntries[i].Action
209+
}
210+
if info.EgressEntries[i].CidrBlock != nil {
211+
cidrBlock = *info.EgressEntries[i].CidrBlock
212+
}
213+
if info.EgressEntries[i].Port == nil || *info.EgressEntries[i].Port == "" {
214+
port = "ALL"
215+
} else {
216+
port = *info.EgressEntries[i].Port
217+
}
218+
if info.EgressEntries[i].Protocol != nil {
219+
protocol = *info.EgressEntries[i].Protocol
220+
}
221+
196222
result := strings.Join([]string{
197-
*info.EgressEntries[i].Action,
198-
*info.EgressEntries[i].CidrBlock,
199-
*info.EgressEntries[i].Port,
200-
*info.EgressEntries[i].Protocol,
223+
action,
224+
cidrBlock,
225+
port,
226+
protocol,
201227
}, FILED_SP)
228+
202229
egressList = append(egressList, strings.ToUpper(result))
203230
}
204231

205232
ingressList := make([]string, 0, len(info.IngressEntries))
206233
for i := range info.IngressEntries {
207-
if info.IngressEntries[i].Port == nil || *info.IngressEntries[i].Port == "" {
234+
// remove default rule
235+
if CheckIfDefaultRule(info.IngressEntries[i]) {
208236
continue
209237
}
238+
239+
var (
240+
action string
241+
cidrBlock string
242+
port string
243+
protocol string
244+
)
245+
246+
if info.IngressEntries[i].Action != nil {
247+
action = *info.IngressEntries[i].Action
248+
}
249+
if info.IngressEntries[i].CidrBlock != nil {
250+
cidrBlock = *info.IngressEntries[i].CidrBlock
251+
}
252+
if info.IngressEntries[i].Port == nil || *info.IngressEntries[i].Port == "" {
253+
port = "ALL"
254+
} else {
255+
port = *info.IngressEntries[i].Port
256+
}
257+
if info.IngressEntries[i].Protocol != nil {
258+
protocol = *info.IngressEntries[i].Protocol
259+
}
260+
210261
result := strings.Join([]string{
211-
*info.IngressEntries[i].Action,
212-
*info.IngressEntries[i].CidrBlock,
213-
*info.IngressEntries[i].Port,
214-
*info.IngressEntries[i].Protocol,
262+
action,
263+
cidrBlock,
264+
port,
265+
protocol,
215266
}, FILED_SP)
216267
ingressList = append(ingressList, strings.ToUpper(result))
217268
}
@@ -350,3 +401,19 @@ func resourceTencentCloudVpcACLDelete(d *schema.ResourceData, meta interface{})
350401
}
351402
return nil
352403
}
404+
405+
func CheckIfDefaultRule(aclEntry *vpc.NetworkAclEntry) bool {
406+
// remove default ipv6 rule
407+
if aclEntry.Protocol != nil && *aclEntry.Protocol == "all" &&
408+
aclEntry.Ipv6CidrBlock != nil && *aclEntry.Ipv6CidrBlock == "::/0" &&
409+
aclEntry.Action != nil && *aclEntry.Action == "Accept" {
410+
return true
411+
}
412+
// remove default cidr rule
413+
if aclEntry.Protocol != nil && *aclEntry.Protocol == "all" &&
414+
aclEntry.CidrBlock != nil && *aclEntry.CidrBlock == "0.0.0.0/0" &&
415+
aclEntry.Action != nil && *aclEntry.Action == "Drop" {
416+
return true
417+
}
418+
return false
419+
}

tencentcloud/resource_tc_vpc_acl_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1010
)
1111

12-
func TestAccTencentCloudVpcAcl_basic(t *testing.T) {
12+
func TestAccTencentCloudVpcAclResource_basic(t *testing.T) {
1313
t.Parallel()
1414
resource.Test(t, resource.TestCase{
1515
PreCheck: func() { testAccPreCheck(t) },
@@ -33,7 +33,7 @@ func TestAccTencentCloudVpcAcl_basic(t *testing.T) {
3333
},
3434
})
3535
}
36-
func TestAccTencentCloudVpcAclRulesUpdate(t *testing.T) {
36+
func TestAccTencentCloudVpcAclRulesResource_Update(t *testing.T) {
3737
t.Parallel()
3838
resource.Test(t, resource.TestCase{
3939
PreCheck: func() { testAccPreCheck(t) },
@@ -89,6 +89,15 @@ func TestAccTencentCloudVpcAclRulesUpdate(t *testing.T) {
8989
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "egress.1", "ACCEPT#192.168.1.0/24#800-900#TCP"),
9090
),
9191
},
92+
{
93+
Config: testAccVpcACLConfigAllRules,
94+
Check: resource.ComposeTestCheckFunc(
95+
testAccCheckVpcACLExists("tencentcloud_vpc_acl.foo"),
96+
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "name", "test_acl_update"),
97+
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "ingress.0", "ACCEPT#0.0.0.0/0#ALL#ALL"),
98+
resource.TestCheckResourceAttr("tencentcloud_vpc_acl.foo", "egress.0", "ACCEPT#0.0.0.0/0#ALL#ALL"),
99+
),
100+
},
92101
},
93102
})
94103
}
@@ -222,3 +231,19 @@ resource "tencentcloud_vpc_acl" "foo" {
222231
]
223232
}
224233
`
234+
const testAccVpcACLConfigAllRules = `
235+
data "tencentcloud_vpc_instances" "default" {
236+
is_default = true
237+
}
238+
239+
resource "tencentcloud_vpc_acl" "foo" {
240+
vpc_id = data.tencentcloud_vpc_instances.default.instance_list.0.vpc_id
241+
name = "test_acl_update"
242+
ingress = [
243+
"ACCEPT#0.0.0.0/0#ALL#ALL"
244+
]
245+
egress = [
246+
"ACCEPT#0.0.0.0/0#ALL#ALL"
247+
]
248+
}
249+
`

0 commit comments

Comments
 (0)