Skip to content

Commit 6fe8f45

Browse files
authored
add dc resource (#1822)
* add dc resource * add changelog
1 parent 3d1d0e8 commit 6fe8f45

20 files changed

+1887
-0
lines changed

.changelog/1822.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```release-note:new-resource
2+
tencentcloud_dcx_extra_config
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_dc_share_dcx_config
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_dc_internet_address
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_dc_internet_address_config
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_dc_gateway_attachment
19+
```

tencentcloud/provider.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ Direct Connect(DC)
372372
373373
Resource
374374
tencentcloud_dcx
375+
tencentcloud_dcx_extra_config
376+
tencentcloud_dc_share_dcx_config
377+
tencentcloud_dc_internet_address
378+
tencentcloud_dc_internet_address_config
375379
376380
Direct Connect Gateway(DCG)
377381
Data Source
@@ -381,6 +385,7 @@ Direct Connect Gateway(DCG)
381385
Resource
382386
tencentcloud_dc_gateway
383387
tencentcloud_dc_gateway_ccn_route
388+
tencentcloud_dc_gateway_attachment
384389
385390
Domain
386391
Data Source
@@ -1785,8 +1790,13 @@ func Provider() *schema.Provider {
17851790
"tencentcloud_ccn_instances_accept_attach": resourceTencentCloudCcnInstancesAcceptAttach(),
17861791
"tencentcloud_ccn_instances_reset_attach": resourceTencentCloudCcnInstancesResetAttach(),
17871792
"tencentcloud_dcx": resourceTencentCloudDcxInstance(),
1793+
"tencentcloud_dcx_extra_config": resourceTencentCloudDcxExtraConfig(),
1794+
"tencentcloud_dc_share_dcx_config": resourceTencentCloudDcShareDcxConfig(),
1795+
"tencentcloud_dc_internet_address": resourceTencentCloudDcInternetAddress(),
1796+
"tencentcloud_dc_internet_address_config": resourceTencentCloudDcInternetAddressConfig(),
17881797
"tencentcloud_dc_gateway": resourceTencentCloudDcGatewayInstance(),
17891798
"tencentcloud_dc_gateway_ccn_route": resourceTencentCloudDcGatewayCcnRouteInstance(),
1799+
"tencentcloud_dc_gateway_attachment": resourceTencentCloudDcGatewayAttachment(),
17901800
"tencentcloud_vpn_customer_gateway": resourceTencentCloudVpnCustomerGateway(),
17911801
"tencentcloud_vpn_gateway": resourceTencentCloudVpnGateway(),
17921802
"tencentcloud_vpn_gateway_route": resourceTencentCloudVpnGatewayRoute(),
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
Provides a resource to create a dc_gateway_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_dc_gateway_attachment" "dc_gateway_attachment" {
8+
vpc_id = "vpc-4h9v4mo3"
9+
nat_gateway_id = "nat-7kanjc6y"
10+
direct_connect_gateway_id = "dcg-dmbhf7jf"
11+
}
12+
```
13+
14+
Import
15+
16+
dc_gateway_attachment can be imported using the id, e.g.
17+
18+
```
19+
terraform import tencentcloud_vpc_dc_gateway_attachment.dc_gateway_attachment vpcId#dcgId#ngId
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"context"
26+
"fmt"
27+
"log"
28+
"strings"
29+
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
32+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
33+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
34+
)
35+
36+
func resourceTencentCloudDcGatewayAttachment() *schema.Resource {
37+
return &schema.Resource{
38+
Create: resourceTencentCloudDcGatewayAttachmentCreate,
39+
Read: resourceTencentCloudDcGatewayAttachmentRead,
40+
Delete: resourceTencentCloudDcGatewayAttachmentDelete,
41+
Importer: &schema.ResourceImporter{
42+
State: schema.ImportStatePassthrough,
43+
},
44+
Schema: map[string]*schema.Schema{
45+
"vpc_id": {
46+
Required: true,
47+
ForceNew: true,
48+
Type: schema.TypeString,
49+
Description: "vpc id.",
50+
},
51+
52+
"nat_gateway_id": {
53+
Required: true,
54+
ForceNew: true,
55+
Type: schema.TypeString,
56+
Description: "NatGatewayId.",
57+
},
58+
59+
"direct_connect_gateway_id": {
60+
Required: true,
61+
ForceNew: true,
62+
Type: schema.TypeString,
63+
Description: "DirectConnectGatewayId.",
64+
},
65+
},
66+
}
67+
}
68+
69+
func resourceTencentCloudDcGatewayAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
70+
defer logElapsed("resource.tencentcloud_dc_gateway_attachment.create")()
71+
defer inconsistentCheck(d, meta)()
72+
73+
logId := getLogId(contextNil)
74+
75+
var (
76+
request = vpc.NewAssociateDirectConnectGatewayNatGatewayRequest()
77+
vpcId string
78+
directConnectGatewayId string
79+
natGatewayId string
80+
)
81+
if v, ok := d.GetOk("vpc_id"); ok {
82+
vpcId = v.(string)
83+
request.VpcId = helper.String(v.(string))
84+
}
85+
86+
if v, ok := d.GetOk("nat_gateway_id"); ok {
87+
natGatewayId = v.(string)
88+
request.NatGatewayId = helper.String(v.(string))
89+
}
90+
91+
if v, ok := d.GetOk("direct_connect_gateway_id"); ok {
92+
directConnectGatewayId = v.(string)
93+
request.DirectConnectGatewayId = helper.String(v.(string))
94+
}
95+
96+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
97+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().AssociateDirectConnectGatewayNatGateway(request)
98+
if e != nil {
99+
return retryError(e)
100+
} else {
101+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
102+
}
103+
return nil
104+
})
105+
if err != nil {
106+
log.Printf("[CRITAL]%s create vpc dcGatewayAttachment failed, reason:%+v", logId, err)
107+
return err
108+
}
109+
110+
d.SetId(vpcId + FILED_SP + directConnectGatewayId + FILED_SP + natGatewayId)
111+
112+
return resourceTencentCloudDcGatewayAttachmentRead(d, meta)
113+
}
114+
115+
func resourceTencentCloudDcGatewayAttachmentRead(d *schema.ResourceData, meta interface{}) error {
116+
defer logElapsed("resource.tencentcloud_vpc_dc_gateway_attachment.read")()
117+
defer inconsistentCheck(d, meta)()
118+
119+
logId := getLogId(contextNil)
120+
121+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
122+
123+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
124+
125+
idSplit := strings.Split(d.Id(), FILED_SP)
126+
if len(idSplit) != 3 {
127+
return fmt.Errorf("id is broken,%s", d.Id())
128+
}
129+
vpcId := idSplit[0]
130+
directConnectGatewayId := idSplit[1]
131+
natGatewayId := idSplit[2]
132+
133+
dcGatewayAttachment, err := service.DescribeDcGatewayAttachmentById(ctx, vpcId, directConnectGatewayId, natGatewayId)
134+
if err != nil {
135+
return err
136+
}
137+
138+
if dcGatewayAttachment == nil {
139+
d.SetId("")
140+
log.Printf("[WARN]%s resource `VpcDcGatewayAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
141+
return nil
142+
}
143+
144+
if dcGatewayAttachment.VpcId != nil {
145+
_ = d.Set("vpc_id", dcGatewayAttachment.VpcId)
146+
}
147+
148+
if dcGatewayAttachment.NatGatewayId != nil {
149+
_ = d.Set("nat_gateway_id", dcGatewayAttachment.NatGatewayId)
150+
}
151+
152+
if dcGatewayAttachment.DirectConnectGatewayId != nil {
153+
_ = d.Set("direct_connect_gateway_id", dcGatewayAttachment.DirectConnectGatewayId)
154+
}
155+
156+
return nil
157+
}
158+
159+
func resourceTencentCloudDcGatewayAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
160+
defer logElapsed("resource.tencentcloud_vpc_dc_gateway_attachment.delete")()
161+
defer inconsistentCheck(d, meta)()
162+
163+
logId := getLogId(contextNil)
164+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
165+
166+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
167+
idSplit := strings.Split(d.Id(), FILED_SP)
168+
if len(idSplit) != 3 {
169+
return fmt.Errorf("id is broken,%s", d.Id())
170+
}
171+
vpcId := idSplit[0]
172+
directConnectGatewayId := idSplit[1]
173+
natGatewayId := idSplit[2]
174+
175+
if err := service.DeleteDcGatewayAttachmentById(ctx, vpcId, directConnectGatewayId, natGatewayId); err != nil {
176+
return err
177+
}
178+
179+
return nil
180+
}
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 TestAccTencentCloudDcGatewayAttachmentResource_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: testAccDcGatewayAttachment,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_dc_gateway_attachment.dc_gateway_attachment", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_dc_gateway_attachment.dc_gateway_attachment",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccDcGatewayAttachment = `
31+
32+
resource "tencentcloud_dc_gateway_attachment" "dc_gateway_attachment" {
33+
vpc_id = "vpc-4h9v4mo3"
34+
nat_gateway_id = "nat-7kanjc6y"
35+
direct_connect_gateway_id = "dcg-dmbhf7jf"
36+
}
37+
38+
`

0 commit comments

Comments
 (0)