Class: Fog::AWS::SNS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/sns.rb,
lib/fog/aws/requests/sns/subscribe.rb,
lib/fog/aws/requests/sns/list_topics.rb,
lib/fog/aws/requests/sns/create_topic.rb,
lib/fog/aws/requests/sns/delete_topic.rb,
lib/fog/aws/requests/sns/add_permission.rb,
lib/fog/aws/requests/sns/remove_permission.rb,
lib/fog/aws/requests/sns/list_subscriptions.rb,
lib/fog/aws/requests/sns/get_topic_attributes.rb,
lib/fog/aws/requests/sns/set_topic_attributes.rb,
lib/fog/aws/requests/sns/list_subscriptions_by_topic.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



44
45
46
47
48
49
50
51
# File 'lib/fog/aws/sns.rb', line 44

def initialize(options={})
  @region            = options[:region] || 'us-east-1'
  @aws_access_key_id = options[:aws_access_key_id]
  @account_id        = Fog::AWS::Mock.owner_id
  @module            = "sns"

  Fog::AWS.validate_region!(@region)
end

Instance Attribute Details

#account_id=(value) ⇒ Object (writeonly)

Sets the attribute account_id

Parameters:

  • value

    the value to set the attribute account_id to.



42
43
44
# File 'lib/fog/aws/sns.rb', line 42

def (value)
  @account_id = value
end

#regionObject (readonly)

Returns the value of attribute region.



41
42
43
# File 'lib/fog/aws/sns.rb', line 41

def region
  @region
end

Class Method Details

.dataObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/aws/sns.rb', line 29

def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :topics        => {},
        :subscriptions => {},
        :permissions   => {},
      }
    end
  end
end

Instance Method Details

#add_permission(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/aws/requests/sns/add_permission.rb', line 16

def add_permission(options = {})
  topic_arn = options.delete('TopicArn')
  label     = options.delete('Label')
  actions   = Hash[options.select { |k,v| k.match(/^ActionName/) }].values
  members   = Hash[options.select { |k,v| k.match(/^AWSAccountId/) }].values

  self.data[:permissions][topic_arn][label] = {
    :members => members,
    :actions => actions,
  }

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end

#create_topic(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/aws/requests/sns/create_topic.rb', line 26

def create_topic(name)
  response = Excon::Response.new

  topic_arn = Fog::AWS::Mock.arn(@module, @account_id, name, @region)

  self.data[:topics][topic_arn] = {
    "Owner"                   => @account_id,
    "SubscriptionsPending"    => 0,
    "SubscriptionsConfirmed"  => 0,
    "SubscriptionsDeleted"    => 0,
    "DisplayName"             => name,
    "TopicArn"                => topic_arn,
    "EffectiveDeliveryPolicy" => %Q|{"http":{"defaultHealthyRetryPolicy":{"minDelayTarget":20,"maxDelayTarget":20,"numRetries":3,"numMaxDelayRetries":0,"numNoDelayRetries":0,"numMinDelayRetries":0,"backoffFunction":"linear"},"disableSubscriptionOverrides":false}}|,
    "Policy"                  => %Q|{"Version":"2008-10-17","Id":"__default_policy_ID","Statement":[{"Sid":"__default_statement_ID","Effect":"Allow","Principal":{"AWS":"*"},"Action":["SNS:Publish","SNS:RemovePermission","SNS:SetTopicAttributes","SNS:DeleteTopic","SNS:ListSubscriptionsByTopic","SNS:GetTopicAttributes","SNS:Receive","SNS:AddPermission","SNS:Subscribe"],"Resource":"arn:aws:sns:us-east-1:990279267269:Smithy","Condition":{"StringEquals":{"AWS:SourceOwner":"990279267269"}}}]}|
  }
  self.data[:permissions][topic_arn] = {}
  response.body = {"TopicArn" => topic_arn, "RequestId" => Fog::AWS::Mock.request_id}
  response
end

#dataObject



53
54
55
# File 'lib/fog/aws/sns.rb', line 53

def data
  self.class.data[@region][@aws_access_key_id]
end

#delete_topic(arn) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/fog/aws/requests/sns/delete_topic.rb', line 26

def delete_topic(arn)
  self.data[:topics].delete(arn)

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end

#get_topic_attributes(arn) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/fog/aws/requests/sns/get_topic_attributes.rb', line 26

def get_topic_attributes(arn)
  response   = Excon::Response.new
  attributes = self.data[:topics][arn]

  response.body = {"Attributes" => attributes, "RequestId" => Fog::AWS::Mock.request_id}
  response
end

#list_subscriptions(options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/fog/aws/requests/sns/list_subscriptions.rb', line 26

def list_subscriptions(options={})
  response = Excon::Response.new

  response.body = {'Subscriptions' => self.data[:subscriptions].values, 'RequestId' => Fog::AWS::Mock.request_id}
  response
end

#list_subscriptions_by_topic(arn, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/fog/aws/requests/sns/list_subscriptions_by_topic.rb', line 28

def list_subscriptions_by_topic(arn, options={})
  response = Excon::Response.new

  subscriptions = self.data[:subscriptions].values.select { |s| s["TopicArn"] == arn }

  response.body = {'Subscriptions' => subscriptions, 'RequestId' => Fog::AWS::Mock.request_id}
  response
end

#list_topics(options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/fog/aws/requests/sns/list_topics.rb', line 26

def list_topics(options={})
  response = Excon::Response.new

  response.body = {'Topics' => self.data[:topics].keys, 'RequestId' => Fog::AWS::Mock.request_id}
  response
end

#remove_permission(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/aws/requests/sns/remove_permission.rb', line 16

def remove_permission(options = {})
  topic_arn = options['TopicArn']
  label     = options['Label']

  self.data[:permissions][topic_arn].delete(label)

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end

#reset_dataObject



57
58
59
# File 'lib/fog/aws/sns.rb', line 57

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end

#set_topic_attributes(arn, attribute_name, attribute_value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/aws/requests/sns/set_topic_attributes.rb', line 30

def set_topic_attributes(arn, attribute_name, attribute_value)
  attributes = self.data[:topics][arn]

  if %w(Policy DisplayName DeliveryPolicy).include?(attribute_name)
    attributes[attribute_name] = attribute_value
    self.data[:topics][arn] = attributes
  end

  response = Excon::Response.new
  response.body = {"RequestId" => Fog::AWS::Mock.request_id}
  response
end

#subscribe(arn, endpoint, protocol) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
# File 'lib/fog/aws/requests/sns/subscribe.rb', line 30

def subscribe(arn, endpoint, protocol)
  response = Excon::Response.new

  unless topic = self.data[:topics][arn]
    response.status = 400
    response.body = {
      'Code'    => 'InvalidParameterValue',
      'Message' => 'Invalid parameter: TopicArn',
      'Type'    => 'Sender',
    }

    return response
  end

  subscription_arn = Fog::AWS::Mock.arn(@module, @account_id, "#{topic["DisplayName"]}:#{Fog::AWS::Mock.request_id}", @region)

  self.data[:subscriptions][subscription_arn] = {
    "Protocol"        => protocol,
    "Owner"           => @account_id.to_s,
    "TopicArn"        => arn,
    "SubscriptionArn" => subscription_arn,
    "Endpoint"        => endpoint,
  }

  mock_data = Fog::AWS::SQS::Mock.data.values.find { |a| a.values.find { |d| d[:queues][endpoint] } }
  access_key = mock_data && mock_data.keys.first

  if protocol == "sqs" && access_key
    token     = SecureRandom.hex(128)
    message   = "You have chosen to subscribe to the topic #{arn}.\nTo confirm the subscription, visit the SubscribeURL included in this message."
    signature = Fog::HMAC.new("sha256", token).sign(message)

    Fog::AWS::SQS.new(
      :region                => self.region,
      :aws_access_key_id     => access_key,
      :aws_secret_access_key => SecureRandom.hex(3)
    ).send_message(endpoint, Fog::JSON.encode(
        "Type"             => "SubscriptionConfirmation",
        "MessageId"        => UUID.uuid,
        "Token"            => token,
        "TopicArn"         => arn,
        "Message"          => message,
        "SubscribeURL"     => "https://sns.#{self.region}.amazonaws.com/?Action=ConfirmSubscription&TopicArn=#{arn}&Token=#{token}",
        "Timestamp"        => Time.now.iso8601,
        "SignatureVersion" => "1",
        "Signature"        => signature,
        "SigningCertURL"   => "https://sns.#{self.region}.amazonaws.com/SimpleNotificationService-#{SecureRandom.hex(16)}.pem"
      ))
  end

  response.body = { 'SubscriptionArn' => 'pending confirmation', 'RequestId' => Fog::AWS::Mock.request_id }
  response
end