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

support tags for dds and dcs instance resource #610

Merged
merged 2 commits into from
Oct 28, 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
4 changes: 3 additions & 1 deletion docs/resources/dcs_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ resource "huaweicloud_dcs_instance" "instance_1" {
begin_at = "00:00-01:00"
period_type = "weekly"
backup_at = [1]
depends_on = ["huaweicloud_networking_secgroup.secgroup_1"]
}
```

Expand Down Expand Up @@ -190,6 +189,8 @@ The following arguments are supported:

* `enterprise_project_id` - (Optional) The enterprise project id of the dcs instance. Changing this creates a new instance.

* `tags` - (Optional) The key/value pairs to associate with the dcs instance.

The `whitelists` block supports:

* `group_name` - (Required) Specifies the name of IP address group.
Expand Down Expand Up @@ -224,6 +225,7 @@ The following attributes are exported:
* `begin_at` - See Argument Reference above.
* `period_type` - See Argument Reference above.
* `backup_at` - See Argument Reference above.
* `tags` - See Argument Reference above.
* `order_id` - An order ID is generated only in the monthly or yearly billing mode.
In other billing modes, no value is returned for this parameter.
* `resource_spec_code` - Resource specifications.
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/dds_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ The following arguments are supported:

**Note:** The instance will be restarted in the background when switching SSL. Please operate with caution.

* `tags` - (Optional) The key/value pairs to associate with the DDS instance.

The `datastore` block supports:

* `type` - (Required) Specifies the DB engine. 'DDS-Community' and 'DDS-Enhanced' are supported.
Expand Down Expand Up @@ -184,6 +186,7 @@ The following attributes are exported:
* `mode` - See Argument Reference above.
* `flavor` - See Argument Reference above.
* `backup_strategy` - See Argument Reference above.
* `tags` - See Argument Reference above.
* `db_username` - Indicates the DB Administator name.
* `status` - Indicates the the DB instance status.
* `port` - Indicates the database port number. The port range is 2100 to 9500.
Expand Down
64 changes: 50 additions & 14 deletions huaweicloud/resource_huaweicloud_dcs_instance_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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/common/tags"
"github.com/huaweicloud/golangsdk/openstack/dcs/v1/instances"
"github.com/huaweicloud/golangsdk/openstack/dcs/v2/whitelists"
)
Expand Down Expand Up @@ -148,6 +149,7 @@ func resourceDcsInstanceV1() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -293,7 +295,7 @@ func resourceDcsInstancesV1Create(d *schema.ResourceData, meta interface{}) erro
config := meta.(*Config)
dcsV1Client, err := config.dcsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud dcs instance client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud dcs instance v1 client: %s", err)
}

if err := resourceDcsInstancesCheck(d); err != nil {
Expand Down Expand Up @@ -351,9 +353,9 @@ func resourceDcsInstancesV1Create(d *schema.ResourceData, meta interface{}) erro
d.SetId(v.InstanceID)

// set whitelist
dcsV2Client, err := config.NewServiceClient("dcsv2", GetRegion(d, config))
dcsV2Client, err := config.dcsV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud dcs instance client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud dcs instance v2 client: %s", err)
}
whitelistOpts := getDcsInstanceWhitelist(d)
log.Printf("[DEBUG] Create whitelist options: %#v", whitelistOpts)
Expand All @@ -365,6 +367,15 @@ func resourceDcsInstancesV1Create(d *schema.ResourceData, meta interface{}) erro
}
}

//set tags
tagRaw := d.Get("tags").(map[string]interface{})
if len(tagRaw) > 0 {
taglist := expandResourceTags(tagRaw)
if tagErr := tags.Create(dcsV2Client, "dcs", v.InstanceID, taglist).ExtractErr(); tagErr != nil {
return fmt.Errorf("Error setting tags of DCS instance %s: %s", v.InstanceID, tagErr)
}
}

return resourceDcsInstancesV1Read(d, meta)
}

Expand All @@ -373,7 +384,7 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error

dcsV1Client, err := config.dcsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud dcs instance client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud dcs instance v1 client: %s", err)
}
v, err := instances.Get(dcsV1Client, d.Id()).Extract()
if err != nil {
Expand Down Expand Up @@ -420,7 +431,10 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
}
d.Set("capacity", capacity)

dcsV2Client, err := config.NewServiceClient("dcsv2", GetRegion(d, config))
dcsV2Client, err := config.dcsV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud dcs instance v2 client: %s", err)
}
object, err := whitelists.Get(dcsV2Client, d.Id()).Extract()

enable := object.Enable
Expand All @@ -434,6 +448,16 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error setting whitelists for DCS instance, err: %s", err)
}

// set tags
resourceTags, err := tags.Get(dcsV2Client, "instances", d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error fetching tags of DCS instance: %s", err)
}
tagmap := tagsToMap(resourceTags.Tags)
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("[DEBUG] Error saving tag to state for DCS instance (%s): %s", d.Id(), err)
}

return nil
}

Expand All @@ -447,7 +471,7 @@ func resourceDcsInstancesV1Update(d *schema.ResourceData, meta interface{}) erro
if d.HasChanges("name", "description", "security_group_id", "maintain_begin", "maintain_end") {
dcsV1Client, err := config.dcsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error updating HuaweiCloud dcs instance client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud dcs instance v1 client: %s", err)
}

description := d.Get("description").(string)
Expand All @@ -465,14 +489,26 @@ func resourceDcsInstancesV1Update(d *schema.ResourceData, meta interface{}) erro
}
}

if d.HasChange("whitelists") {
dcsV2Client, err := config.NewServiceClient("dcsv2", GetRegion(d, config))
whitelistOpts := getDcsInstanceWhitelist(d)
log.Printf("[DEBUG] update whitelist options: %#v", whitelistOpts)

err = whitelists.Put(dcsV2Client, d.Id(), whitelistOpts).ExtractErr()
if d.HasChanges("whitelists", "tags") {
dcsV2Client, err := config.dcsV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error updating whitelist for instance (%s): %s", d.Id(), err)
return fmt.Errorf("Error creating HuaweiCloud dcs instance v2 client: %s", err)
}

if d.HasChange("whitelists") {
whitelistOpts := getDcsInstanceWhitelist(d)
log.Printf("[DEBUG] update whitelist options: %#v", whitelistOpts)

err = whitelists.Put(dcsV2Client, d.Id(), whitelistOpts).ExtractErr()
if err != nil {
return fmt.Errorf("Error updating whitelist for instance (%s): %s", d.Id(), err)
}
}

// update tags
tagErr := UpdateResourceTags(dcsV2Client, d, "dcs", d.Id())
if tagErr != nil {
return fmt.Errorf("Error updating tags of DCS instance:%s, err:%s", d.Id(), tagErr)
}
}

Expand All @@ -483,7 +519,7 @@ func resourceDcsInstancesV1Delete(d *schema.ResourceData, meta interface{}) erro
config := meta.(*Config)
dcsV1Client, err := config.dcsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud dcs instance client: %s", err)
return fmt.Errorf("Error creating HuaweiCloud dcs instance v1 client: %s", err)
}

_, err = instances.Get(dcsV1Client, d.Id()).Extract()
Expand Down
15 changes: 10 additions & 5 deletions huaweicloud/resource_huaweicloud_dcs_instance_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ 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/dcs/v1/instances"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccDcsInstancesV1_basic(t *testing.T) {
Expand All @@ -29,6 +28,8 @@ func TestAccDcsInstancesV1_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "engine", "Redis"),
resource.TestCheckResourceAttr(resourceName, "engine_version", "3.0"),
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key", "value"),
resource.TestCheckResourceAttr(resourceName, "tags.owner", "terraform"),
resource.TestCheckResourceAttrSet(resourceName, "ip"),
resource.TestCheckResourceAttrSet(resourceName, "port"),
),
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestAccDcsInstancesV1_whitelists(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "engine", "Redis"),
resource.TestCheckResourceAttr(resourceName, "engine_version", "5.0"),
resource.TestCheckResourceAttr(resourceName, "whitelist_enable", "true"),
resource.TestCheckResourceAttr(resourceName, "whitelists.#", "2"),
),
},
},
Expand Down Expand Up @@ -174,16 +176,20 @@ func testAccDcsV1Instance_basic(instanceName string) string {
engine = "Redis"
capacity = 2
vpc_id = "%s"
security_group_id = huaweicloud_networking_secgroup.secgroup_1.id
subnet_id = "%s"
security_group_id = huaweicloud_networking_secgroup.secgroup_1.id
available_zones = [data.huaweicloud_dcs_az.az_1.id]
product_id = "dcs.master_standby-h"
save_days = 1
backup_type = "manual"
begin_at = "00:00-01:00"
period_type = "weekly"
backup_at = [1]
depends_on = ["huaweicloud_networking_secgroup.secgroup_1"]

tags = {
key = "value"
owner = "terraform"
}
}
`, OS_AVAILABILITY_ZONE, instanceName, OS_VPC_ID, OS_NETWORK_ID)
}
Expand Down Expand Up @@ -214,7 +220,6 @@ func testAccDcsV1Instance_epsId(instanceName string) string {
begin_at = "00:00-01:00"
period_type = "weekly"
backup_at = [1]
depends_on = ["huaweicloud_networking_secgroup.secgroup_1"]
enterprise_project_id = "%s"
}
`, OS_AVAILABILITY_ZONE, instanceName, OS_VPC_ID, OS_NETWORK_ID, OS_ENTERPRISE_PROJECT_ID)
Expand Down
38 changes: 35 additions & 3 deletions huaweicloud/resource_huaweicloud_dds_instance_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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/common/tags"
"github.com/huaweicloud/golangsdk/openstack/dds/v3/instances"
)

Expand Down Expand Up @@ -170,6 +171,7 @@ func resourceDdsInstanceV3() *schema.Resource {
Optional: true,
Default: true,
},
"tags": tagsSchema(),
"db_username": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -319,7 +321,9 @@ func resourceDdsInstanceV3Create(d *schema.ResourceData, meta interface{}) error
Flavor: resourceDdsFlavors(d),
BackupStrategy: resourceDdsBackupStrategy(d),
}
if ssl := d.Get("ssl").(bool); !ssl {
if d.Get("ssl").(bool) {
createOpts.Ssl = "1"
} else {
createOpts.Ssl = "0"
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
Expand All @@ -332,7 +336,7 @@ func resourceDdsInstanceV3Create(d *schema.ResourceData, meta interface{}) error

d.SetId(instance.Id)
stateConf := &resource.StateChangeConf{
Pending: []string{"creating"},
Pending: []string{"creating", "updating"},
Target: []string{"normal"},
Refresh: DdsInstanceStateRefreshFunc(client, instance.Id),
Timeout: d.Timeout(schema.TimeoutCreate),
Expand All @@ -347,6 +351,15 @@ func resourceDdsInstanceV3Create(d *schema.ResourceData, meta interface{}) error
instance.Id, err)
}

//set tags
tagRaw := d.Get("tags").(map[string]interface{})
if len(tagRaw) > 0 {
taglist := expandResourceTags(tagRaw)
if tagErr := tags.Create(client, "instances", instance.Id, taglist).ExtractErr(); tagErr != nil {
return fmt.Errorf("Error setting tags of DDS instance %s: %s", instance.Id, tagErr)
}
}

return resourceDdsInstanceV3Read(d, meta)
}

Expand All @@ -370,7 +383,9 @@ func resourceDdsInstanceV3Read(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error extracting DDS instance: %s", err)
}
if instances.TotalCount == 0 {
return fmt.Errorf("Error fetching DDS instance: deleted")
log.Printf("[WARN] DDS instance (%s) was not found", instanceID)
d.SetId("")
return nil
}
insts := instances.Instances
instance := insts[0]
Expand Down Expand Up @@ -417,6 +432,16 @@ func resourceDdsInstanceV3Read(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error setting nodes of DDS instance, err: %s", err)
}

// save tags
resourceTags, err := tags.Get(client, "instances", d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error fetching tags of DDS instance: %s", err)
}
tagmap := tagsToMap(resourceTags.Tags)
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("[DEBUG] Error saving tag to state for DDS instance (%s): %s", d.Id(), err)
}

return nil
}

Expand Down Expand Up @@ -493,6 +518,13 @@ func resourceDdsInstanceV3Update(d *schema.ResourceData, meta interface{}) error
d.Id(), err)
}

if d.HasChange("tags") {
tagErr := UpdateResourceTags(client, d, "instances", d.Id())
if tagErr != nil {
return fmt.Errorf("Error updating tags of DDS instance:%s, err:%s", d.Id(), tagErr)
}
}

return resourceDdsInstanceV3Read(d, meta)
}

Expand Down
Loading