Skip to content

Commit fccd521

Browse files
author
mikatong
committed
support tencentcloud_vpc_private_nat_gateway
1 parent 2837924 commit fccd521

File tree

14 files changed

+523
-5
lines changed

14 files changed

+523
-5
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require (
4646
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.961
4747
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
4848
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.970
49-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.971
49+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.978
5050
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.960
5151
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
5252
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
@@ -96,7 +96,7 @@ require (
9696
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.857
9797
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf v1.0.674
9898
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.860
99-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.971
99+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.978
100100
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.833
101101
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.792
102102
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.970 h1:IsF+
970970
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.970/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
971971
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.971 h1:T5zEH1QM1XX+hJoAe4krnSjrnsTgHv1KfGC4Sos0Y4M=
972972
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.971/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
973+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.978 h1:3piderD6Cl4bt8mnSOROm+My1nHM5jtReECZeAn15oQ=
974+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.978/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
973975
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU=
974976
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860/go.mod h1:NZo1WplQcC314kMlCRUoy8NQju2BnolIJj7NAWgsuhY=
975977
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624 h1:nEZqsoqt1pEoaP9JjkHQy3/H00suCfzlHW1qOm2nYD8=
@@ -1118,6 +1120,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.964 h1:+HsB7iM
11181120
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.964/go.mod h1:ohfNiTUrMTR50FAUe4wALZVYa1404YQvYB57pvVVADM=
11191121
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.971 h1:eQsRAk1n6z4du93ebKe1XyQpTELY0YIJ4Y7jvMLdNNg=
11201122
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.971/go.mod h1:QpI0wEhuqhVI3jZbakGNzsJlt7YJdB8rqY9b/SI+NMM=
1123+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.978 h1:EZlxS90zY799+zYB7dmbMqU8XyWT0lesPTPkCtibC54=
1124+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.978/go.mod h1:+8DNFolL3rl8vlN1hiY8HhJG0SchGhzLbCN0K0TA0vU=
11211125
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.833 h1:avaBlZ+Qqv7bfMg/u0jlRsbEBbE18CfqKWbfGc84PLg=
11221126
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.833/go.mod h1:fUWG217b//46Oa3VXxC5mgeYeewVAbF+lc81uET89EM=
11231127
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.792 h1:NLgKNOIHWa38AmW7dyfI9Jlcp2Kr9VRD94f48pPNmxM=

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ func Provider() *schema.Provider {
11341134
"tencentcloud_nat_gateway": vpc.ResourceTencentCloudNatGateway(),
11351135
"tencentcloud_nat_gateway_snat": vpc.ResourceTencentCloudNatGatewaySnat(),
11361136
"tencentcloud_nat_refresh_nat_dc_route": vpc.ResourceTencentCloudNatRefreshNatDcRoute(),
1137+
"tencentcloud_vpc_private_nat_gateway": vpc.ResourceTencentCloudVpcPrivateNatGateway(),
11371138
"tencentcloud_oceanus_job": oceanus.ResourceTencentCloudOceanusJob(),
11381139
"tencentcloud_oceanus_job_config": oceanus.ResourceTencentCloudOceanusJobConfig(),
11391140
"tencentcloud_oceanus_job_copy": oceanus.ResourceTencentCloudOceanusJobCopy(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,7 @@ Virtual Private Cloud(VPC)
12421242
tencentcloud_protocol_template
12431243
tencentcloud_protocol_template_group
12441244
tencentcloud_route_table
1245+
tencentcloud_vpc_private_nat_gateway
12451246
tencentcloud_route_table_association
12461247
tencentcloud_route_entry
12471248
tencentcloud_route_table_entry
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
package vpc
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
11+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
12+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
13+
)
14+
15+
func ResourceTencentCloudVpcPrivateNatGateway() *schema.Resource {
16+
return &schema.Resource{
17+
Create: resourceTencentCloudVpcPrivateNatGatewayCreate,
18+
Read: resourceTencentCloudVpcPrivateNatGatewayRead,
19+
Update: resourceTencentCloudVpcPrivateNatGatewayUpdate,
20+
Delete: resourceTencentCloudVpcPrivateNatGatewayDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
24+
Schema: map[string]*schema.Schema{
25+
"nat_gateway_name": {
26+
Required: true,
27+
Type: schema.TypeString,
28+
Description: "Private network gateway name.",
29+
},
30+
31+
"vpc_id": {
32+
Optional: true,
33+
Computed: true,
34+
Type: schema.TypeString,
35+
Description: "Private Cloud instance ID. This parameter is required when creating a VPC type private network NAT gateway or a private network NAT gateway of private network gateway.",
36+
},
37+
38+
"cross_domain": {
39+
Optional: true,
40+
Computed: true,
41+
Type: schema.TypeBool,
42+
Description: "Cross-domain parameters. Cross-domain binding of VPCs is supported only when the value is True.",
43+
},
44+
45+
"vpc_type": {
46+
Optional: true,
47+
Computed: true,
48+
Type: schema.TypeBool,
49+
Description: "VPC type private network NAT gateway. Only when the value is True will a VPC type private network NAT gateway be created.",
50+
},
51+
52+
"ccn_id": {
53+
Optional: true,
54+
Computed: true,
55+
Type: schema.TypeString,
56+
Description: "Cloud Connect Network type The Cloud Connect Network instance ID required to be bound to the private network NAT gateway.",
57+
},
58+
},
59+
}
60+
}
61+
62+
func resourceTencentCloudVpcPrivateNatGatewayCreate(d *schema.ResourceData, meta interface{}) error {
63+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.create")()
64+
defer tccommon.InconsistentCheck(d, meta)()
65+
66+
logId := tccommon.GetLogId(tccommon.ContextNil)
67+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
68+
69+
var (
70+
request = vpc.NewCreatePrivateNatGatewayRequest()
71+
response = vpc.NewCreatePrivateNatGatewayResponse()
72+
instanceId string
73+
)
74+
if v, ok := d.GetOk("nat_gateway_name"); ok {
75+
request.NatGatewayName = helper.String(v.(string))
76+
}
77+
78+
if v, ok := d.GetOk("vpc_id"); ok {
79+
request.VpcId = helper.String(v.(string))
80+
}
81+
82+
if v, ok := d.GetOkExists("cross_domain"); ok {
83+
request.CrossDomain = helper.Bool(v.(bool))
84+
}
85+
86+
if v, ok := d.GetOkExists("vpc_type"); ok {
87+
request.VpcType = helper.Bool(v.(bool))
88+
}
89+
90+
if v, ok := d.GetOk("ccn_id"); ok {
91+
request.CcnId = helper.String(v.(string))
92+
}
93+
94+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
95+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().CreatePrivateNatGateway(request)
96+
if e != nil {
97+
return tccommon.RetryError(e)
98+
} else {
99+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
100+
}
101+
response = result
102+
return nil
103+
})
104+
if err != nil {
105+
log.Printf("[CRITAL]%s create vpc privateNatGateway failed, reason:%+v", logId, err)
106+
return err
107+
}
108+
109+
if response.Response != nil && len(response.Response.PrivateNatGatewaySet) > 0 {
110+
privateNatGateway := response.Response.PrivateNatGatewaySet[0]
111+
if privateNatGateway.NatGatewayId != nil {
112+
instanceId = *response.Response.PrivateNatGatewaySet[0].NatGatewayId
113+
}
114+
}
115+
if instanceId == "" {
116+
d.SetId("")
117+
return fmt.Errorf("instanceId is nil")
118+
}
119+
120+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
121+
err = resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError {
122+
privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
123+
if errRet != nil {
124+
return tccommon.RetryError(errRet)
125+
}
126+
if privateNatGateway.Status == nil {
127+
return resource.RetryableError(fmt.Errorf("waiting for instance create"))
128+
}
129+
if *privateNatGateway.Status != "AVAILABLE" {
130+
return resource.RetryableError(fmt.Errorf("waiting for instance create"))
131+
}
132+
return nil
133+
})
134+
d.SetId(instanceId)
135+
136+
return resourceTencentCloudVpcPrivateNatGatewayRead(d, meta)
137+
}
138+
139+
func resourceTencentCloudVpcPrivateNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
140+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.read")()
141+
defer tccommon.InconsistentCheck(d, meta)()
142+
143+
logId := tccommon.GetLogId(tccommon.ContextNil)
144+
145+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
146+
147+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
148+
149+
instanceId := d.Id()
150+
151+
privateNatGateway, err := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
152+
if err != nil {
153+
return err
154+
}
155+
156+
if privateNatGateway == nil {
157+
d.SetId("")
158+
log.Printf("[WARN]%s resource `VpcPrivateNatGateway` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
159+
return nil
160+
}
161+
162+
if privateNatGateway.NatGatewayName != nil {
163+
_ = d.Set("nat_gateway_name", privateNatGateway.NatGatewayName)
164+
}
165+
166+
if privateNatGateway.VpcId != nil {
167+
_ = d.Set("vpc_id", privateNatGateway.VpcId)
168+
}
169+
170+
if privateNatGateway.CrossDomain != nil {
171+
_ = d.Set("cross_domain", privateNatGateway.CrossDomain)
172+
}
173+
174+
if privateNatGateway.VpcType != nil {
175+
_ = d.Set("vpc_type", privateNatGateway.VpcType)
176+
}
177+
178+
if privateNatGateway.CcnId != nil {
179+
_ = d.Set("ccn_id", privateNatGateway.CcnId)
180+
}
181+
182+
return nil
183+
}
184+
185+
func resourceTencentCloudVpcPrivateNatGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
186+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.update")()
187+
defer tccommon.InconsistentCheck(d, meta)()
188+
189+
logId := tccommon.GetLogId(tccommon.ContextNil)
190+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
191+
192+
request := vpc.NewModifyPrivateNatGatewayAttributeRequest()
193+
194+
instanceId := d.Id()
195+
196+
request.NatGatewayId = &instanceId
197+
198+
immutableArgs := []string{"vpc_id", "cross_domain", "vpc_type", "ccn_id"}
199+
200+
for _, v := range immutableArgs {
201+
if d.HasChange(v) {
202+
return fmt.Errorf("argument `%s` cannot be changed", v)
203+
}
204+
}
205+
206+
if d.HasChange("nat_gateway_name") {
207+
if v, ok := d.GetOk("nat_gateway_name"); ok {
208+
request.NatGatewayName = helper.String(v.(string))
209+
}
210+
}
211+
212+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
213+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ModifyPrivateNatGatewayAttribute(request)
214+
if e != nil {
215+
return tccommon.RetryError(e)
216+
} else {
217+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
218+
}
219+
return nil
220+
})
221+
if err != nil {
222+
log.Printf("[CRITAL]%s update vpc privateNatGateway failed, reason:%+v", logId, err)
223+
return err
224+
}
225+
226+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
227+
err = resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError {
228+
privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
229+
if errRet != nil {
230+
return tccommon.RetryError(errRet)
231+
}
232+
if privateNatGateway.Status == nil {
233+
return resource.RetryableError(fmt.Errorf("waiting for instance update"))
234+
}
235+
if *privateNatGateway.Status != "AVAILABLE" {
236+
return resource.RetryableError(fmt.Errorf("waiting for instance update"))
237+
}
238+
return nil
239+
})
240+
return resourceTencentCloudVpcPrivateNatGatewayRead(d, meta)
241+
}
242+
243+
func resourceTencentCloudVpcPrivateNatGatewayDelete(d *schema.ResourceData, meta interface{}) error {
244+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.delete")()
245+
defer tccommon.InconsistentCheck(d, meta)()
246+
247+
logId := tccommon.GetLogId(tccommon.ContextNil)
248+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
249+
250+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
251+
instanceId := d.Id()
252+
253+
if err := service.DeleteVpcPrivateNatGatewayById(ctx, instanceId); err != nil {
254+
return err
255+
}
256+
257+
err := resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError {
258+
privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
259+
if errRet != nil {
260+
return tccommon.RetryError(errRet)
261+
}
262+
if privateNatGateway != nil {
263+
return resource.RetryableError(fmt.Errorf("waiting for instance delete"))
264+
}
265+
return nil
266+
})
267+
268+
if err != nil {
269+
return err
270+
}
271+
return nil
272+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Provides a resource to create a vpc private nat gateway
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_vpc_private_nat_gateway" "private_nat_gateway" {
7+
nat_gateway_name = "xxx"
8+
vpc_id = "xxx"
9+
}
10+
```
11+
12+
Import
13+
14+
vpc private_nat_gateway can be imported using the id, e.g.
15+
16+
```
17+
terraform import tencentcloud_vpc_private_nat_gateway.private_nat_gateway private_nat_gateway_id
18+
```

0 commit comments

Comments
 (0)