From 48d038a2d99d4593d156067039cec50d29fb58ec Mon Sep 17 00:00:00 2001 From: BenBeng Date: Tue, 29 Aug 2023 12:04:42 +0800 Subject: [PATCH] resource/baiducloud_instance: Add parameter `stop_with_no_charge` --- CHANGELOG.md | 6 +++++- baiducloud/connectivity/client.go | 2 +- baiducloud/resource_baiducloud_instance.go | 17 +++++++++++++---- baiducloud/service_baiducloud_instance.go | 4 ++-- website/docs/r/instance.html.markdown | 1 + 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a50f7d..6c362338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ - ## 1.19.14 (Unreleased) + ## 1.19.15 (Unreleased) + +## 1.19.14 (August 29, 2023) +ENHANCEMENTS: +- resource/baiducloud_instance: Add parameter `stop_with_no_charge` to support stopping charging after shutdown for postpaid instance without local disks. ## 1.19.13 (August 22, 2023) FEATURES: diff --git a/baiducloud/connectivity/client.go b/baiducloud/connectivity/client.go index 81dd541d..1a30ac5d 100644 --- a/baiducloud/connectivity/client.go +++ b/baiducloud/connectivity/client.go @@ -72,7 +72,7 @@ type ApiVersion string var goSdkMutex = sync.RWMutex{} // The Go SDK is not thread-safe -var providerVersion = "1.19.13" +var providerVersion = "1.19.14" // Client for BaiduCloudClient func (c *Config) Client() (*BaiduClient, error) { diff --git a/baiducloud/resource_baiducloud_instance.go b/baiducloud/resource_baiducloud_instance.go index 32006dd6..87e05932 100644 --- a/baiducloud/resource_baiducloud_instance.go +++ b/baiducloud/resource_baiducloud_instance.go @@ -28,6 +28,9 @@ $ terraform import baiducloud_instance.my-server id package baiducloud import ( + "strconv" + "time" + "github.com/baidubce/bce-sdk-go/bce" "github.com/baidubce/bce-sdk-go/services/bcc" "github.com/baidubce/bce-sdk-go/services/bcc/api" @@ -36,8 +39,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity" "github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/rateLimit" - "strconv" - "time" ) func resourceBaiduCloudInstance() *schema.Resource { @@ -403,6 +404,12 @@ func resourceBaiduCloudInstance() *schema.Resource { Description: "Resource group Id of the instance.", Optional: true, }, + "stop_with_no_charge": { + Type: schema.TypeBool, + Description: "Whether to enable stopping charging after shutdown for postpaid instance without local disks. Defaults to false.", + Optional: true, + Default: false, + }, }, } } @@ -515,7 +522,8 @@ func resourceBaiduCloudInstanceCreate(d *schema.ResourceData, meta interface{}) // stop the instance if the action field is stop. if d.Get("action").(string) == INSTANCE_ACTION_STOP { - if err := bccService.StopInstance(d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { + stopWithNoCharge := d.Get("stop_with_no_charge").(bool) + if err := bccService.StopInstance(d.Id(), stopWithNoCharge, d.Timeout(schema.TimeoutUpdate)); err != nil { return err } } @@ -1335,7 +1343,8 @@ func updateInstanceAction(d *schema.ResourceData, meta interface{}, instanceID s return err } } else if act == INSTANCE_ACTION_STOP { - if err := bccService.StopInstance(instanceID, d.Timeout(schema.TimeoutUpdate)); err != nil { + stopWithNoCharge := d.Get("stop_with_no_charge").(bool) + if err := bccService.StopInstance(instanceID, stopWithNoCharge, d.Timeout(schema.TimeoutUpdate)); err != nil { return err } } diff --git a/baiducloud/service_baiducloud_instance.go b/baiducloud/service_baiducloud_instance.go index 571cfcbf..b0eeb630 100644 --- a/baiducloud/service_baiducloud_instance.go +++ b/baiducloud/service_baiducloud_instance.go @@ -229,11 +229,11 @@ func (s *BccService) StartInstance(instanceID string, timeout time.Duration) err return nil } -func (s *BccService) StopInstance(instanceID string, timeout time.Duration) error { +func (s *BccService) StopInstance(instanceID string, stopWithNoCharge bool, timeout time.Duration) error { action := "Stop instance " + instanceID _, err := s.client.WithBccClient(func(bccClient *bcc.Client) (i interface{}, e error) { - return nil, bccClient.StopInstance(instanceID, false) + return nil, bccClient.StopInstanceWithNoCharge(instanceID, false, stopWithNoCharge) }) if err != nil { return WrapErrorf(err, DefaultErrorMsg, "baiducloud_instance", action, BCESDKGoERROR) diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 9ea75e15..8c06d72f 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -87,6 +87,7 @@ The following arguments are supported: * `root_disk_size_in_gb` - (Optional, ForceNew) System disk size(GB) of the instance to be created. The value range is [40,500]GB, Default to 40GB, and more than 40GB is charged according to the cloud disk price. Note that the specified system disk size needs to meet the minimum disk space limit of the mirror used. * `root_disk_storage_type` - (Optional, ForceNew) System disk storage type of the instance. Available values are std1, hp1, cloud_hp1, local, sata, ssd. Default to cloud_hp1. * `security_groups` - (Optional) Security groups of the instance. +* `stop_with_no_charge` - (Optional) Whether to enable stopping charging after shutdown for postpaid instance without local disks. Defaults to false. * `subnet_id` - (Optional) The subnet ID of VPC. The default subnet will be used when it is empty. The instance will restart after changing the subnet. * `tags` - (Optional, ForceNew) Tags, do not support modify * `user_data` - (Optional) User Data