Skip to content

Commit 6b82393

Browse files
author
linyus
committed
feat: nat gateway snat
1 parent 07ce0ee commit 6b82393

16 files changed

+1359
-48
lines changed

CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
## 1.58.0 (Aug 11, 2021)
2+
3+
FEATURES:
4+
5+
* **New Resource**: `tencentcloud_nat_gateway_snat`
6+
* **New Data Source**: `tencentcloud_nat_gateway_snat`
7+
18
## 1.57.3 (Aug 10, 2021)
29

10+
BUG FIXES:
11+
312
* Resource `data_source_tc_elasticsearch_instances.go` skip kibana node info record after elastic search instance create
413
* Resource `resource_tc_postgresql_instance.go` skip kibana node info record after elastic search instance create
514

@@ -24,14 +33,14 @@ FEATURES:
2433

2534
## 1.56.15 (July 07, 2021)
2635

27-
BUG FIXES
36+
BUG FIXES:
2837

2938
* Resource `tencentcloud_tc_kubernetes_cluster` filter the request field of *bandwidth_package_id* when it is null
3039
* Resource `tencentcloud_tc_kubernetes_node_pool` filter the request field of *bandwidth_package_id* when it is null
3140

3241
## 1.56.14 (July 06, 2021)
3342

34-
BUG FIXES
43+
BUG FIXES:
3544

3645
* Resource `tencentcloud_tc_clb_listener` exec the plan will lead the resource rebuild.
3746

examples/tencentcloud-nat/main.tf

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
data "tencentcloud_availability_zones" "my_favorate_zones" {}
1+
data "tencentcloud_availability_zones" "my_zones" {}
22

3-
data "tencentcloud_images" "my_favorate_image" {
4-
image_type = ["PUBLIC_IMAGE"]
5-
os_name = "centos"
3+
data "tencentcloud_vpc" "my_vpc" {
4+
name = "Default-VPC"
65
}
76

8-
# Create VPC and Subnet
9-
resource "tencentcloud_vpc" "main" {
10-
name = "terraform test"
11-
cidr_block = "10.6.0.0/16"
7+
data "tencentcloud_images" "my_image" {
8+
os_name = "centos"
129
}
1310

14-
resource "tencentcloud_subnet" "main_subnet" {
15-
vpc_id = tencentcloud_vpc.main.id
16-
name = "terraform test subnet"
17-
cidr_block = "10.6.7.0/24"
18-
availability_zone = data.tencentcloud_availability_zones.my_favorate_zones.zones.0.name
11+
data "tencentcloud_instance_types" "my_instance_types" {
12+
cpu_core_count = 1
13+
memory_size = 1
1914
}
2015

2116
# Create EIP
2217
resource "tencentcloud_eip" "eip_dev_dnat" {
2318
name = "terraform_test"
2419
}
25-
2620
resource "tencentcloud_eip" "eip_test_dnat" {
2721
name = "terraform_test"
2822
}
2923

3024
# Create NAT Gateway
3125
resource "tencentcloud_nat_gateway" "my_nat" {
32-
vpc_id = tencentcloud_vpc.main.id
26+
vpc_id = data.tencentcloud_vpc.my_vpc.id
3327
name = "terraform test"
3428
max_concurrent = 3000000
3529
bandwidth = 500
@@ -40,13 +34,40 @@ resource "tencentcloud_nat_gateway" "my_nat" {
4034
]
4135
}
4236

43-
# Create CVM
44-
resource "tencentcloud_instance" "foo" {
45-
availability_zone = data.tencentcloud_availability_zones.my_favorate_zones.zones.0.name
46-
image_id = data.tencentcloud_images.my_favorate_image.images.0.image_id
47-
vpc_id = tencentcloud_vpc.main.id
48-
subnet_id = tencentcloud_subnet.main_subnet.id
49-
system_disk_type = "CLOUD_PREMIUM"
37+
# Create route_table and entry
38+
resource "tencentcloud_route_table" "my_route_table" {
39+
vpc_id = data.tencentcloud_vpc.my_vpc.id
40+
name = "terraform test"
41+
}
42+
resource "tencentcloud_route_table_entry" "my_route_entry" {
43+
route_table_id = tencentcloud_route_table.my_route_table.id
44+
destination_cidr_block = "10.0.0.0/8"
45+
next_type = "NAT"
46+
next_hub = tencentcloud_nat_gateway.my_nat.id
47+
}
48+
49+
# Create Subnet
50+
resource "tencentcloud_subnet" "my_subnet" {
51+
vpc_id = data.tencentcloud_vpc.my_vpc.id
52+
name = "terraform test"
53+
cidr_block = "172.29.23.0/24"
54+
availability_zone = data.tencentcloud_availability_zones.my_zones.zones.0.name
55+
route_table_id = tencentcloud_route_table.my_route_table.id
56+
}
57+
58+
# Create instance
59+
resource "tencentcloud_instance" "my_instance" {
60+
instance_name = "terraform test"
61+
availability_zone = data.tencentcloud_availability_zones.my_zones.zones.0.name
62+
image_id = data.tencentcloud_images.my_image.images.0.image_id
63+
instance_type = data.tencentcloud_instance_types.my_instance_types.instance_types.0.instance_type
64+
system_disk_type = "CLOUD_PREMIUM"
65+
system_disk_size = 50
66+
hostname = "user"
67+
project_id = 0
68+
vpc_id = data.tencentcloud_vpc.my_vpc.id
69+
subnet_id = tencentcloud_subnet.my_subnet.id
70+
internet_max_bandwidth_out = 20
5071
}
5172

5273
# Add DNAT Entry
@@ -56,7 +77,7 @@ resource "tencentcloud_dnat" "dev_dnat" {
5677
protocol = "TCP"
5778
elastic_ip = tencentcloud_eip.eip_dev_dnat.public_ip
5879
elastic_port = "80"
59-
private_ip = tencentcloud_instance.foo.private_ip
80+
private_ip = tencentcloud_instance.my_instance.private_ip
6081
private_port = "9001"
6182
}
6283

@@ -66,6 +87,31 @@ resource "tencentcloud_dnat" "test_dnat" {
6687
protocol = "UDP"
6788
elastic_ip = tencentcloud_eip.eip_test_dnat.public_ip
6889
elastic_port = "8080"
69-
private_ip = tencentcloud_instance.foo.private_ip
90+
private_ip = tencentcloud_instance.my_instance.private_ip
7091
private_port = "9002"
7192
}
93+
94+
# Subnet Nat gateway snat
95+
resource "tencentcloud_nat_gateway_snat" "my_subnet_snat" {
96+
nat_gateway_id = tencentcloud_nat_gateway.my_nat.id
97+
resource_type = "SUBNET"
98+
subnet_id = tencentcloud_subnet.my_subnet.id
99+
subnet_cidr_block = tencentcloud_subnet.my_subnet.cidr_block
100+
description = "terraform test"
101+
public_ip_addr = [
102+
tencentcloud_eip.eip_dev_dnat.public_ip,
103+
tencentcloud_eip.eip_test_dnat.public_ip,
104+
]
105+
}
106+
107+
# NetWorkInterface Nat gateway snat
108+
resource "tencentcloud_nat_gateway_snat" "my_instance_snat" {
109+
nat_gateway_id = tencentcloud_nat_gateway.my_nat.id
110+
resource_type = "NETWORKINTERFACE"
111+
instance_id = tencentcloud_instance.my_instance.id
112+
instance_private_ip_addr = tencentcloud_instance.my_instance.private_ip
113+
description = "terraform test"
114+
public_ip_addr = [
115+
tencentcloud_eip.eip_dev_dnat.public_ip,
116+
]
117+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
Use this data source to query detailed information of VPN gateways.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_nat_gateway_snats" "snat" {
8+
nat_gateway_id = tencentcloud_nat_gateway.my_nat.id
9+
subnet_id = tencentcloud_nat_gateway_snat.my_subnet.id
10+
public_ip_addr = ["50.29.23.234"]
11+
description = "snat demo"
12+
result_output_file = "./snat.txt"
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
21+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
"log"
24+
)
25+
26+
func dataSourceTencentCloudNatGatewaySnats() *schema.Resource {
27+
return &schema.Resource{
28+
Read: dataSourceTencentCloudNatGatewaySnatRead,
29+
30+
Schema: map[string]*schema.Schema{
31+
"nat_gateway_id": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
Description: "NAT gateway ID.",
35+
},
36+
"subnet_id": {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
Description: "Subnet instance ID.",
40+
},
41+
"instance_id": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Description: "Instance ID.",
45+
},
46+
"public_ip_addr": {
47+
Type: schema.TypeList,
48+
Optional: true,
49+
Elem: &schema.Schema{Type: schema.TypeString},
50+
Description: "Elastic IP address pool.",
51+
},
52+
"description": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
Description: "Description.",
56+
},
57+
"result_output_file": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
Description: "Used to save results.",
61+
},
62+
63+
// Computed values
64+
"snat_list": {
65+
Type: schema.TypeList,
66+
Computed: true,
67+
Description: "Information list of the nat gateway snat.",
68+
Elem: &schema.Resource{
69+
Schema: NatGatewaySnatPara(),
70+
},
71+
},
72+
},
73+
}
74+
}
75+
76+
func dataSourceTencentCloudNatGatewaySnatRead(d *schema.ResourceData, meta interface{}) error {
77+
defer logElapsed("data_source.tencentcloud_nat_gateway_snats.read")()
78+
79+
var (
80+
logId = getLogId(contextNil)
81+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
82+
natGatewayId = d.Get("nat_gateway_id").(string)
83+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
84+
)
85+
86+
params := make(map[string]string)
87+
if v, ok := d.GetOk("subnet_id"); ok {
88+
params["resource-id"] = v.(string)
89+
}
90+
if v, ok := d.GetOk("instance_id"); ok {
91+
params["resource-id"] = v.(string)
92+
}
93+
if v, ok := d.GetOk("public_ip_addr"); ok {
94+
params["public-ip-address"] = v.(string)
95+
}
96+
if v, ok := d.GetOk("description"); ok {
97+
params["description"] = v.(string)
98+
}
99+
100+
filters := make([]*vpc.Filter, 0, len(params))
101+
for k, v := range params {
102+
filter := &vpc.Filter{
103+
Name: helper.String(k),
104+
Values: []*string{helper.String(v)},
105+
}
106+
filters = append(filters, filter)
107+
}
108+
err, result := vpcService.DescribeNatGatewaySnats(ctx, natGatewayId, filters)
109+
if err != nil {
110+
log.Printf("[CRITAL]%s read nat gateway snat failed, reason:%s\n ", logId, err.Error())
111+
return err
112+
}
113+
ids := make([]string, len(result))
114+
snatList := make([]map[string]interface{}, len(result))
115+
for _, snat := range result {
116+
m := map[string]interface{}{}
117+
m["nat_gateway_id"] = snat.NatGatewayId
118+
m["resource_type"] = snat.ResourceType
119+
m["public_ip_addr"] = snat.PublicIpAddresses
120+
m["description"] = snat.Description
121+
m["snat_id"] = snat.NatGatewaySnatId
122+
m["create_time"] = snat.CreatedTime
123+
snatList = append(snatList, m)
124+
ids = append(ids, *snat.NatGatewaySnatId)
125+
}
126+
d.SetId(helper.DataResourceIdsHash(ids))
127+
if e := d.Set("snat_list", snatList); e != nil {
128+
log.Printf("[CRITAL]%s provider set nat gateway snat list fail, reason:%s\n ", logId, e.Error())
129+
return e
130+
}
131+
132+
output, ok := d.GetOk("result_output_file")
133+
if ok && output.(string) != "" {
134+
if e := writeToFile(output.(string), snatList); e != nil {
135+
return e
136+
}
137+
}
138+
139+
return nil
140+
}

0 commit comments

Comments
 (0)