Skip to content

Commit

Permalink
Merge pull request #7072 from kterada0509/feature/fix-iam-policy-reso…
Browse files Browse the repository at this point in the history
…urce-error-handling

Fix error handling of aws_iam_policy resouce
  • Loading branch information
bflad committed Jan 9, 2019
2 parents 687aa87 + 70000dd commit e908e5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
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

0 comments on commit e908e5d

Please sign in to comment.