Skip to content

Commit

Permalink
data-source/aws_sqs_queue: Implement #2311 feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Apr 11, 2018
1 parent 6faa2ce commit bbefc33
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
19 changes: 10 additions & 9 deletions aws/data_source_aws_sqs_queue.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package aws

import (
"fmt"

"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"
)

Expand All @@ -29,26 +30,26 @@ func dataSourceAwsSqsQueue() *schema.Resource {

func dataSourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sqsconn
target := d.Get("name").(string)
name := d.Get("name").(string)

urlOutput, err := conn.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(target),
QueueName: aws.String(name),
})
if err != nil {
return errwrap.Wrapf("Error getting queue URL: {{err}}", err)
if err != nil || urlOutput.QueueUrl == nil {
return fmt.Errorf("Error getting queue URL: %s", err)
}

queueURL := *urlOutput.QueueUrl
queueURL := aws.StringValue(urlOutput.QueueUrl)

attributesOutput, err := conn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
QueueUrl: &queueURL,
QueueUrl: aws.String(queueURL),
AttributeNames: []*string{aws.String(sqs.QueueAttributeNameQueueArn)},
})
if err != nil {
return errwrap.Wrapf("Error getting queue attributes: {{err}}", err)
return fmt.Errorf("Error getting queue attributes: %s", err)
}

d.Set("arn", *attributesOutput.Attributes[sqs.QueueAttributeNameQueueArn])
d.Set("arn", aws.StringValue(attributesOutput.Attributes[sqs.QueueAttributeNameQueueArn]))
d.Set("url", queueURL)
d.SetId(queueURL)

Expand Down
62 changes: 34 additions & 28 deletions aws/data_source_aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,73 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsSqsQueue(t *testing.T) {
rName := acctest.RandomWithPrefix("tf_acc_test_")
resourceName := "aws_sqs_queue.test"
datasourceName := "data.aws_sqs_queue.by_name"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsSqsQueueConfig,
Config: testAccDataSourceAwsSqsQueueConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsSqsQueueCheck("data.aws_sqs_queue.by_name"),
testAccDataSourceAwsSqsQueueCheck(datasourceName, resourceName),
),
},
},
})
}

func testAccDataSourceAwsSqsQueueCheck(name string) resource.TestCheckFunc {
func testAccDataSourceAwsSqsQueueCheck(datasourceName, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
rs, ok := s.RootModule().Resources[datasourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", name)
return fmt.Errorf("root module has no resource called %s", datasourceName)
}

sqsQueueRs, ok := s.RootModule().Resources["aws_sqs_queue.tf_test"]
sqsQueueRs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("can't find aws_sqs_queue.tf_test in state")
return fmt.Errorf("root module has no resource called %s", resourceName)
}

attr := rs.Primary.Attributes
attrNames := []string{
"arn",
"name",
}

if attr["name"] != sqsQueueRs.Primary.Attributes["name"] {
return fmt.Errorf(
"name is %s; want %s",
attr["name"],
sqsQueueRs.Primary.Attributes["name"],
)
for _, attrName := range attrNames {
if rs.Primary.Attributes[attrName] != sqsQueueRs.Primary.Attributes[attrName] {
return fmt.Errorf(
"%s is %s; want %s",
attrName,
rs.Primary.Attributes[attrName],
sqsQueueRs.Primary.Attributes[attrName],
)
}
}

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"
func testAccDataSourceAwsSqsQueueConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "wrong" {
name = "%[1]s_wrong"
}
resource "aws_sqs_queue" "tf_wrong2" {
name = "wrong2"
resource "aws_sqs_queue" "test" {
name = "%[1]s"
}
data "aws_sqs_queue" "by_name" {
name = "${aws_sqs_queue.tf_test.name}"
name = "${aws_sqs_queue.test.name}"
}
`, rName)
}
`
2 changes: 1 addition & 1 deletion website/docs/d/sqs_queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: |-
Get information on an Amazon Simple Queue Service (SQS) Queue
---

# aws\_sqs\_queue
# Data Source: 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
Expand Down

0 comments on commit bbefc33

Please sign in to comment.