Skip to content

Commit

Permalink
Merge pull request #21769 from tmarlok88/f-iot-thing-type-tags_support
Browse files Browse the repository at this point in the history
aws_iot_thing_type tags support
  • Loading branch information
ewbankkit authored Nov 15, 2021
2 parents 6b07a24 + 84a6d94 commit e597e2e
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/21769.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_iot_thing_type: Add `tags` argument and `tags_all` attribute to support resource tagging
```
38 changes: 37 additions & 1 deletion internal/service/iot/thing_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)
Expand Down Expand Up @@ -69,17 +70,22 @@ func ResourceThingType() *schema.Resource {
Optional: true,
Default: false,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"arn": {
Type: schema.TypeString,
Computed: true,
},
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceThingTypeCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).IoTConn

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))
params := &iot.CreateThingTypeInput{
ThingTypeName: aws.String(d.Get("name").(string)),
}
Expand All @@ -92,6 +98,9 @@ func resourceThingTypeCreate(d *schema.ResourceData, meta interface{}) error {
params.ThingTypeProperties = expandThingTypeProperties(config)
}
}
if len(tags) > 0 {
params.Tags = Tags(tags.IgnoreAWS())
}

log.Printf("[DEBUG] Creating IoT Thing Type: %s", params)
out, err := conn.CreateThingType(params)
Expand Down Expand Up @@ -122,6 +131,9 @@ func resourceThingTypeCreate(d *schema.ResourceData, meta interface{}) error {
func resourceThingTypeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).IoTConn

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

params := &iot.DescribeThingTypeInput{
ThingTypeName: aws.String(d.Id()),
}
Expand All @@ -142,6 +154,22 @@ func resourceThingTypeRead(d *schema.ResourceData, meta interface{}) error {

d.Set("arn", out.ThingTypeArn)

tags, err := ListTags(conn, aws.StringValue(out.ThingTypeArn))
if err != nil {
return fmt.Errorf("error listing tags for IoT Thing Type (%s): %w", aws.StringValue(out.ThingTypeArn), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

if err := d.Set("properties", flattenIoTThingTypeProperties(out.ThingTypeProperties)); err != nil {
return fmt.Errorf("error setting properties: %s", err)
}
Expand All @@ -166,6 +194,14 @@ func resourceThingTypeUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

return resourceThingTypeRead(d, meta)
}

Expand Down
112 changes: 111 additions & 1 deletion internal/service/iot/thing_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func TestAccIoTThingType_basic(t *testing.T) {
{
Config: testAccThingTypeConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists("aws_iot_thing_type.foo"),
resource.TestCheckResourceAttrSet("aws_iot_thing_type.foo", "arn"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "name", fmt.Sprintf("tf_acc_iot_thing_type_%d", rInt)),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags.%", "0"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags_all.%", "0"),
),
},
{
Expand All @@ -50,10 +53,13 @@ func TestAccIoTThingType_full(t *testing.T) {
{
Config: testAccThingTypeConfig_full(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists("aws_iot_thing_type.foo"),
resource.TestCheckResourceAttrSet("aws_iot_thing_type.foo", "arn"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "properties.0.description", "MyDescription"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "properties.0.searchable_attributes.#", "3"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "deprecated", "true"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags_all.%", "1"),
),
},
{
Expand All @@ -71,6 +77,76 @@ func TestAccIoTThingType_full(t *testing.T) {
})
}

func TestAccIoTThingType_tags(t *testing.T) {
rName := sdkacctest.RandString(5)
resourceName := "aws_iot_thing_type.foo"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, iot.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckThingTypeDestroy,
Steps: []resource.TestStep{
{
Config: testAccIoTThingTypeTags1(rName, "key1", "user@example"),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists("aws_iot_thing_type.foo"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "user@example"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccIoTThingTypeTags2(rName, "key1", "user@example", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists("aws_iot_thing_type.foo"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "user@example"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccIoTThingTypeTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists("aws_iot_thing_type.foo"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func testAccCheckThingTypeExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).IoTConn
input := &iot.ListThingTypesInput{}

output, err := conn.ListThingTypes(input)

if err != nil {
return err
}

for _, rule := range output.ThingTypes {
if aws.StringValue(rule.ThingTypeName) == rs.Primary.ID {
return nil
}
}

return fmt.Errorf("IoT Topic Rule (%s) not found", rs.Primary.ID)
}
}

func testAccCheckThingTypeDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).IoTConn

Expand All @@ -87,7 +163,6 @@ func testAccCheckThingTypeDestroy(s *terraform.State) error {
if err == nil {
return fmt.Errorf("Expected IoT Thing Type to be destroyed, %s found", rs.Primary.ID)
}

}

return nil
Expand All @@ -111,6 +186,10 @@ resource "aws_iot_thing_type" "foo" {
description = "MyDescription"
searchable_attributes = ["foo", "bar", "baz"]
}
tags = {
testtag = "MyTagValue"
}
}
`, rName)
}
Expand All @@ -125,6 +204,37 @@ resource "aws_iot_thing_type" "foo" {
description = "MyDescription"
searchable_attributes = ["foo", "bar", "baz"]
}
tags = {
testtag = "MyTagValue"
}
}
`, rName)
}

func testAccIoTThingTypeTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%[1]s"
deprecated = false
tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccIoTThingTypeTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%[1]s"
deprecated = false
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
2 changes: 2 additions & 0 deletions website/docs/r/iot_thing_type.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ resource "aws_iot_thing_type" "foo" {
* `properties` - (Optional), Configuration block that can contain the following properties of the thing type:
* `description` - (Optional, Forces New Resource) The description of the thing type.
* `searchable_attributes` - (Optional, Forces New Resource) A list of searchable thing attribute names.
* `tags` - (Optional) Key-value mapping of resource tags. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.


## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The ARN of the created AWS IoT Thing Type.
* `tags_all` - Map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).

## Import

Expand Down

0 comments on commit e597e2e

Please sign in to comment.