Skip to content

Commit 7b87853

Browse files
committed
add acl resource and data source
1 parent fcbe0ee commit 7b87853

File tree

32 files changed

+3702
-45
lines changed

32 files changed

+3702
-45
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ ENHANCEMENTS:
1313
* Resource: `tencentcloud_mongodb_sharding_instance` supports prepaid type, new mongodb SDK version `2019-07-25` and standby instance.
1414
* Resource: `tencentcloud_security_group_lite_rule` refine update process and doc.
1515

16+
BUG FIXES:
17+
18+
* Resource: `tencentcloud_instance` fix set `key_name` error.
19+
1620
## 1.39.0 (July 18, 2020)
1721

1822
ENHANCEMENTS:

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,6 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
458458
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
459459
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2 h1:Xr9gkxfOP0KQWXKNqmwe8vEeSUiUj4Rlee9CMVX2ZUQ=
460460
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
461-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.196+incompatible h1:aKWXYPLQ9NARP0tBESEtTfRLes88LaI+4qm3TSRDCjk=
462-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.196+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
463-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.215+incompatible h1:QsJWTxBNUjZjST5ehuyeaU/4YLQ3vwyTy6fI4LJ7pts=
464-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.215+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
465461
github.com/tencentcloud/tencentcloud-sdk-go v3.0.217+incompatible h1:c0N5XTxnRv3e6lgIEw3Ml0RdIV0fA8qDRuVEhQnDViE=
466462
github.com/tencentcloud/tencentcloud-sdk-go v3.0.217+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
467463
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
/*
2+
Use this data source to query VPC Network ACL information.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_vpc_instances" "foo" {
8+
}
9+
10+
resource "tencentcloud_vpc_acl" "main" {
11+
vpc_id = data.tencentcloud_vpc_instances.foo.instance_list.0.vpc_id
12+
}
13+
14+
resource "tencentcloud_vpc_acl" "main" {
15+
name = "test_acl"
16+
}
17+
18+
```
19+
*/
20+
package tencentcloud
21+
22+
import (
23+
"context"
24+
"log"
25+
26+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
27+
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
28+
)
29+
30+
func dataSourceTencentCloudVpcAcls() *schema.Resource {
31+
return &schema.Resource{
32+
Read: dataSourceTencentCloudVpcACLRead,
33+
34+
Schema: map[string]*schema.Schema{
35+
"vpc_id": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
ValidateFunc: validateNotEmpty,
39+
Description: "ID of the VPC instance.",
40+
},
41+
"name": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
ValidateFunc: validateStringLengthInRange(0, 60),
45+
Description: "Name of the network ACL.",
46+
},
47+
"id": {
48+
Type: schema.TypeString,
49+
Optional: true,
50+
ValidateFunc: validateNotEmpty,
51+
Description: "`ID` of the network ACL instance.",
52+
},
53+
"result_output_file": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Description: "Used to save results.",
57+
},
58+
"acl_list": {
59+
Type: schema.TypeList,
60+
Computed: true,
61+
Description: "The information list of the VPC. Each element contains the following attributes:",
62+
Elem: &schema.Resource{
63+
Schema: map[string]*schema.Schema{
64+
"vpc_id": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "`ID` of the VPC instance.",
68+
},
69+
"id": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "`ID` of the network ACL instance.",
73+
},
74+
"name": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "Name of the network ACL.",
78+
},
79+
"create_time": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
Description: "Creation time.",
83+
},
84+
"subnets": {
85+
Type: schema.TypeList,
86+
Computed: true,
87+
Description: "",
88+
Elem: &schema.Resource{
89+
Schema: map[string]*schema.Schema{
90+
"vpc_id": {
91+
Type: schema.TypeString,
92+
Computed: true,
93+
Description: "ID of the VPC instance.",
94+
},
95+
"subnet_id": {
96+
Type: schema.TypeString,
97+
Computed: true,
98+
Description: "Subnet instance `ID`",
99+
},
100+
"subnet_name": {
101+
Type: schema.TypeString,
102+
Computed: true,
103+
Description: "Subnet name.",
104+
},
105+
"cidr_block": {
106+
Type: schema.TypeString,
107+
Computed: true,
108+
Description: "The `IPv4` `CIDR` of the subnet.",
109+
},
110+
"tags": {
111+
Type: schema.TypeMap,
112+
Computed: true,
113+
Description: "Tags of the subnet.",
114+
},
115+
},
116+
},
117+
},
118+
"ingress": {
119+
Type: schema.TypeList,
120+
Computed: true,
121+
Description: "",
122+
Elem: &schema.Resource{
123+
Schema: map[string]*schema.Schema{
124+
"protocol": {
125+
Type: schema.TypeString,
126+
Computed: true,
127+
Description: "Type of ip protocol. ",
128+
},
129+
"port": {
130+
Type: schema.TypeString,
131+
Computed: true,
132+
Description: "Range of the port.",
133+
},
134+
"policy": {
135+
Type: schema.TypeString,
136+
Computed: true,
137+
Description: "Rule policy of.",
138+
},
139+
"cidr_block": {
140+
Type: schema.TypeString,
141+
Computed: true,
142+
Description: "An IP address network or segment.",
143+
},
144+
},
145+
},
146+
},
147+
"egress": {
148+
Type: schema.TypeList,
149+
Computed: true,
150+
Description: "",
151+
Elem: &schema.Resource{
152+
Schema: map[string]*schema.Schema{
153+
"protocol": {
154+
Type: schema.TypeString,
155+
Computed: true,
156+
Description: "Type of ip protocol. ",
157+
},
158+
"port": {
159+
Type: schema.TypeString,
160+
Computed: true,
161+
Description: "Range of the port.",
162+
},
163+
"policy": {
164+
Type: schema.TypeString,
165+
Computed: true,
166+
Description: "Rule policy of.",
167+
},
168+
"cidr_block": {
169+
Type: schema.TypeString,
170+
Computed: true,
171+
Description: "An IP address network or segment.",
172+
},
173+
},
174+
},
175+
},
176+
},
177+
},
178+
},
179+
},
180+
}
181+
}
182+
183+
func dataSourceTencentCloudVpcACLRead(d *schema.ResourceData, meta interface{}) error {
184+
defer logElapsed("data_source.tencentcloud_vpc_acl.read")()
185+
var (
186+
logId = getLogId(contextNil)
187+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
188+
service = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
189+
190+
vpcID string
191+
name string
192+
id string
193+
)
194+
195+
if temp, ok := d.GetOk("vpc_id"); ok {
196+
tempStr := temp.(string)
197+
if tempStr != "" {
198+
vpcID = tempStr
199+
}
200+
}
201+
if temp, ok := d.GetOk("name"); ok {
202+
tempStr := temp.(string)
203+
if tempStr != "" {
204+
name = tempStr
205+
}
206+
}
207+
if temp, ok := d.GetOk("id"); ok {
208+
tempStr := temp.(string)
209+
if tempStr != "" {
210+
id = tempStr
211+
}
212+
}
213+
214+
networkAcls, err := service.DescribeNetWorkAcls(ctx, id, vpcID, name)
215+
if err != nil {
216+
return err
217+
}
218+
219+
aclList := make([]map[string]interface{}, 0, len(networkAcls))
220+
ids := make([]string, 0, len(networkAcls))
221+
222+
for _, info := range networkAcls {
223+
subnetInfo := info.SubnetSet
224+
subnets := make([]map[string]interface{}, 0, len(subnetInfo))
225+
for i := range subnetInfo {
226+
v := subnetInfo[i]
227+
subnet := make(map[string]interface{}, 5)
228+
subnet["vpc_id"] = *v.VpcId
229+
subnet["subnet_id"] = *v.SubnetId
230+
subnet["subnet_name"] = *v.SubnetName
231+
subnet["cidr_block"] = *v.CidrBlock
232+
233+
tag := make(map[string]string, len(v.TagSet))
234+
for t := range v.TagSet {
235+
tagValue := v.TagSet[t]
236+
tag[*tagValue.Key] = *tagValue.Value
237+
}
238+
subnet["tags"] = tag
239+
240+
subnets = append(subnets, subnet)
241+
}
242+
243+
ingressInfo := info.IngressEntries
244+
ingress := make([]map[string]interface{}, 0, len(ingressInfo))
245+
for i := range ingressInfo {
246+
v := ingressInfo[i]
247+
egressMap := make(map[string]interface{}, 4)
248+
egressMap["protocol"] = *v.Protocol
249+
egressMap["port"] = *v.Port
250+
egressMap["cidr_block"] = *v.CidrBlock
251+
egressMap["policy"] = *v.Action
252+
egressMap["description"] = *v.Description
253+
254+
ingress = append(ingress, egressMap)
255+
}
256+
257+
egressInfo := info.EgressEntries
258+
egress := make([]map[string]interface{}, 0, len(egressInfo))
259+
for i := range egressInfo {
260+
v := egressInfo[i]
261+
egressMap := make(map[string]interface{}, 4)
262+
egressMap["protocol"] = *v.Protocol
263+
egressMap["port"] = *v.Port
264+
egressMap["cidr_block"] = *v.CidrBlock
265+
egressMap["policy"] = *v.Action
266+
egressMap["description"] = *v.Description
267+
268+
egress = append(egress, egressMap)
269+
}
270+
271+
aclResult := map[string]interface{}{
272+
"vpc_id": info.VpcId,
273+
"id": info.NetworkAclId,
274+
"name": info.NetworkAclName,
275+
"create_time": info.CreatedTime,
276+
"subnets": subnets,
277+
"ingress": ingress,
278+
"egress": egress,
279+
}
280+
aclList = append(aclList, aclResult)
281+
ids = append(ids, *info.NetworkAclId)
282+
}
283+
284+
d.SetId(helper.DataResourceIdsHash(ids))
285+
err = d.Set("acl_list", aclList)
286+
if err != nil {
287+
log.Printf("[CRITAL]%s provider set acl list fail, reason:%v \n ", logId, err)
288+
return err
289+
}
290+
291+
output, ok := d.GetOk("result_output_file")
292+
if ok && output.(string) != "" {
293+
if err := writeToFile(output.(string), aclList); err != nil {
294+
return err
295+
}
296+
}
297+
return nil
298+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccDataSourceTencentCloudVpcACLBasic(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: TestAccDataSourceTencentCloudVpcACLInstances,
16+
17+
Check: resource.ComposeTestCheckFunc(
18+
// id filter
19+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_vpc_acl.default"),
20+
resource.TestCheckResourceAttr("data.tencentcloud_vpc_acl.default", "name", "test_acl"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_vpc_acl.default", "egress.#", "1"),
22+
resource.TestCheckResourceAttr("data.tencentcloud_vpc_acl.default", "ingress.#", "1"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
const TestAccDataSourceTencentCloudVpcACLInstances = `
30+
data "tencentcloud_vpc_instances" "test" {
31+
}
32+
33+
resource "tencentcloud_vpc_acl" "foo" {
34+
vpc_id = data.tencentcloud_vpc_instances.test.instance_list.0.vpc_id
35+
name = "test_acl"
36+
ingress = [
37+
"ACCEPT#192.168.1.0/24#80#TCP",
38+
]
39+
egress = [
40+
"ACCEPT#192.168.1.0/24#80#TCP",
41+
]
42+
}
43+
44+
data "tencentcloud_vpc_instances" "default" {
45+
name = "test_acl"
46+
result_output_file="data_source_tc_vpc_acls.txt"
47+
}
48+
`

0 commit comments

Comments
 (0)