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

resource/aws_sns_topic_subscription: Always set Terraform state on reads, fix filter_policy removal #6023

Merged
merged 1 commit into from
Oct 3, 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
22 changes: 22 additions & 0 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -112,6 +113,27 @@ func testAccCheckResourceAttrRegionalARN(resourceName, attributeName, arnService
}
}

// testAccCheckResourceAttrRegionalARN ensures the Terraform state regexp matches a formatted ARN with region
func testAccMatchResourceAttrRegionalARN(resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
return func(s *terraform.State) error {
arnRegexp := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Region: testAccGetRegion(),
Resource: arnResourceRegexp.String(),
Service: arnService,
}.String()

attributeMatch, err := regexp.Compile(arnRegexp)

if err != nil {
return fmt.Errorf("Unable to compile ARN regexp (%s): %s", arnRegexp, err)
}

return resource.TestMatchResourceAttr(resourceName, attributeName, attributeMatch)(s)
}
}

// testAccCheckResourceAttrGlobalARN ensures the Terraform state exactly matches a formatted ARN without region
func testAccCheckResourceAttrGlobalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down
80 changes: 32 additions & 48 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/hashicorp/terraform/helper/validation"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/service/sns"
)
Expand All @@ -24,15 +23,6 @@ const awsSNSPendingConfirmationMessage = "pending confirmation"
const awsSNSPendingConfirmationMessageWithoutSpaces = "pendingconfirmation"
const awsSNSPasswordObfuscationPattern = "****"

var SNSSubscriptionAttributeMap = map[string]string{
"topic_arn": "TopicArn",
"endpoint": "Endpoint",
"protocol": "Protocol",
"raw_message_delivery": "RawMessageDelivery",
"filter_policy": "FilterPolicy",
"delivery_policy": "DeliveryPolicy",
}

func resourceAwsSnsTopicSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSnsTopicSubscriptionCreate,
Expand Down Expand Up @@ -134,31 +124,26 @@ func resourceAwsSnsTopicSubscriptionUpdate(d *schema.ResourceData, meta interfac
snsconn := meta.(*AWSClient).snsconn

if d.HasChange("raw_message_delivery") {
_, n := d.GetChange("raw_message_delivery")

attrValue := "false"

if n.(bool) {
attrValue = "true"
}

if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "raw_message_delivery", attrValue); err != nil {
if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "RawMessageDelivery", fmt.Sprintf("%t", d.Get("raw_message_delivery").(bool))); err != nil {
return err
}
}

if d.HasChange("filter_policy") {
_, n := d.GetChange("filter_policy")
filterPolicy := d.Get("filter_policy").(string)

// https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html#message-filtering-policy-remove
if filterPolicy == "" {
filterPolicy = "{}"
}

if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "filter_policy", n.(string)); err != nil {
if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "FilterPolicy", filterPolicy); err != nil {
return err
}
}

if d.HasChange("delivery_policy") {
_, n := d.GetChange("delivery_policy")

if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "delivery_policy", n.(string)); err != nil {
if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "DeliveryPolicy", d.Get("delivery_policy").(string)); err != nil {
return err
}
}
Expand All @@ -174,34 +159,34 @@ func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{
attributeOutput, err := snsconn.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{
SubscriptionArn: aws.String(d.Id()),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFound" {
log.Printf("[WARN] SNS Topic Subscription (%s) not found, error code (404)", d.Id())
d.SetId("")
return nil
}

return err
if isAWSErr(err, sns.ErrCodeNotFoundException, "") {
log.Printf("[WARN] SNS Topic Subscription (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading SNS Topic Subscription (%s) attributes: %s", d.Id(), err)
}

if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
attrHash := attributeOutput.Attributes
resource := *resourceAwsSnsTopicSubscription()
if attributeOutput == nil || len(attributeOutput.Attributes) == 0 {
return fmt.Errorf("error reading SNS Topic Subscription (%s) attributes: no attributes found", d.Id())
}

for iKey, oKey := range SNSSubscriptionAttributeMap {
log.Printf("[DEBUG] Reading %s => %s", iKey, oKey)
d.Set("arn", attributeOutput.Attributes["SubscriptionArn"])
d.Set("delivery_policy", attributeOutput.Attributes["DeliveryPolicy"])
d.Set("endpoint", attributeOutput.Attributes["Endpoint"])
d.Set("filter_policy", attributeOutput.Attributes["FilterPolicy"])
d.Set("protocol", attributeOutput.Attributes["Protocol"])

if attrHash[oKey] != nil {
if resource.Schema[iKey] != nil {
var value string
value = *attrHash[oKey]
log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, value)
d.Set(iKey, value)
}
}
}
d.Set("raw_message_delivery", false)
if v, ok := attributeOutput.Attributes["RawMessageDelivery"]; ok && aws.StringValue(v) == "true" {
d.Set("raw_message_delivery", true)
}

d.Set("topic_arn", attributeOutput.Attributes["TopicArn"])

return nil
}

Expand Down Expand Up @@ -338,16 +323,15 @@ func obfuscateEndpoint(endpoint string) string {
}

func snsSubscriptionAttributeUpdate(snsconn *sns.SNS, subscriptionArn, attributeName, attributeValue string) error {
awsAttributeName := SNSSubscriptionAttributeMap[attributeName]
req := &sns.SetSubscriptionAttributesInput{
SubscriptionArn: aws.String(subscriptionArn),
AttributeName: aws.String(awsAttributeName),
AttributeName: aws.String(attributeName),
AttributeValue: aws.String(attributeValue),
}
_, err := snsconn.SetSubscriptionAttributes(req)

if err != nil {
return fmt.Errorf("Unable to set %s attribute on subscription: %s", attributeName, err)
return fmt.Errorf("error setting subscription (%s) attribute (%s): %s", subscriptionArn, attributeName, err)
}
return nil
}
Expand Down
Loading