Skip to content

Commit

Permalink
Add read_replicas update for gaussdb_mysql (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Jul 28, 2020
1 parent 0f1b652 commit 0b6f035
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 7,233 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/terraform-providers/terraform-provider-huaweicloud

go 1.14

require (
github.com/aws/aws-sdk-go v1.25.3
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/terraform-plugin-sdk v1.13.0
github.com/huaweicloud/golangsdk v0.0.0-20200725024158-c552ea123a3b
github.com/huaweicloud/golangsdk v0.0.0-20200728004128-63646e8b1ea7
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/mitchellh/go-homedir v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20200719033133-e6883dd4cac6 h1:7gIUBTKXTHUJQFlJMUejFG6WOtz1d2ZLOOwD/BCIHMs=
github.com/huaweicloud/golangsdk v0.0.0-20200719033133-e6883dd4cac6/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200721030010-4ccadda0c1d0 h1:v0WvhM9CBkbxy8kGhLc0MsQc8zzOlrgWqc/FcgvhGGk=
github.com/huaweicloud/golangsdk v0.0.0-20200721030010-4ccadda0c1d0/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200721081039-51b301b04100 h1:jbgMKCCuT/jb/x3peMLgcWYKRcKzWP5LUC+3uv2U8fI=
github.com/huaweicloud/golangsdk v0.0.0-20200721081039-51b301b04100/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200723120552-5579d6e91594 h1:jbFBM/IuMs+BDdJC1OC5DFs3NtWqPAvnY6lON38WZbM=
github.com/huaweicloud/golangsdk v0.0.0-20200723120552-5579d6e91594/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200725024158-c552ea123a3b h1:1fjV/LS+v5FYpJRRQsIHLpBkRXPT/yQcQD7/1NI3NNw=
github.com/huaweicloud/golangsdk v0.0.0-20200725024158-c552ea123a3b/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20200728004128-63646e8b1ea7 h1:ZczeotQ12mnp9sAPdO8+1J60I7e7O4PfYmw8abeu+UM=
github.com/huaweicloud/golangsdk v0.0.0-20200728004128-63646e8b1ea7/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
78 changes: 77 additions & 1 deletion huaweicloud/resource_huaweicloud_gaussdb_mysql_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand All @@ -16,6 +17,7 @@ import (
func resourceGaussDBInstance() *schema.Resource {
return &schema.Resource{
Create: resourceGaussDBInstanceCreate,
Update: resourceGaussDBInstanceUpdate,
Read: resourceGaussDBInstanceRead,
Delete: resourceGaussDBInstanceDelete,
Importer: &schema.ResourceImporter{
Expand All @@ -24,6 +26,7 @@ func resourceGaussDBInstance() *schema.Resource {

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

Expand Down Expand Up @@ -72,7 +75,7 @@ func resourceGaussDBInstance() *schema.Resource {
"read_replicas": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ForceNew: false,
Default: 1,
},
"time_zone": {
Expand Down Expand Up @@ -404,6 +407,79 @@ func resourceGaussDBInstanceRead(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceGaussDBInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.initServiceClient("gaussdb", GetRegion(d, config), "mysql/v3")
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud GaussDB client: %s ", err)
}
instanceId := d.Id()

if d.HasChange("read_replicas") {
old, newnum := d.GetChange("read_replicas")
if newnum.(int) > old.(int) {
expand_size := newnum.(int) - old.(int)
priorities := []int{}
for i := 0; i < expand_size; i++ {
priorities = append(priorities, 1)
}
createReplicaOpts := instances.CreateReplicaOpts{
Priorities: priorities,
}
log.Printf("[DEBUG] Create Replica Options: %+v", createReplicaOpts)

n, err := instances.CreateReplica(client, instanceId, createReplicaOpts).ExtractJobResponse()
if err != nil {
d.Set("read_replicas", old.(int))
return fmt.Errorf("Error creating read replicas for instance %s: %s ", instanceId, err)
}

job_list := strings.Split(n.JobID, ",")
log.Printf("[DEBUG] Create Replica Jobs: %#v", job_list)
for i := 0; i < len(job_list); i++ {
job_id := job_list[i]
log.Printf("[DEBUG] Waiting for job: %s", job_id)
if err := instances.WaitForJobSuccess(client, int(d.Timeout(schema.TimeoutCreate)/time.Second), job_id); err != nil {
return err
}
}
}
if newnum.(int) < old.(int) {
shrink_size := old.(int) - newnum.(int)

slave_nodes := []string{}
nodes := d.Get("nodes").([]interface{})
for i := range nodes {
node := nodes[i].(map[string]interface{})
if node["type"].(string) == "slave" && node["status"] == "ACTIVE" {
slave_nodes = append(slave_nodes, node["id"].(string))
}
}
log.Printf("[DEBUG] Slave Nodes: %+v", slave_nodes)
if len(slave_nodes) <= shrink_size {
d.Set("read_replicas", old.(int))
return fmt.Errorf("Error deleting read replicas for instance %s: Shrink Size is bigger than slave nodes", instanceId)
}
for i := 0; i < shrink_size; i++ {
n, err := instances.DeleteReplica(client, instanceId, slave_nodes[i]).ExtractJobResponse()
read_replicas := old.(int) - i
if err != nil {
d.Set("read_replicas", read_replicas)
return fmt.Errorf("Error creating read replica %s for instance %s: %s ", slave_nodes[i], instanceId, err)
}

if err := instances.WaitForJobSuccess(client, int(d.Timeout(schema.TimeoutCreate)/time.Second), n.JobID); err != nil {
d.Set("read_replicas", read_replicas)
return err
}
log.Printf("[DEBUG] Deleted Read Replica: %s", slave_nodes[i])
}
}
}

return resourceGaussDBInstanceRead(d, meta)
}

func resourceGaussDBInstanceDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.initServiceClient("gaussdb", GetRegion(d, config), "mysql/v3")
Expand Down
Loading

0 comments on commit 0b6f035

Please sign in to comment.