Skip to content

Commit 6fe0ae9

Browse files
authored
feat: cvm - support prepaid/postpaid charge type dual modify (#1091)
* feat: cvm - support prepaid/postpaid charge type dual modify * fix: cvm data testcase * fix: cvm data testcase az
1 parent f42d230 commit 6fe0ae9

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

tencentcloud/data_source_tc_instances_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func TestAccTencentCloudDataSourceInstancesBase(t *testing.T) {
2121
resource.TestCheckResourceAttrSet("data.tencentcloud_instances.foo", "instance_list.0.instance_id"),
2222
resource.TestCheckResourceAttr("data.tencentcloud_instances.foo", "instance_list.0.instance_name", defaultInsName),
2323
resource.TestCheckResourceAttrSet("data.tencentcloud_instances.foo", "instance_list.0.instance_type"),
24-
resource.TestCheckResourceAttr("data.tencentcloud_instances.foo", "instance_list.0.cpu", "1"),
25-
resource.TestCheckResourceAttr("data.tencentcloud_instances.foo", "instance_list.0.memory", "1"),
26-
resource.TestCheckResourceAttr("data.tencentcloud_instances.foo", "instance_list.0.availability_zone", defaultAZone),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_instances.foo", "instance_list.0.cpu"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_instances.foo", "instance_list.0.memory"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_instances.foo", "instance_list.0.availability_zone"),
2727
resource.TestCheckResourceAttr("data.tencentcloud_instances.foo", "instance_list.0.project_id", "0"),
28-
resource.TestCheckResourceAttr("data.tencentcloud_instances.foo", "instance_list.0.system_disk_type", "CLOUD_PREMIUM"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_instances.foo", "instance_list.0.system_disk_type"),
2929
),
3030
},
3131
},

tencentcloud/resource_tc_instance.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,28 +1016,31 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
10161016
}
10171017

10181018
var (
1019-
periodSet = false
1020-
renewFlagSet = false
1021-
alreadyPrepaid = *instanceInfo.InstanceChargeType == CVM_CHARGE_TYPE_PREPAID
1019+
periodSet = false
1020+
renewFlagSet = false
1021+
expectChargeType = CVM_CHARGE_TYPE_POSTPAID
1022+
currentChargeType = *instanceInfo.InstanceChargeType
10221023
)
10231024

1024-
if d.HasChange("instance_charge_type") && !alreadyPrepaid {
1025-
old, chargeType := d.GetChange("instance_charge_type")
1026-
if old.(string) != CVM_CHARGE_TYPE_POSTPAID || chargeType.(string) != CVM_CHARGE_TYPE_PREPAID {
1027-
return fmt.Errorf("Only support change chargeType from POSTPAID_BY_HOUR to PREPAID.")
1028-
}
1025+
chargeType, chargeOk := d.GetOk("instance_charge_type")
1026+
if chargeOk {
1027+
expectChargeType = chargeType.(string)
1028+
}
1029+
1030+
if d.HasChange("instance_charge_type") && expectChargeType != currentChargeType {
10291031
var (
1030-
period int
1032+
period = -1
10311033
renewFlag string
10321034
)
1035+
10331036
if v, ok := d.GetOk("instance_charge_type_prepaid_period"); ok {
10341037
period = v.(int)
10351038
}
10361039
if v, ok := d.GetOk("instance_charge_type_prepaid_renew_flag"); ok {
10371040
renewFlag = v.(string)
10381041
}
10391042
// change charge type
1040-
err := cvmService.ModifyInstanceChargeType(ctx, instanceId, chargeType.(string), period, renewFlag)
1043+
err := cvmService.ModifyInstanceChargeType(ctx, instanceId, expectChargeType, period, renewFlag)
10411044
if err != nil {
10421045
return err
10431046
}

tencentcloud/resource_tc_instance_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,39 @@ func TestAccTencentCloudInstancePostpaidToPrepaid(t *testing.T) {
529529
})
530530
}
531531

532+
func TestAccTencentCloudInstancePrepaidFallbackToPostpaid(t *testing.T) {
533+
534+
id := "tencentcloud_instance.foo"
535+
resource.Test(t, resource.TestCase{
536+
PreCheck: func() { testAccPreCheck(t) },
537+
IDRefreshName: id,
538+
Providers: testAccProviders,
539+
CheckDestroy: testAccCheckInstanceDestroy,
540+
Steps: []resource.TestStep{
541+
{
542+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
543+
Config: testAccTencentCloudInstanceBasicToPrepaid,
544+
Check: resource.ComposeTestCheckFunc(
545+
testAccCheckTencentCloudDataSourceID(id),
546+
testAccCheckTencentCloudInstanceExists(id),
547+
resource.TestCheckResourceAttr(id, "instance_charge_type", "PREPAID"),
548+
resource.TestCheckResourceAttr(id, "instance_charge_type_prepaid_period", "1"),
549+
resource.TestCheckResourceAttr(id, "instance_charge_type_prepaid_renew_flag", "NOTIFY_AND_MANUAL_RENEW"),
550+
),
551+
},
552+
{
553+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
554+
Config: testAccTencentCloudInstancePostPaid,
555+
Check: resource.ComposeTestCheckFunc(
556+
testAccCheckTencentCloudDataSourceID(id),
557+
testAccCheckTencentCloudInstanceExists(id),
558+
resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"),
559+
),
560+
},
561+
},
562+
})
563+
}
564+
532565
func testAccCheckTencentCloudInstanceExists(n string) resource.TestCheckFunc {
533566
return func(s *terraform.State) error {
534567
logId := getLogId(contextNil)

tencentcloud/service_tencentcloud_cvm.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,12 +1038,14 @@ func (me *CvmService) ModifyInstanceChargeType(ctx context.Context, instanceId s
10381038
request := cvm.NewModifyInstancesChargeTypeRequest()
10391039
request.InstanceIds = []*string{&instanceId}
10401040
request.InstanceChargeType = &chargeType
1041-
request.InstanceChargePrepaid = &cvm.InstanceChargePrepaid{}
1042-
if period != -1 {
1043-
request.InstanceChargePrepaid.Period = helper.IntInt64(period)
1044-
}
1045-
if renewFlag != "" {
1046-
request.InstanceChargePrepaid.RenewFlag = &renewFlag
1041+
if chargeType == CVM_CHARGE_TYPE_PREPAID {
1042+
request.InstanceChargePrepaid = &cvm.InstanceChargePrepaid{}
1043+
if period != -1 {
1044+
request.InstanceChargePrepaid.Period = helper.IntInt64(period)
1045+
}
1046+
if renewFlag != "" {
1047+
request.InstanceChargePrepaid.RenewFlag = &renewFlag
1048+
}
10471049
}
10481050

10491051
ratelimit.Check(request.GetAction())

0 commit comments

Comments
 (0)