Skip to content

Commit

Permalink
fix cvm release private ip (#2509)
Browse files Browse the repository at this point in the history
* fix cvm release private ip

* add changelog

---------

Co-authored-by: mikatong <[email protected]>
  • Loading branch information
tongyiming and mikatong authored Jan 30, 2024
1 parent 57cf580 commit 126bedc
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .changelog/2509.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
resource/tencentcloud_instance: fix private ip release problem
```

53 changes: 35 additions & 18 deletions tencentcloud/services/cvm/resource_tc_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -1592,6 +1579,11 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{}
}
}
}

err = waitIpRelease(ctx, vpcService, instance)
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -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
}
46 changes: 46 additions & 0 deletions tencentcloud/services/cvm/resource_tc_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 126bedc

Please sign in to comment.