From 68bcfa49f4ba69db4503d24f8aecf5ddd9e05399 Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Tue, 6 Apr 2021 16:50:38 +0800 Subject: [PATCH] [dds instance]support 0 value of keep_days and support update backup_strategy --- docs/resources/dds_instance.md | 4 +- go.mod | 2 +- go.sum | 10 +- .../resource_huaweicloud_dds_instance_v3.go | 24 +++- ...source_huaweicloud_dds_instance_v3_test.go | 104 ++++++++++++++++-- .../openstack/dds/v3/instances/requests.go | 3 +- vendor/modules.txt | 2 +- 7 files changed, 120 insertions(+), 29 deletions(-) diff --git a/docs/resources/dds_instance.md b/docs/resources/dds_instance.md index 1914d8b1b1..67c3184187 100644 --- a/docs/resources/dds_instance.md +++ b/docs/resources/dds_instance.md @@ -109,8 +109,8 @@ The following arguments are supported: * `flavor` - (Required, List, ForceNew) Specifies the flavors information. The structure is described below. Changing this creates a new instance. -* `backup_strategy` - (Optional, List, ForceNew) Specifies the advanced backup policy. The structure is - described below. Changing this creates a new instance. +* `backup_strategy` - (Optional, List) Specifies the advanced backup policy. The structure is + described below. * `ssl` - (Optional, Bool) Specifies whether to enable or disable SSL. Defaults to true. diff --git a/go.mod b/go.mod index 20e931680e..66ea9e7725 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.14 require ( github.com/hashicorp/errwrap v1.0.0 github.com/hashicorp/terraform-plugin-sdk v1.16.0 - github.com/huaweicloud/golangsdk v0.0.0-20210402075152-aba310c2a950 + github.com/huaweicloud/golangsdk v0.0.0-20210406114125-0f07278d722c github.com/jen20/awspolicyequivalence v1.1.0 github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect github.com/stretchr/testify v1.4.0 diff --git a/go.sum b/go.sum index 45b4044120..bf851040e0 100644 --- a/go.sum +++ b/go.sum @@ -225,14 +225,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-20210323020602-6d5ee0030244 h1:Jzq3828aWxaRquRxPqv8aG1A6B+LatrIHiD3SMlHpFM= -github.com/huaweicloud/golangsdk v0.0.0-20210323020602-6d5ee0030244/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0= -github.com/huaweicloud/golangsdk v0.0.0-20210329082007-ee3a1fd6c542 h1:rA1o0dd7F2cR/zBe9Fyk7DjjgSv85MM6PdToMabNXq4= -github.com/huaweicloud/golangsdk v0.0.0-20210329082007-ee3a1fd6c542/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0= -github.com/huaweicloud/golangsdk v0.0.0-20210401071036-b8be64ba4002 h1:RxtuAdoAiJI9bScWcXf09sVDAEjP0w4X8zIe5e4pljU= -github.com/huaweicloud/golangsdk v0.0.0-20210401071036-b8be64ba4002/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0= -github.com/huaweicloud/golangsdk v0.0.0-20210402075152-aba310c2a950 h1:Ys3DRL6qJGYL/9mX8gvzSQade5TOJi8COrPyG58HOQs= -github.com/huaweicloud/golangsdk v0.0.0-20210402075152-aba310c2a950/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0= +github.com/huaweicloud/golangsdk v0.0.0-20210406114125-0f07278d722c h1:CwxvUSdetbTeImxmNxmVTLUE+5ryDAf6DLYE47/28Yo= +github.com/huaweicloud/golangsdk v0.0.0-20210406114125-0f07278d722c/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= diff --git a/huaweicloud/resource_huaweicloud_dds_instance_v3.go b/huaweicloud/resource_huaweicloud_dds_instance_v3.go index 85db3eef7a..8616aa128e 100644 --- a/huaweicloud/resource_huaweicloud_dds_instance_v3.go +++ b/huaweicloud/resource_huaweicloud_dds_instance_v3.go @@ -150,7 +150,6 @@ func resourceDdsInstanceV3() *schema.Resource { "backup_strategy": { Type: schema.TypeList, Optional: true, - ForceNew: true, Computed: true, MaxItems: 1, Elem: &schema.Resource{ @@ -260,13 +259,14 @@ func resourceDdsBackupStrategy(d *schema.ResourceData) instances.BackupStrategy var backupStrategy instances.BackupStrategy backupStrategyRaw := d.Get("backup_strategy").([]interface{}) log.Printf("[DEBUG] backupStrategyRaw: %+v", backupStrategyRaw) + startTime := "00:00-01:00" + keepDays := 7 if len(backupStrategyRaw) == 1 { - backupStrategy.StartTime = backupStrategyRaw[0].(map[string]interface{})["start_time"].(string) - backupStrategy.KeepDays = backupStrategyRaw[0].(map[string]interface{})["keep_days"].(int) - } else { - backupStrategy.StartTime = "00:00-01:00" - backupStrategy.KeepDays = 7 + startTime = backupStrategyRaw[0].(map[string]interface{})["start_time"].(string) + keepDays = backupStrategyRaw[0].(map[string]interface{})["keep_days"].(int) } + backupStrategy.StartTime = startTime + backupStrategy.KeepDays = &keepDays log.Printf("[DEBUG] backupStrategy: %+v", backupStrategy) return backupStrategy } @@ -497,6 +497,18 @@ func resourceDdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error opts = append(opts, opt) } + if d.HasChange("backup_strategy") { + backupStrategy := resourceDdsBackupStrategy(d) + backupStrategy.Period = "1,2,3,4,5,6,7" + opt := instances.UpdateOpt{ + Param: "backup_policy", + Value: backupStrategy, + Action: "backups/policy", + Method: "put", + } + opts = append(opts, opt) + } + r := instances.Update(client, d.Id(), opts) if r.Err != nil { return fmt.Errorf("Error updating instance from result: %s ", r.Err) diff --git a/huaweicloud/resource_huaweicloud_dds_instance_v3_test.go b/huaweicloud/resource_huaweicloud_dds_instance_v3_test.go index 3212ef22fb..8fd485da1b 100644 --- a/huaweicloud/resource_huaweicloud_dds_instance_v3_test.go +++ b/huaweicloud/resource_huaweicloud_dds_instance_v3_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" "github.com/huaweicloud/golangsdk/openstack/dds/v3/instances" @@ -11,6 +12,7 @@ import ( func TestAccDDSV3Instance_basic(t *testing.T) { var instance instances.Instance + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5)) resourceName := "huaweicloud_dds_instance.instance" resource.ParallelTest(t, resource.TestCase{ @@ -19,13 +21,24 @@ func TestAccDDSV3Instance_basic(t *testing.T) { CheckDestroy: testAccCheckDDSV3InstanceDestroy, Steps: []resource.TestStep{ { - Config: TestAccDDSInstanceV3Config_basic, + Config: testAccDDSInstanceV3Config_basic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDDSV3InstanceExists(resourceName, &instance), - resource.TestCheckResourceAttr(resourceName, "name", "dds-instance"), + resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "ssl", "true"), resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"), resource.TestCheckResourceAttr(resourceName, "tags.owner", "terraform"), + resource.TestCheckResourceAttr(resourceName, "backup_strategy.0.start_time", "08:00-09:00"), + resource.TestCheckResourceAttr(resourceName, "backup_strategy.0.keep_days", "8"), + ), + }, + { + Config: testAccDDSInstanceV3Config_updateBackupStrategy(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckDDSV3InstanceExists(resourceName, &instance), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "backup_strategy.0.start_time", "00:00-01:00"), + resource.TestCheckResourceAttr(resourceName, "backup_strategy.0.keep_days", "7"), ), }, }, @@ -100,22 +113,32 @@ func testAccCheckDDSV3InstanceExists(n string, instance *instances.Instance) res } } -var TestAccDDSInstanceV3Config_basic = fmt.Sprintf(` +func testAccDDSInstanceV3Config_basic(rName string) string { + return fmt.Sprintf(` +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_vpc" "test" { + name = "vpc-default" +} + +data "huaweicloud_vpc_subnet" "test" { + name = "subnet-default" +} + resource "huaweicloud_networking_secgroup" "secgroup_acc" { - name = "secgroup_acc" + name = "%s" } resource "huaweicloud_dds_instance" "instance" { - name = "dds-instance" + name = "%s" datastore { type = "DDS-Community" version = "3.4" storage_engine = "wiredTiger" } - region = "%s" - availability_zone = "%s" - vpc_id = "%s" - subnet_id = "%s" + availability_zone = data.huaweicloud_availability_zones.test.names[0] + vpc_id = data.huaweicloud_vpc.test.id + subnet_id = data.huaweicloud_vpc_subnet.test.id security_group_id = huaweicloud_networking_secgroup.secgroup_acc.id password = "Test@123" mode = "Sharding" @@ -147,4 +170,65 @@ resource "huaweicloud_dds_instance" "instance" { foo = "bar" owner = "terraform" } -}`, HW_REGION_NAME, HW_AVAILABILITY_ZONE, HW_VPC_ID, HW_NETWORK_ID) +}`, rName, rName) +} + +func testAccDDSInstanceV3Config_updateBackupStrategy(rName string) string { + return fmt.Sprintf(` +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_vpc" "test" { + name = "vpc-default" +} + +data "huaweicloud_vpc_subnet" "test" { + name = "subnet-default" +} + +resource "huaweicloud_networking_secgroup" "secgroup_acc" { + name = "%s" +} + +resource "huaweicloud_dds_instance" "instance" { + name = "%s" + datastore { + type = "DDS-Community" + version = "3.4" + storage_engine = "wiredTiger" + } + availability_zone = data.huaweicloud_availability_zones.test.names[0] + vpc_id = data.huaweicloud_vpc.test.id + subnet_id = data.huaweicloud_vpc_subnet.test.id + security_group_id = huaweicloud_networking_secgroup.secgroup_acc.id + password = "Test@123" + mode = "Sharding" + + flavor { + type = "mongos" + num = 2 + spec_code = "dds.mongodb.c3.medium.4.mongos" + } + flavor { + type = "shard" + num = 2 + storage = "ULTRAHIGH" + size = 20 + spec_code = "dds.mongodb.c3.medium.4.shard" + } + flavor { + type = "config" + num = 1 + storage = "ULTRAHIGH" + size = 20 + spec_code = "dds.mongodb.c3.large.2.config" + } + backup_strategy { + start_time = "00:00-01:00" + keep_days = "7" + } + tags = { + foo = "bar" + owner = "terraform" + } +}`, rName, rName) +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/dds/v3/instances/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/dds/v3/instances/requests.go index 15c129e411..5dd697a3b9 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/dds/v3/instances/requests.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/dds/v3/instances/requests.go @@ -39,7 +39,8 @@ type Flavor struct { type BackupStrategy struct { StartTime string `json:"start_time" required:"true"` - KeepDays int `json:"keep_days,omitempty"` + KeepDays *int `json:"keep_days,omitempty"` + Period string `json:"period,omitempty"` } type CreateInstanceBuilder interface { diff --git a/vendor/modules.txt b/vendor/modules.txt index 71bf80eb17..b6bfbbf66d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -257,7 +257,7 @@ github.com/hashicorp/terraform-svchost/auth github.com/hashicorp/terraform-svchost/disco # github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d github.com/hashicorp/yamux -# github.com/huaweicloud/golangsdk v0.0.0-20210402075152-aba310c2a950 +# github.com/huaweicloud/golangsdk v0.0.0-20210406114125-0f07278d722c ## explicit github.com/huaweicloud/golangsdk github.com/huaweicloud/golangsdk/internal