Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prePaid support for gaussdb_mysql_instance #733

Merged
merged 1 commit into from
Dec 8, 2020
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
9 changes: 9 additions & 0 deletions docs/resources/gaussdb_mysql_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ The following arguments are supported:

* `force_import` - (Optional, Bool, ForceNew) If specified, try to import the instance instead of creating if the name already existed.

* `charging_mode` - (Optional, String, ForceNew) The charging mode of the instance. Valid options are: prePaid and postPaid, defaults to postPaid. Changing this creates a new resource.

* `period_unit` - (Optional, String, ForceNew) The charging period unit of the instance. Valid options are: month and year, defaults to month. Changing this creates a new resource.

* `period` - (Optional, Int, ForceNew) The charging period of the instance. Changing this creates a new resource.

* `auto_renew` - (Optional, String, ForceNew) Specifies whether auto renew is enabled. Changing this creates a new resource.


The `datastore` block supports:

* `engine` - (Optional, String, ForceNew) Specifies the database engine. Only "gauss-mysql" is supported now.
Expand Down
74 changes: 65 additions & 9 deletions huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/bss/v2/orders"
"github.com/huaweicloud/golangsdk/openstack/taurusdb/v3/backups"
"github.com/huaweicloud/golangsdk/openstack/taurusdb/v3/instances"
)
Expand Down Expand Up @@ -143,6 +144,32 @@ func resourceGaussDBInstance() *schema.Resource {
Optional: true,
ForceNew: true,
},
"charging_mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"prePaid", "postPaid",
}, true),
},
"period_unit": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"month", "year",
}, true),
},
"period": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"auto_renew": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -290,6 +317,18 @@ func resourceGaussDBInstanceCreate(d *schema.ResourceData, meta interface{}) err
createOpts.MasterAZ = v.(string)
}

// PrePaid
if d.Get("charging_mode") == "prePaid" {
chargeInfo := &instances.ChargeInfoOpt{
ChargingMode: d.Get("charging_mode").(string),
PeriodType: d.Get("period_unit").(string),
PeriodNum: d.Get("period").(int),
IsAutoPay: "true",
IsAutoRenew: d.Get("auto_renew").(string),
}
createOpts.ChargeInfo = chargeInfo
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)

instance, err := instances.Create(client, createOpts).Extract()
Expand All @@ -302,12 +341,12 @@ func resourceGaussDBInstanceCreate(d *schema.ResourceData, meta interface{}) err

// waiting for the instance to become ready
stateConf := &resource.StateChangeConf{
Pending: []string{"BUILD", "BACKING UP"},
Target: []string{"ACTIVE"},
Refresh: GaussDBInstanceStateRefreshFunc(client, id),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 180 * time.Second,
MinTimeout: 20 * time.Second,
Pending: []string{"BUILD", "BACKING UP"},
Target: []string{"ACTIVE"},
Refresh: GaussDBInstanceStateRefreshFunc(client, id),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 180 * time.Second,
PollInterval: 20 * time.Second,
}

_, err = stateConf.WaitForState()
Expand Down Expand Up @@ -581,9 +620,26 @@ func resourceGaussDBInstanceDelete(d *schema.ResourceData, meta interface{}) err
}

instanceId := d.Id()
result := instances.Delete(client, instanceId)
if result.Err != nil {
return CheckDeleted(d, result.Err, "GaussDB instance")
if d.Get("charging_mode") == "prePaid" {
bssV2Client, err := config.BssV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud bss V2 client: %s", err)
}

resourceIds := []string{instanceId}
unsubscribeOpts := orders.UnsubscribeOpts{
ResourceIds: resourceIds,
UnsubscribeType: 1,
}
_, err = orders.Unsubscribe(bssV2Client, unsubscribeOpts).Extract()
if err != nil {
return fmt.Errorf("Error unsubscribe HuaweiCloud GaussDB instance: %s", err)
}
} else {
result := instances.Delete(client, instanceId)
if result.Err != nil {
return CheckDeleted(d, result.Err, "GaussDB instance")
}
}

stateConf := &resource.StateChangeConf{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ func testAccGaussDBInstanceConfig_basic(rName string) string {
return fmt.Sprintf(`
%s

data "huaweicloud_availability_zones" "test" {}

data "huaweicloud_networking_secgroup" "test" {
name = "default"
}
Expand Down