Skip to content

Commit c1b28db

Browse files
author
“guojunchu”
committed
add tencentcloud_vpc_acl_attachment
1 parent 171dd67 commit c1b28db

File tree

4 files changed

+402
-0
lines changed

4 files changed

+402
-0
lines changed

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ VPC
371371
tencentcloud_nat_gateway
372372
tencentcloud_ha_vip
373373
tencentcloud_ha_vip_eip_attachment
374+
tencentcloud_vpc_acl_attachment
374375
375376
VPN
376377
Data Source
@@ -615,6 +616,7 @@ func Provider() terraform.ResourceProvider {
615616
"tencentcloud_cbs_storage_attachment": resourceTencentCloudCbsStorageAttachment(),
616617
"tencentcloud_cbs_snapshot_policy_attachment": resourceTencentCloudCbsSnapshotPolicyAttachment(),
617618
"tencentcloud_vpc": resourceTencentCloudVpcInstance(),
619+
"tencentcloud_vpc_acl_attachment": resourceTencentCloudVpcAclAttachment(),
618620
"tencentcloud_subnet": resourceTencentCloudVpcSubnet(),
619621
"tencentcloud_route_entry": resourceTencentCloudRouteEntry(),
620622
"tencentcloud_route_table_entry": resourceTencentCloudVpcRouteEntry(),
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"log"
9+
"strings"
10+
"time"
11+
)
12+
13+
func resourceTencentCloudVpcAclAttachment() *schema.Resource {
14+
return &schema.Resource{
15+
Create: resourceTencentCloudVpcAclAttachmentCreate,
16+
Read: resourceTencentCloudVpcAclAttachmentRead,
17+
Delete: resourceTencentCloudVpcAclAttachmentDelete,
18+
Importer: &schema.ResourceImporter{
19+
State: schema.ImportStatePassthrough,
20+
},
21+
22+
Schema: map[string]*schema.Schema{
23+
"id": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
Description: "Id of the attached ACL.",
28+
},
29+
"subnet_ids": {
30+
Type: schema.TypeList,
31+
Required: true,
32+
ForceNew: true,
33+
Elem: &schema.Schema{
34+
Type: schema.TypeString,
35+
},
36+
Description: "ID list of the Subnet instance ID.",
37+
},
38+
},
39+
}
40+
}
41+
42+
func resourceTencentCloudVpcAclAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
43+
defer logElapsed("resource.tencentcloud_acl_attachment.create")()
44+
var (
45+
logId = getLogId(contextNil)
46+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
47+
service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
48+
aclId string
49+
subnetIds []string
50+
sub_id string
51+
)
52+
53+
if temp, ok := d.GetOk("id"); ok {
54+
aclId = temp.(string)
55+
if len(aclId) < 1 {
56+
return fmt.Errorf("acl_id should be not empty string")
57+
}
58+
}
59+
if temp, ok := d.GetOk("subnet_ids"); ok {
60+
subnetIds = temp.([]string)
61+
}
62+
63+
err := service.AssociateAclSubnets(ctx, aclId, subnetIds)
64+
if err != nil {
65+
return err
66+
}
67+
68+
for _, temp_id := range subnetIds {
69+
sub_id = sub_id + "#" + temp_id
70+
}
71+
d.SetId(aclId + "#" + sub_id)
72+
73+
aclAttachmentId := d.Id()
74+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
75+
e := service.DescribeByAclId(ctx, aclAttachmentId)
76+
if e != nil {
77+
return retryError(e)
78+
}
79+
return nil
80+
})
81+
if err != nil {
82+
log.Printf("[CRITAL]%s read acl attachment failed, reason:%s\n", logId, err.Error())
83+
return err
84+
}
85+
time.Sleep(10 * time.Second)
86+
87+
return resourceTencentCloudVpcAclAttachmentRead(d, meta)
88+
}
89+
90+
func resourceTencentCloudVpcAclAttachmentRead(d *schema.ResourceData, meta interface{}) error {
91+
defer logElapsed("resource.tencentcloud_acl_attachment.read")()
92+
defer inconsistentCheck(d, meta)()
93+
94+
var (
95+
logId = getLogId(contextNil)
96+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
97+
service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
98+
attachmentId = d.Id()
99+
aclId string
100+
)
101+
102+
if attachmentId == "" {
103+
return fmt.Errorf("attachmentId does not exist")
104+
}
105+
106+
aclId = strings.Split(attachmentId, "#")[0]
107+
108+
results, err := service.DescribeNetWorkAcls(ctx, aclId, "", "")
109+
if err != nil {
110+
return err
111+
}
112+
if len(results) < 1 && len(results[0].SubnetSet) < 1 {
113+
return fmt.Errorf("[TECENT_TERRAFORM_CHECK][ACL attachment][Exists] check: CAM group policy attachment id is not set")
114+
}
115+
return nil
116+
117+
}
118+
119+
func resourceTencentCloudVpcAclAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
120+
defer logElapsed("resource.tencentcloud_acl_attachment.delete")()
121+
122+
logId := getLogId(contextNil)
123+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
124+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
125+
attachmentAcl := d.Id()
126+
127+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
128+
e := service.DeleteAclAttachment(ctx, attachmentAcl)
129+
if e != nil {
130+
log.Printf("[CRITAL]%s reason[%s]\n", logId, e.Error())
131+
return retryError(e)
132+
}
133+
return nil
134+
})
135+
if err != nil {
136+
log.Printf("[CRITAL]%s delete acl attachment failed, reason:%s\n", logId, err.Error())
137+
return err
138+
}
139+
140+
return nil
141+
142+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
)
11+
12+
func TestAccTencentCloudVpcAclAttachment_basic(t *testing.T) {
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testVpcAclAttachmentDestroy,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccCamGroupPolicyAttachment_basic,
20+
Check: resource.ComposeTestCheckFunc(
21+
testVpcAclAttachmentExists("tencentcloud_vpc_acl_attachment.attachment"),
22+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_acl_attachment.attachment", "id"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_acl_attachment.attachment", "subnet_ids"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testVpcAclAttachmentDestroy(s *terraform.State) error {
31+
logId := getLogId(contextNil)
32+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
33+
service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
34+
35+
for _, rs := range s.RootModule().Resources {
36+
if rs.Type != "tencentcloud_vpc_acl_attachment" {
37+
continue
38+
}
39+
err := service.DescribeByAclId(ctx, rs.Primary.ID)
40+
if err == nil {
41+
return fmt.Errorf("[TECENT_TERRAFORM_CHECK][][Destroy] check: acl attachment still exists: %s", rs.Primary.ID)
42+
}
43+
}
44+
return nil
45+
}
46+
47+
func testVpcAclAttachmentExists(n string) resource.TestCheckFunc {
48+
return func(s *terraform.State) error {
49+
logId := getLogId(contextNil)
50+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
51+
service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
52+
53+
rs, ok := s.RootModule().Resources[n]
54+
if !ok {
55+
return fmt.Errorf("[TECENT_TERRAFORM_CHECK][][Exists] check: %s is not found", n)
56+
}
57+
if rs.Primary.ID == "" {
58+
return fmt.Errorf("[TECENT_TERRAFORM_CHECK][][Exists] check: id is not set")
59+
}
60+
err := service.DescribeByAclId(ctx, rs.Primary.ID)
61+
if err != nil {
62+
return fmt.Errorf("[TECENT_TERRAFORM_CHECK][][Destroy] check: still exists: %s", rs.Primary.ID)
63+
}
64+
return nil
65+
}
66+
}
67+
68+
const testAccCamGroupPolicyAttachment_basic = `
69+
resource "tencentcloud_vpc" "foo" {
70+
name = "guagua_vpc_instance_test"
71+
cidr_block = "10.0.0.0/16"
72+
}
73+
74+
data "tencentcloud_vpc_instances" "id_instances" {
75+
vpc_id = tencentcloud_vpc.foo.id
76+
}
77+
78+
resource "tencentcloud_vpc_acl" "foo" {
79+
vpc_id = data.tencentcloud_vpc_instances.default.instance_list.0.vpc_id
80+
name = "test_acl_gogoowang"
81+
ingress = [
82+
"ACCEPT#192.168.1.0/24#800#TCP",
83+
"ACCEPT#192.168.1.0/24#800-900#TCP",
84+
]
85+
egress = [
86+
"ACCEPT#192.168.1.0/24#800#TCP",
87+
"ACCEPT#192.168.1.0/24#800-900#TCP",
88+
]
89+
}
90+
resource "tencentcloud_vpc_acl_attachment" "attachment"{
91+
id = data.tencentcloud_source_vpc_acls.instances.
92+
subnet_ids = data.tencentcloud_vpc_instances.id_instances.instance_list[*].subnet_ids
93+
}
94+
`

0 commit comments

Comments
 (0)