Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3562.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_instance: support `stop_type`
```
3 changes: 3 additions & 0 deletions tencentcloud/services/cvm/extension_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 14 additions & 2 deletions tencentcloud/services/cvm/resource_tc_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 29 additions & 0 deletions tencentcloud/services/cvm/resource_tc_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion tencentcloud/services/cvm/service_tencentcloud_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
Loading