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

New resource: aws_iot_policy_attachment #5864

Merged
merged 5 commits into from
Oct 29, 2018

Conversation

spirius
Copy link
Contributor

@spirius spirius commented Sep 13, 2018

Added aws_iot_policy_attachment resource.

Test results:

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSIoTPolicyAttachment_basic'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSIoTPolicyAttachment_basic -timeout 120m
=== RUN   TestAccAWSIoTPolicyAttachment_basic
--- PASS: TestAccAWSIoTPolicyAttachment_basic (23.61s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	23.650s

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Sep 13, 2018
@bflad bflad added new-resource Introduces a new resource. service/iot Issues and PRs that pertain to the iot service. labels Sep 13, 2018
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Sep 17, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @spirius 👋 Thanks for submitting this! Its off to a good start. Can you see the below initial feedback and let us know if you have any questions or do not have time to implement them?

target := rs.Primary.Attributes["target"]
policyName := rs.Primary.Attributes["policy"]

out, err := conn.ListAttachedPolicies(&iot.ListAttachedPoliciesInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears it should be using listIotPolicyAttachmentPages to properly paginate. 👍

if err != nil {
return fmt.Errorf("Error: Failed to get attached policies for target %s (%s)", target, n)
}
if len(out.Policies) != c {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Exists acceptance test function should only concern itself with the singular target <-> policy attachment the resource manages.


}

func testAccCheckAWSIotPolicyAttchmentDestroy_basic(s *terraform.State) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This testAccCheckAWSIotPolicyAttchmentDestroy_basic function should be renamed testAccCheckAWSIotPolicyAttchmentDestroy and perform a check that each resource that defined attachments have been removed as expected:

func testAccCheckAWSIotPolicyAttchmentDestroy(s *terraform.State) error {
  conn := testAccProvider.Meta().(*AWSClient).iotconn

  for _, rs := range s.RootModule().Resources {
    if rs.Type != "aws_iot_policy_attachment" {
      continue
    }

    input := &iot.ListAttachedPoliciesInput{
      PageSize:  aws.Int64(250),
      Recursive: aws.Bool(false),
      Target:    aws.String(rs.Primary.Attributes["target"]),
    }

    var policy *iot.Policy
    err := listIotPolicyAttachmentPages(conn, input, /* ... */)

    if err != nil {
      return err
    }

    if policy == nil {
      continue
    }

    return fmt.Errorf("IOT Policy Attachment (%s) still exists", d.Id())
  }

  return nil
}


var policy *iot.Policy

err := listIotPolicyAttachmentPages(conn, &iot.ListAttachedPoliciesInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it might make sense to create a wrapper function that encapsulates this logic since it will be needed multiple times, e.g.

func getIotPolicyAttachment(conn, target, policyName) (*iot.Policy, error) {
  var policy *iot.Policy

  input := &iot.ListAttachedPoliciesInput{
    PageSize:  aws.Int64(250),
    Recursive: aws.Bool(false),
    Target:    aws.String(target),
  }

  err := listIotPolicyAttachmentPages(conn, input, func(out *iot.ListAttachedPoliciesOutput, lastPage bool) bool {
    for _, att := range out.Policies {
      if policyName == aws.StringValue(att.PolicyName) {
        policy = att
        return false
      }
    }
    return true
  })

  return policy, err
}

aws/resource_aws_iot_policy_attachment.go Show resolved Hide resolved
})

if err != nil {
log.Printf("[ERROR] Error attaching policy %s to target %s: %s", policyName, target, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: instead of logging the error message, it would be more helpful to return it so operators see it without enabling logging:

if err != nil {
  return fmt.Errorf("error attaching policy %s to target %s: %s", policyName, target, err)
}


if err != nil {
log.Printf("[ERROR] Error listing policy attachments for target %s: %s", target, err)
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Same here regarding error messages:

if err != nil {
  return fmt.Errorf("error listing policy attachments for target %s: %s", target, err)
}

})

if err != nil {
log.Printf("[ERROR] Error detaching policy %s from target %s: %s", policyName, target, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Same here regarding error messages:

if err != nil {
  return fmt.Errorf("error detaching policy %s from target %s: %s", policyName, target, err)
}

aws/resource_aws_iot_policy_attachment.go Show resolved Hide resolved
@spirius
Copy link
Contributor Author

spirius commented Oct 1, 2018

hi @bflad, thanks for the review, I will update the PR and let you know

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Oct 8, 2018
@bflad bflad mentioned this pull request Oct 15, 2018
7 tasks
@ghost ghost added size/XL Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. and removed size/L Managed by automation to categorize the size of a PR. labels Oct 20, 2018
@spirius
Copy link
Contributor Author

spirius commented Oct 20, 2018

hi @bflad, sorry for delay, I've made appropriate changes, can you please review. Similar changes are done for #5868 as well.

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Oct 29, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this in, thanks @spirius! 🚀

--- PASS: TestAccAWSIotPolicyAttachment_basic (14.58s)

@bflad bflad added this to the v1.42.0 milestone Oct 29, 2018
@bflad bflad merged commit 12ee9cb into hashicorp:master Oct 29, 2018
bflad added a commit that referenced this pull request Oct 29, 2018
@bflad
Copy link
Contributor

bflad commented Nov 1, 2018

This has been released in version 1.42.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@spirius spirius deleted the feature/iot-policy-attachment branch November 12, 2018 17:40
@ghost
Copy link

ghost commented Apr 2, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. new-resource Introduces a new resource. service/iot Issues and PRs that pertain to the iot service. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants