Skip to content

Commit e08154e

Browse files
authored
add route table association (#2345)
* add route table association * add route table association * update test
1 parent b39806b commit e08154e

File tree

6 files changed

+268
-0
lines changed

6 files changed

+268
-0
lines changed

.changelog/2345.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_route_table_association
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ Virtual Private Cloud(VPC)
12031203
tencentcloud_protocol_template
12041204
tencentcloud_protocol_template_group
12051205
tencentcloud_route_table
1206+
tencentcloud_route_table_association
12061207
tencentcloud_route_entry
12071208
tencentcloud_route_table_entry
12081209
tencentcloud_dnat
@@ -2991,6 +2992,7 @@ func Provider() *schema.Provider {
29912992
"tencentcloud_route_entry": resourceTencentCloudRouteEntry(),
29922993
"tencentcloud_route_table_entry": resourceTencentCloudVpcRouteEntry(),
29932994
"tencentcloud_route_table": resourceTencentCloudVpcRouteTable(),
2995+
"tencentcloud_route_table_association": resourceTencentCloudRouteTableAssociation(),
29942996
"tencentcloud_dnat": resourceTencentCloudDnat(),
29952997
"tencentcloud_nat_gateway": resourceTencentCloudNatGateway(),
29962998
"tencentcloud_nat_gateway_snat": resourceTencentCloudNatGatewaySnat(),
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Provides a resource to create a vpc route_table
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_route_table_association" "route_table_association" {
8+
route_table_id = "rtb-5toos5sy"
9+
subnet_id = "subnet-2y2omd4k"
10+
}
11+
```
12+
13+
Import
14+
15+
vpc route_table can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_route_table_association.route_table_association subnet_id
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"log"
27+
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
30+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
31+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
32+
)
33+
34+
func resourceTencentCloudRouteTableAssociation() *schema.Resource {
35+
return &schema.Resource{
36+
Create: resourceTencentCloudRouteTableAssociationCreate,
37+
Read: resourceTencentCloudRouteTableAssociationRead,
38+
Update: resourceTencentCloudRouteTableAssociationUpdate,
39+
Delete: resourceTencentCloudRouteTableAssociationDelete,
40+
Importer: &schema.ResourceImporter{
41+
State: schema.ImportStatePassthrough,
42+
},
43+
Schema: map[string]*schema.Schema{
44+
"subnet_id": {
45+
Required: true,
46+
ForceNew: true,
47+
Type: schema.TypeString,
48+
Description: "Subnet instance ID, such as `subnet-3x5lf5q0`. This can be queried using the DescribeSubnets API.",
49+
},
50+
"route_table_id": {
51+
Required: true,
52+
Type: schema.TypeString,
53+
Description: "The route table instance ID, such as `rtb-azd4dt1c`.",
54+
},
55+
},
56+
}
57+
}
58+
59+
func resourceTencentCloudRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
60+
defer logElapsed("resource.tencentcloud_route_table_association.create")()
61+
defer inconsistentCheck(d, meta)()
62+
63+
var subnetId string
64+
if v, ok := d.GetOk("subnet_id"); ok {
65+
subnetId = v.(string)
66+
}
67+
68+
d.SetId(subnetId)
69+
70+
return resourceTencentCloudRouteTableAssociationUpdate(d, meta)
71+
}
72+
73+
func resourceTencentCloudRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
74+
defer logElapsed("resource.tencentcloud_route_table_association.read")()
75+
defer inconsistentCheck(d, meta)()
76+
77+
logId := getLogId(contextNil)
78+
79+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
80+
81+
subnetId := d.Id()
82+
83+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
84+
85+
var (
86+
info VpcSubnetBasicInfo
87+
has int
88+
e error
89+
)
90+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
91+
info, has, e = service.DescribeSubnet(ctx, subnetId, nil, "", "")
92+
if e != nil {
93+
return retryError(e)
94+
}
95+
96+
// deleted
97+
if has == 0 {
98+
d.SetId("")
99+
return nil
100+
}
101+
102+
if has != 1 {
103+
errRet := fmt.Errorf("one subnet_id read get %d subnet info", has)
104+
log.Printf("[CRITAL]%s %s", logId, errRet.Error())
105+
return resource.NonRetryableError(errRet)
106+
}
107+
return nil
108+
})
109+
if err != nil {
110+
return err
111+
}
112+
if has == 0 {
113+
return nil
114+
}
115+
116+
_ = d.Set("subnet_id", subnetId)
117+
if info.routeTableId != "" {
118+
_ = d.Set("route_table_id", info.routeTableId)
119+
}
120+
121+
return nil
122+
}
123+
124+
func resourceTencentCloudRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
125+
defer logElapsed("resource.tencentcloud_route_table_association.update")()
126+
defer inconsistentCheck(d, meta)()
127+
128+
logId := getLogId(contextNil)
129+
130+
request := vpc.NewReplaceRouteTableAssociationRequest()
131+
132+
subnetId := d.Id()
133+
134+
request.SubnetId = &subnetId
135+
request.RouteTableId = helper.String(d.Get("route_table_id").(string))
136+
137+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
138+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ReplaceRouteTableAssociation(request)
139+
if e != nil {
140+
return retryError(e)
141+
} else {
142+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
143+
}
144+
return nil
145+
})
146+
if err != nil {
147+
log.Printf("[CRITAL]%s update vpc routeTable failed, reason:%+v", logId, err)
148+
return err
149+
}
150+
151+
return resourceTencentCloudRouteTableAssociationRead(d, meta)
152+
}
153+
154+
func resourceTencentCloudRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
155+
defer logElapsed("resource.tencentcloud_route_table_association.delete")()
156+
defer inconsistentCheck(d, meta)()
157+
158+
return nil
159+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudRouteTableAssociationResource_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: testAccRouteTableAssociation,
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttrSet("tencentcloud_route_table_association.route_table_association", "id"),
21+
resource.TestCheckResourceAttr("tencentcloud_route_table_association.route_table_association", "route_table_id", "rtb-5toos5sy"),
22+
),
23+
},
24+
{
25+
Config: testAccRouteTableAssociationUpdate,
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttrSet("tencentcloud_route_table_association.route_table_association", "id"),
28+
resource.TestCheckResourceAttr("tencentcloud_route_table_association.route_table_association", "route_table_id", "rtb-pp764dr4"),
29+
),
30+
},
31+
{
32+
ResourceName: "tencentcloud_route_table_association.route_table_association",
33+
ImportState: true,
34+
ImportStateVerify: true,
35+
},
36+
},
37+
})
38+
}
39+
40+
const testAccRouteTableAssociation = `
41+
42+
resource "tencentcloud_route_table_association" "route_table_association" {
43+
route_table_id = "rtb-5toos5sy"
44+
subnet_id = "subnet-2y2omd4k"
45+
}
46+
47+
`
48+
49+
const testAccRouteTableAssociationUpdate = `
50+
51+
resource "tencentcloud_route_table_association" "route_table_association" {
52+
route_table_id = "rtb-pp764dr4"
53+
subnet_id = "subnet-2y2omd4k"
54+
}
55+
56+
`
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
subcategory: "Virtual Private Cloud(VPC)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_route_table_association"
5+
sidebar_current: "docs-tencentcloud-resource-route_table_association"
6+
description: |-
7+
Provides a resource to create a vpc route_table
8+
---
9+
10+
# tencentcloud_route_table_association
11+
12+
Provides a resource to create a vpc route_table
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_route_table_association" "route_table_association" {
18+
route_table_id = "rtb-5toos5sy"
19+
subnet_id = "subnet-2y2omd4k"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `route_table_id` - (Required, String) The route table instance ID, such as `rtb-azd4dt1c`.
28+
* `subnet_id` - (Required, String, ForceNew) Subnet instance ID, such as `subnet-3x5lf5q0`. This can be queried using the DescribeSubnets API.
29+
30+
## Attributes Reference
31+
32+
In addition to all arguments above, the following attributes are exported:
33+
34+
* `id` - ID of the resource.
35+
36+
37+
38+
## Import
39+
40+
vpc route_table can be imported using the id, e.g.
41+
42+
```
43+
terraform import tencentcloud_route_table_association.route_table_association subnet_id
44+
```
45+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5951,6 +5951,9 @@
59515951
<li>
59525952
<a href="/docs/providers/tencentcloud/r/route_table.html">tencentcloud_route_table</a>
59535953
</li>
5954+
<li>
5955+
<a href="/docs/providers/tencentcloud/r/route_table_association.html">tencentcloud_route_table_association</a>
5956+
</li>
59545957
<li>
59555958
<a href="/docs/providers/tencentcloud/r/route_table_entry.html">tencentcloud_route_table_entry</a>
59565959
</li>

0 commit comments

Comments
 (0)