Skip to content

Commit

Permalink
Add fifo_topic and content_based_deduplication for SNS Topics
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekr committed Oct 24, 2020
1 parent 6702479 commit 9cdf5bb
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
49 changes: 49 additions & 0 deletions aws/resource_aws_sns_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ func resourceAwsSnsTopic() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"fifo_topic": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"content_based_deduplication": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"lambda_success_feedback_role_arn": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -142,13 +152,30 @@ func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
name = resource.UniqueId()
}

fifoTopic := d.Get("fifo_topic").(bool)
cbd := d.Get("content_based_deduplication").(bool)

if !fifoTopic && cbd {
return fmt.Errorf("Content based deduplication can only be set with FIFO topics")
}

attributes := make(map[string]*string)
// If FifoTopic is true, then the attribute must be passed into the call to CreateTopic
if fifoTopic {
attributes["FifoTopic"] = aws.String(strconv.FormatBool(fifoTopic))
}

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

req := &sns.CreateTopicInput{
Name: aws.String(name),
Tags: tags,
}

if len(attributes) > 0 {
req.Attributes = attributes
}

output, err := snsconn.CreateTopic(req)
if err != nil {
return fmt.Errorf("Error creating SNS topic: %s", err)
Expand Down Expand Up @@ -205,6 +232,12 @@ func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
return err
}
}
if d.HasChange("content_based_deduplication") {
_, v := d.GetChange("content_based_deduplication")
if err := updateAwsSnsTopicAttribute(d.Id(), "ContentBasedDeduplication", v, snsconn); err != nil {
return err
}
}
if d.HasChange("lambda_failure_feedback_role_arn") {
_, v := d.GetChange("lambda_failure_feedback_role_arn")
if err := updateAwsSnsTopicAttribute(d.Id(), "LambdaFailureFeedbackRoleArn", v, snsconn); err != nil {
Expand Down Expand Up @@ -315,6 +348,12 @@ func resourceAwsSnsTopicUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}
}
if d.HasChange("content_based_deduplication") {
_, v := d.GetChange("content_based_deduplication")
if err := updateAwsSnsTopicAttribute(d.Id(), "ContentBasedDeduplication", v, snsconn); err != nil {
return err
}
}
if d.HasChange("lambda_failure_feedback_role_arn") {
_, v := d.GetChange("lambda_failure_feedback_role_arn")
if err := updateAwsSnsTopicAttribute(d.Id(), "LambdaFailureFeedbackRoleArn", v, snsconn); err != nil {
Expand Down Expand Up @@ -415,6 +454,16 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
d.Set("sqs_failure_feedback_role_arn", aws.StringValue(attributeOutput.Attributes["SQSFailureFeedbackRoleArn"]))
d.Set("sqs_success_feedback_role_arn", aws.StringValue(attributeOutput.Attributes["SQSSuccessFeedbackRoleArn"]))

// set the boolean values
d.Set("fifo_topic", false)
if v, ok := attributeOutput.Attributes["FifoTopic"]; ok && aws.StringValue(v) == "true" {
d.Set("fifo_topic", true)
}
d.Set("content_based_deduplication", false)
if v, ok := attributeOutput.Attributes["ContentBasedDeduplication"]; ok && aws.StringValue(v) == "true" {
d.Set("content_based_deduplication", true)
}

// set the number values
var vStr string
var v int64
Expand Down
107 changes: 107 additions & 0 deletions aws/resource_aws_sns_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,85 @@ func TestAccAWSSNSTopic_deliveryStatus(t *testing.T) {
})
}

func TestAccAWSSNSTopic_FIFO(t *testing.T) {
attributes := make(map[string]string)
resourceName := "aws_sns_topic.test"
rName := acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSNSTopicConfigWithFIFO(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists(resourceName, attributes),
resource.TestCheckResourceAttr(resourceName, "fifo_topic", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSSNSTopic_FIFOWithContentBasedDeduplication(t *testing.T) {
attributes := make(map[string]string)
resourceName := "aws_sns_topic.test"
rName := acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSNSTopicConfigWithFIFOContentBasedDeduplication(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists(resourceName, attributes),
resource.TestCheckResourceAttr(resourceName, "fifo_topic", "true"),
resource.TestCheckResourceAttr(resourceName, "content_based_deduplication", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
// Test attribute update
{
Config: testAccAWSSNSTopicConfigWithFIFOContentBasedDeduplication(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists(resourceName, attributes),
resource.TestCheckResourceAttr(resourceName, "content_based_deduplication", "false"),
),
},
},
})
}

func TestAccAWSSNSTopic_FIFOExpectContentBasedDeduplicationError(t *testing.T) {
rName := acctest.RandString(10)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_sns_topic.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSNSTopicExpectContentBasedDeduplicationError(rName),
ExpectError: regexp.MustCompile(`Content based deduplication can only be set with FIFO topics`),
},
},
})
}

func TestAccAWSSNSTopic_encryption(t *testing.T) {
attributes := make(map[string]string)
resourceName := "aws_sns_topic.test"
Expand Down Expand Up @@ -754,6 +833,34 @@ resource "aws_sns_topic" "test" {
`, r)
}

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

func testAccAWSSNSTopicConfigWithFIFOContentBasedDeduplication(r string, cbd bool) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test" {
name = "terraform-test-topic-%s.fifo"
fifo_topic = true
content_based_deduplication = %t
}
`, r, cbd)
}

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

func testAccAWSSNSTopicConfigTags1(r, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test" {
Expand Down

0 comments on commit 9cdf5bb

Please sign in to comment.