Skip to content

Commit 0a60330

Browse files
authored
fix(cvm): [116173103] tencentcloud_instance support delete prepaid disk (#2794)
* add * add * add * add
1 parent 68714d7 commit 0a60330

File tree

3 files changed

+189
-15
lines changed

3 files changed

+189
-15
lines changed

.changelog/2794.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_instance: support delete prepaid disk.
3+
```

tencentcloud/services/cvm/resource_tc_instance.go

Lines changed: 185 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@ func ResourceTencentCloudInstance() *schema.Resource {
288288
ForceNew: true,
289289
Description: "Decides whether the disk is deleted with instance(only applied to `CLOUD_BASIC`, `CLOUD_SSD` and `CLOUD_PREMIUM` disk with `POSTPAID_BY_HOUR` instance), default is true.",
290290
},
291+
"delete_with_instance_prepaid": {
292+
Type: schema.TypeBool,
293+
Optional: true,
294+
Default: false,
295+
ForceNew: true,
296+
Description: "Decides whether the disk is deleted with instance(only applied to `CLOUD_BASIC`, `CLOUD_SSD` and `CLOUD_PREMIUM` disk with `PREPAID` instance), default is false.",
297+
},
291298
"encrypt": {
292299
Type: schema.TypeBool,
293300
Optional: true,
@@ -997,7 +1004,12 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
9971004
}
9981005
}
9991006

1000-
for _, disk := range instance.DataDisks {
1007+
tmpDataDisks := make([]interface{}, 0, len(instance.DataDisks))
1008+
if v, ok := d.GetOk("data_disks"); ok {
1009+
tmpDataDisks = v.([]interface{})
1010+
}
1011+
1012+
for index, disk := range instance.DataDisks {
10011013
dataDisk := make(map[string]interface{}, 5)
10021014
dataDisk["data_disk_id"] = disk.DiskId
10031015
if disk.DiskId == nil {
@@ -1006,6 +1018,15 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
10061018
dataDisk["data_disk_size"] = size
10071019
}
10081020

1021+
dataDisk["delete_with_instance_prepaid"] = false
1022+
if len(tmpDataDisks) == len(instance.DataDisks) {
1023+
tmpDataDisk := tmpDataDisks[index].(map[string]interface{})
1024+
if deleteWithInstancePrepaid, ok := tmpDataDisk["delete_with_instance_prepaid"]; ok {
1025+
deleteWithInstancePrepaidBool := deleteWithInstancePrepaid.(bool)
1026+
dataDisk["delete_with_instance_prepaid"] = deleteWithInstancePrepaidBool
1027+
}
1028+
}
1029+
10091030
dataDisk["data_disk_type"] = disk.DiskType
10101031
dataDisk["data_disk_snapshot_id"] = disk.SnapshotId
10111032
dataDisk["delete_with_instance"] = disk.DeleteWithInstance
@@ -1558,20 +1579,20 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{}
15581579
}
15591580

15601581
// prepaid need delete again
1561-
if instanceChargeType == CVM_CHARGE_TYPE_PREPAID {
1562-
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1563-
errRet := cvmService.DeleteInstance(ctx, instanceId)
1564-
if errRet != nil {
1565-
return tccommon.RetryError(errRet)
1566-
}
1567-
1568-
return nil
1569-
})
1570-
1571-
if err != nil {
1572-
return err
1573-
}
1574-
}
1582+
//if instanceChargeType == CVM_CHARGE_TYPE_PREPAID {
1583+
// err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1584+
// errRet := cvmService.DeleteInstance(ctx, instanceId)
1585+
// if errRet != nil {
1586+
// return tccommon.RetryError(errRet)
1587+
// }
1588+
//
1589+
// return nil
1590+
// })
1591+
//
1592+
// if err != nil {
1593+
// return err
1594+
// }
1595+
//}
15751596

15761597
//check recycling
15771598
notExist := false
@@ -1610,6 +1631,101 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{}
16101631
return nil
16111632
}
16121633

1634+
if instanceChargeType == CVM_CHARGE_TYPE_PREPAID {
1635+
if v, ok := d.GetOk("data_disks"); ok {
1636+
dataDisks := v.([]interface{})
1637+
for _, d := range dataDisks {
1638+
value := d.(map[string]interface{})
1639+
diskId := value["data_disk_id"].(string)
1640+
deleteWithInstancePrepaid := value["delete_with_instance_prepaid"].(bool)
1641+
if deleteWithInstancePrepaid {
1642+
cbsService := svccbs.NewCbsService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
1643+
err := resource.Retry(tccommon.ReadRetryTimeout*2, func() *resource.RetryError {
1644+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1645+
if e != nil {
1646+
return tccommon.RetryError(e, tccommon.InternalError)
1647+
}
1648+
1649+
if *diskInfo.DiskState != svccbs.CBS_STORAGE_STATUS_ATTACHED {
1650+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1651+
}
1652+
1653+
return nil
1654+
})
1655+
1656+
if err != nil {
1657+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1658+
return err
1659+
}
1660+
1661+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1662+
e := cbsService.DetachDisk(ctx, diskId, instanceId)
1663+
if e != nil {
1664+
return tccommon.RetryError(e, tccommon.InternalError)
1665+
}
1666+
1667+
return nil
1668+
})
1669+
1670+
if err != nil {
1671+
log.Printf("[CRITAL]%s detach cbs failed, reason:%s\n ", logId, err.Error())
1672+
return err
1673+
}
1674+
1675+
err = resource.Retry(tccommon.ReadRetryTimeout*2, func() *resource.RetryError {
1676+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1677+
if e != nil {
1678+
return tccommon.RetryError(e, tccommon.InternalError)
1679+
}
1680+
1681+
if *diskInfo.DiskState != svccbs.CBS_STORAGE_STATUS_UNATTACHED {
1682+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1683+
}
1684+
1685+
return nil
1686+
})
1687+
1688+
if err != nil {
1689+
log.Printf("[CRITAL]%s read cbs status failed, reason:%s\n ", logId, err.Error())
1690+
return err
1691+
}
1692+
1693+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1694+
e := cbsService.DeleteDiskById(ctx, diskId)
1695+
if e != nil {
1696+
return tccommon.RetryError(e, tccommon.InternalError)
1697+
}
1698+
1699+
return nil
1700+
})
1701+
1702+
if err != nil {
1703+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1704+
return err
1705+
}
1706+
1707+
err = resource.Retry(tccommon.ReadRetryTimeout*2, func() *resource.RetryError {
1708+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1709+
if e != nil {
1710+
return tccommon.RetryError(e, tccommon.InternalError)
1711+
}
1712+
1713+
if *diskInfo.DiskState != svccbs.CBS_STORAGE_STATUS_TORECYCLE {
1714+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1715+
}
1716+
1717+
return nil
1718+
})
1719+
1720+
if err != nil {
1721+
log.Printf("[CRITAL]%s read cbs status failed, reason:%s\n ", logId, err.Error())
1722+
return err
1723+
}
1724+
}
1725+
}
1726+
}
1727+
}
1728+
16131729
if !forceDelete {
16141730
return nil
16151731
}
@@ -1746,6 +1862,60 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{}
17461862
return err
17471863
}
17481864
}
1865+
1866+
deleteWithInstancePrepaid := value["delete_with_instance_prepaid"].(bool)
1867+
if deleteWithInstancePrepaid {
1868+
cbsService := svccbs.NewCbsService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
1869+
err := resource.Retry(tccommon.ReadRetryTimeout*2, func() *resource.RetryError {
1870+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1871+
if e != nil {
1872+
return tccommon.RetryError(e, tccommon.InternalError)
1873+
}
1874+
1875+
if *diskInfo.DiskState != svccbs.CBS_STORAGE_STATUS_TORECYCLE {
1876+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1877+
}
1878+
1879+
return nil
1880+
})
1881+
1882+
if err != nil {
1883+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1884+
return err
1885+
}
1886+
1887+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1888+
e := cbsService.DeleteDiskById(ctx, diskId)
1889+
if e != nil {
1890+
return tccommon.RetryError(e, tccommon.InternalError)
1891+
}
1892+
1893+
return nil
1894+
})
1895+
1896+
if err != nil {
1897+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1898+
return err
1899+
}
1900+
1901+
err = resource.Retry(tccommon.ReadRetryTimeout*2, func() *resource.RetryError {
1902+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1903+
if e != nil {
1904+
return tccommon.RetryError(e, tccommon.InternalError)
1905+
}
1906+
1907+
if diskInfo != nil {
1908+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1909+
}
1910+
1911+
return nil
1912+
})
1913+
1914+
if err != nil {
1915+
log.Printf("[CRITAL]%s read cbs status failed, reason:%s\n ", logId, err.Error())
1916+
return err
1917+
}
1918+
}
17491919
}
17501920
}
17511921

website/docs/r/instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ The `data_disks` object supports the following:
266266
* `data_disk_type` - (Required, String, ForceNew) Data disk type. For more information about limits on different data disk types, see [Storage Overview](https://intl.cloud.tencent.com/document/product/213/4952). Valid values: LOCAL_BASIC: local disk, LOCAL_SSD: local SSD disk, LOCAL_NVME: local NVME disk, specified in the InstanceType, LOCAL_PRO: local HDD disk, specified in the InstanceType, CLOUD_BASIC: HDD cloud disk, CLOUD_PREMIUM: Premium Cloud Storage, CLOUD_SSD: SSD, CLOUD_HSSD: Enhanced SSD, CLOUD_TSSD: Tremendous SSD, CLOUD_BSSD: Balanced SSD.
267267
* `data_disk_id` - (Optional, String) Data disk ID used to initialize the data disk. When data disk type is `LOCAL_BASIC` and `LOCAL_SSD`, disk id is not supported.
268268
* `data_disk_snapshot_id` - (Optional, String, ForceNew) Snapshot ID of the data disk. The selected data disk snapshot size must be smaller than the data disk size.
269+
* `delete_with_instance_prepaid` - (Optional, Bool, ForceNew) Decides whether the disk is deleted with instance(only applied to `CLOUD_BASIC`, `CLOUD_SSD` and `CLOUD_PREMIUM` disk with `PREPAID` instance), default is false.
269270
* `delete_with_instance` - (Optional, Bool, ForceNew) Decides whether the disk is deleted with instance(only applied to `CLOUD_BASIC`, `CLOUD_SSD` and `CLOUD_PREMIUM` disk with `POSTPAID_BY_HOUR` instance), default is true.
270271
* `encrypt` - (Optional, Bool, ForceNew) Decides whether the disk is encrypted. Default is `false`.
271272
* `throughput_performance` - (Optional, Int, ForceNew) Add extra performance to the data disk. Only works when disk type is `CLOUD_TSSD` or `CLOUD_HSSD`.

0 commit comments

Comments
 (0)