This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
SNS Support #332
Merged
Merged
SNS Support #332
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
728b80f
initial attempt at sns_topic support
uberblah 6ffdc81
fixes, tests for sns topics
uberblah 3c06598
sns subscriptions, bug fixes
uberblah 975d380
clarity, add bugfix to sns topics
uberblah 3e966ca
ensure all results are collected
uberblah 77d5df4
readability, remove unneeded bugfix
uberblah 49340e4
additional fields, some mandatory in tfstate
uberblah 7ba1108
style and formatting
uberblah 19506c0
fix rubocop violations
uberblah 4ddb5e0
add SNS subscriptions to readme
uberblah 479c05f
mark a field as optional
uberblah 260db3d
filter out email subscriptions
uberblah 1d7fd3d
minor template/test mismatch
uberblah 30facaf
style comments
uberblah 23e1a32
tests for commenting of esns email subscription tf
uberblah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module Terraforming | ||
module Resource | ||
class SNSTopic | ||
include Terraforming::Util | ||
|
||
def self.tf(client: Aws::SNS::Client.new) | ||
self.new(client).tf | ||
end | ||
|
||
def self.tfstate(client: Aws::SNS::Client.new) | ||
self.new(client).tfstate | ||
end | ||
|
||
def initialize(client) | ||
@client = client | ||
end | ||
|
||
def tf | ||
apply_template(@client, "tf/sns_topic") | ||
end | ||
|
||
def tfstate | ||
topics.inject({}) do |resources, topic| | ||
attributes = { | ||
"name" => module_name_of(topic), | ||
"id" => topic["TopicArn"], | ||
"arn" => topic["TopicArn"], | ||
"display_name" => topic["DisplayName"], | ||
"policy" => topic.key?("Policy") ? topic["Policy"] : "", | ||
"delivery_policy" => topic.key?("DeliveryPolicy") ? topic["DeliveryPolicy"] : "" | ||
} | ||
resources["aws_sns_topic.#{module_name_of(topic)}"] = { | ||
"type" => "aws_sns_topic", | ||
"primary" => { | ||
"id" => topic["TopicArn"], | ||
"attributes" => attributes | ||
} | ||
} | ||
|
||
resources | ||
end | ||
end | ||
|
||
private | ||
|
||
def topics | ||
topic_arns.map do |topic_arn| | ||
attributes = @client.get_topic_attributes({ | ||
topic_arn: topic_arn, | ||
}).attributes | ||
attributes["TopicArn"] = topic_arn | ||
attributes | ||
end | ||
end | ||
|
||
def topic_arns | ||
token = "" | ||
arns = [] | ||
|
||
begin | ||
resp = @client.list_topics(next_token: token) | ||
arns += resp.topics.map(&:topic_arn).flatten | ||
token = resp.next_token | ||
end until token.nil? | ||
|
||
arns | ||
end | ||
|
||
def module_name_of(topic) | ||
normalize_module_name(topic["TopicArn"].split(":").last) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module Terraforming | ||
module Resource | ||
class SNSTopicSubscription | ||
include Terraforming::Util | ||
|
||
def self.tf(client: Aws::SNS::Client.new) | ||
self.new(client).tf | ||
end | ||
|
||
def self.tfstate(client: Aws::SNS::Client.new) | ||
self.new(client).tfstate | ||
end | ||
|
||
def initialize(client) | ||
@client = client | ||
end | ||
|
||
def tf | ||
apply_template(@client, "tf/sns_topic_subscription") | ||
end | ||
|
||
def tfstate | ||
subscriptions.inject({}) do |resources, subscription| | ||
attributes = { | ||
"id" => subscription["SubscriptionArn"], | ||
"topic_arn" => subscription["TopicArn"], | ||
"protocol" => subscription["Protocol"], | ||
"endpoint" => subscription["Endpoint"], | ||
"raw_message_delivery" => subscription["RawMessageDelivery"], | ||
"confirmation_timeout_in_minutes" => | ||
subscription.key?("ConfirmationTimeoutInMinutes") ? subscription["ConfirmationTimeoutInMinutes"] : "1", | ||
"endpoint_auto_confirms" => | ||
subscription.key?("EndpointAutoConfirms") ? subscription["EndpointAutoConfirms"] : "false" | ||
} | ||
resources["aws_sns_topic_subscription.#{module_name_of(subscription)}"] = { | ||
"type" => "aws_sns_topic_subscription", | ||
"primary" => { | ||
"id" => subscription["SubscriptionArn"], | ||
"attributes" => attributes | ||
} | ||
} | ||
|
||
resources | ||
end | ||
end | ||
|
||
private | ||
|
||
def subscriptions | ||
subscription_arns.map do |subscription_arn| | ||
# check explicitly for an issue with some subscriptions that returns ARN=PendingConfirmation | ||
if subscription_arn == "PendingConfirmation" then next end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [rubocop]
|
||
|
||
attributes = @client.get_subscription_attributes({ | ||
subscription_arn: subscription_arn, | ||
}).attributes | ||
attributes["SubscriptionArn"] = subscription_arn | ||
attributes | ||
end.compact | ||
end | ||
|
||
def subscription_arns | ||
token = "" | ||
arns = [] | ||
|
||
begin | ||
resp = @client.list_subscriptions(next_token: token) | ||
arns += resp.subscriptions.map(&:subscription_arn).flatten | ||
token = resp.next_token | ||
end until token.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [rubocop]
|
||
|
||
arns | ||
end | ||
|
||
def module_name_of(subscription) | ||
normalize_module_name(subscription["SubscriptionArn"].split(":").last) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<% topics.each do |topic| -%> | ||
resource "aws_sns_topic" "<%= module_name_of(topic) %>" { | ||
name = "<%= module_name_of(topic) %>" | ||
display_name = "<%= topic["DisplayName"] %>" | ||
<% if topic.key? "Policy" -%> | ||
policy = <<POLICY | ||
<%= prettify_policy(topic["Policy"], unescape: true) %> | ||
POLICY | ||
<% end -%> | ||
<% if topic.key? "DeliveryPolicy" -%> | ||
delivery_policy = <<POLICY | ||
<%= prettify_policy(topic["DeliveryPolicy"], unescape: true) %> | ||
POLICY | ||
<% end -%> | ||
} | ||
|
||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<% subscriptions.each do |subscription| -%> | ||
resource "aws_sns_topic_subscription" "<%= module_name_of(subscription) %>" { | ||
topic_arn = "<%= subscription["TopicArn"] %>" | ||
protocol = "<%= subscription["Protocol"] %>" | ||
endpoint = "<%= subscription["Endpoint"] %>" | ||
<% if subscription.key? "RawMessageDelivery" -%> | ||
raw_message_delivery = "<%= subscription["RawMessageDelivery"] %>" | ||
<% end -%> | ||
<% if subscription.key? "ConfirmationTimeoutInMinutes" %> | ||
confirmation_timeout_in_minutes = "<%= subscription["ConfirmationTimeoutInMinutes"] %>" | ||
<% end -%> | ||
<% if subscription.key? "EndpointAutoConfirms" %> | ||
endpoint_auto_confirms = "<%= subscription["EndpointAutoConfirms"] %>" | ||
<% end -%> | ||
} | ||
|
||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
require "spec_helper" | ||
|
||
module Terraforming | ||
module Resource | ||
describe SNSTopic do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [rubocop]
|
||
let(:client) do | ||
Aws::SNS::Client.new(stub_responses: true) | ||
end | ||
|
||
let(:topics) do | ||
[ | ||
Aws::SNS::Types::Topic.new(topic_arn: "arn:aws:sns:us-west-2:012345678901:topicOfFanciness"), | ||
] | ||
end | ||
|
||
let(:attributes) do | ||
{ | ||
"SubscriptionsConfirmed" => "1", | ||
"DisplayName" => "topicOfFancinessDisplayName", | ||
"SubscriptionsDeleted" => "0", | ||
"EffectiveDeliveryPolicy" => "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":2,\"maxDelayTarget\":20,\"numRetries\":12,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":12,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", | ||
"Owner" => "012345678901", | ||
"Policy" => "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\",\"SNS:Receive\"],\"Resource\":\"arn:aws:sns:us-west-2:012345678901:topicOfFanciness\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"012345678901\"}}}]}", | ||
"DeliveryPolicy" => "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":2,\"maxDelayTarget\":20,\"numRetries\":12,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":12,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", | ||
"TopicArn" => "arn:aws:sns:us-west-2:012345678901:topicOfFanciness", | ||
"SubscriptionsPending" => "0" | ||
} | ||
end | ||
|
||
before do | ||
client.stub_responses(:list_topics, topics: topics) | ||
client.stub_responses(:get_topic_attributes, attributes: attributes) | ||
end | ||
|
||
describe ".tf" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [rubocop]
|
||
it "should generate tf" do | ||
expect(described_class.tf(client: client)).to eq <<-EOS | ||
resource "aws_sns_topic" "topicOfFanciness" { | ||
name = "topicOfFanciness" | ||
display_name = "topicOfFancinessDisplayName" | ||
policy = <<POLICY | ||
{ | ||
"Version": "2008-10-17", | ||
"Id": "__default_policy_ID", | ||
"Statement": [ | ||
{ | ||
"Sid": "__default_statement_ID", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "*" | ||
}, | ||
"Action": [ | ||
"SNS:GetTopicAttributes", | ||
"SNS:SetTopicAttributes", | ||
"SNS:AddPermission", | ||
"SNS:RemovePermission", | ||
"SNS:DeleteTopic", | ||
"SNS:Subscribe", | ||
"SNS:ListSubscriptionsByTopic", | ||
"SNS:Publish", | ||
"SNS:Receive" | ||
], | ||
"Resource": "arn:aws:sns:us-west-2:012345678901:topicOfFanciness", | ||
"Condition": { | ||
"StringEquals": { | ||
"AWS:SourceOwner": "012345678901" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
delivery_policy = <<POLICY | ||
{ | ||
"http": { | ||
"defaultHealthyRetryPolicy": { | ||
"minDelayTarget": 2, | ||
"maxDelayTarget": 20, | ||
"numRetries": 12, | ||
"numMaxDelayRetries": 0, | ||
"numNoDelayRetries": 0, | ||
"numMinDelayRetries": 12, | ||
"backoffFunction": "linear" | ||
}, | ||
"disableSubscriptionOverrides": false | ||
} | ||
} | ||
POLICY | ||
} | ||
|
||
EOS | ||
end | ||
end | ||
|
||
describe ".tfstate" do | ||
it "should generate tfstate" do | ||
expect(described_class.tfstate(client: client)).to eq({ | ||
"aws_sns_topic.topicOfFanciness" => { | ||
"type" => "aws_sns_topic", | ||
"primary" => { | ||
"id" => "arn:aws:sns:us-west-2:012345678901:topicOfFanciness", | ||
"attributes" => { | ||
"name" => "topicOfFanciness", | ||
"id" => "arn:aws:sns:us-west-2:012345678901:topicOfFanciness", | ||
"arn" => "arn:aws:sns:us-west-2:012345678901:topicOfFanciness", | ||
"display_name" => "topicOfFancinessDisplayName", | ||
"policy" => "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\",\"SNS:Receive\"],\"Resource\":\"arn:aws:sns:us-west-2:012345678901:topicOfFanciness\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"012345678901\"}}}]}", | ||
"delivery_policy" => "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":2,\"maxDelayTarget\":20,\"numRetries\":12,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":12,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}" | ||
}, | ||
}, | ||
} | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[rubocop]
Kernel#loop
withbreak
rather thanbegin/end/until
(orwhile
). :ref