diff --git a/aws/resource_aws_sqs_queue_policy.go b/aws/resource_aws_sqs_queue_policy.go index 3432497994b..2ec1f37821f 100644 --- a/aws/resource_aws_sqs_queue_policy.go +++ b/aws/resource_aws_sqs_queue_policy.go @@ -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" ) @@ -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, @@ -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) + 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 @@ -78,6 +82,7 @@ func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) err } d.Set("policy", policy) + d.Set("queue_url", d.Id()) return nil } @@ -85,10 +90,9 @@ func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) err 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": "", }), diff --git a/aws/resource_aws_sqs_queue_policy_migrate.go b/aws/resource_aws_sqs_queue_policy_migrate.go new file mode 100644 index 00000000000..2906c171919 --- /dev/null +++ b/aws/resource_aws_sqs_queue_policy_migrate.go @@ -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 + +} diff --git a/aws/resource_aws_sqs_queue_policy_migrate_test.go b/aws/resource_aws_sqs_queue_policy_migrate_test.go new file mode 100644 index 00000000000..6a9954a7d27 --- /dev/null +++ b/aws/resource_aws_sqs_queue_policy_migrate_test.go @@ -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) + } + } +} diff --git a/aws/resource_aws_sqs_queue_policy_test.go b/aws/resource_aws_sqs_queue_policy_test.go index c663cac1943..72d5ab743e0 100644 --- a/aws/resource_aws_sqs_queue_policy_test.go +++ b/aws/resource_aws_sqs_queue_policy_test.go @@ -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) } diff --git a/website/docs/r/sqs_queue_policy.html.markdown b/website/docs/r/sqs_queue_policy.html.markdown index ecb0ccb28a9..579a54d8be7 100644 --- a/website/docs/r/sqs_queue_policy.html.markdown +++ b/website/docs/r/sqs_queue_policy.html.markdown @@ -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. @@ -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 +```