Skip to content

Commit 28f18dc

Browse files
authored
Merge pull request #1775 from tencentcloudstack/feat/add_nat_resource
add nat resource
2 parents 1825392 + 1084ff0 commit 28f18dc

10 files changed

+507
-0
lines changed

.changelog/1775.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_nat_refresh_nat_dc_route
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_nat_dc_route
7+
```
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Use this data source to query detailed information of vpc nat_dc_route
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_nat_dc_route" "nat_dc_route" {
8+
nat_gateway_id = "nat-gnxkey2e"
9+
vpc_id = "vpc-pyyv5k3v"
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudNatDcRoute() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudNatDcRouteRead,
27+
Schema: map[string]*schema.Schema{
28+
"nat_gateway_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "Unique identifier of Nat Gateway.",
32+
},
33+
34+
"vpc_id": {
35+
Required: true,
36+
Type: schema.TypeString,
37+
Description: "Unique identifier of Vpc.",
38+
},
39+
40+
"nat_direct_connect_gateway_route_set": {
41+
Computed: true,
42+
Type: schema.TypeList,
43+
Description: "Data of route.",
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"destination_cidr_block": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "IPv4 CIDR of subnet.",
50+
},
51+
"gateway_type": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "Type of next-hop gateway, valid values: DIRECTCONNECT.",
55+
},
56+
"gateway_id": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Description: "Id of next-hop gateway.",
60+
},
61+
"create_time": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "Create time of route.",
65+
},
66+
"update_time": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Update time of route.",
70+
},
71+
},
72+
},
73+
},
74+
75+
"result_output_file": {
76+
Type: schema.TypeString,
77+
Optional: true,
78+
Description: "Used to save results.",
79+
},
80+
},
81+
}
82+
}
83+
84+
func dataSourceTencentCloudNatDcRouteRead(d *schema.ResourceData, meta interface{}) error {
85+
defer logElapsed("data_source.tencentcloud_nat_dc_route.read")()
86+
defer inconsistentCheck(d, meta)()
87+
88+
logId := getLogId(contextNil)
89+
90+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
91+
92+
paramMap := make(map[string]interface{})
93+
if v, ok := d.GetOk("nat_gateway_id"); ok {
94+
paramMap["NatGatewayId"] = helper.String(v.(string))
95+
}
96+
97+
if v, ok := d.GetOk("vpc_id"); ok {
98+
paramMap["VpcId"] = helper.String(v.(string))
99+
}
100+
101+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
102+
103+
var natDirectConnectGatewayRouteSet []*vpc.NatDirectConnectGatewayRoute
104+
105+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
106+
result, e := service.DescribeNatDcRouteByFilter(ctx, paramMap)
107+
if e != nil {
108+
return retryError(e)
109+
}
110+
natDirectConnectGatewayRouteSet = result
111+
return nil
112+
})
113+
if err != nil {
114+
return err
115+
}
116+
117+
ids := make([]string, 0, len(natDirectConnectGatewayRouteSet))
118+
tmpList := make([]map[string]interface{}, 0, len(natDirectConnectGatewayRouteSet))
119+
120+
if natDirectConnectGatewayRouteSet != nil {
121+
for _, natDirectConnectGatewayRoute := range natDirectConnectGatewayRouteSet {
122+
natDirectConnectGatewayRouteMap := map[string]interface{}{}
123+
124+
if natDirectConnectGatewayRoute.DestinationCidrBlock != nil {
125+
natDirectConnectGatewayRouteMap["destination_cidr_block"] = natDirectConnectGatewayRoute.DestinationCidrBlock
126+
}
127+
128+
if natDirectConnectGatewayRoute.GatewayType != nil {
129+
natDirectConnectGatewayRouteMap["gateway_type"] = natDirectConnectGatewayRoute.GatewayType
130+
}
131+
132+
if natDirectConnectGatewayRoute.GatewayId != nil {
133+
natDirectConnectGatewayRouteMap["gateway_id"] = natDirectConnectGatewayRoute.GatewayId
134+
}
135+
136+
if natDirectConnectGatewayRoute.CreateTime != nil {
137+
natDirectConnectGatewayRouteMap["create_time"] = natDirectConnectGatewayRoute.CreateTime
138+
}
139+
140+
if natDirectConnectGatewayRoute.UpdateTime != nil {
141+
natDirectConnectGatewayRouteMap["update_time"] = natDirectConnectGatewayRoute.UpdateTime
142+
}
143+
144+
ids = append(ids, *natDirectConnectGatewayRoute.GatewayId)
145+
tmpList = append(tmpList, natDirectConnectGatewayRouteMap)
146+
}
147+
148+
_ = d.Set("nat_direct_connect_gateway_route_set", tmpList)
149+
}
150+
151+
d.SetId(helper.DataResourceIdsHash(ids))
152+
output, ok := d.GetOk("result_output_file")
153+
if ok && output.(string) != "" {
154+
if e := writeToFile(output.(string), tmpList); e != nil {
155+
return e
156+
}
157+
}
158+
return nil
159+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixNatDcRouteDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccNatDcRouteDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_nat_dc_route.nat_dc_route")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccNatDcRouteDataSource = `
26+
27+
data "tencentcloud_nat_dc_route" "nat_dc_route" {
28+
nat_gateway_id = "nat-gnxkey2e"
29+
vpc_id = "vpc-pyyv5k3v"
30+
}
31+
`

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ Virtual Private Cloud(VPC)
707707
tencentcloud_nat_gateways
708708
tencentcloud_nat_gateway_snats
709709
tencentcloud_nats
710+
tencentcloud_nat_dc_route
710711
711712
Resource
712713
tencentcloud_eni
@@ -729,6 +730,7 @@ Virtual Private Cloud(VPC)
729730
tencentcloud_dnat
730731
tencentcloud_nat_gateway
731732
tencentcloud_nat_gateway_snat
733+
tencentcloud_nat_refresh_nat_dc_route
732734
tencentcloud_ha_vip
733735
tencentcloud_ha_vip_eip_attachment
734736
tencentcloud_vpc_bandwidth_package
@@ -1306,6 +1308,7 @@ func Provider() *schema.Provider {
13061308
"tencentcloud_dnats": dataSourceTencentCloudDnats(),
13071309
"tencentcloud_nat_gateways": dataSourceTencentCloudNatGateways(),
13081310
"tencentcloud_nat_gateway_snats": dataSourceTencentCloudNatGatewaySnats(),
1311+
"tencentcloud_nat_dc_route": dataSourceTencentCloudNatDcRoute(),
13091312
"tencentcloud_vpn_customer_gateways": dataSourceTencentCloudVpnCustomerGateways(),
13101313
"tencentcloud_vpn_gateways": dataSourceTencentCloudVpnGateways(),
13111314
"tencentcloud_vpn_gateway_routes": dataSourceTencentCloudVpnGatewayRoutes(),
@@ -1619,6 +1622,7 @@ func Provider() *schema.Provider {
16191622
"tencentcloud_dnat": resourceTencentCloudDnat(),
16201623
"tencentcloud_nat_gateway": resourceTencentCloudNatGateway(),
16211624
"tencentcloud_nat_gateway_snat": resourceTencentCloudNatGatewaySnat(),
1625+
"tencentcloud_nat_refresh_nat_dc_route": resourceTencentCloudNatRefreshNatDcRoute(),
16221626
"tencentcloud_eip": resourceTencentCloudEip(),
16231627
"tencentcloud_eip_association": resourceTencentCloudEipAssociation(),
16241628
"tencentcloud_eip_address_transform": resourceTencentCloudEipAddressTransform(),
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Provides a resource to create a vpc refresh_nat_dc_route
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_nat_refresh_nat_dc_route" "refresh_nat_dc_route" {
8+
nat_gateway_id = "nat-gnxkey2e"
9+
vpc_id = "vpc-pyyv5k3v"
10+
dry_run = true
11+
}
12+
```
13+
14+
Import
15+
16+
vpc refresh_nat_dc_route can be imported using the id, e.g.
17+
18+
```
19+
terraform import tencentcloud_nat_refresh_nat_dc_route.refresh_nat_dc_route vpc_id#nat_gateway_id
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"log"
26+
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
29+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
30+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
31+
)
32+
33+
func resourceTencentCloudNatRefreshNatDcRoute() *schema.Resource {
34+
return &schema.Resource{
35+
Create: resourceTencentCloudNatRefreshNatDcRouteCreate,
36+
Read: resourceTencentCloudNatRefreshNatDcRouteRead,
37+
Delete: resourceTencentCloudNatRefreshNatDcRouteDelete,
38+
Importer: &schema.ResourceImporter{
39+
State: schema.ImportStatePassthrough,
40+
},
41+
Schema: map[string]*schema.Schema{
42+
"vpc_id": {
43+
Required: true,
44+
ForceNew: true,
45+
Type: schema.TypeString,
46+
Description: "Unique identifier of Vpc.",
47+
},
48+
49+
"nat_gateway_id": {
50+
Required: true,
51+
ForceNew: true,
52+
Type: schema.TypeString,
53+
Description: "Unique identifier of Nat Gateway.",
54+
},
55+
56+
"dry_run": {
57+
Required: true,
58+
ForceNew: true,
59+
Type: schema.TypeBool,
60+
Description: "Whether to pre-refresh, valid values: True:yes, False:no.",
61+
},
62+
},
63+
}
64+
}
65+
66+
func resourceTencentCloudNatRefreshNatDcRouteCreate(d *schema.ResourceData, meta interface{}) error {
67+
defer logElapsed("resource.tencentcloud_nat_refresh_nat_dc_route.create")()
68+
defer inconsistentCheck(d, meta)()
69+
70+
logId := getLogId(contextNil)
71+
72+
var (
73+
request = vpc.NewRefreshDirectConnectGatewayRouteToNatGatewayRequest()
74+
vpcId string
75+
natGatewayId string
76+
)
77+
if v, ok := d.GetOk("vpc_id"); ok {
78+
vpcId = v.(string)
79+
request.VpcId = helper.String(v.(string))
80+
}
81+
82+
if v, ok := d.GetOk("nat_gateway_id"); ok {
83+
natGatewayId = v.(string)
84+
request.NatGatewayId = helper.String(v.(string))
85+
}
86+
87+
if v, ok := d.GetOkExists("dry_run"); ok {
88+
request.DryRun = helper.Bool(v.(bool))
89+
}
90+
91+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
92+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().RefreshDirectConnectGatewayRouteToNatGateway(request)
93+
if e != nil {
94+
return retryError(e)
95+
} else {
96+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
97+
}
98+
return nil
99+
})
100+
if err != nil {
101+
log.Printf("[CRITAL]%s operate vpc refreshNatDcRoute failed, reason:%+v", logId, err)
102+
return err
103+
}
104+
105+
d.SetId(vpcId + FILED_SP + natGatewayId)
106+
107+
return resourceTencentCloudNatRefreshNatDcRouteRead(d, meta)
108+
}
109+
110+
func resourceTencentCloudNatRefreshNatDcRouteRead(d *schema.ResourceData, meta interface{}) error {
111+
defer logElapsed("resource.tencentcloud_nat_refresh_nat_dc_route.read")()
112+
defer inconsistentCheck(d, meta)()
113+
114+
return nil
115+
}
116+
117+
func resourceTencentCloudNatRefreshNatDcRouteDelete(d *schema.ResourceData, meta interface{}) error {
118+
defer logElapsed("resource.tencentcloud_nat_refresh_nat_dc_route.delete")()
119+
defer inconsistentCheck(d, meta)()
120+
121+
return nil
122+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixNatRefreshNatDcRouteResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccNatRefreshNatDcRoute,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_nat_refresh_nat_dc_route.refresh_nat_dc_route", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_nat_refresh_nat_dc_route.refresh_nat_dc_route",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccNatRefreshNatDcRoute = `
31+
32+
resource "tencentcloud_nat_refresh_nat_dc_route" "refresh_nat_dc_route" {
33+
nat_gateway_id = "nat-gnxkey2e"
34+
vpc_id = "vpc-pyyv5k3v"
35+
dry_run = true
36+
}
37+
38+
`

0 commit comments

Comments
 (0)