Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/tencentcloud-persistent-resource/cvm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "tencentcloud_instance" "cvm" {
availability_zone = var.default_az
image_id = var.image_id
instance_name = var.cvm_name
}
13 changes: 13 additions & 0 deletions examples/tencentcloud-persistent-resource/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
variable "default_az" {
default = "ap-guangzhou-3"
}

variable "cvm_name" {
default = "keep-cvm"
}

# this persist exist and barely removed
variable "image_id" {
default = "img-2lr9q49h"
}

variable "vpn_gw" {
default = "kepp-vpn-gw"
}
Expand Down
12 changes: 12 additions & 0 deletions tencentcloud/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ resource "tencentcloud_instance" "default" {
}
`

const defaultCVMName = "keep-cvm"
const presetCVM = `
data "tencentcloud_instances" "instance" {
instance_name = "` + defaultCVMName + `"
}

locals {
cvm_id = data.tencentcloud_instances.instance.instance_list.0.instance_id
cvm_az = "` + defaultAZone + `"
}
`

const mysqlInstanceCommonTestCase = defaultVpcVariable + `
resource "tencentcloud_mysql_instance" "default" {
mem_size = 1000
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/data_source_tc_cbs_snapshot_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestAccTencentCloudCbsSnapshotPoliciesDataSource(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsSnapshotPolicyDestroy,
Steps: []resource.TestStep{
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/data_source_tc_cbs_snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestAccTencentCloudCbsSnapshotsDataSource(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsSnapshotDestroy,
Steps: []resource.TestStep{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestAccTencentCloudCbsSnapshotPolicyAttachment(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsSnapshotPolicyAttachmentDestroy,
Steps: []resource.TestStep{
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/resource_tc_cbs_snapshot_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccTencentCloudCbsSnapshotPolicy(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsSnapshotPolicyDestroy,
Steps: []resource.TestStep{
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/resource_tc_cbs_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccTencentCloudCbsSnapshot(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsSnapshotDestroy,
Steps: []resource.TestStep{
Expand Down
6 changes: 5 additions & 1 deletion tencentcloud/resource_tc_cbs_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,13 @@ func resourceTencentCloudCbsStorageUpdate(d *schema.ResourceData, meta interface
if e != nil {
return retryError(e)
}
if storage != nil && *storage.DiskState == CBS_STORAGE_STATUS_EXPANDING {

if *storage.DiskState == CBS_STORAGE_STATUS_EXPANDING {
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *storage.DiskState))
}
if *storage.DiskSize != uint64(newValue) {
return resource.RetryableError(fmt.Errorf("waiting for cbs size changed to %d, now %d", newValue, *storage.DiskSize))
}
return nil
})
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions tencentcloud/resource_tc_cbs_storage_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,17 @@ func testAccCheckCbsStorageAttachmentExists(n string) resource.TestCheckFunc {
}
}

const testAccCbsStorageAttachmentConfig = instanceCommonTestCase + `
const testAccCbsStorageAttachmentConfig = presetCVM + `
resource "tencentcloud_cbs_storage" "foo" {
availability_zone = var.availability_zone
availability_zone = local.cvm_az
storage_size = 100
storage_type = "CLOUD_PREMIUM"
storage_name = var.instance_name
storage_name = "test-cbs-attachment"
charge_type = "POSTPAID_BY_HOUR"
}

resource "tencentcloud_cbs_storage_attachment" "foo" {
storage_id = tencentcloud_cbs_storage.foo.id
instance_id = tencentcloud_instance.default.id
instance_id = local.cvm_id
}
`
53 changes: 51 additions & 2 deletions tencentcloud/resource_tc_cbs_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,59 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_cbs_storage
resource.AddTestSweepers("tencentcloud_cbs_storage", &resource.Sweeper{
Name: "tencentcloud_cbs_storage",
F: func(r string) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
cli, _ := sharedClientForRegion(r)
client := cli.(*TencentCloudClient).apiV3Conn

service := CbsService{client}

disks, err := service.DescribeDisksByFilter(ctx, nil)

if err != nil {
return err
}

for i := range disks {
disk := disks[i]
id := *disk.DiskId
if disk.DiskName == nil {
continue
}
name := *disk.DiskName
created, err := time.Parse("2006-01-02 15:04:05", *disk.CreateTime)
if err != nil {
created = time.Now()
}
if isResourcePersist(name, &created) {
continue
}
if *disk.DiskState == CBS_STORAGE_STATUS_ATTACHED {
continue
}
err = service.DeleteDiskById(ctx, id)
if err != nil {
continue
}

}

return nil
},
})
}

func TestAccTencentCloudCbsStorage_basic(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -78,7 +126,7 @@ func TestAccTencentCloudCbsStorage_prepaid(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsStorageDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -108,7 +156,7 @@ func TestAccTencentCloudCbsStorage_upgrade(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCbsStorageDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -258,6 +306,7 @@ resource "tencentcloud_cbs_storage" "storage_upgrade" {
storage_name = "tf-storage-upgrade"
storage_size = 50
availability_zone = "ap-guangzhou-3"
charge_type = "POSTPAID_BY_HOUR"
}
`

Expand Down