Skip to content

Commit 2165417

Browse files
author
hellertang
authored
cvm support change chargeType (#811)
1 parent 8ad6cca commit 2165417

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

tencentcloud/resource_tc_instance.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ func resourceTencentCloudInstance() *schema.Resource {
229229
"instance_charge_type": {
230230
Type: schema.TypeString,
231231
Optional: true,
232-
ForceNew: true,
233232
Default: CVM_CHARGE_TYPE_POSTPAID,
234233
ValidateFunc: validateAllowedStringValue(CVM_CHARGE_TYPE),
235234
Description: "The charge type of instance. Valid values are `PREPAID`, `POSTPAID_BY_HOUR`, `SPOTPAID` and `CDHPAID`. The default is `POSTPAID_BY_HOUR`. Note: TencentCloud International only supports `POSTPAID_BY_HOUR` and `CDHPAID`. `PREPAID` instance may not allow to delete before expired. `SPOTPAID` instance must set `spot_instance_type` and `spot_max_price` at the same time. `CDHPAID` instance must set `cdh_instance_type` and `cdh_host_id`.",
@@ -968,12 +967,53 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
968967

969968
d.Partial(true)
970969

971-
unsupportedUpdateFields := []string{
972-
"instance_charge_type_prepaid_period",
970+
if d.HasChange("instance_charge_type") {
971+
old, chargeType := d.GetChange("instance_charge_type")
972+
if old.(string) != CVM_CHARGE_TYPE_POSTPAID || chargeType.(string) != CVM_CHARGE_TYPE_PREPAID {
973+
return fmt.Errorf("Only support change chargeType from POSTPAID_BY_HOUR to PREPAID.")
974+
}
975+
var (
976+
period int
977+
renewFlag string
978+
)
979+
if v, ok := d.GetOk("instance_charge_type_prepaid_period"); ok {
980+
period = v.(int)
981+
} else {
982+
return fmt.Errorf("instance charge type prepaid period can not"+
983+
" be empty when charge type is %s", chargeType)
984+
}
985+
if v, ok := d.GetOk("instance_charge_type_prepaid_renew_flag"); ok {
986+
renewFlag = v.(string)
987+
}
988+
// change charge type
989+
err := cvmService.ModifyInstanceChargeType(ctx, instanceId, chargeType.(string), period, renewFlag)
990+
if err != nil {
991+
return err
992+
}
993+
// query cvm status
994+
err = resource.Retry(5*readRetryTimeout, func() *resource.RetryError {
995+
instance, errRet := cvmService.DescribeInstanceById(ctx, instanceId)
996+
if errRet != nil {
997+
return retryError(errRet, InternalError)
998+
}
999+
if instance != nil && (*instance.InstanceState == CVM_STATUS_RUNNING ||
1000+
*instance.InstanceState == CVM_STATUS_LAUNCH_FAILED) {
1001+
return nil
1002+
}
1003+
return resource.RetryableError(fmt.Errorf("cvm instance status is %s, retry...", *instance.InstanceState))
1004+
})
1005+
if err != nil {
1006+
return err
1007+
}
9731008
}
974-
for _, field := range unsupportedUpdateFields {
975-
if d.HasChange(field) {
976-
return fmt.Errorf("tencentcloud_cvm_instance update on %s is not support yet", field)
1009+
1010+
if d.HasChange("instance_charge_type_prepaid_period") {
1011+
chargeType := d.Get("instance_charge_type").(string)
1012+
if chargeType != CVM_CHARGE_TYPE_PREPAID {
1013+
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")
1014+
}
1015+
if !d.HasChange("instance_charge_type") {
1016+
return fmt.Errorf("tencentcloud_cvm_instance update on instance_charge_type_prepaid_period is only with charge type change to PREPAID")
9771017
}
9781018
}
9791019

tencentcloud/service_tencentcloud_cvm.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,30 @@ func (me *CvmService) ModifyRenewParam(ctx context.Context, instanceId string, r
10271027

10281028
return nil
10291029
}
1030+
1031+
func (me *CvmService) ModifyInstanceChargeType(ctx context.Context, instanceId string, chargeType string, period int,
1032+
renewFlag string) error {
1033+
logId := getLogId(ctx)
1034+
request := cvm.NewModifyInstancesChargeTypeRequest()
1035+
request.InstanceIds = []*string{&instanceId}
1036+
request.InstanceChargeType = &chargeType
1037+
request.InstanceChargePrepaid = &cvm.InstanceChargePrepaid{}
1038+
if period != -1 {
1039+
request.InstanceChargePrepaid.Period = helper.IntInt64(period)
1040+
}
1041+
if renewFlag != "" {
1042+
request.InstanceChargePrepaid.RenewFlag = &renewFlag
1043+
}
1044+
1045+
ratelimit.Check(request.GetAction())
1046+
response, err := me.client.UseCvmClient().ModifyInstancesChargeType(request)
1047+
if err != nil {
1048+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
1049+
logId, request.GetAction(), request.ToJsonString(), err.Error())
1050+
return err
1051+
}
1052+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
1053+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1054+
1055+
return nil
1056+
}

website/docs/r/instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ The following arguments are supported:
153153
* `hostname` - (Optional) The hostname of the instance. Windows instance: The name should be a combination of 2 to 15 characters comprised of letters (case insensitive), numbers, and hyphens (-). Period (.) is not supported, and the name cannot be a string of pure numbers. Other types (such as Linux) of instances: The name should be a combination of 2 to 60 characters, supporting multiple periods (.). The piece between two periods is composed of letters (case insensitive), numbers, and hyphens (-). Modifying will cause the instance reset.
154154
* `instance_charge_type_prepaid_period` - (Optional) The tenancy (time unit is month) of the prepaid instance, NOTE: it only works when instance_charge_type is set to `PREPAID`. Valid values are `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`.
155155
* `instance_charge_type_prepaid_renew_flag` - (Optional) Auto renewal flag. Valid values: `NOTIFY_AND_AUTO_RENEW`: notify upon expiration and renew automatically, `NOTIFY_AND_MANUAL_RENEW`: notify upon expiration but do not renew automatically, `DISABLE_NOTIFY_AND_MANUAL_RENEW`: neither notify upon expiration nor renew automatically. Default value: `NOTIFY_AND_MANUAL_RENEW`. If this parameter is specified as `NOTIFY_AND_AUTO_RENEW`, the instance will be automatically renewed on a monthly basis if the account balance is sufficient. NOTE: it only works when instance_charge_type is set to `PREPAID`.
156-
* `instance_charge_type` - (Optional, ForceNew) The charge type of instance. Valid values are `PREPAID`, `POSTPAID_BY_HOUR`, `SPOTPAID` and `CDHPAID`. The default is `POSTPAID_BY_HOUR`. Note: TencentCloud International only supports `POSTPAID_BY_HOUR` and `CDHPAID`. `PREPAID` instance may not allow to delete before expired. `SPOTPAID` instance must set `spot_instance_type` and `spot_max_price` at the same time. `CDHPAID` instance must set `cdh_instance_type` and `cdh_host_id`.
156+
* `instance_charge_type` - (Optional) The charge type of instance. Valid values are `PREPAID`, `POSTPAID_BY_HOUR`, `SPOTPAID` and `CDHPAID`. The default is `POSTPAID_BY_HOUR`. Note: TencentCloud International only supports `POSTPAID_BY_HOUR` and `CDHPAID`. `PREPAID` instance may not allow to delete before expired. `SPOTPAID` instance must set `spot_instance_type` and `spot_max_price` at the same time. `CDHPAID` instance must set `cdh_instance_type` and `cdh_host_id`.
157157
* `instance_count` - (Optional, **Deprecated**) It has been deprecated from version 1.59.18. Use built-in `count` instead. The number of instances to be purchased. Value range:[1,100]; default value: 1.
158158
* `instance_name` - (Optional) The name of the instance. The max length of instance_name is 60, and default value is `Terraform-CVM-Instance`.
159159
* `instance_type` - (Optional) The type of the instance.

0 commit comments

Comments
 (0)