From 3f828391aea25f2fbb154063abc9ad5d6548dcd8 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Mon, 20 Oct 2025 19:44:14 +0800 Subject: [PATCH 1/2] add --- tencentcloud/services/cvm/extension_cvm.go | 3 ++ .../services/cvm/resource_tc_instance.go | 16 ++++++++-- .../services/cvm/resource_tc_instance.md | 29 ++++++++++++++++++ .../services/cvm/service_tencentcloud_cvm.go | 6 +++- website/docs/r/instance.html.markdown | 30 +++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) diff --git a/tencentcloud/services/cvm/extension_cvm.go b/tencentcloud/services/cvm/extension_cvm.go index 1d22b627f3..f8e4f7109c 100644 --- a/tencentcloud/services/cvm/extension_cvm.go +++ b/tencentcloud/services/cvm/extension_cvm.go @@ -61,6 +61,9 @@ const ( // @Deprecated use cvm.RESOURCEINSUFFICIENT_CLOUDDISKSOLDOUT instead CVM_CLOUD_DISK_SOLD_OUT_ERROR = "ResourceInsufficient.CloudDiskSoldOut" + CVM_STOP_TYPE_SOFT_FIRST = "SOFT_FIRST" + CVM_STOP_TYPE_HARD = "HARD" + CVM_STOP_TYPE_SOFT = "SOFT" CVM_STOP_MODE_KEEP_CHARGING = "KEEP_CHARGING" CVM_STOP_MODE_STOP_CHARGING = "STOP_CHARGING" CVM_SELL_STATUS = "SELL" diff --git a/tencentcloud/services/cvm/resource_tc_instance.go b/tencentcloud/services/cvm/resource_tc_instance.go index 321b52fe2c..8fc61b9ac3 100644 --- a/tencentcloud/services/cvm/resource_tc_instance.go +++ b/tencentcloud/services/cvm/resource_tc_instance.go @@ -95,6 +95,16 @@ func ResourceTencentCloudInstance() *schema.Resource { Computed: true, Description: "Set instance to running or stop. Default value is true, the instance will shutdown when this flag is false.", }, + "stop_type": { + Type: schema.TypeString, + Optional: true, + Description: "Instance shutdown mode. Valid values: SOFT_FIRST: perform a soft shutdown first, and force shut down the instance if the soft shutdown fails; HARD: force shut down the instance directly; SOFT: soft shutdown only. Default value: SOFT.", + ValidateFunc: tccommon.ValidateAllowedStringValue([]string{ + CVM_STOP_TYPE_SOFT_FIRST, + CVM_STOP_TYPE_HARD, + CVM_STOP_TYPE_SOFT, + }), + }, "stopped_mode": { Type: schema.TypeString, Optional: true, @@ -1086,8 +1096,9 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} if v, ok := d.GetOkExists("running_flag"); ok { if !v.(bool) { + stopType := d.Get("stop_type").(string) stoppedMode := d.Get("stopped_mode").(string) - err = cvmService.StopInstance(ctx, instanceId, stoppedMode) + err = cvmService.StopInstance(ctx, instanceId, stopType, stoppedMode) if err != nil { return err } @@ -2784,12 +2795,13 @@ func switchInstance(cvmService *CvmService, ctx context.Context, d *schema.Resou return err } } else { + stopType := d.Get("stop_type").(string) stoppedMode := d.Get("stopped_mode").(string) skipStopApi := false err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { // when retry polling instance status, stop instance should skipped if !skipStopApi { - err := cvmService.StopInstance(ctx, instanceId, stoppedMode) + err := cvmService.StopInstance(ctx, instanceId, stopType, stoppedMode) if err != nil { return resource.NonRetryableError(err) } diff --git a/tencentcloud/services/cvm/resource_tc_instance.md b/tencentcloud/services/cvm/resource_tc_instance.md index b15622f652..c63ec2b396 100644 --- a/tencentcloud/services/cvm/resource_tc_instance.md +++ b/tencentcloud/services/cvm/resource_tc_instance.md @@ -279,6 +279,35 @@ resource "tencentcloud_instance" "example" { } ``` +Create CVM instance with setting running flag + +```hcl +resource "tencentcloud_instance" "example" { + instance_name = "tf-example" + availability_zone = "ap-guangzhou-6" + image_id = "img-eb30mz89" + instance_type = "S5.MEDIUM4" + system_disk_type = "CLOUD_HSSD" + system_disk_size = 50 + hostname = "user" + project_id = 0 + vpc_id = "vpc-i5yyodl9" + subnet_id = "subnet-hhi88a58" + orderly_security_groups = ["sg-ma82yjwp"] + running_flag = false + stop_type = "SOFT_FIRST" + stopped_mode = "KEEP_CHARGING" + data_disks { + data_disk_type = "CLOUD_HSSD" + data_disk_size = 100 + encrypt = false + } + tags = { + tagKey = "tagValue" + } +} +``` + Import CVM instance can be imported using the id, e.g. diff --git a/tencentcloud/services/cvm/service_tencentcloud_cvm.go b/tencentcloud/services/cvm/service_tencentcloud_cvm.go index cac7376503..333241cf1b 100644 --- a/tencentcloud/services/cvm/service_tencentcloud_cvm.go +++ b/tencentcloud/services/cvm/service_tencentcloud_cvm.go @@ -436,10 +436,14 @@ func (me *CvmService) ModifyVpc(ctx context.Context, instanceId, vpcId, subnetId return nil } -func (me *CvmService) StopInstance(ctx context.Context, instanceId string, stoppedMode string) error { +func (me *CvmService) StopInstance(ctx context.Context, instanceId string, stopType string, stoppedMode string) error { logId := tccommon.GetLogId(ctx) request := cvm.NewStopInstancesRequest() request.InstanceIds = []*string{&instanceId} + if stopType != "" { + request.StopType = &stopType + } + if stoppedMode != "" { request.StoppedMode = &stoppedMode } diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 618f316d83..cfd0db0eb7 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -290,6 +290,35 @@ resource "tencentcloud_instance" "example" { } ``` +### Create CVM instance with setting running flag + +```hcl +resource "tencentcloud_instance" "example" { + instance_name = "tf-example" + availability_zone = "ap-guangzhou-6" + image_id = "img-eb30mz89" + instance_type = "S5.MEDIUM4" + system_disk_type = "CLOUD_HSSD" + system_disk_size = 50 + hostname = "user" + project_id = 0 + vpc_id = "vpc-i5yyodl9" + subnet_id = "subnet-hhi88a58" + orderly_security_groups = ["sg-ma82yjwp"] + running_flag = false + stop_type = "SOFT_FIRST" + stopped_mode = "KEEP_CHARGING" + data_disks { + data_disk_type = "CLOUD_HSSD" + data_disk_size = 100 + encrypt = false + } + tags = { + tagKey = "tagValue" + } +} +``` + ## Argument Reference The following arguments are supported: @@ -337,6 +366,7 @@ The following arguments are supported: * `security_groups` - (Optional, Set: [`String`], **Deprecated**) It will be deprecated. Use `orderly_security_groups` instead. A list of security group IDs to associate with. * `spot_instance_type` - (Optional, String) Type of spot instance, only support `ONE-TIME` now. Note: it only works when instance_charge_type is set to `SPOTPAID`. * `spot_max_price` - (Optional, String, ForceNew) Max price of a spot instance, is the format of decimal string, for example "0.50". Note: it only works when instance_charge_type is set to `SPOTPAID`. +* `stop_type` - (Optional, String) Instance shutdown mode. Valid values: SOFT_FIRST: perform a soft shutdown first, and force shut down the instance if the soft shutdown fails; HARD: force shut down the instance directly; SOFT: soft shutdown only. Default value: SOFT. * `stopped_mode` - (Optional, String) Billing method of a pay-as-you-go instance after shutdown. Available values: `KEEP_CHARGING`,`STOP_CHARGING`. Default `KEEP_CHARGING`. * `subnet_id` - (Optional, String) The ID of a VPC subnet. If you want to create instances in a VPC network, this parameter must be set. * `system_disk_id` - (Optional, String) System disk snapshot ID used to initialize the system disk. When system disk type is `LOCAL_BASIC` and `LOCAL_SSD`, disk id is not supported. From 41fc5b1e8816ee4689634def3a9e94be6caf30a0 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Mon, 20 Oct 2025 19:46:03 +0800 Subject: [PATCH 2/2] add --- .changelog/3562.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3562.txt diff --git a/.changelog/3562.txt b/.changelog/3562.txt new file mode 100644 index 0000000000..b0682e1901 --- /dev/null +++ b/.changelog/3562.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_instance: support `stop_type` +```