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 IAM Group Policy No Such Entity Exception Handling #7071

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
8 changes: 6 additions & 2 deletions aws/resource_aws_iam_group_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package aws

import (
"fmt"
"log"
"net/url"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -88,7 +88,8 @@ func resourceAwsIamGroupPolicyRead(d *schema.ResourceData, meta interface{}) err
var err error
getResp, err := iamconn.GetGroupPolicy(request)
if err != nil {
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { // XXX test me
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
log.Printf("[WARN] IAM Group Policy (%s) for %s not found, removing from state", name, group)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -122,6 +123,9 @@ func resourceAwsIamGroupPolicyDelete(d *schema.ResourceData, meta interface{}) e
}

if _, err := iamconn.DeleteGroupPolicy(request); err != nil {
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
return nil
}
return fmt.Errorf("Error deleting IAM group policy %s: %s", d.Id(), err)
}
return nil
Expand Down
41 changes: 41 additions & 0 deletions aws/resource_aws_iam_group_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ func TestAccAWSIAMGroupPolicy_basic(t *testing.T) {
})
}

func TestAccAWSIAMGroupPolicy_disappears(t *testing.T) {
var out iam.GetGroupPolicyOutput
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIAMGroupPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccIAMGroupPolicyConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckIAMGroupPolicyExists(
"aws_iam_group.group",
"aws_iam_group_policy.foo",
&out,
),
testAccCheckIAMGroupPolicyDisappears(&out),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSIAMGroupPolicy_namePrefix(t *testing.T) {
var groupPolicy1, groupPolicy2 iam.GetGroupPolicyOutput
rInt := acctest.RandInt()
Expand Down Expand Up @@ -144,6 +169,22 @@ func testAccCheckIAMGroupPolicyDestroy(s *terraform.State) error {
return nil
}

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

params := &iam.DeleteGroupPolicyInput{
PolicyName: out.PolicyName,
GroupName: out.GroupName,
}

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

func testAccCheckIAMGroupPolicyExists(
iamGroupResource string,
iamGroupPolicyResource string,
Expand Down