provider/aws: data_aws_sns_topic (#11752)

* Initial commit of provider/aws: data_aws_sns_topic

* Pull-request fixes
This commit is contained in:
Cameron Wood 2017-02-21 16:47:48 +01:00 committed by Paul Stack
parent 85fdca8cbb
commit 7c122604a0
5 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,71 @@
package aws
import (
"fmt"
"regexp"
"time"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsSnsTopic() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsSnsTopicsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
validNamePattern := "^[A-Za-z0-9_-]+$"
validName, nameMatchErr := regexp.MatchString(validNamePattern, value)
if !validName || nameMatchErr != nil {
errors = append(errors, fmt.Errorf(
"%q must match regex '%v'", k, validNamePattern))
}
return
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsSnsTopicsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).snsconn
params := &sns.ListTopicsInput{}
target := d.Get("name")
var arns []string
err := conn.ListTopicsPages(params, func(page *sns.ListTopicsOutput, lastPage bool) bool {
for _, topic := range page.Topics {
topicPattern := fmt.Sprintf(".*:%v$", target)
matched, regexpErr := regexp.MatchString(topicPattern, *topic.TopicArn)
if matched && regexpErr == nil {
arns = append(arns, *topic.TopicArn)
}
}
return true
})
if err != nil {
return errwrap.Wrapf("Error describing topics: {{err}}", err)
}
if len(arns) == 0 {
return fmt.Errorf("No topic with name %q found in this region.", target)
}
if len(arns) > 1 {
return fmt.Errorf("Multiple topics with name %q found in this region.", target)
}
d.SetId(time.Now().UTC().String())
d.Set("arn", arns[0])
return nil
}

View File

@ -0,0 +1,70 @@
package aws
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDataSourceAwsSnsTopic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsSnsTopicConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsSnsTopicCheck("data.aws_sns_topic.by_name"),
),
},
},
})
}
func testAccDataSourceAwsSnsTopicCheck(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)
}
snsTopicRs, ok := s.RootModule().Resources["aws_sns_topic.tf_test"]
if !ok {
return fmt.Errorf("can't find aws_sns_topic.tf_test in state")
}
attr := rs.Primary.Attributes
if attr["name"] != snsTopicRs.Primary.Attributes["name"] {
return fmt.Errorf(
"name is %s; want %s",
attr["name"],
snsTopicRs.Primary.Attributes["name"],
)
}
return nil
}
}
const testAccDataSourceAwsSnsTopicConfig = `
provider "aws" {
region = "us-west-2"
}
resource "aws_sns_topic" "tf_wrong1" {
name = "wrong1"
}
resource "aws_sns_topic" "tf_test" {
name = "tf_test"
}
resource "aws_sns_topic" "tf_wrong2" {
name = "wrong2"
}
data "aws_sns_topic" "by_name" {
name = "${aws_sns_topic.tf_test.name}"
}
`

View File

@ -185,6 +185,7 @@ func Provider() terraform.ResourceProvider {
"aws_route_table": dataSourceAwsRouteTable(),
"aws_route53_zone": dataSourceAwsRoute53Zone(),
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
"aws_sns_topic": dataSourceAwsSnsTopic(),
"aws_subnet": dataSourceAwsSubnet(),
"aws_security_group": dataSourceAwsSecurityGroup(),
"aws_vpc": dataSourceAwsVpc(),

View File

@ -0,0 +1,29 @@
---
layout: "aws"
page_title: "AWS: aws_sns_topic
sidebar_current: "docs-aws-datasource-sns-topic"
description: |-
Get information on a Amazon Simple Notification Service (SNS) Topic
---
# aws\_sns\_topic
Use this data source to get the ARN of a topic in AWS Simple Notification
Service (SNS). By using this data source, you can reference SNS topics
without having to hard code the ARNs as input.
## Example Usage
```
data "aws_sns_topic" "example" {
name = "an_example_topic"
}
```
## Argument Reference
* `name` - (Required) The friendly name of the topic to match.
## Attributes Reference
* `arn` - Set to the ARN of the found topic, suitable for referencing in other resources that support SNS topics.

View File

@ -107,6 +107,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-sns-topic") %>>
<a href="/docs/providers/aws/d/sns_topic.html">aws_sns_topic</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-subnet") %>>
<a href="/docs/providers/aws/d/subnet.html">aws_subnet</a>
</li>