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

r/aws_sns_topic: Add support of prefixes #2753

Merged
merged 4 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 17 additions & 2 deletions aws/resource_aws_sns_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -32,8 +33,15 @@ func resourceAwsSnsTopic() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
},
"name_prefix": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
},
"display_name": &schema.Schema{
Expand Down Expand Up @@ -74,7 +82,14 @@ func resourceAwsSnsTopic() *schema.Resource {
func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
snsconn := meta.(*AWSClient).snsconn

name := d.Get("name").(string)
var name string
if v, ok := d.GetOk("name"); ok {
name = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
name = resource.PrefixedUniqueId(v.(string))
} else {
name = resource.UniqueId()
}

log.Printf("[DEBUG] SNS create topic: %s", name)

Expand Down
28 changes: 28 additions & 0 deletions aws/resource_aws_sns_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ func TestAccAWSSNSTopic_basic(t *testing.T) {
})
}

func TestAccAWSSNSTopic_prefix(t *testing.T) {
startsWithPrefix := regexp.MustCompile("^terraform-test-topic-")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_sns_topic.test_topic",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSNSTopicConfig_withPrefix(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
resource.TestMatchResourceAttr("aws_sns_topic.test_topic", "name", startsWithPrefix),
),
},
},
})
}

func TestAccAWSSNSTopic_policy(t *testing.T) {
rName := acctest.RandString(10)
expectedPolicy := `{"Statement":[{"Sid":"Stmt1445931846145","Effect":"Allow","Principal":{"AWS":"*"},"Action":"sns:Publish","Resource":"arn:aws:sns:us-west-2::example"}],"Version":"2012-10-17","Id":"Policy1445931846145"}`
Expand Down Expand Up @@ -258,6 +278,14 @@ resource "aws_sns_topic" "test_topic" {
`, r)
}

func testAccAWSSNSTopicConfig_withPrefix() string {
return `
resource "aws_sns_topic" "test_topic" {
name_prefix = "terraform-test-topic-"
}
`
}

func testAccAWSSNSTopicWithPolicy(r string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
Expand Down
5 changes: 3 additions & 2 deletions website/docs/r/sns_topic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ resource "aws_sns_topic" "user_updates" {

The following arguments are supported:

* `name` - (Required) The friendly name for the SNS topic
* `name` - (Optional) The friendly name for the SNS topic. By default generated by Terraform.
* `name_prefix` - (Optional) The friendly name for the SNS topic. Conflicts with `name`.
* `display_name` - (Optional) The display name for the SNS topic
* `policy` - (Optional) The fully-formed AWS policy as JSON
* `delivery_policy` - (Optional) The SNS delivery policy
Expand All @@ -40,4 +41,4 @@ SNS Topics can be imported using the `topic arn`, e.g.

```
$ terraform import aws_sns_topic.user_updates arn:aws:sns:us-west-2:0123456789012:my-topic
```
```