Skip to content

Commit

Permalink
Merge pull request #5401 from terraform-providers/t-aws_iam_role-disa…
Browse files Browse the repository at this point in the history
…ppears

resource/aws_iam_role: Ensure resource recreates when deleted outside Terraform
  • Loading branch information
bflad authored Aug 1, 2018
2 parents 850ecdb + 8a3c5b6 commit 449d996
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
8 changes: 7 additions & 1 deletion aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,20 @@ func resourceAwsIamRoleRead(d *schema.ResourceData, meta interface{}) error {

getResp, err := iamconn.GetRole(request)
if err != nil {
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") { // XXX test me
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
log.Printf("[WARN] IAM Role %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading IAM Role %s: %s", d.Id(), err)
}

if getResp == nil || getResp.Role == nil {
log.Printf("[WARN] IAM Role %q not found, removing from state", d.Id())
d.SetId("")
return nil
}

role := getResp.Role

d.Set("arn", role.Arn)
Expand Down
40 changes: 40 additions & 0 deletions aws/resource_aws_iam_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ func TestAccAWSIAMRole_badJSON(t *testing.T) {
})
}

func TestAccAWSIAMRole_disappears(t *testing.T) {
var role iam.GetRoleOutput

rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_iam_role.role"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRoleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSIAMRoleConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
testAccCheckAWSRoleDisappears(&role),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSIAMRole_force_detach_policies(t *testing.T) {
var conf iam.GetRoleOutput
rName := acctest.RandString(10)
Expand Down Expand Up @@ -311,6 +334,23 @@ func testAccCheckAWSRoleExists(n string, res *iam.GetRoleOutput) resource.TestCh
}
}

func testAccCheckAWSRoleDisappears(getRoleOutput *iam.GetRoleOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn

roleName := aws.StringValue(getRoleOutput.Role.RoleName)

_, err := iamconn.DeleteRole(&iam.DeleteRoleInput{
RoleName: aws.String(roleName),
})
if err != nil {
return fmt.Errorf("error deleting role %q: %s", roleName, err)
}

return nil
}
}

func testAccCheckAWSRoleGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource]
Expand Down

0 comments on commit 449d996

Please sign in to comment.