diff --git a/.changelog/3614.txt b/.changelog/3614.txt new file mode 100644 index 0000000000..95869f624b --- /dev/null +++ b/.changelog/3614.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_instance: optimiz create function code logic +``` \ No newline at end of file diff --git a/tencentcloud/services/cvm/resource_tc_instance.go b/tencentcloud/services/cvm/resource_tc_instance.go index 8fc61b9ac3..196bc4c8d5 100644 --- a/tencentcloud/services/cvm/resource_tc_instance.go +++ b/tencentcloud/services/cvm/resource_tc_instance.go @@ -579,9 +579,10 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} defer tccommon.LogElapsed("resource.tencentcloud_instance.create")() var ( - logId = tccommon.GetLogId(tccommon.ContextNil) - ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - cvmService = CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + cvmService = CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + instanceChargeType = CVM_CHARGE_TYPE_POSTPAID ) request := cvm.NewRunInstancesRequest() @@ -625,7 +626,7 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} } if v, ok := d.GetOk("instance_charge_type"); ok { - instanceChargeType := v.(string) + instanceChargeType = v.(string) request.InstanceChargeType = &instanceChargeType if instanceChargeType == CVM_CHARGE_TYPE_PREPAID || instanceChargeType == CVM_CHARGE_TYPE_UNDERWRITE { request.InstanceChargePrepaid = &cvm.InstanceChargePrepaid{} @@ -827,6 +828,10 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} if deleteWithInstance, ok := value["delete_with_instance"]; ok { deleteWithInstanceBool := deleteWithInstance.(bool) + if (instanceChargeType != CVM_CHARGE_TYPE_POSTPAID) && deleteWithInstanceBool { + return fmt.Errorf("param `delete_with_instance` only can be true when `instance_charge_type` is %s", CVM_CHARGE_TYPE_POSTPAID) + } + dataDisk.DeleteWithInstance = &deleteWithInstanceBool }