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 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
2 changes: 1 addition & 1 deletion aws/import_aws_sns_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccAWSSNSTopic_importBasic(t *testing.T) {
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSNSTopicConfig(rName),
Config: testAccAWSSNSTopicConfig_withName(rName),
},

resource.TestStep{
Expand Down
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
53 changes: 51 additions & 2 deletions aws/resource_aws_sns_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ import (
)

func TestAccAWSSNSTopic_basic(t *testing.T) {
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_withGeneratedName,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
),
},
},
})
}

func TestAccAWSSNSTopic_name(t *testing.T) {
rName := acctest.RandString(10)

resource.Test(t, resource.TestCase{
Expand All @@ -24,9 +41,29 @@ func TestAccAWSSNSTopic_basic(t *testing.T) {
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSNSTopicConfig(rName),
Config: testAccAWSSNSTopicConfig_withName(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
),
},
},
})
}

func TestAccAWSSNSTopic_namePrefix(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_withNamePrefix(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
resource.TestMatchResourceAttr("aws_sns_topic.test_topic", "name", startsWithPrefix),
),
},
},
Expand Down Expand Up @@ -250,14 +287,26 @@ func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
}
}

func testAccAWSSNSTopicConfig(r string) string {
const testAccAWSSNSTopicConfig_withGeneratedName = `
resource "aws_sns_topic" "test_topic" {}
`

func testAccAWSSNSTopicConfig_withName(r string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
name = "terraform-test-topic-%s"
}
`, r)
}

func testAccAWSSNSTopicConfig_withNamePrefix() 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
```
```