Skip to content

Commit ee90bc6

Browse files
authored
set default while destory (#2404)
1 parent 28db30e commit ee90bc6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tencentcloud/resource_tc_route_table_association.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,75 @@ func resourceTencentCloudRouteTableAssociationDelete(d *schema.ResourceData, met
135135
defer logElapsed("resource.tencentcloud_route_table_association.delete")()
136136
defer inconsistentCheck(d, meta)()
137137

138+
logId := getLogId(contextNil)
139+
140+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
141+
142+
subnetId := d.Id()
143+
144+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
145+
146+
var (
147+
info VpcSubnetBasicInfo
148+
has int
149+
e error
150+
)
151+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
152+
info, has, e = service.DescribeSubnet(ctx, subnetId, nil, "", "")
153+
if e != nil {
154+
return retryError(e)
155+
}
156+
157+
// deleted
158+
if has == 0 {
159+
d.SetId("")
160+
return nil
161+
}
162+
163+
if has != 1 {
164+
errRet := fmt.Errorf("one subnet_id read get %d subnet info", has)
165+
log.Printf("[CRITAL]%s %s", logId, errRet.Error())
166+
return resource.NonRetryableError(errRet)
167+
}
168+
return nil
169+
})
170+
if err != nil {
171+
return err
172+
}
173+
if has == 0 {
174+
return fmt.Errorf("Unable to find the vpc corresponding to the current subnet: %s", subnetId)
175+
}
176+
177+
routeTables, err := service.DescribeRouteTables(ctx, "", "", info.vpcId, nil, helper.Bool(true), "")
178+
179+
if err != nil {
180+
log.Printf("[WARN] Describe default Route Table error: %s", err.Error())
181+
}
182+
183+
if len(routeTables) < 1 {
184+
return fmt.Errorf("Unable to find the default routetable corresponding to the current vpc: %s", info.vpcId)
185+
}
186+
187+
defaultRoutetableId := routeTables[0].routeTableId
188+
189+
request := vpc.NewReplaceRouteTableAssociationRequest()
190+
191+
request.SubnetId = &subnetId
192+
request.RouteTableId = helper.String(defaultRoutetableId)
193+
194+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
195+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ReplaceRouteTableAssociation(request)
196+
if e != nil {
197+
return retryError(e)
198+
} else {
199+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
200+
}
201+
return nil
202+
})
203+
if err != nil {
204+
log.Printf("[CRITAL]%s update vpc routeTable failed, reason:%+v", logId, err)
205+
return err
206+
}
207+
138208
return nil
139209
}

0 commit comments

Comments
 (0)