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

Add SNS subscription filter #2806

Merged
merged 7 commits into from
Feb 7, 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
28 changes: 28 additions & 0 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -24,6 +25,7 @@ var SNSSubscriptionAttributeMap = map[string]string{
"endpoint": "Endpoint",
"protocol": "Protocol",
"raw_message_delivery": "RawMessageDelivery",
"filter_policy": "FilterPolicy",
}

func resourceAwsSnsTopicSubscription() *schema.Resource {
Expand Down Expand Up @@ -74,6 +76,16 @@ func resourceAwsSnsTopicSubscription() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"filter_policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressEquivalentJsonDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
},
}
}
Expand Down Expand Up @@ -143,6 +155,22 @@ func resourceAwsSnsTopicSubscriptionUpdate(d *schema.ResourceData, meta interfac
}
}

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

attrValue := n.(string)

req := &sns.SetSubscriptionAttributesInput{
SubscriptionArn: aws.String(d.Id()),
AttributeName: aws.String("FilterPolicy"),
AttributeValue: aws.String(attrValue),
}
_, err := snsconn.SetSubscriptionAttributes(req)

if err != nil {
return fmt.Errorf("Unable to set filter policy attribute on subscription: %s", err)
}
}
return resourceAwsSnsTopicSubscriptionRead(d, meta)
}

Expand Down
41 changes: 41 additions & 0 deletions aws/resource_aws_sns_topic_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"strconv"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -31,6 +32,27 @@ func TestAccAWSSNSTopicSubscription_basic(t *testing.T) {
})
}

func TestAccAWSSNSTopicSubscription_filterPolicy(t *testing.T) {
ri := acctest.RandInt()
filterPolicy1 := `{"key1": ["val1"], "key2": ["val2"]}`
filterPolicy2 := `{"key3": ["val3"], "key4": ["val4"]}`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSNSTopicSubscriptionConfig_filterPolicy(ri, strconv.Quote(filterPolicy1)),
Check: resource.TestCheckResourceAttr("aws_sns_topic_subscription.test_subscription", "filter_policy", filterPolicy1),
},
{
Config: testAccAWSSNSTopicSubscriptionConfig_filterPolicy(ri, strconv.Quote(filterPolicy2)),
Check: resource.TestCheckResourceAttr("aws_sns_topic_subscription.test_subscription", "filter_policy", filterPolicy2),
},
},
})
}
func TestAccAWSSNSTopicSubscription_autoConfirmingEndpoint(t *testing.T) {
ri := acctest.RandInt()

Expand Down Expand Up @@ -157,6 +179,25 @@ resource "aws_sns_topic_subscription" "test_subscription" {
`, i, i)
}

func testAccAWSSNSTopicSubscriptionConfig_filterPolicy(i int, policy string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
name = "terraform-test-topic-%d"
}

resource "aws_sqs_queue" "test_queue" {
name = "terraform-subscription-test-queue-%d"
}

resource "aws_sns_topic_subscription" "test_subscription" {
topic_arn = "${aws_sns_topic.test_topic.arn}"
protocol = "sqs"
endpoint = "${aws_sqs_queue.test_queue.arn}"
filter_policy = %s
}
`, i, i, policy)
}

func testAccAWSSNSTopicSubscriptionConfig_autoConfirmingEndpoint(i int) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
Expand Down