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

New Data Source: aws_sqs_queue #2311

Merged
merged 1 commit into from
Apr 11, 2018
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should also set the other attributes, as in the resources, otherwise we would need to do it at some point. In the same way as the resource, could you do that? :)


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"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some randomization to the name here and below?

}
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, those antislashes are not needed anymore :)


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.