Skip to content

Commit

Permalink
resource/aws_iam_policy_attachment: Fixes for tfproviderlint R006 (#1…
Browse files Browse the repository at this point in the history
…2043)

Reference: #11864

`RetryFunc` should only be used when logic has a retryable condition. In the case of working with the AWS Go SDK, it also arbitrarily restricts the automatic retrying logic of API calls to the timeout, which is generally undesired.

This particular case looks like the original intent was to verify via read-after-write that the appropriate write was persisted, however the logic errantly returned `return resource.NonRetryableError()` with `nil` when the role policy was not found, ending the `RetryFunc` without an actual error and never retrying on any condition.

Previously:

```
aws/resource_aws_iam_policy_attachment.go:220:53: R006: RetryFunc should include RetryableError() handling or be removed
```

Output from acceptance testing:

```
--- PASS: TestAccAWSIAMPolicyAttachment_Groups_RenamedGroup (22.54s)
--- PASS: TestAccAWSIAMPolicyAttachment_Users_RenamedUser (23.00s)
--- PASS: TestAccAWSIAMPolicyAttachment_Roles_RenamedRole (24.95s)
--- PASS: TestAccAWSIAMPolicyAttachment_basic (39.85s)
--- PASS: TestAccAWSIAMPolicyAttachment_paginatedEntities (242.04s)
```
  • Loading branch information
bflad authored Feb 20, 2020
1 parent 6f3de6f commit d40fad2
Showing 1 changed file with 0 additions and 36 deletions.
36 changes: 0 additions & 36 deletions aws/resource_aws_iam_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package aws
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-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
Expand Down Expand Up @@ -216,39 +213,6 @@ func attachPolicyToRoles(conn *iam.IAM, roles []*string, arn string) error {
if err != nil {
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
}
}

if !foundPolicy {
return resource.NonRetryableError(err)
}
}

return nil
})

if attachmentErr != nil {
return attachmentErr
}

}
return nil
}
Expand Down

0 comments on commit d40fad2

Please sign in to comment.