Skip to content

Commit

Permalink
resource/alicloud_instance: add the field stopped_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanghx0991 committed May 30, 2022
1 parent 725ce2b commit 41f7a7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
16 changes: 11 additions & 5 deletions alicloud/resource_alicloud_instance.go
Expand Up @@ -438,6 +438,12 @@ func resourceAliyunInstance() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"upgrade", "downgrade"}, false),
},
"stopped_mode": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"StopCharging", "KeepCharging"}, false),
},
},
}
}
Expand Down Expand Up @@ -549,6 +555,7 @@ func resourceAliyunInstanceRead(d *schema.ResourceData, meta interface{}) error
d.Set("hpc_cluster_id", instance.HpcClusterId)
d.Set("deployment_set_id", instance.DeploymentSetId)
d.Set("deployment_set_group_no", instance.DeploymentSetGroupNo)
d.Set("stopped_mode", instance.StoppedMode)
if len(instance.PublicIpAddress.IpAddress) > 0 {
d.Set("public_ip", instance.PublicIpAddress.IpAddress[0])
} else {
Expand Down Expand Up @@ -810,15 +817,14 @@ func resourceAliyunInstanceUpdate(d *schema.ResourceData, meta interface{}) erro
}
if imageUpdate || vpcUpdate || passwordUpdate || typeUpdate || statusUpdate {
run = true
instance, errDesc := ecsService.DescribeInstance(d.Id())
if errDesc != nil {
return WrapError(errDesc)
}
if (statusUpdate && targetExist && target == string(Stopped)) || instance.Status == string(Running) {
if statusUpdate && targetExist && target == string(Stopped) {
stopRequest := ecs.CreateStopInstanceRequest()
stopRequest.RegionId = client.RegionId
stopRequest.InstanceId = d.Id()
stopRequest.ForceStop = requests.NewBoolean(false)
if v, ok := d.GetOk("stopped_mode"); ok {
stopRequest.StoppedMode = v.(string)
}
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithEcsClient(func(ecsClient *ecs.Client) (interface{}, error) {
return ecsClient.StopInstance(stopRequest)
Expand Down
12 changes: 12 additions & 0 deletions alicloud/resource_alicloud_instance_test.go
Expand Up @@ -2014,6 +2014,18 @@ func TestAccAlicloudECSInstance_StatusUpdated(t *testing.T) {
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"status": "Stopped",
"stopped_mode": "KeepCharging",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"status": "Stopped",
"stopped_mode": "KeepCharging",
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/instance.html.markdown
Expand Up @@ -212,7 +212,9 @@ Set it to null can cancel automatic release attribute and the ECS instance will
* `secondary_private_ip_address_count` - (Optional, Available in 1.145.0+) The number of private IP addresses to be automatically assigned from within the CIDR block of the vswitch. **NOTE:** To assign secondary private IP addresses, you must specify `secondary_private_ips` or `secondary_private_ip_address_count` but not both.
* `deployment_set_id` - (Optional, Available in 1.149.0+) The ID of the deployment set to which to deploy the instance.
* `operator_type` - (Optional, Available in 1.164.0+) The operation type. It is valid when `instance_charge_type` is `PrePaid`. Default value: `upgrade`. Valid values: `upgrade`, `downgrade`. **NOTE:** When the new instance type specified by the `instance_type` parameter has lower specifications than the current instance type, you must set `operator_type` to `downgrade`.

* `stopped_mode` - (Optional,Available in 1.170.0+ ) The stop mode of the pay-as-you-go instance. Valid values: `StopCharging`,`KeepCharging`. Default value: If the prerequisites required for enabling the economical mode are met, and you have enabled this mode in the ECS console, the default value is `StopCharging`. For more information, see "Enable the economical mode" in [Economical mode](https://www.alibabacloud.com/help/en/elastic-compute-service/latest/economical-mode). Otherwise, the default value is `KeepCharging`. **Note:** `Not-applicable`: Economical mode is not applicable to the instance.`
* `KeepCharging` : standard mode. Billing of the instance continues after the instance is stopped, and resources are retained for the instance.
* `StopCharging` : economical mode. Billing of some resources of the instance stops after the instance is stopped. When the instance is stopped, its resources such as vCPUs, memory, and public IP address are released. You may be unable to restart the instance if some types of resources are out of stock in the current region.

-> **NOTE:** System disk category `cloud` has been outdated and it only can be used none I/O Optimized ECS instances. Recommend `cloud_efficiency` and `cloud_ssd` disk.

Expand Down

0 comments on commit 41f7a7b

Please sign in to comment.