|
| 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 | +} |
0 commit comments