diff --git a/.changelog/2509.txt b/.changelog/2509.txt new file mode 100644 index 0000000000..b55ce4f80d --- /dev/null +++ b/.changelog/2509.txt @@ -0,0 +1,4 @@ +```release-note:enhancement +resource/tencentcloud_instance: fix private ip release problem +``` + diff --git a/tencentcloud/services/cvm/resource_tc_instance.go b/tencentcloud/services/cvm/resource_tc_instance.go index c06b99e310..f994d7282f 100644 --- a/tencentcloud/services/cvm/resource_tc_instance.go +++ b/tencentcloud/services/cvm/resource_tc_instance.go @@ -1455,30 +1455,17 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{} return err } - // wait ip release - if len(instance.PrivateIpAddresses) > 0 { - vpcService := vpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) - params := make(map[string]interface{}) - params["VpcId"] = instance.VirtualPrivateCloud.VpcId - params["SubnetId"] = instance.VirtualPrivateCloud.SubnetId - params["IpAddresses"] = instance.PrivateIpAddresses - err := resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError { - usedIpAddress, errRet := vpcService.DescribeVpcUsedIpAddressByFilter(ctx, params) - if errRet != nil { - return tccommon.RetryError(errRet, tccommon.InternalError) - } - if len(usedIpAddress) > 0 { - return resource.RetryableError(fmt.Errorf("wait cvm private ip release...")) - } + vpcService := vpc.NewVpcService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) - return nil - }) + if notExist { + err := waitIpRelease(ctx, vpcService, instance) if err != nil { return err } + return nil } - if notExist || !forceDelete { + if !forceDelete { return nil } @@ -1592,6 +1579,11 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{} } } } + + err = waitIpRelease(ctx, vpcService, instance) + if err != nil { + return err + } return nil } @@ -1702,3 +1694,28 @@ func waitForOperationFinished(d *schema.ResourceData, meta interface{}, timeout } return nil } + +func waitIpRelease(ctx context.Context, vpcService vpc.VpcService, instance *cvm.Instance) error { + // wait ip release + if len(instance.PrivateIpAddresses) > 0 { + params := make(map[string]interface{}) + params["VpcId"] = instance.VirtualPrivateCloud.VpcId + params["SubnetId"] = instance.VirtualPrivateCloud.SubnetId + params["IpAddresses"] = instance.PrivateIpAddresses + err := resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError { + usedIpAddress, errRet := vpcService.DescribeVpcUsedIpAddressByFilter(ctx, params) + if errRet != nil { + return tccommon.RetryError(errRet, tccommon.InternalError) + } + if len(usedIpAddress) > 0 { + return resource.RetryableError(fmt.Errorf("wait cvm private ip release...")) + } + + return nil + }) + if err != nil { + return err + } + } + return nil +} diff --git a/tencentcloud/services/cvm/resource_tc_instance_test.go b/tencentcloud/services/cvm/resource_tc_instance_test.go index 5e9e8d6e54..a39053e744 100644 --- a/tencentcloud/services/cvm/resource_tc_instance_test.go +++ b/tencentcloud/services/cvm/resource_tc_instance_test.go @@ -102,6 +102,32 @@ func TestAccTencentCloudInstanceResource_Basic(t *testing.T) { }) } +func TestAccTencentCloudInstanceResource_PrepaidBasic(t *testing.T) { + t.Parallel() + + id := "tencentcloud_instance.cvm_prepaid_basic" + resource.Test(t, resource.TestCase{ + PreCheck: func() { tcacctest.AccPreCheck(t) }, + IDRefreshName: id, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTencentCloudInstancePrepaidBasic, + Check: resource.ComposeTestCheckFunc( + tcacctest.AccCheckTencentCloudDataSourceID(id), + testAccCheckTencentCloudInstanceExists(id), + resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"), + resource.TestCheckResourceAttrSet(id, "private_ip"), + resource.TestCheckResourceAttrSet(id, "vpc_id"), + resource.TestCheckResourceAttrSet(id, "subnet_id"), + resource.TestCheckResourceAttrSet(id, "project_id"), + resource.TestCheckResourceAttr(id, "tags.hostname", "tci"), + ), + }, + }, + }) +} + func TestAccTencentCloudInstanceResource_WithDataDisk(t *testing.T) { t.Parallel() @@ -756,6 +782,26 @@ resource "tencentcloud_instance" "cvm_basic" { } ` +const testAccTencentCloudInstancePrepaidBasic = tcacctest.DefaultInstanceVariable + ` +resource "tencentcloud_instance" "cvm_prepaid_basic" { + instance_name = var.instance_name + availability_zone = var.availability_cvm_zone + image_id = data.tencentcloud_images.default.images.0.image_id + instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type + vpc_id = var.cvm_vpc_id + subnet_id = var.cvm_subnet_id + system_disk_type = "CLOUD_PREMIUM" + project_id = 0 + instance_charge_type = "PREPAID" + instance_charge_type_prepaid_period = 1 + instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW" + force_delete = true + tags = { + hostname = "tci" + } +} +` + const testAccTencentCloudInstanceWithDataDiskOrder = tcacctest.DefaultInstanceVariable + ` resource "tencentcloud_instance" "foo" { instance_name = var.instance_name