Skip to content

Commit 7ca2d40

Browse files
authored
fix: cvm optimize charge type modification (#924)
1 parent a96f764 commit 7ca2d40

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

tencentcloud/resource_tc_instance.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,11 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
994994

995995
d.Partial(true)
996996

997+
var (
998+
periodSet = false
999+
renewFlagSet = false
1000+
)
1001+
9971002
if d.HasChange("instance_charge_type") {
9981003
old, chargeType := d.GetChange("instance_charge_type")
9991004
if old.(string) != CVM_CHARGE_TYPE_POSTPAID || chargeType.(string) != CVM_CHARGE_TYPE_PREPAID {
@@ -1030,17 +1035,17 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
10301035
if *instance.LatestOperationState == CVM_LATEST_OPERATION_STATE_FAILED {
10311036
return resource.NonRetryableError(fmt.Errorf("failed operation when modify instance charge type"))
10321037
}
1033-
} else if instance != nil && *instance.InstanceChargeType == CVM_CHARGE_TYPE_POSTPAID {
1034-
return resource.RetryableError(fmt.Errorf("cvm charge type is still %s, retry", CVM_CHARGE_TYPE_POSTPAID))
10351038
}
10361039
return nil
10371040
})
10381041
if err != nil {
10391042
return err
10401043
}
1044+
periodSet = true
1045+
renewFlagSet = true
10411046
}
10421047

1043-
if d.HasChange("instance_charge_type_prepaid_period") {
1048+
if d.HasChange("instance_charge_type_prepaid_period") && !periodSet {
10441049
chargeType := d.Get("instance_charge_type").(string)
10451050
if chargeType != CVM_CHARGE_TYPE_PREPAID {
10461051
return fmt.Errorf("tencentcloud_cvm_instance update on instance_charge_type_prepaid_period or instance_charge_type_prepaid_renew_flag is only supported with charge type PREPAID")
@@ -1050,7 +1055,7 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
10501055
}
10511056
}
10521057

1053-
if d.HasChange("instance_charge_type_prepaid_renew_flag") {
1058+
if d.HasChange("instance_charge_type_prepaid_renew_flag") && !renewFlagSet {
10541059
//check
10551060
chargeType := d.Get("instance_charge_type").(string)
10561061
if chargeType != CVM_CHARGE_TYPE_PREPAID {

tencentcloud/resource_tc_instance_test.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,38 @@ func TestAccTencentCloudInstanceWithSpotpaid(t *testing.T) {
464464
})
465465
}
466466

467+
func TestAccTencentCloudInstancePostpaidToPrepaid(t *testing.T) {
468+
t.Parallel()
469+
470+
id := "tencentcloud_instance.foo"
471+
resource.Test(t, resource.TestCase{
472+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
473+
IDRefreshName: id,
474+
Providers: testAccProviders,
475+
CheckDestroy: testAccCheckInstanceDestroy,
476+
Steps: []resource.TestStep{
477+
{
478+
Config: testAccTencentCloudInstancePostPaid,
479+
Check: resource.ComposeTestCheckFunc(
480+
testAccCheckTencentCloudDataSourceID(id),
481+
testAccCheckTencentCloudInstanceExists(id),
482+
resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"),
483+
),
484+
},
485+
{
486+
Config: testAccTencentCloudInstanceBasicToPrepaid,
487+
Check: resource.ComposeTestCheckFunc(
488+
testAccCheckTencentCloudDataSourceID(id),
489+
testAccCheckTencentCloudInstanceExists(id),
490+
resource.TestCheckResourceAttr(id, "instance_charge_type", "PREPAID"),
491+
resource.TestCheckResourceAttr(id, "instance_charge_type_prepaid_period", "1"),
492+
resource.TestCheckResourceAttr(id, "instance_charge_type_prepaid_renew_flag", "NOTIFY_AND_MANUAL_RENEW"),
493+
),
494+
},
495+
},
496+
})
497+
}
498+
467499
func testAccCheckTencentCloudInstanceExists(n string) resource.TestCheckFunc {
468500
return func(s *terraform.State) error {
469501
logId := getLogId(contextNil)
@@ -524,7 +556,7 @@ func testAccCheckInstanceDestroy(s *terraform.State) error {
524556
if err != nil {
525557
return err
526558
}
527-
if instance != nil {
559+
if instance != nil && *instance.InstanceState != CVM_STATUS_SHUTDOWN && *instance.InstanceState != CVM_STATUS_TERMINATING {
528560
return fmt.Errorf("cvm instance still exists: %s", rs.Primary.ID)
529561
}
530562
}
@@ -544,6 +576,49 @@ resource "tencentcloud_instance" "foo" {
544576
}
545577
`
546578

579+
const testAccTencentCloudInstancePostPaid = `
580+
data "tencentcloud_instance_types" "default" {
581+
filter {
582+
name = "instance-family"
583+
values = ["S1"]
584+
}
585+
586+
cpu_core_count = 1
587+
memory_size = 1
588+
}
589+
590+
resource "tencentcloud_instance" "foo" {
591+
instance_name = "` + defaultInsName + `"
592+
availability_zone = "` + defaultAZone + `"
593+
image_id = "` + defaultTkeOSImageId + `"
594+
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
595+
system_disk_type = "CLOUD_PREMIUM"
596+
}
597+
`
598+
599+
const testAccTencentCloudInstanceBasicToPrepaid = `
600+
data "tencentcloud_instance_types" "default" {
601+
filter {
602+
name = "instance-family"
603+
values = ["S1"]
604+
}
605+
606+
cpu_core_count = 1
607+
memory_size = 1
608+
}
609+
610+
resource "tencentcloud_instance" "foo" {
611+
instance_name = "` + defaultInsName + `"
612+
availability_zone = "` + defaultAZone + `"
613+
image_id = "` + defaultTkeOSImageId + `"
614+
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
615+
system_disk_type = "CLOUD_PREMIUM"
616+
instance_charge_type = "PREPAID"
617+
instance_charge_type_prepaid_period = 1
618+
instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW"
619+
}
620+
`
621+
547622
const testAccTencentCloudInstanceModifyInstanceType = defaultInstanceVariable + `
548623
data "tencentcloud_instance_types" "new_type" {
549624
filter {

0 commit comments

Comments
 (0)