Skip to content

Commit

Permalink
[SMN] tags support in resource/opentelekomcloud_smn_topic_v2 (#2340)
Browse files Browse the repository at this point in the history
[SMN] tags support in `resource/opentelekomcloud_smn_topic_v2`

Summary of the Pull Request
Resource tags support
PR Checklist

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

Acceptance Steps Performed
=== RUN   TestAccSMNV2Topic_basic
--- PASS: TestAccSMNV2Topic_basic (70.22s)

PASS

Process finished with the exit code 0

Reviewed-by: Aloento
Reviewed-by: Artem Lifshits
Reviewed-by: Vladimir Vshivkov
  • Loading branch information
anton-sidelnikov authored Oct 13, 2023
1 parent 8d1a5d1 commit a059851
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 31 deletions.
13 changes: 10 additions & 3 deletions docs/resources/smn_topic_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ Manages a V2 topic resource within OpenTelekomCloud.
resource "opentelekomcloud_smn_topic_v2" "topic_1" {
name = "topic_1"
display_name = "The display name of topic_1"
tags = {
foo = "bar"
key = "value"
}
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the topic to be created.
* `name` - (Required, ForceNew, String) The name of the topic to be created.

* `display_name` - (Optional) Topic display name, which is presented as the
* `display_name` - (Optional, String) Topic display name, which is presented as the
name of the email sender in an email message.

* `project_name` - (Optional) The project name for the topic.
* `project_name` - (Optional, ForceNew, String) The project name for the topic.

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

## Attributes Reference

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/jinzhu/copier v0.3.5
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.20230929172336-7233c2d98471
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231011130319-58bd572a1dd9
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.1.0
golang.org/x/sync v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +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.20230929172336-7233c2d98471 h1:L8xU0pDXwBhI73aCSRLZSu3Ifk1IhZnTDahwnlLzaBw=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20230929172336-7233c2d98471/go.mod h1:9Deb3q2gJvq5dExV+aX+iO+G+mD9Zr9uFt+YY9ONmq0=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231011130319-58bd572a1dd9 h1:Coq9L2f9X59P+vaxGAicEtnSNDGB5uXMrKPHIchWop4=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231011130319-58bd572a1dd9/go.mod h1:9Deb3q2gJvq5dExV+aX+iO+G+mD9Zr9uFt+YY9ONmq0=
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
)

const resourceTopicName = "opentelekomcloud_smn_topic_v2.topic_1"

func TestAccSMNV2Topic_basic(t *testing.T) {
var topic topics.TopicGet

Expand All @@ -25,22 +27,19 @@ func TestAccSMNV2Topic_basic(t *testing.T) {
{
Config: TestAccSMNV2TopicConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSMNV2TopicExists("opentelekomcloud_smn_topic_v2.topic_1", &topic, env.OS_TENANT_NAME),
resource.TestCheckResourceAttr(
"opentelekomcloud_smn_topic_v2.topic_1", "name", "topic_1"),
resource.TestCheckResourceAttr(
"opentelekomcloud_smn_topic_v2.topic_1", "display_name",
"The display name of topic_1"),
testAccCheckSMNV2TopicExists(resourceTopicName, &topic, env.OS_TENANT_NAME),
resource.TestCheckResourceAttr(resourceTopicName, "name", "topic_1"),
resource.TestCheckResourceAttr(resourceTopicName, "display_name", "The display name of topic_1"),
resource.TestCheckResourceAttr(resourceTopicName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(resourceTopicName, "tags.key", "value"),
),
},
{
Config: TestAccSMNV2TopicConfig_update,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"opentelekomcloud_smn_topic_v2.topic_1", "display_name",
"The update display name of topic_1"),
resource.TestCheckResourceAttr(
"opentelekomcloud_smn_topic_v2.topic_1", "name", "topic_1"),
resource.TestCheckResourceAttr(resourceTopicName, "display_name", "The update display name of topic_1"),
resource.TestCheckResourceAttr(resourceTopicName, "name", "topic_1"),
resource.TestCheckResourceAttr(resourceTopicName, "tags.foo", "bar_ch"),
),
},
},
Expand All @@ -63,9 +62,8 @@ func TestAccSMNV2Topic_schemaProjectName(t *testing.T) {
{
Config: testAccSMNV2TopicConfig_projectName(env.OS_TENANT_NAME),
Check: resource.ComposeTestCheckFunc(
testAccCheckSMNV2TopicExists("opentelekomcloud_smn_topic_v2.topic_1", &topic, env.OS_TENANT_NAME),
resource.TestCheckResourceAttr(
"opentelekomcloud_smn_topic_v2.topic_1", "project_name", string(env.OS_TENANT_NAME)),
testAccCheckSMNV2TopicExists(resourceTopicName, &topic, env.OS_TENANT_NAME),
resource.TestCheckResourceAttr(resourceTopicName, "project_name", string(env.OS_TENANT_NAME)),
),
},
},
Expand Down Expand Up @@ -130,13 +128,22 @@ var TestAccSMNV2TopicConfig_basic = `
resource "opentelekomcloud_smn_topic_v2" "topic_1" {
name = "topic_1"
display_name = "The display name of topic_1"
tags = {
foo = "bar"
key = "value"
}
}
`

var TestAccSMNV2TopicConfig_update = `
resource "opentelekomcloud_smn_topic_v2" "topic_1" {
name = "topic_1"
display_name = "The update display name of topic_1"
tags = {
foo = "bar_ch"
}
}
`

Expand Down
7 changes: 7 additions & 0 deletions opentelekomcloud/common/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,13 @@ func (c *Config) SmnV2Client(projectName ProjectName) (*golangsdk.ServiceClient,
})
}

func (c *Config) SmnV2TagClient(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewSMNV2Tags(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Availability: c.getEndpointType(),
})
}

func (c *Config) CesV1Client(region string) (*golangsdk.ServiceClient, error) {
return openstack.NewCESClient(c.HwClient, golangsdk.EndpointOpts{
Region: region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/smn/v2/topics"

"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
Expand Down Expand Up @@ -37,8 +37,8 @@ func ResourceTopic() *schema.Resource {
"display_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"tags": common.TagsSchema(),
"topic_urn": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -77,6 +77,24 @@ func resourceTopicCreate(ctx context.Context, d *schema.ResourceData, meta inter
return fmterr.Errorf("error getting topic from result: %s", err)
}
log.Printf("[DEBUG] Create : topic.TopicUrn %s", topic.TopicUrn)

if common.HasFilledOpt(d, "tags") {
tagClient, err := config.SmnV2TagClient(config.GetRegion(d))
if err != nil {
return fmterr.Errorf("error creating OpenTelekomCloud smn tags client: %s", err)
}
tagClient.MoreHeaders = map[string]string{
"X-SMN-RESOURCEID-TYPE": "name",
}
tagRaw := d.Get("tags").(map[string]interface{})
if len(tagRaw) > 0 {
tagList := common.ExpandResourceTags(tagRaw)
if err := tags.Create(tagClient, "smn_topic", d.Get("name").(string), tagList).ExtractErr(); err != nil {
return fmterr.Errorf("error setting tags of SMN topic: %w", err)
}
}
}

if topic.TopicUrn != "" {
d.SetId(topic.TopicUrn)
return resourceTopicRead(ctx, d, meta)
Expand All @@ -93,22 +111,40 @@ func resourceTopicRead(_ context.Context, d *schema.ResourceData, meta interface
}

topicUrn := d.Id()
topicGet, err := topics.Get(client, topicUrn).ExtractGet()
topic, err := topics.Get(client, topicUrn).ExtractGet()
if err != nil {
return common.CheckDeletedDiag(d, err, "topic")
}

log.Printf("[DEBUG] Retrieved topic %s: %#v", topicUrn, topicGet)
log.Printf("[DEBUG] Retrieved topic %s: %#v", topicUrn, topic)

mErr := multierror.Append(
d.Set("topic_urn", topicGet.TopicUrn),
d.Set("display_name", topicGet.DisplayName),
d.Set("name", topicGet.Name),
d.Set("push_policy", topicGet.PushPolicy),
d.Set("update_time", topicGet.UpdateTime),
d.Set("create_time", topicGet.CreateTime),
d.Set("topic_urn", topic.TopicUrn),
d.Set("display_name", topic.DisplayName),
d.Set("name", topic.Name),
d.Set("push_policy", topic.PushPolicy),
d.Set("update_time", topic.UpdateTime),
d.Set("create_time", topic.CreateTime),
)

// read tags
tagClient, err := config.SmnV2TagClient(config.GetRegion(d))
if err != nil {
return fmterr.Errorf("error creating OpenTelekomCloud smn tags client: %s", err)
}
if err != nil {
return diag.Errorf("error creating SMN tag client: %s", err)
}
tagClient.MoreHeaders = map[string]string{
"X-SMN-RESOURCEID-TYPE": "name",
}
if resourceTags, err := tags.Get(tagClient, "smn_topic", d.Get("name").(string)).Extract(); err == nil {
tagMap := common.TagsToMap(resourceTags)
mErr = multierror.Append(mErr, d.Set("tags", tagMap))
} else {
return fmterr.Errorf("error saving tags for OpenTelekomCloud SMN topic: %s", err)
}

if err := mErr.ErrorOrNil(); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -148,7 +184,18 @@ func resourceTopicUpdate(ctx context.Context, d *schema.ResourceData, meta inter
if d.HasChange("display_name") {
updateOpts.DisplayName = d.Get("display_name").(string)
}

if d.HasChange("tags") {
tagClient, err := config.SmnV2TagClient(config.GetRegion(d))
if err != nil {
return fmterr.Errorf("error creating OpenTelekomCloud smn tags client: %s", err)
}
tagClient.MoreHeaders = map[string]string{
"X-SMN-RESOURCEID-TYPE": "name",
}
if err := common.UpdateResourceTags(tagClient, d, "smn_topic", d.Get("name").(string)); err != nil {
return fmterr.Errorf("error updating tags of SMN topic %s: %s", d.Id(), err)
}
}
topic, err := topics.Update(client, updateOpts, id).Extract()
if err != nil {
return fmterr.Errorf("error updating topic from result: %s", err)
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/smn-topic-tags-d0a576a7615e04e3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
enhancements:
- |
**[SMN]** Add ``tags`` argument to ``resource/opentelekomcloud_smn_topic_v2``
(`#2340 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2340>`_)

0 comments on commit a059851

Please sign in to comment.