Skip to content

Commit

Permalink
Merge pull request #2806 from yohei1126/sub-attributes
Browse files Browse the repository at this point in the history
Add SNS subscription filter
  • Loading branch information
bflad authored Feb 7, 2018
2 parents 770062d + 1a8817c commit 02b2848
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
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 @@ -76,6 +78,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 @@ -127,6 +139,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

0 comments on commit 02b2848

Please sign in to comment.