Skip to content

Commit

Permalink
Merge pull request #21799 from hashicorp/f-r/aws_iot_thing_group+aws_…
Browse files Browse the repository at this point in the history
…iot_thing_group_member

New resources: `aws_iot_thing_group` and `aws_iot_thing_group_membership`
  • Loading branch information
ewbankkit authored Nov 18, 2021
2 parents 8020a3f + 2b937d8 commit 63e7a6d
Show file tree
Hide file tree
Showing 15 changed files with 1,503 additions and 115 deletions.
7 changes: 7 additions & 0 deletions .changelog/21799.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_iot_thing_group
```

```release-note:new-resource
aws_iot_thing_group_membership
```
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,8 @@ func Provider() *schema.Provider {
"aws_iot_policy_attachment": iot.ResourcePolicyAttachment(),
"aws_iot_role_alias": iot.ResourceRoleAlias(),
"aws_iot_thing": iot.ResourceThing(),
"aws_iot_thing_group": iot.ResourceThingGroup(),
"aws_iot_thing_group_membership": iot.ResourceThingGroupMembership(),
"aws_iot_thing_principal_attachment": iot.ResourceThingPrincipalAttachment(),
"aws_iot_thing_type": iot.ResourceThingType(),
"aws_iot_topic_rule": iot.ResourceTopicRule(),
Expand Down
2 changes: 1 addition & 1 deletion internal/service/iot/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func resourceAuthorizerCreate(d *schema.ResourceData, meta interface{}) error {
func resourceAuthorizerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).IoTConn

authorizer, err := AuthorizerByName(conn, d.Id())
authorizer, err := FindAuthorizerByName(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] IoT Authorizer (%s) not found, removing from state", d.Id())
Expand Down
4 changes: 2 additions & 2 deletions internal/service/iot/authorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func testAccCheckAuthorizerExists(n string, v *iot.AuthorizerDescription) resour

conn := acctest.Provider.Meta().(*conns.AWSClient).IoTConn

output, err := tfiot.AuthorizerByName(conn, rs.Primary.ID)
output, err := tfiot.FindAuthorizerByName(conn, rs.Primary.ID)

if err != nil {
return err
Expand All @@ -177,7 +177,7 @@ func testAccCheckAuthorizerDestroy(s *terraform.State) error {
continue
}

_, err := tfiot.AuthorizerByName(conn, rs.Primary.ID)
_, err := tfiot.FindAuthorizerByName(conn, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
Expand Down
121 changes: 121 additions & 0 deletions internal/service/iot/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package iot

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func FindAuthorizerByName(conn *iot.IoT, name string) (*iot.AuthorizerDescription, error) {
input := &iot.DescribeAuthorizerInput{
AuthorizerName: aws.String(name),
}

output, err := conn.DescribeAuthorizer(input)

if tfawserr.ErrCodeEquals(err, iot.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.AuthorizerDescription == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.AuthorizerDescription, nil
}

func FindThingByName(conn *iot.IoT, name string) (*iot.DescribeThingOutput, error) {
input := &iot.DescribeThingInput{
ThingName: aws.String(name),
}

output, err := conn.DescribeThing(input)

if tfawserr.ErrCodeEquals(err, iot.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}

func FindThingGroupByName(conn *iot.IoT, name string) (*iot.DescribeThingGroupOutput, error) {
input := &iot.DescribeThingGroupInput{
ThingGroupName: aws.String(name),
}

output, err := conn.DescribeThingGroup(input)

if tfawserr.ErrCodeEquals(err, iot.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}

func FindThingGroupMembership(conn *iot.IoT, thingGroupName, thingName string) error {
input := &iot.ListThingGroupsForThingInput{
ThingName: aws.String(thingName),
}

var v *iot.GroupNameAndArn

err := conn.ListThingGroupsForThingPages(input, func(page *iot.ListThingGroupsForThingOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, group := range page.ThingGroups {
if aws.StringValue(group.GroupName) == thingGroupName {
v = group

return false
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, iot.ErrCodeResourceNotFoundException) {
return &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if v == nil {
return tfresource.NewEmptyResultError(input)
}

return nil
}
34 changes: 0 additions & 34 deletions internal/service/iot/iot.go

This file was deleted.

48 changes: 48 additions & 0 deletions internal/service/iot/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func init() {
Dependencies: []string{"aws_iot_thing_principal_attachment"},
})

resource.AddTestSweepers("aws_iot_thing_group", &resource.Sweeper{
Name: "aws_iot_policy_attachment",
F: sweepThingGroups,
})

resource.AddTestSweepers("aws_iot_thing_type", &resource.Sweeper{
Name: "aws_iot_thing_type",
F: sweepThingTypes,
Expand Down Expand Up @@ -475,3 +480,46 @@ func sweepTopicRules(region string) error {

return sweeperErrs.ErrorOrNil()
}

func sweepThingGroups(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*conns.AWSClient).IoTConn
input := &iot.ListThingGroupsInput{}
sweepResources := make([]*sweep.SweepResource, 0)

err = conn.ListThingGroupsPages(input, func(page *iot.ListThingGroupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, group := range page.ThingGroups {
r := ResourceThingGroup()
d := r.Data(nil)
d.SetId(aws.StringValue(group.GroupName))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping IoT Thing Group sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing IoT Thing Groups (%s): %w", region, err)
}

err = sweep.SweepOrchestrator(sweepResources)

if err != nil {
return fmt.Errorf("error sweeping IoT Thing Groups (%s): %w", region, err)
}

return nil
}
Loading

0 comments on commit 63e7a6d

Please sign in to comment.