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 User Policy No Such Entity Exception Handling #7069

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_user_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 @@ -101,7 +101,8 @@ func resourceAwsIamUserPolicyRead(d *schema.ResourceData, meta interface{}) erro

getResp, err := iamconn.GetUserPolicy(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 User Policy (%s) for %s not found, removing from state", name, user)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -139,6 +140,9 @@ func resourceAwsIamUserPolicyDelete(d *schema.ResourceData, meta interface{}) er
}

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

func TestAccAWSIAMUserPolicy_disappears(t *testing.T) {
var out iam.GetUserPolicyOutput
suffix := randomString(10)
resourceName := fmt.Sprintf("aws_iam_user_policy.foo_%s", suffix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIAMUserPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsIamUserPolicyConfig(suffix),
Check: resource.ComposeTestCheckFunc(
testAccCheckIAMUserPolicyExists(resourceName, &out),
testAccCheckIAMUserPolicyDisappears(&out),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSIAMUserPolicy_namePrefix(t *testing.T) {
rInt := acctest.RandInt()
policy1 := `{"Version":"2012-10-17","Statement":{"Effect":"Allow","Action":"*","Resource":"*"}}`
Expand Down Expand Up @@ -204,6 +226,38 @@ func TestAccAWSIAMUserPolicy_multiplePolicies(t *testing.T) {
})
}

func testAccCheckIAMUserPolicyExists(resource string, res *iam.GetUserPolicyOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("Not found: %s", resource)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Policy name is set")
}

user, name, err := resourceAwsIamUserPolicyParseId(rs.Primary.ID)
if err != nil {
return err
}

iamconn := testAccProvider.Meta().(*AWSClient).iamconn

resp, err := iamconn.GetUserPolicy(&iam.GetUserPolicyInput{
PolicyName: aws.String(name),
UserName: aws.String(user),
})
if err != nil {
return err
}

*res = *resp

return nil
}
}

func testAccCheckIAMUserPolicyDestroy(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn

Expand Down Expand Up @@ -239,6 +293,22 @@ func testAccCheckIAMUserPolicyDestroy(s *terraform.State) error {
return nil
}

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

params := &iam.DeleteUserPolicyInput{
PolicyName: out.PolicyName,
UserName: out.UserName,
}

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

func testAccCheckIAMUserPolicy(
iamUserResource string,
iamUserPolicyResource string) resource.TestCheckFunc {
Expand Down