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

Final retries for IAM resources #9766

Merged
merged 1 commit into from
Aug 16, 2019
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
3 changes: 3 additions & 0 deletions aws/resource_aws_iam_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func instanceProfileAddRole(iamconn *iam.IAM, profileName, roleName string) erro
}
return nil
})
if isResourceTimeoutError(err) {
_, err = iamconn.AddRoleToInstanceProfile(request)
}
if err != nil {
return fmt.Errorf("Error adding IAM Role %s to Instance Profile %s: %s", roleName, profileName, err)
}
Expand Down
8 changes: 6 additions & 2 deletions aws/resource_aws_iam_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ func resourceAwsIamPolicyRead(d *schema.ResourceData, meta interface{}) error {

return nil
})

if isResourceTimeoutError(err) {
getPolicyResponse, err = iamconn.GetPolicy(getPolicyRequest)
}
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
log.Printf("[WARN] IAM Policy (%s) not found, removing from state", d.Id())
d.SetId("")
Expand Down Expand Up @@ -187,7 +189,9 @@ func resourceAwsIamPolicyRead(d *schema.ResourceData, meta interface{}) error {

return nil
})

if isResourceTimeoutError(err) {
getPolicyVersionResponse, err = iamconn.GetPolicyVersion(getPolicyVersionRequest)
}
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
log.Printf("[WARN] IAM Policy (%s) not found, removing from state", d.Id())
d.SetId("")
Expand Down
45 changes: 17 additions & 28 deletions aws/resource_aws_iam_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"fmt"
"log"
"strings"
"time"

"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"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
Expand Down Expand Up @@ -217,37 +215,28 @@ func attachPolicyToRoles(conn *iam.IAM, roles []*string, arn string) error {
return err
}

var attachmentErr = resource.Retry(2*time.Minute, func() *resource.RetryError {

input := iam.ListRolePoliciesInput{
RoleName: r,
}

attachedPolicies, err := conn.ListRolePolicies(&input)
if err != nil {
return resource.NonRetryableError(err)
}

if len(attachedPolicies.PolicyNames) > 0 {
var foundPolicy bool
for _, policyName := range attachedPolicies.PolicyNames {
if strings.HasSuffix(arn, *policyName) {
foundPolicy = true
break
}
}
input := iam.ListRolePoliciesInput{
RoleName: r,
}
attachedPolicies, err := conn.ListRolePolicies(&input)
if err != nil {
return fmt.Errorf("Error listing role policies: %s", err)
}

if !foundPolicy {
return resource.NonRetryableError(err)
if len(attachedPolicies.PolicyNames) > 0 {
var foundPolicy bool
for _, policyName := range attachedPolicies.PolicyNames {
if strings.HasSuffix(arn, *policyName) {
foundPolicy = true
break
}
}

return nil
})

if attachmentErr != nil {
return attachmentErr
if !foundPolicy {
return fmt.Errorf("Error: Attached policy not found")
}
}

}
return nil
}
Expand Down
12 changes: 11 additions & 1 deletion aws/resource_aws_iam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func resourceAwsIamRoleCreate(d *schema.ResourceData, meta interface{}) error {
}
return resource.NonRetryableError(err)
})
if isResourceTimeoutError(err) {
createResp, err = iamconn.CreateRole(request)
}
if err != nil {
return fmt.Errorf("Error creating IAM Role %s: %s", name, err)
}
Expand Down Expand Up @@ -362,7 +365,7 @@ func resourceAwsIamRoleDelete(d *schema.ResourceData, meta interface{}) error {
}

// IAM is eventually consistent and deletion of attached policies may take time
return resource.Retry(30*time.Second, func() *resource.RetryError {
err := resource.Retry(30*time.Second, func() *resource.RetryError {
_, err := iamconn.DeleteRole(deleteRoleInput)
if err != nil {
if isAWSErr(err, iam.ErrCodeDeleteConflictException, "") {
Expand All @@ -373,6 +376,13 @@ func resourceAwsIamRoleDelete(d *schema.ResourceData, meta interface{}) error {
}
return nil
})
if isResourceTimeoutError(err) {
_, err = iamconn.DeleteRole(deleteRoleInput)
}
if err != nil {
return fmt.Errorf("Error deleting IAM role: %s", err)
}
return nil
}

func deleteAwsIamRoleInstanceProfiles(conn *iam.IAM, rolename string) error {
Expand Down
11 changes: 7 additions & 4 deletions aws/resource_aws_iam_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,11 @@ func deleteAwsIamUserMFADevices(svc *iam.IAM, username string) error {

func deleteAwsIamUserLoginProfile(svc *iam.IAM, username string) error {
var err error
input := &iam.DeleteLoginProfileInput{
UserName: aws.String(username),
}
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err = svc.DeleteLoginProfile(&iam.DeleteLoginProfileInput{
UserName: aws.String(username),
})
_, err = svc.DeleteLoginProfile(input)
if err != nil {
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
return nil
Expand All @@ -374,7 +375,9 @@ func deleteAwsIamUserLoginProfile(svc *iam.IAM, username string) error {
}
return nil
})

if isResourceTimeoutError(err) {
_, err = svc.DeleteLoginProfile(input)
}
if err != nil {
return fmt.Errorf("Error deleting Account Login Profile: %s", err)
}
Expand Down