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

resource/aws_iam_role: Ensure resource recreates when deleted outside Terraform #5401

Merged
merged 1 commit into from
Aug 1, 2018
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: 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