Skip to content

Commit

Permalink
fixup: move FIFO topic validation to CustomizeDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekr committed Jan 20, 2021
1 parent 0d04477 commit 95fd6be
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions aws/resource_aws_sns_topic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"context"
"fmt"
"log"
"strconv"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/naming"
)

func resourceAwsSnsTopic() *schema.Resource {
Expand All @@ -24,6 +26,7 @@ func resourceAwsSnsTopic() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
CustomizeDiff: resourceAwsSnsTopicCustomizeDiff,

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -154,21 +157,6 @@ func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
}

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

if fifoTopic {
if errors := validateSNSFifoTopicName(name); len(errors) > 0 {
return fmt.Errorf("Error validating the SNS FIFO topic name: %v", errors)
}
} else {
if errors := validateSNSNonFifoTopicName(name); len(errors) > 0 {
return fmt.Errorf("Error validating SNS topic name: %v", errors)
}
}

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
Expand Down Expand Up @@ -552,6 +540,33 @@ func resourceAwsSnsTopicDelete(d *schema.ResourceData, meta interface{}) error {
return err
}

func resourceAwsSnsTopicCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
fifoTopic := diff.Get("fifo_topic").(bool)
contentBasedDeduplication := diff.Get("content_based_deduplication").(bool)

if diff.Id() == "" {
// Create.

name := naming.Generate(diff.Get("name").(string), diff.Get("name_prefix").(string))

if fifoTopic {
if errors := validateSNSFifoTopicName(name); len(errors) > 0 {
return fmt.Errorf("Error validating the SNS FIFO topic name: %v", errors)
}
} else {
if errors := validateSNSNonFifoTopicName(name); len(errors) > 0 {
return fmt.Errorf("Error validating SNS topic name: %v", errors)
}
}
}

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

return nil
}

func updateAwsSnsTopicAttribute(topicArn, name string, value interface{}, conn *sns.SNS) error {
// Ignore an empty policy
if name == "Policy" && value == "" {
Expand Down

0 comments on commit 95fd6be

Please sign in to comment.