Skip to content

Commit 4fe99f2

Browse files
tongyimingmikatong
and
mikatong
authored
feat(vpc): [118297120] support tencentcloud_vpc_private_nat_gateway (#2763)
* support tencentcloud_vpc_private_nat_gateway * add changelog * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 2837924 commit 4fe99f2

File tree

15 files changed

+532
-5
lines changed

15 files changed

+532
-5
lines changed

.changelog/2763.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_vpc_private_nat_gateway
3+
```

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: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
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+
if err != nil {
135+
return err
136+
}
137+
d.SetId(instanceId)
138+
139+
return resourceTencentCloudVpcPrivateNatGatewayRead(d, meta)
140+
}
141+
142+
func resourceTencentCloudVpcPrivateNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
143+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.read")()
144+
defer tccommon.InconsistentCheck(d, meta)()
145+
146+
logId := tccommon.GetLogId(tccommon.ContextNil)
147+
148+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
149+
150+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
151+
152+
instanceId := d.Id()
153+
154+
privateNatGateway, err := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
155+
if err != nil {
156+
return err
157+
}
158+
159+
if privateNatGateway == nil {
160+
d.SetId("")
161+
log.Printf("[WARN]%s resource `VpcPrivateNatGateway` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
162+
return nil
163+
}
164+
165+
if privateNatGateway.NatGatewayName != nil {
166+
_ = d.Set("nat_gateway_name", privateNatGateway.NatGatewayName)
167+
}
168+
169+
if privateNatGateway.VpcId != nil {
170+
_ = d.Set("vpc_id", privateNatGateway.VpcId)
171+
}
172+
173+
if privateNatGateway.CrossDomain != nil {
174+
_ = d.Set("cross_domain", privateNatGateway.CrossDomain)
175+
}
176+
177+
if privateNatGateway.VpcType != nil {
178+
_ = d.Set("vpc_type", privateNatGateway.VpcType)
179+
}
180+
181+
if privateNatGateway.CcnId != nil {
182+
_ = d.Set("ccn_id", privateNatGateway.CcnId)
183+
}
184+
185+
return nil
186+
}
187+
188+
func resourceTencentCloudVpcPrivateNatGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
189+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.update")()
190+
defer tccommon.InconsistentCheck(d, meta)()
191+
192+
logId := tccommon.GetLogId(tccommon.ContextNil)
193+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
194+
195+
request := vpc.NewModifyPrivateNatGatewayAttributeRequest()
196+
197+
instanceId := d.Id()
198+
199+
request.NatGatewayId = &instanceId
200+
201+
immutableArgs := []string{"vpc_id", "cross_domain", "vpc_type", "ccn_id"}
202+
203+
for _, v := range immutableArgs {
204+
if d.HasChange(v) {
205+
return fmt.Errorf("argument `%s` cannot be changed", v)
206+
}
207+
}
208+
209+
if d.HasChange("nat_gateway_name") {
210+
if v, ok := d.GetOk("nat_gateway_name"); ok {
211+
request.NatGatewayName = helper.String(v.(string))
212+
}
213+
}
214+
215+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
216+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVpcClient().ModifyPrivateNatGatewayAttribute(request)
217+
if e != nil {
218+
return tccommon.RetryError(e)
219+
} else {
220+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
221+
}
222+
return nil
223+
})
224+
if err != nil {
225+
log.Printf("[CRITAL]%s update vpc privateNatGateway failed, reason:%+v", logId, err)
226+
return err
227+
}
228+
229+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
230+
err = resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError {
231+
privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
232+
if errRet != nil {
233+
return tccommon.RetryError(errRet)
234+
}
235+
if privateNatGateway.Status == nil {
236+
return resource.RetryableError(fmt.Errorf("waiting for instance update"))
237+
}
238+
if *privateNatGateway.Status != "AVAILABLE" {
239+
return resource.RetryableError(fmt.Errorf("waiting for instance update"))
240+
}
241+
return nil
242+
})
243+
if err != nil {
244+
return err
245+
}
246+
return resourceTencentCloudVpcPrivateNatGatewayRead(d, meta)
247+
}
248+
249+
func resourceTencentCloudVpcPrivateNatGatewayDelete(d *schema.ResourceData, meta interface{}) error {
250+
defer tccommon.LogElapsed("resource.tencentcloud_vpc_private_nat_gateway.delete")()
251+
defer tccommon.InconsistentCheck(d, meta)()
252+
253+
logId := tccommon.GetLogId(tccommon.ContextNil)
254+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
255+
256+
service := VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
257+
instanceId := d.Id()
258+
259+
if err := service.DeleteVpcPrivateNatGatewayById(ctx, instanceId); err != nil {
260+
return err
261+
}
262+
263+
err := resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError {
264+
privateNatGateway, errRet := service.DescribeVpcPrivateNatGatewayById(ctx, instanceId)
265+
if errRet != nil {
266+
return tccommon.RetryError(errRet)
267+
}
268+
if privateNatGateway != nil {
269+
return resource.RetryableError(fmt.Errorf("waiting for instance delete"))
270+
}
271+
return nil
272+
})
273+
274+
if err != nil {
275+
return err
276+
}
277+
return nil
278+
}
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)