Module: Awspec::Helper::Finder::SNSTopic

Included in:
Awspec::Helper::Finder
Defined in:
lib/awspec/helper/finder/sns_topic.rb

Defined Under Namespace

Classes: SnsTopic

Instance Method Summary collapse

Instance Method Details

#find_sns_topic(topic_arn) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/awspec/helper/finder/sns_topic.rb', line 32

def find_sns_topic(topic_arn)
  # Find a SNS topic by searching from it's ARN.
  # Expects as parameter the SNS topic ARN.
  # Returns a Awspec::Helper::Finder::SNSTopic::SnsTopic if the topic is
  # found, nil otherwise.
  # If the SNS topic does not exist, AWS SDK throws an exception
  # which is acceptable in some cases, but in others will generate a
  # NoMethodFound during tests execution.
  # In order to avoid that and follow a uniform interface, the exception
  # will be captured and nil will be returned instead.
  # By convention, the returned object will be checked (see check_existence
  # method from Awspec::Type::Base class for an example) and an exception
  # Awspec::NoExistingResource will be raised.
  begin
    response = sns_client.get_topic_attributes({ topic_arn: topic_arn })
    topic = SnsTopic.new(response.attributes)
  rescue Aws::SNS::Errors::NotFound
    topic = nil
  end

  topic
end

#find_sns_topic_subs(topic_arn) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/awspec/helper/finder/sns_topic.rb', line 55

def find_sns_topic_subs(topic_arn)
  # Find a SNS topic subscribers by searching from it's ARN.
  # Expects as parameter the SNS topic ARN.
  # Returns a map (with keys as the subscribed ARN and the value instances
  # of respective objects) if the topic is found, nil otherwise.
  # If the SNS topic does not exist, AWS SDK throws an exception
  # which is acceptable in some cases, but in others will generate a
  # NoMethodFound during tests execution.
  # In order to avoid that and follow a uniform interface, the exception
  # will be captured and nil will be returned instead.
  # By convention, the returned object will be checked (see check_existence
  # method from Awspec::Type::Base class for an example) and an exception
  # Awspec::NoExistingResource will be raised.
  begin
    response = sns_client.list_subscriptions_by_topic({ topic_arn: topic_arn })
    subscriptions = {}
    response.subscriptions.each do |subscribed|
      subscriptions[subscribed['subscription_arn'].to_sym] = subscribed
    end
  rescue Aws::SNS::Errors::NotFound
    subscriptions = nil
  end

  subscriptions
end