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

sqs: Fix diffs with policies #22194

Merged
merged 15 commits into from
Dec 14, 2021
15 changes: 15 additions & 0 deletions .changelog/22194.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:bug
resource/aws_sqs_queue: Fix erroneous diffs in `policy` when no changes made or policies are equivalent
```

```release-note:bug
resource/aws_sqs_queue_policy: Fix erroneous diffs in `policy` when no changes made or policies are equivalent
```

```release-note:bug
resource/aws_sqs_queue: Fix "error reading, empty result" and various eventual consistency errors
```

```release-note:bug
resource/aws_sqs_queue_policy: Fix "error reading, empty result" and various eventual consistency errors
```
73 changes: 69 additions & 4 deletions internal/service/sqs/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ var (
Computed: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},

"receive_wait_time_seconds": {
Expand Down Expand Up @@ -216,6 +220,14 @@ func resourceQueueCreate(d *schema.ResourceData, meta interface{}) error {
return err
}

policy, err := structure.NormalizeJsonString(attributes[sqs.QueueAttributeNamePolicy])

if err != nil {
return fmt.Errorf("policy (%s) is invalid JSON: %w", attributes[sqs.QueueAttributeNamePolicy], err)
}

attributes[sqs.QueueAttributeNamePolicy] = policy

input.Attributes = aws.StringMap(attributes)

// Tag-on-create is currently only supported in AWS Commercial
Expand Down Expand Up @@ -272,7 +284,30 @@ func resourceQueueRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

output, err := FindQueueAttributesByURL(conn, d.Id())
var output map[string]string
err := resource.Retry(queueReadTimeout, func() *resource.RetryError {
var err error

output, err = FindQueueAttributesByURL(conn, d.Id())

if tfresource.NotFound(err) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
output, err = FindQueueAttributesByURL(conn, d.Id())
}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return fmt.Errorf("error reading SQS Queue (%s): %w", d.Id(), err)
}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] SQS Queue (%s) not found, removing from state", d.Id())
Expand Down Expand Up @@ -309,15 +344,45 @@ func resourceQueueRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("url", d.Id())

tags, err := ListTags(conn, d.Id())
policyToSet, err := verify.PolicyToSet(d.Get("policy").(string), output[sqs.QueueAttributeNamePolicy])

if err != nil {
return err
}

d.Set("policy", policyToSet)

var tags tftags.KeyValueTags

err = resource.Retry(queueTagsTimeout, func() *resource.RetryError {
var err error

tags, err = ListTags(conn, d.Id())

if tfawserr.ErrCodeEquals(err, sqs.ErrCodeQueueDoesNotExist) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
tags, err = ListTags(conn, d.Id())
}

if err != nil {
// Non-standard partitions (e.g. US Gov) and some local development
// solutions do not yet support this API call. Depending on the
// implementation it may return InvalidAction or AWS.SimpleQueueService.UnsupportedOperation
if !tfawserr.ErrCodeEquals(err, ErrCodeInvalidAction) && !tfawserr.ErrCodeEquals(err, sqs.ErrCodeUnsupportedOperation) {
return fmt.Errorf("error listing tags for SQS Queue (%s): %w", d.Id(), err)
if tfawserr.ErrCodeEquals(err, ErrCodeInvalidAction) || tfawserr.ErrCodeEquals(err, sqs.ErrCodeUnsupportedOperation) {
return nil
}

return fmt.Errorf("error listing tags for SQS Queue (%s): %w", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
Expand Down
51 changes: 47 additions & 4 deletions internal/service/sqs/queue_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -39,6 +41,10 @@ func ResourceQueuePolicy() *schema.Resource {
Required: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},

"queue_url": {
Expand All @@ -53,17 +59,24 @@ func ResourceQueuePolicy() *schema.Resource {
func resourceQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SQSConn

policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

if err != nil {
return fmt.Errorf("policy (%s) is invalid JSON: %w", d.Get("policy").(string), err)
}

policyAttributes := map[string]string{
sqs.QueueAttributeNamePolicy: d.Get("policy").(string),
sqs.QueueAttributeNamePolicy: policy,
}

url := d.Get("queue_url").(string)
input := &sqs.SetQueueAttributesInput{
Attributes: aws.StringMap(policyAttributes),
QueueUrl: aws.String(url),
}

log.Printf("[DEBUG] Setting SQS Queue Policy: %s", input)
_, err := conn.SetQueueAttributes(input)
_, err = conn.SetQueueAttributes(input)

if err != nil {
return fmt.Errorf("error setting SQS Queue Policy (%s): %w", url, err)
Expand All @@ -83,7 +96,30 @@ func resourceQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) error {
func resourceQueuePolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SQSConn

policy, err := FindQueuePolicyByURL(conn, d.Id())
var policy string
err := resource.Retry(queuePolicyReadTimeout, func() *resource.RetryError {
var err error

policy, err = FindQueuePolicyByURL(conn, d.Id())

if tfresource.NotFound(err) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
policy, err = FindQueuePolicyByURL(conn, d.Id())
}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return fmt.Errorf("error reading SQS Queue Policy (%s): %w", d.Id(), err)
}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] SQS Queue Policy (%s) not found, removing from state", d.Id())
Expand All @@ -95,7 +131,14 @@ func resourceQueuePolicyRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error reading SQS Queue Policy (%s): %w", d.Id(), err)
}

d.Set("policy", policy)
policyToSet, err := verify.PolicyToSet(d.Get("policy").(string), policy)

if err != nil {
return err
}

d.Set("policy", policyToSet)

d.Set("queue_url", d.Id())

return nil
Expand Down
Loading