Skip to content

Commit

Permalink
[DCS] tags support in resource/opentelekomcloud_dcs_instance_v1 (#2459
Browse files Browse the repository at this point in the history
)

[DCS] tags support in `resource/opentelekomcloud_dcs_instance_v1`

Summary of the Pull Request
PR Checklist

 Refers to: #2457
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccDcsInstancesV1_basic
=== PAUSE TestAccDcsInstancesV1_basic
=== CONT  TestAccDcsInstancesV1_basic
--- PASS: TestAccDcsInstancesV1_basic (127.06s)
PASS

Process finished with the exit code 0

Reviewed-by: Artem Lifshits
  • Loading branch information
anton-sidelnikov committed Mar 7, 2024
1 parent efb9698 commit bce3bc0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/resources/dcs_instance_v1.md
Expand Up @@ -49,6 +49,10 @@ resource "opentelekomcloud_dcs_instance_v1" "instance_1" {
period_type = "weekly"
backup_at = [1, 2, 4, 6]
}
tags = {
environment = "basic"
managed_by = "terraform"
}
}
```

Expand Down Expand Up @@ -183,6 +187,8 @@ the default start time `02:00` and the default end time `06:00`.
ranges can be added to an instance. Separate multiple IP addresses or IP address ranges with commas (,).
IP address 0.0.0.0 and IP address range 0.0.0/0 are not supported.

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

## Attributes Reference

The following attributes are exported:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20240305133323-fbea97a37330
github.com/opentelekomcloud/gophertelekomcloud v0.9.0
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.17.0
golang.org/x/sync v0.1.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Expand Up @@ -156,10 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20240131123828-bbf049e73593 h1:QYlZMMkOYo4zy69bBBRWEYQEPii++LjjNzHL81JadAg=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20240131123828-bbf049e73593/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20240305133323-fbea97a37330 h1:I3j2PGzL6KDB8P7qy/KbQa7OQmfTIzhg1POLbgxZI4w=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20240305133323-fbea97a37330/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.0 h1:9H8ZY0+lC7obhRJXCwUfnl644kAOoVT8R0IfEOtdmnU=
github.com/opentelekomcloud/gophertelekomcloud v0.9.0/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Expand Up @@ -32,6 +32,8 @@ func TestAccDcsInstancesV1_basic(t *testing.T) {
testAccCheckDcsV1InstanceExists(resourceInstanceName, instance),
resource.TestCheckResourceAttr(resourceInstanceName, "name", instanceName),
resource.TestCheckResourceAttr(resourceInstanceName, "engine", "Redis"),
resource.TestCheckResourceAttr(resourceInstanceName, "tags.environment", "basic"),
resource.TestCheckResourceAttr(resourceInstanceName, "tags.managed_by", "terraform"),
),
},
{
Expand All @@ -40,6 +42,9 @@ func TestAccDcsInstancesV1_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceInstanceName, "backup_policy.0.begin_at", "01:00-02:00"),
resource.TestCheckResourceAttr(resourceInstanceName, "backup_policy.0.save_days", "2"),
resource.TestCheckResourceAttr(resourceInstanceName, "backup_policy.0.backup_at.#", "3"),
resource.TestCheckResourceAttr(resourceInstanceName, "tags.environment", "update"),
resource.TestCheckResourceAttr(resourceInstanceName, "tags.managed_by", "terraform"),
resource.TestCheckResourceAttr(resourceInstanceName, "tags.user", "admin"),
),
},
},
Expand Down Expand Up @@ -327,6 +332,11 @@ resource "opentelekomcloud_dcs_instance_v1" "instance_1" {
parameter_name = "timeout"
parameter_value = "100"
}
tags = {
environment = "basic"
managed_by = "terraform"
}
}
`, common.DataSourceSecGroupDefault, common.DataSourceSubnet, env.OS_AVAILABILITY_ZONE, instanceName)
}
Expand Down Expand Up @@ -368,6 +378,12 @@ resource "opentelekomcloud_dcs_instance_v1" "instance_1" {
parameter_name = "timeout"
parameter_value = "200"
}
tags = {
environment = "update"
managed_by = "terraform"
user = "admin"
}
}
`, common.DataSourceSecGroupDefault, common.DataSourceSubnet, env.OS_AVAILABILITY_ZONE, instanceName)
}
Expand Down
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/configs"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/lifecycle"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v1/others"
dcsTags "github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v2/tags"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/dcs/v2/whitelists"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
Expand Down Expand Up @@ -282,10 +284,23 @@ func ResourceDcsInstanceV1() *schema.Resource {
},
},
},
"tags": common.TagsSchema(),
},
}
}

func buildDcsTags(tagsMap map[string]interface{}) []tags.ResourceTag {
tagsList := make([]tags.ResourceTag, 0, len(tagsMap))
for k, v := range tagsMap {
tag := tags.ResourceTag{
Key: k,
Value: v.(string),
}
tagsList = append(tagsList, tag)
}
return tagsList
}

func formatAts(src []interface{}) []int {
res := make([]int, len(src))
for i, at := range src {
Expand Down Expand Up @@ -421,6 +436,7 @@ func resourceDcsInstancesV1Create(ctx context.Context, d *schema.ResourceData, m
InstanceBackupPolicy: getInstanceBackupPolicy(d),
MaintainBegin: d.Get("maintain_begin").(string),
MaintainEnd: d.Get("maintain_end").(string),
Tags: buildDcsTags(d.Get("tags").(map[string]interface{})),
}

if ip, ok := d.GetOk("private_ip"); ok {
Expand Down Expand Up @@ -581,6 +597,15 @@ func resourceDcsInstancesV1Read(_ context.Context, d *schema.ResourceData, meta
}
}

if resourceTags, err := tags.Get(client, "instances", d.Id()).Extract(); err == nil {
tagMap := common.TagsToMap(resourceTags)
if err := d.Set("tags", tagMap); err != nil {
return diag.Errorf("[DEBUG] error saving tags for OpenTelekomCloud DCS instance (%s): %s", d.Id(), err)
}
} else {
log.Printf("[WARN] fetching tags of OpenTelekomCloud DCS instance failed: %s", err)
}

if err := mErr.ErrorOrNil(); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -662,6 +687,14 @@ func resourceDcsInstancesV1Update(ctx context.Context, d *schema.ResourceData, m
}
}

if d.HasChange("tags") {
oldVal, newVal := d.GetChange("tags")
err = updateDcsTags(client, d.Id(), oldVal.(map[string]interface{}), newVal.(map[string]interface{}))
if err != nil {
return diag.FromErr(err)
}
}

return resourceDcsInstancesV1Read(ctx, d, meta)
}

Expand Down Expand Up @@ -799,3 +832,21 @@ func resourceDcsInstanceV1ImportState(ctx context.Context, d *schema.ResourceDat

return results, nil
}

func updateDcsTags(c *golangsdk.ServiceClient, id string, oldVal, newVal map[string]interface{}) error {
if len(oldVal) > 0 {
tagList := buildDcsTags(oldVal)
err := dcsTags.Delete(c, id, tagList)
if err != nil {
return err
}
}
if len(newVal) > 0 {
tagList := buildDcsTags(newVal)
err := dcsTags.Create(c, id, tagList)
if err != nil {
return err
}
}
return nil
}
4 changes: 4 additions & 0 deletions releasenotes/notes/dcs-tags-9af0920455943cc7.yaml
@@ -0,0 +1,4 @@
---
enhancements:
- |
**[DCS]** `tags` feature implemented for ``resource/opentelekomcloud_dcs_instance_v1`` (`#2459 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2459>`_)

0 comments on commit bce3bc0

Please sign in to comment.