Skip to content

Commit

Permalink
Merge pull request #7069 from kterada0509/feature/fix-iam-user-policy…
Browse files Browse the repository at this point in the history
…-no-such-entity-exception

Fix IAM User Policy No Such Entity Exception Handling
  • Loading branch information
bflad authored Jan 9, 2019
2 parents f10861f + 252139f commit cc0d212
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
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

0 comments on commit cc0d212

Please sign in to comment.