diff --git a/aws/data_source_aws_sqs_queue.go b/aws/data_source_aws_sqs_queue.go new file mode 100644 index 00000000000..a27f931faa7 --- /dev/null +++ b/aws/data_source_aws_sqs_queue.go @@ -0,0 +1,56 @@ +package aws + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/sqs" + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsSqsQueue() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsSqsQueueRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "url": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).sqsconn + target := d.Get("name").(string) + + urlOutput, err := conn.GetQueueUrl(&sqs.GetQueueUrlInput{ + QueueName: aws.String(target), + }) + if err != nil { + return errwrap.Wrapf("Error getting queue URL: {{err}}", err) + } + + queueURL := *urlOutput.QueueUrl + + attributesOutput, err := conn.GetQueueAttributes(&sqs.GetQueueAttributesInput{ + QueueUrl: &queueURL, + AttributeNames: []*string{aws.String(sqs.QueueAttributeNameQueueArn)}, + }) + if err != nil { + return errwrap.Wrapf("Error getting queue attributes: {{err}}", err) + } + + d.Set("arn", *attributesOutput.Attributes[sqs.QueueAttributeNameQueueArn]) + d.Set("url", queueURL) + d.SetId(queueURL) + + return nil +} diff --git a/aws/data_source_aws_sqs_queue_test.go b/aws/data_source_aws_sqs_queue_test.go new file mode 100644 index 00000000000..4687407d13b --- /dev/null +++ b/aws/data_source_aws_sqs_queue_test.go @@ -0,0 +1,70 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceAwsSqsQueue(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsSqsQueueConfig, + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsSqsQueueCheck("data.aws_sqs_queue.by_name"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsSqsQueueCheck(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("root module has no resource called %s", name) + } + + sqsQueueRs, ok := s.RootModule().Resources["aws_sqs_queue.tf_test"] + if !ok { + return fmt.Errorf("can't find aws_sqs_queue.tf_test in state") + } + + attr := rs.Primary.Attributes + + if attr["name"] != sqsQueueRs.Primary.Attributes["name"] { + return fmt.Errorf( + "name is %s; want %s", + attr["name"], + sqsQueueRs.Primary.Attributes["name"], + ) + } + + return nil + } +} + +const testAccDataSourceAwsSqsQueueConfig = ` +provider "aws" { + region = "us-west-2" +} + +resource "aws_sqs_queue" "tf_wrong1" { + name = "wrong1" +} +resource "aws_sqs_queue" "tf_test" { + name = "tf_test" +} +resource "aws_sqs_queue" "tf_wrong2" { + name = "wrong2" +} + +data "aws_sqs_queue" "by_name" { + name = "${aws_sqs_queue.tf_test.name}" +} +` diff --git a/aws/provider.go b/aws/provider.go index 219efe2fe33..37242fb2e9b 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -214,6 +214,7 @@ func Provider() terraform.ResourceProvider { "aws_s3_bucket": dataSourceAwsS3Bucket(), "aws_s3_bucket_object": dataSourceAwsS3BucketObject(), "aws_sns_topic": dataSourceAwsSnsTopic(), + "aws_sqs_queue": dataSourceAwsSqsQueue(), "aws_ssm_parameter": dataSourceAwsSsmParameter(), "aws_subnet": dataSourceAwsSubnet(), "aws_subnet_ids": dataSourceAwsSubnetIDs(), diff --git a/website/aws.erb b/website/aws.erb index 71453affcc6..50d5136ac5f 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -193,6 +193,9 @@