Skip to content

Commit

Permalink
Support automatic backup capability of SCS
Browse files Browse the repository at this point in the history
  • Loading branch information
tamperMonkeyZQ committed Jan 31, 2024
1 parent 0fbbb47 commit c674b30
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 66 deletions.
191 changes: 132 additions & 59 deletions baiducloud/resource_baiducloud_scs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,78 @@ More information about SCS can be found in the [Developer Guide](https://cloud.b
~> **NOTE:** The terminate operation of scs does NOT take effect immediately,maybe takes for several minites.
Example Usage
# Example Usage
### Memcache
~> **NOTE:** Memcache currently does NOT support specifying `node_type`, set to `cache.n1.micro` directly.
```terraform
resource "baiducloud_scs" "default" {
payment_timing = "Postpaid"
instance_name = "terraform-memcache"
engine = "memcache"
port = 11211
node_type = "cache.n1.micro"
cluster_type = "defalut"
shard_num = 2
}
resource "baiducloud_scs" "default" {
payment_timing = "Postpaid"
instance_name = "terraform-memcache"
engine = "memcache"
port = 11211
node_type = "cache.n1.micro"
cluster_type = "defalut"
shard_num = 2
}
```
### Redis
```terraform
resource "baiducloud_scs" "default" {
payment_timing = "Postpaid"
instance_name = "terraform-redis"
port = 6379
engine_version = "3.2"
node_type = "cache.n1.micro"
cluster_type = "master_slave"
replication_num = 1
shard_num = 1
}
resource "baiducloud_scs" "default" {
payment_timing = "Postpaid"
instance_name = "terraform-redis"
port = 6379
engine_version = "3.2"
node_type = "cache.n1.micro"
cluster_type = "master_slave"
replication_num = 1
shard_num = 1
}
```
### PegaDb
```terraform
resource "baiducloud_scs" "default" {
payment_timing = "Prepaid"
reservation_length = 2
reservation_time_unit = "month"
instance_name = "terraform-pegadb"
purchase_count = 1
engine = "PegaDB"
node_type = "pega.g4s1.micro"
cluster_type = "cluster"
store_type = 3
disk_flavor = 60
port = 6379
replication_num = 2
shard_num = 1
proxy_num = 2
vpc_id = "vpc-ne32rahkaceu"
subnets {
subnet_id = "sbn-vhnqd71mivjq"
zone_name = "cn-bj-d"
}
replication_info {
availability_zone = "cn-bj-d"
is_master = 1
subnet_id = "sbn-vhnqd71mivjq"
}
replication_info {
availability_zone = "cn-bj-d"
is_master = 0
subnet_id = "sbn-vhnqd71mivjq"
resource "baiducloud_scs" "default" {
payment_timing = "Prepaid"
reservation_length = 2
reservation_time_unit = "month"
instance_name = "terraform-pegadb"
purchase_count = 1
engine = "PegaDB"
node_type = "pega.g4s1.micro"
cluster_type = "cluster"
store_type = 3
disk_flavor = 60
port = 6379
replication_num = 2
shard_num = 1
proxy_num = 2
vpc_id = "vpc-ne32rahkaceu"
subnets {
subnet_id = "sbn-vhnqd71mivjq"
zone_name = "cn-bj-d"
}
replication_info {
availability_zone = "cn-bj-d"
is_master = 1
subnet_id = "sbn-vhnqd71mivjq"
}
replication_info {
availability_zone = "cn-bj-d"
is_master = 0
subnet_id = "sbn-vhnqd71mivjq"
}
}
}
```
```
Import
# Import
SCS can be imported, e.g.
Expand Down Expand Up @@ -107,9 +111,9 @@ func resourceBaiduCloudScs() *schema.Resource {
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
Create: schema.DefaultTimeout(120 * time.Minute),
Update: schema.DefaultTimeout(120 * time.Minute),
Delete: schema.DefaultTimeout(120 * time.Minute),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -349,9 +353,10 @@ func resourceBaiduCloudScs() *schema.Resource {
ValidateFunc: validation.IntInSlice([]int{1, 2}),
},
"disk_flavor": {
Type: schema.TypeInt,
Description: "Storage size(GB) when use PegaDB.",
Optional: true,
Type: schema.TypeInt,
Description: "Storage size(GB) when use PegaDB. Must be between `50` and `160`",
Optional: true,
ValidateFunc: validation.IntBetween(50, 160),
},
"disk_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -399,6 +404,27 @@ func resourceBaiduCloudScs() *schema.Resource {
},
Set: schema.HashString,
},
"backup_days": {
Type: schema.TypeString,
Description: "Identifies which days of the week the backup cycle is performed: Mon (Monday) " +
"Tue (Tuesday) Wed (Wednesday) Thu (Thursday) Fri (Friday) Sat (Saturday) Sun (Sunday) " +
"comma separated, the values are as follows: Sun,Mon,Tue,Wed,Thu,Fri,Sta. Note: Automatic backup is " +
"only supported if the number of slave nodes is greater than 1",
ValidateFunc: validation.StringInSlice([]string{"Mon", "Tue", "Wed",
"Thu", "Fri", "Sat", "Sun"}, false),
Optional: true,
},
"backup_time": {
Type: schema.TypeString,
Description: "Identifies when to perform backup in a day, UTC time (+8 is Beijing time) " +
"value such as: 01:05:00",
Optional: true,
},
"expire_day": {
Type: schema.TypeInt,
Description: "Backup file expiration time, value such as: 3",
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -457,6 +483,11 @@ func resourceBaiduCloudScsCreate(d *schema.ResourceData, meta interface{}) error
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_scs", action, BCESDKGoERROR)
}

err = setScsBackupPolicy(d, meta, d.Id())
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_scs", action, BCESDKGoERROR)
}

return resourceBaiduCloudScsRead(d, meta)
}

Expand Down Expand Up @@ -579,6 +610,11 @@ func resourceBaiduCloudScsUpdate(d *schema.ResourceData, meta interface{}) error
return err
}

// update back policy
if err := setScsBackupPolicy(d, meta, instanceID); err != nil {
return err
}

d.Partial(false)

return resourceBaiduCloudScsRead(d, meta)
Expand Down Expand Up @@ -1005,3 +1041,40 @@ func updateInstanceSecurityIPs(d *schema.ResourceData, meta interface{}, instanc
}
return nil
}

func setScsBackupPolicy(d *schema.ResourceData, meta interface{}, instanceID string) error {
action := "Set scs backup policy " + instanceID
client := meta.(*connectivity.BaiduClient)

if d.HasChange("backup_days") || d.HasChange("backup_time") || d.HasChange("expire_in_days") {
args := &scs.ModifyBackupPolicyArgs{
BackupDays: d.Get("backup_days").(string),
BackupTime: d.Get("backup_time").(string),
ExpireDay: d.Get("expire_day").(int),
}

addDebug(action, args)
err := resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
_, err := client.WithScsClient(func(scsClient *scs.Client) (interface{}, error) {
return nil, scsClient.ModifyBackupPolicy(instanceID, args)
})
if err != nil {
if IsExceptedErrors(err, []string{InvalidInstanceStatus, OperationException, bce.EINTERNAL_ERROR}) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_scs_instance", action, BCESDKGoERROR)
}

d.SetPartial("backup_days")
d.SetPartial("backup_time")
d.SetPartial("expire_day")
}

return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/terraform-providers/terraform-provider-baiducloud

require (
github.com/baidubce/bce-sdk-go v0.9.161
github.com/baidubce/bce-sdk-go v0.9.164
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/hashicorp/vault v0.10.4
github.com/mitchellh/go-homedir v1.1.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM=
github.com/aws/aws-sdk-go v1.37.0 h1:GzFnhOIsrGyQ69s7VgqtrG2BG8v7X7vwB3Xpbd/DBBk=
github.com/aws/aws-sdk-go v1.37.0/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/baidubce/bce-sdk-go v0.9.155 h1:4TeNC0+NSOW5h5+nmZLSKquZsC1WnX5n2ohxxH8/iWg=
github.com/baidubce/bce-sdk-go v0.9.155/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg=
github.com/baidubce/bce-sdk-go v0.9.161 h1:dNf2K7clz167h7XOffqPsBg0GPntUp7EYGHd6yOFSJw=
github.com/baidubce/bce-sdk-go v0.9.161/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg=
github.com/baidubce/bce-sdk-go v0.9.164 h1:7gswLMsdQyarovMKuv3i6wxFQ3BQgvc5CmyGXb/D/xA=
github.com/baidubce/bce-sdk-go v0.9.164/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
Expand Down
7 changes: 5 additions & 2 deletions website/docs/r/scs.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ The following arguments are supported:

* `instance_name` - (Required) Name of the instance. Support for uppercase and lowercase letters, numbers, Chinese and special characters, such as `-`, `_`, `/`, `.`. Must start with a letter, length 1-65.
* `node_type` - (Required) Node type of the instance. e.g. `cache.n1.micro`. To learn about supported node type, see documentation on [Supported Node Types](https://cloud.baidu.com/doc/SCS/s/1jwvxtsh0#%E5%AE%9E%E4%BE%8B%E8%A7%84%E6%A0%BC)
* `backup_days` - (Optional) Identifies which days of the week the backup cycle is performed: Mon (Monday) Tue (Tuesday) Wed (Wednesday) Thu (Thursday) Fri (Friday) Sat (Saturday) Sun (Sunday) comma separated, the values are as follows: Sun,Mon,Tue,Wed,Thu,Fri,Sta. Note: Automatic backup is only supported if the number of slave nodes is greater than 1
* `backup_time` - (Optional) Identifies when to perform backup in a day, UTC time (+8 is Beijing time) value such as: 01:05:00
* `billing` - (Optional) **Deprecated**. Use `payment_timing`, `reservation_length`, `reservation_time_unit` instead. Billing information of the Scs.
* `client_auth` - (Optional, Sensitive) Access password of the instance. Should be 8-16 characters, and contains at least two types of letters, numbers and symbols. Allowed symbols include `$ ^ * ( ) _ + - =`.
* `cluster_type` - (Optional, ForceNew) Type of the instance. If `engine` is `memcache`, must be `default`. Valid values for other engine type: `cluster`, `master_slave`. Defaults to `master_slave`.
* `disk_flavor` - (Optional) Storage size(GB) when use PegaDB.
* `disk_flavor` - (Optional) Storage size(GB) when use PegaDB. Must be between `50` and `160`
* `disk_type` - (Optional) Disk type of the instance. Valid values: `cloud_hp1`, `enhanced_ssd_pl1`.
* `enable_read_only` - (Optional) Whether the copies are read only. Valid values: `1`(enabled), `2`(disabled). Defaults to `2`.
* `engine_version` - (Optional) Engine version of the instance. Must be set when `engine` is `redis`. Valid values: `3.2`, `4.0`, `5.0`, `6.0`.
* `engine` - (Optional) Engine of the instance. Valid values: `memcache`, `redis`, `PegaDB`. Defaults to `redis`.
* `expire_day` - (Optional) Backup file expiration time, value such as: 3
* `payment_timing` - (Optional) Payment timing of billing, Valid values: `Prepaid`, `Postpaid`.
* `port` - (Optional, ForceNew) Port number used to access the instance. Must be between `1025` and `65534`. Defaults to `6379`.
* `proxy_num` - (Optional, ForceNew) The number of instance proxy. If `cluster_type` is `cluster`, set to the value of `shard_num` (if `shard_num` equals `1`, set to `2`). If `cluster_type` is `master_slave`, set to `0`. Defaults to `0`.
Expand All @@ -115,12 +118,12 @@ The following arguments are supported:
* `replication_resize_type` - (Optional) Replica resize type. Must set when change `replication_info`. Valid values: `add`, `delete`.
* `reservation_length` - (Optional) Prepaid billing reservation length, only useful when `payment_timing` is `Prepaid`. Valid values: `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `12`, `24`, `36`
* `reservation_time_unit` - (Optional) Prepaid billing reservation time unit, only useful when `payment_timing` is `Prepaid`. Only support `month` now.
* `security_ips` - (Optional) Security ips of the scs.
* `shard_num` - (Optional) The number of instance shard. Defaults to `1`. To learn about supported shard number, see documentation on [Supported Node Types](https://cloud.baidu.com/doc/SCS/s/1jwvxtsh0#%E5%AE%9E%E4%BE%8B%E8%A7%84%E6%A0%BC)
* `store_type` - (Optional) Store type of the instance. Valid values: `0`(high performance memory), `1`(ssd local disk), `3`(capacity storage, only for PegaDB).
* `subnets` - (Optional) Subnets of the instance.
* `tags` - (Optional) Tags, support setting when creating instance, do not support modify
* `vpc_id` - (Optional) ID of the specific VPC
* `security_ips` - (Optional) Security ips of the scs.

The `billing` object supports the following:

Expand Down

0 comments on commit c674b30

Please sign in to comment.