Skip to content

Commit

Permalink
resource/baiducloud_instance: Add parameter stop_with_no_charge
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBeng committed Aug 29, 2023
1 parent ce9a663 commit 48d038a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion baiducloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions baiducloud/resource_baiducloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions baiducloud/service_baiducloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 48d038a

Please sign in to comment.