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

r/aws_sqs_queue_policy: Support import by queue URL #2544

Merged
merged 1 commit into from
Dec 11, 2017
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
24 changes: 14 additions & 10 deletions aws/resource_aws_sqs_queue_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/schema"
)
Expand All @@ -16,15 +15,20 @@ func resourceAwsSqsQueuePolicy() *schema.Resource {
Read: resourceAwsSqsQueuePolicyRead,
Update: resourceAwsSqsQueuePolicyUpsert,
Delete: resourceAwsSqsQueuePolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
MigrateState: resourceAwsSqsQueuePolicyMigrateState,
SchemaVersion: 1,

Schema: map[string]*schema.Schema{
"queue_url": &schema.Schema{
"queue_url": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"policy": &schema.Schema{
"policy": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateJsonString,
Expand All @@ -48,20 +52,20 @@ func resourceAwsSqsQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Error updating SQS attributes: %s", err)
}

d.SetId("sqs-policy-" + url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@radeksimko Do you recall why this was set as is?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there was a strong reason - maybe just to separate it from SQS queue IDs, but really we don't need to do that.

However I'd be cautious about changing the ID here in this context. Can you please verify this actually works for existing SQS policy which is already in state with the old ID? I suspect refresh might fail because it will try to read queue attributes for queue URL sqs-policy-"+url.

In other words I believe we need state migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @radeksimko, are you referring to something different than my state migration implementation?

MigrateState:  resourceAwsSqsQueuePolicyMigrateState,
SchemaVersion: 1,

If there's a better way to handle that, please let me know!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh 🤦‍♂️ sorry! I missed that one... In that case it's all good! Thanks.

d.SetId(url)

return resourceAwsSqsQueuePolicyRead(d, meta)
}

func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sqsconn
url := d.Get("queue_url").(string)

out, err := conn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
QueueUrl: aws.String(url),
QueueUrl: aws.String(d.Id()),
AttributeNames: []*string{aws.String("Policy")},
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "AWS.SimpleQueueService.NonExistentQueue" {
if isAWSErr(err, "AWS.SimpleQueueService.NonExistentQueue", "") {
log.Printf("[WARN] SQS Queue (%s) not found", d.Id())
d.SetId("")
return nil
Expand All @@ -78,17 +82,17 @@ func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) err
}

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

return nil
}

func resourceAwsSqsQueuePolicyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sqsconn

url := d.Get("queue_url").(string)
log.Printf("[DEBUG] Deleting SQS Queue Policy of %s", url)
log.Printf("[DEBUG] Deleting SQS Queue Policy of %s", d.Id())
_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
QueueUrl: aws.String(url),
QueueUrl: aws.String(d.Id()),
Attributes: aws.StringMap(map[string]string{
"Policy": "",
}),
Expand Down
38 changes: 38 additions & 0 deletions aws/resource_aws_sqs_queue_policy_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package aws

import (
"fmt"
"log"

"github.com/hashicorp/terraform/terraform"
)

func resourceAwsSqsQueuePolicyMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found AWS SQS Query Policy State v0; migrating to v1")
return migrateSqsQueuePolicyStateV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateSqsQueuePolicyStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {

if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")

return is, nil
}

log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

is.Attributes["id"] = is.Attributes["queue_url"]
is.ID = is.Attributes["queue_url"]

log.Printf("[DEBUG] Attributes after migration: %#v, new id: %s", is.Attributes, is.Attributes["queue_url"])

return is, nil

}
45 changes: 45 additions & 0 deletions aws/resource_aws_sqs_queue_policy_migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestAWSSqsQueuePolicyMigrateState(t *testing.T) {

cases := map[string]struct {
StateVersion int
ID string
Attributes map[string]string
Expected string
Meta interface{}
}{
"v0_1": {
StateVersion: 0,
ID: "sqs-policy-https://queue.amazonaws.com/0123456789012/myqueue",
Attributes: map[string]string{
"policy": "{}",
"queue_url": "https://queue.amazonaws.com/0123456789012/myqueue",
},
Expected: "https://queue.amazonaws.com/0123456789012/myqueue",
},
}

for tn, tc := range cases {
is := &terraform.InstanceState{
ID: tc.ID,
Attributes: tc.Attributes,
}
is, err := resourceAwsSqsQueuePolicyMigrateState(
tc.StateVersion, is, tc.Meta)

if err != nil {
t.Fatalf("bad: %s, err: %#v", tn, err)
}

if is.ID != tc.Expected {
t.Fatalf("bad sqs queue policy id: %s\n\n expected: %s", is.ID, tc.Expected)
}
}
}
22 changes: 22 additions & 0 deletions aws/resource_aws_sqs_queue_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ func TestAccAWSSQSQueuePolicy_basic(t *testing.T) {
})
}

func TestAccAWSSQSQueuePolicy_import(t *testing.T) {
queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5))
resourceName := "aws_sqs_queue_policy.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSQSPolicyConfig_basic(queueName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSSQSPolicyConfig_basic(r string) string {
return fmt.Sprintf(testAccAWSSQSPolicyConfig_basic_tpl, r)
}
Expand Down
10 changes: 9 additions & 1 deletion website/docs/r/sqs_queue_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: |-
Provides a SQS Queue Policy resource.
---

# aws\_sqs\_queue\_policy
# aws_sqs_queue_policy

Allows you to set a policy of an SQS Queue
while referencing ARN of the queue within the policy.
Expand Down Expand Up @@ -50,3 +50,11 @@ The following arguments are supported:

* `queue_url` - (Required) The URL of the SQS Queue to which to attach the policy
* `policy` - (Required) The JSON policy for the SQS queue

## Import

SQS Queue Policies can be imported using the queue URL, e.g.

```
$ terraform import aws_sqs_queue_policy.test https://queue.amazonaws.com/0123456789012/myqueue
```