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

Update resource_aws_sns_topic_subscription to create the subscription… #10496

Merged
Merged
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
34 changes: 30 additions & 4 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func resourceAwsSnsTopicSubscriptionCreate(d *schema.ResourceData, meta interfac
// Write the ARN to the 'arn' field for export
d.Set("arn", output.SubscriptionArn)

return resourceAwsSnsTopicSubscriptionUpdate(d, meta)
return resourceAwsSnsTopicSubscriptionRead(d, meta)
}

func resourceAwsSnsTopicSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -201,12 +201,37 @@ func resourceAwsSnsTopicSubscriptionDelete(d *schema.ResourceData, meta interfac
return err
}

// Assembles supplied attributes into a single map - empty/default values are excluded from the map
func getResourceAttributes(d *schema.ResourceData) (output map[string]*string) {
delivery_policy := d.Get("delivery_policy").(string)
filter_policy := d.Get("filter_policy").(string)
raw_message_delivery := d.Get("raw_message_delivery").(bool)

// Collect attributes if available
attributes := map[string]*string{}

if delivery_policy != "" {
attributes["DeliveryPolicy"] = aws.String(delivery_policy)
}

if filter_policy != "" {
attributes["FilterPolicy"] = aws.String(filter_policy)
}

if raw_message_delivery {
attributes["RawMessageDelivery"] = aws.String(fmt.Sprintf("%t", raw_message_delivery))
}

return attributes
}

func subscribeToSNSTopic(d *schema.ResourceData, snsconn *sns.SNS) (output *sns.SubscribeOutput, err error) {
protocol := d.Get("protocol").(string)
endpoint := d.Get("endpoint").(string)
topic_arn := d.Get("topic_arn").(string)
endpoint_auto_confirms := d.Get("endpoint_auto_confirms").(bool)
confirmation_timeout_in_minutes := d.Get("confirmation_timeout_in_minutes").(int)
attributes := getResourceAttributes(d)

if strings.Contains(protocol, "http") && !endpoint_auto_confirms {
return nil, fmt.Errorf("Protocol http/https is only supported for endpoints which auto confirms!")
Expand All @@ -215,9 +240,10 @@ func subscribeToSNSTopic(d *schema.ResourceData, snsconn *sns.SNS) (output *sns.
log.Printf("[DEBUG] SNS create topic subscription: %s (%s) @ '%s'", endpoint, protocol, topic_arn)

req := &sns.SubscribeInput{
Protocol: aws.String(protocol),
Endpoint: aws.String(endpoint),
TopicArn: aws.String(topic_arn),
Protocol: aws.String(protocol),
Endpoint: aws.String(endpoint),
TopicArn: aws.String(topic_arn),
Attributes: attributes,
}

output, err = snsconn.Subscribe(req)
Expand Down