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

Fix error handling of aws_iam_policy resouce #7072

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions aws/resource_aws_iam_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,11 @@ func resourceAwsIamPolicyDelete(d *schema.ResourceData, meta interface{}) error
PolicyArn: aws.String(d.Id()),
}

_, err := iamconn.DeletePolicy(request)
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
return nil
}

if err != nil {
return fmt.Errorf("Error deleting IAM policy %s: %#v", d.Id(), err)
if _, err := iamconn.DeletePolicy(request); err != nil {
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
return nil
}
return fmt.Errorf("Error deleting IAM policy %s: %s", d.Id(), err)
}

return nil
Expand Down
37 changes: 37 additions & 0 deletions aws/resource_aws_iam_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ func TestAccAWSIAMPolicy_description(t *testing.T) {
})
}

func TestAccAWSIAMPolicy_disappears(t *testing.T) {
var out iam.GetPolicyOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_iam_policy.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSIAMPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSIAMPolicyConfigName(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSIAMPolicyExists(resourceName, &out),
testAccCheckAWSIAMPolicyDisappears(&out),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSIAMPolicy_namePrefix(t *testing.T) {
var out iam.GetPolicyOutput
namePrefix := "tf-acc-test-"
Expand Down Expand Up @@ -212,6 +234,21 @@ func testAccCheckAWSIAMPolicyDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSIAMPolicyDisappears(out *iam.GetPolicyOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn

params := &iam.DeletePolicyInput{
PolicyArn: out.Policy.Arn,
}

if _, err := iamconn.DeletePolicy(params); err != nil {
return err
}
return nil
}
}

func testAccAWSIAMPolicyConfigDescription(rName, description string) string {
return fmt.Sprintf(`
resource "aws_iam_policy" "test" {
Expand Down