Skip to content

Commit 22a0bfc

Browse files
committed
add
1 parent 2616fdf commit 22a0bfc

8 files changed

+154
-70
lines changed

tencentcloud/services/vpc/data_source_tc_subnet.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func DataSourceTencentCloudSubnet() *schema.Resource {
2525
Required: true,
2626
Description: "The ID of the Subnet.",
2727
},
28+
"cdc_id": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
Computed: true,
32+
Description: "ID of CDC instance.",
33+
},
2834
"cidr_block": {
2935
Type: schema.TypeString,
3036
Computed: true,
@@ -52,14 +58,22 @@ func DataSourceTencentCloudSubnet() *schema.Resource {
5258
func dataSourceTencentCloudSubnetRead(d *schema.ResourceData, meta interface{}) error {
5359
defer tccommon.LogElapsed("data_source.tencentcloud_subnet.read")()
5460

55-
logId := tccommon.GetLogId(tccommon.ContextNil)
56-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
57-
vpcService := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
61+
var (
62+
logId = tccommon.GetLogId(tccommon.ContextNil)
63+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
64+
vpcService = VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
65+
vpcId string
66+
subnetId string
67+
cdcId string
68+
)
5869

59-
vpcId := d.Get("vpc_id").(string)
60-
subnetId := d.Get("subnet_id").(string)
70+
vpcId = d.Get("vpc_id").(string)
71+
subnetId = d.Get("subnet_id").(string)
72+
if temp, ok := d.GetOk("cdc_id"); ok {
73+
cdcId = temp.(string)
74+
}
6175

62-
infos, err := vpcService.DescribeSubnets(ctx, subnetId, vpcId, "", "", map[string]string{}, nil, nil, "", "")
76+
infos, err := vpcService.DescribeSubnets(ctx, subnetId, vpcId, "", "", map[string]string{}, nil, nil, "", "", cdcId)
6377
if err != nil {
6478
return err
6579
}
@@ -70,6 +84,7 @@ func dataSourceTencentCloudSubnetRead(d *schema.ResourceData, meta interface{})
7084

7185
subnet := infos[0]
7286
d.SetId(subnet.subnetId)
87+
_ = d.Set("cdc_id", subnet.cdcId)
7388
_ = d.Set("cidr_block", subnet.cidr)
7489
_ = d.Set("name", subnet.name)
7590
_ = d.Set("route_table_id", subnet.routeTableId)

tencentcloud/services/vpc/data_source_tc_subnet.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,21 @@ This resource can prove useful when a module accepts a subnet id as an input var
66

77
Example Usage
88

9-
```hcl
10-
variable "subnet_id" {}
11-
variable "vpc_id" {}
9+
Query method 1
1210

13-
data "tencentcloud_subnet" "selected" {
14-
vpc_id = var.vpc_id
15-
subnet_id = var.subnet_id
11+
```hcl
12+
data "tencentcloud_subnet" "subnet" {
13+
vpc_id = "vpc-ha5l97e3"
14+
subnet_id = "subnet-ezgfompo"
1615
}
16+
```
1717

18-
resource "tencentcloud_security_group" "default" {
19-
name = "test subnet data"
20-
description = "test subnet data description"
21-
}
18+
Query method 2
2219

23-
resource "tencentcloud_security_group_rule" "subnet" {
24-
security_group_id = tencentcloud_security_group.default.id
25-
type = "ingress"
26-
cidr_ip = data.tencentcloud_subnet.selected.cidr_block
27-
ip_protocol = "tcp"
28-
port_range = "80,8080"
29-
policy = "accept"
20+
```hcl
21+
data "tencentcloud_subnet" "subnet" {
22+
vpc_id = "vpc-ha5l97e3"
23+
subnet_id = "subnet-ezgfompo"
24+
cdc_id = "cluster-lchwgxhs"
3025
}
31-
```
26+
```

tencentcloud/services/vpc/data_source_tc_vpc_instances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func dataSourceTencentCloudVpcInstancesRead(d *schema.ResourceData, meta interfa
194194
}
195195
infoMap["tags"] = respTags
196196

197-
subnetInfos, err := service.DescribeSubnets(ctx, "", item.vpcId, "", "", nil, nil, nil, "", "")
197+
subnetInfos, err := service.DescribeSubnets(ctx, "", item.vpcId, "", "", nil, nil, nil, "", "", "")
198198
if err != nil {
199199
return err
200200
}

tencentcloud/services/vpc/data_source_tc_vpc_subnets.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ func DataSourceTencentCloudVpcSubnets() *schema.Resource {
5656
Optional: true,
5757
Description: "Filter subnet with this CIDR.",
5858
},
59+
"cdc_id": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
Description: "ID of CDC instance.",
63+
},
5964
"is_remote_vpc_snat": {
6065
Type: schema.TypeBool,
6166
Optional: true,
@@ -103,6 +108,11 @@ func DataSourceTencentCloudVpcSubnets() *schema.Resource {
103108
Computed: true,
104109
Description: "A network address block of the subnet.",
105110
},
111+
"cdc_id": {
112+
Type: schema.TypeString,
113+
Computed: true,
114+
Description: "ID of CDC instance.",
115+
},
106116
"is_default": {
107117
Type: schema.TypeBool,
108118
Computed: true,
@@ -143,14 +153,12 @@ func DataSourceTencentCloudVpcSubnets() *schema.Resource {
143153
func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface{}) error {
144154
defer tccommon.LogElapsed("data_source.tencentcloud_vpc_subnets.read")()
145155

146-
logId := tccommon.GetLogId(tccommon.ContextNil)
147-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
148-
149-
vpcService := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
150-
tagService := svctag.NewTagService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
151-
region := meta.(tccommon.ProviderMeta).GetAPIV3Conn().Region
152-
153156
var (
157+
logId = tccommon.GetLogId(tccommon.ContextNil)
158+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
159+
vpcService = VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
160+
tagService = svctag.NewTagService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
161+
region = meta.(tccommon.ProviderMeta).GetAPIV3Conn().Region
154162
vpcId string
155163
subnetId string
156164
name string
@@ -159,7 +167,9 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
159167
isRemoteVpcSNAT *bool
160168
tagKey string
161169
cidrBlock string
170+
cdcId string
162171
)
172+
163173
if temp, ok := d.GetOk("vpc_id"); ok {
164174
vpcId = temp.(string)
165175
}
@@ -192,6 +202,10 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
192202
cidrBlock = temp.(string)
193203
}
194204

205+
if temp, ok := d.GetOk("cdc_id"); ok {
206+
cdcId = temp.(string)
207+
}
208+
195209
var (
196210
tags = helper.GetTags(d, "tags")
197211
infos []VpcSubnetBasicInfo
@@ -202,10 +216,12 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
202216
infos, err = vpcService.DescribeSubnets(ctx, subnetId, vpcId,
203217
name, availabilityZone, tags,
204218
isDefault, isRemoteVpcSNAT, tagKey,
205-
cidrBlock)
219+
cidrBlock, cdcId)
220+
206221
if err != nil {
207222
return tccommon.RetryError(err, tccommon.InternalError)
208223
}
224+
209225
return nil
210226
})
211227

@@ -214,15 +230,13 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
214230
}
215231

216232
var infoList = make([]map[string]interface{}, 0, len(infos))
217-
218233
for _, item := range infos {
219234
respTags, err := tagService.DescribeResourceTags(ctx, "vpc", "subnet", region, item.subnetId)
220235
if err != nil {
221236
return err
222237
}
223238

224239
var infoMap = make(map[string]interface{})
225-
226240
infoMap["availability_zone"] = item.zone
227241
infoMap["vpc_id"] = item.vpcId
228242
infoMap["subnet_id"] = item.subnetId
@@ -234,7 +248,6 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
234248
infoMap["available_ip_count"] = item.availableIpCount
235249
infoMap["create_time"] = item.createTime
236250
infoMap["tags"] = respTags
237-
238251
infoList = append(infoList, infoMap)
239252
}
240253

@@ -254,6 +267,7 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
254267
"cidrBlock": cidrBlock,
255268
"tags": tags,
256269
})
270+
257271
if err != nil {
258272
log.Printf("[CRITAL]%s create data source id error, reason:%s\n ", logId, err.Error())
259273
return err
@@ -271,5 +285,6 @@ func dataSourceTencentCloudVpcSubnetsRead(d *schema.ResourceData, meta interface
271285
return err
272286
}
273287
}
288+
274289
return nil
275290
}

tencentcloud/services/vpc/data_source_tc_vpc_subnets.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ Use this data source to query vpc subnets information.
22

33
Example Usage
44

5+
Create subnet resource
6+
57
```hcl
68
variable "availability_zone" {
79
default = "ap-guangzhou-3"
810
}
911
10-
resource "tencentcloud_vpc" "foo" {
11-
name = "guagua_vpc_instance_test"
12+
resource "tencentcloud_vpc" "vpc" {
13+
name = "vpc"
1214
cidr_block = "10.0.0.0/16"
1315
}
1416
1517
resource "tencentcloud_subnet" "subnet" {
1618
availability_zone = var.availability_zone
17-
name = "guagua_vpc_subnet_test"
18-
vpc_id = tencentcloud_vpc.foo.id
19+
name = "subnet1"
20+
vpc_id = tencentcloud_vpc.vpc.id
1921
cidr_block = "10.0.20.0/28"
2022
is_multicast = false
2123
@@ -24,15 +26,42 @@ resource "tencentcloud_subnet" "subnet" {
2426
}
2527
}
2628
27-
data "tencentcloud_vpc_subnets" "id_instances" {
29+
resource "tencentcloud_subnet" "subnetCDC" {
30+
vpc_id = tencentcloud_vpc.vpc.id
31+
name = "subnet2"
32+
cidr_block = "10.0.0.0/16"
33+
cdc_id = "cluster-lchwgxhs"
34+
availability_zone = data.tencentcloud_availability_zones.zones.zones.0.name
35+
is_multicast = false
36+
}
37+
```
38+
39+
Query all subnets
40+
41+
```hcl
42+
data "tencentcloud_vpc_subnets" "subnets" {}
43+
```
44+
45+
Query subnets by filter
46+
47+
```hcl
48+
data "tencentcloud_vpc_subnets" "subnets" {
49+
vpc_id = tencentcloud_vpc.vpc.id
50+
}
51+
52+
data "tencentcloud_vpc_subnets" "subnets" {
2853
subnet_id = tencentcloud_subnet.subnet.id
2954
}
3055
31-
data "tencentcloud_vpc_subnets" "name_instances" {
56+
data "tencentcloud_vpc_subnets" "subnets" {
3257
name = tencentcloud_subnet.subnet.name
3358
}
3459
35-
data "tencentcloud_vpc_subnets" "tags_instances" {
60+
data "tencentcloud_vpc_subnets" "subnets" {
3661
tags = tencentcloud_subnet.subnet.tags
3762
}
38-
```
63+
64+
data "tencentcloud_vpc_subnets" "subnets" {
65+
cdc_id = tencentcloud_subnet.subnetCDC.cdc_id
66+
}
67+
```

tencentcloud/services/vpc/service_tencentcloud_vpc.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ func (me *VpcService) DescribeSubnet(ctx context.Context,
484484
isRemoteVpcSNAT *bool,
485485
tagKey,
486486
cidrBlock string) (info VpcSubnetBasicInfo, has int, errRet error) {
487-
infos, err := me.DescribeSubnets(ctx, subnetId, "", "", "", nil, nil, isRemoteVpcSNAT, tagKey, cidrBlock)
487+
infos, err := me.DescribeSubnets(ctx, subnetId, "", "", "", nil, nil, isRemoteVpcSNAT, tagKey, cidrBlock, "")
488488
if err != nil {
489489
errRet = err
490490
return
@@ -505,7 +505,7 @@ func (me *VpcService) DescribeSubnets(ctx context.Context,
505505
isDefaultPtr *bool,
506506
isRemoteVpcSNAT *bool,
507507
tagKey,
508-
cidrBlock string) (infos []VpcSubnetBasicInfo, errRet error) {
508+
cidrBlock, cdcId string) (infos []VpcSubnetBasicInfo, errRet error) {
509509

510510
logId := tccommon.GetLogId(ctx)
511511
request := vpc.NewDescribeSubnetsRequest()
@@ -551,6 +551,9 @@ func (me *VpcService) DescribeSubnets(ctx context.Context,
551551
if cidrBlock != "" {
552552
filters = me.fillFilter(filters, "cidr-block", cidrBlock)
553553
}
554+
if cdcId != "" {
555+
filters = me.fillFilter(filters, "cdc-id", cdcId)
556+
}
554557

555558
for k, v := range tags {
556559
filters = me.fillFilter(filters, "tag:"+k, v)

website/docs/d/subnet.html.markdown

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@ This resource can prove useful when a module accepts a subnet id as an input var
1717

1818
## Example Usage
1919

20-
```hcl
21-
variable "subnet_id" {}
22-
variable "vpc_id" {}
20+
### Query method 1
2321

24-
data "tencentcloud_subnet" "selected" {
25-
vpc_id = var.vpc_id
26-
subnet_id = var.subnet_id
22+
```hcl
23+
data "tencentcloud_subnet" "subnet" {
24+
vpc_id = "vpc-ha5l97e3"
25+
subnet_id = "subnet-ezgfompo"
2726
}
27+
```
2828

29-
resource "tencentcloud_security_group" "default" {
30-
name = "test subnet data"
31-
description = "test subnet data description"
32-
}
29+
### Query method 2
3330

34-
resource "tencentcloud_security_group_rule" "subnet" {
35-
security_group_id = tencentcloud_security_group.default.id
36-
type = "ingress"
37-
cidr_ip = data.tencentcloud_subnet.selected.cidr_block
38-
ip_protocol = "tcp"
39-
port_range = "80,8080"
40-
policy = "accept"
31+
```hcl
32+
data "tencentcloud_subnet" "subnet" {
33+
vpc_id = "vpc-ha5l97e3"
34+
subnet_id = "subnet-ezgfompo"
35+
cdc_id = "cluster-lchwgxhs"
4136
}
4237
```
4338

@@ -47,6 +42,7 @@ The following arguments are supported:
4742

4843
* `subnet_id` - (Required, String) The ID of the Subnet.
4944
* `vpc_id` - (Required, String) The VPC ID.
45+
* `cdc_id` - (Optional, String) ID of CDC instance.
5046

5147
## Attributes Reference
5248

0 commit comments

Comments
 (0)