Skip to content

Commit

Permalink
Add 'aws_sqs_queue' data source
Browse files Browse the repository at this point in the history
  • Loading branch information
idubinskiy committed Nov 15, 2017
1 parent 359ebf9 commit 4519529
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
56 changes: 56 additions & 0 deletions aws/data_source_aws_sqs_queue.go
Original file line number Diff line number Diff line change
@@ -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
}
70 changes: 70 additions & 0 deletions aws/data_source_aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
@@ -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}"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
<li<%= sidebar_current("docs-aws-datasource-security-group") %>>
<a href="/docs/providers/aws/d/security_group.html">aws_security_group</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-sqs-queue") %>>
<a href="/docs/providers/aws/d/sqs_queue.html">aws_sqs_queue</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-sns-topic") %>>
<a href="/docs/providers/aws/d/sns_topic.html">aws_sns_topic</a>
</li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/sqs_queue.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "aws"
page_title: "AWS: aws_sqs_queue"
sidebar_current: "docs-aws-datasource-sqs-queue"
description: |-
Get information on an Amazon Simple Queue Service (SQS) Queue
---

# aws\_sqs\_queue

Use this data source to get the ARN and URL of queue in AWS Simple Queue Service (SQS).
By using this data source, you can reference SQS queues without having to hardcode
the ARNs as input.

## Example Usage

```hcl
data "aws_sqs_queue" "example" {
name = "queue"
}
```

## Argument Reference

* `name` - (Required) The name of the queue to match.

## Attributes Reference

* `arn` - The Amazon Resource Name (ARN) of the queue.
* `url` - The URL of the queue.

0 comments on commit 4519529

Please sign in to comment.