Class: Fog::DNS::AWS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/dns.rb,
lib/fog/aws/requests/dns/get_hosted_zone.rb,
lib/fog/aws/requests/dns/list_hosted_zones.rb,
lib/fog/aws/requests/dns/create_hosted_zone.rb,
lib/fog/aws/requests/dns/change_resource_record_sets.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



47
48
49
50
51
52
# File 'lib/fog/aws/dns.rb', line 47

def initialize(options={})
  require 'mime/types'
  @use_iam_profile = options[:use_iam_profile]
  setup_credentials(options)
  @region             = options[:region]
end

Class Method Details

.dataObject



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

def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :buckets => {},
        :limits => {
          :duplicate_domains => 5
        },
        :zones => {}
      }
    end
  end
end

.resetObject



43
44
45
# File 'lib/fog/aws/dns.rb', line 43

def self.reset
  @data = nil
end

Instance Method Details

#change_resource_record_sets(zone_id, change_batch, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fog/aws/requests/dns/change_resource_record_sets.rb', line 137

def change_resource_record_sets(zone_id, change_batch, options = {})
  response = Excon::Response.new
  errors   = []

  if (zone = self.data[:zones][zone_id])
    response.status = 200

    change_batch.each do |change|
      case change[:action]
      when "CREATE"
        if zone[:records][change[:type]].nil?
          zone[:records][change[:type]] = {}
        end

        if zone[:records][change[:type]][change[:name]].nil?
          zone[:records][change[:type]][change[:name]] = {
            :name => change[:name],
            :type => change[:type],
            :ttl => change[:ttl],
            :resource_records => change[:resource_records]
          }
        else
          errors << "Tried to create resource record set #{change[:name]}. type #{change[:type]}, but it already exists"
        end
      when "DELETE"
        if zone[:records][change[:type]].nil? || zone[:records][change[:type]].delete(change[:name]).nil?
          errors << "Tried to delete resource record set #{change[:name]}. type #{change[:type]}, but it was not found"
        end
      end
    end

    if errors.empty?
      response.body = {
        'ChangeInfo' => {
          'Id' => "/change/#{Fog::AWS::Mock.change_id}",
          'Status' => 'INSYNC',
          'SubmittedAt' => Time.now.utc.iso8601
        }
      }
      response
    else
      response.status = 400
      response.body = "<?xml version=\"1.0\"?><InvalidChangeBatch xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Messages>#{errors.map {|e| "<Message>#{e}</Message>"}.join()}</Messages></InvalidChangeBatch>"
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  else
    response.status = 404
    response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>NoSuchHostedZone</Code><Message>A hosted zone with the specified hosted zone ID does not exist.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestID></Response>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
end

#create_hosted_zone(name, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fog/aws/requests/dns/create_hosted_zone.rb', line 62

def create_hosted_zone(name, options = {})
  response = Excon::Response.new
  if list_hosted_zones.body['HostedZones'].find_all {|z| z['Name'] == name}.size < self.data[:limits][:duplicate_domains]
    response.status = 201
    if options[:caller_ref]
      caller_ref = options[:caller_ref]
    else
      #make sure we have a unique call reference
      caller_ref = "ref-#{rand(1000000).to_s}"
    end
    zone_id = "/hostedzone/#{Fog::AWS::Mock.zone_id}"
    self.data[:zones][zone_id] = {
      :id => zone_id,
      :name => name,
      :reference => caller_ref,
      :comment => options[:comment],
      :records => {}
    }
    response.body = {
      'HostedZone' => {
        'Id' => zone_id,
        'Name' => name,
        'CallerReference' => caller_ref,
        'Comment' => options[:comment]
      },
      'ChangeInfo' => {
        'Id' => "/change/#{Fog::AWS::Mock.change_id}",
        'Status' => 'INSYNC',
        'SubmittedAt' => Time.now.utc.iso8601
      },
      'NameServers' => Fog::AWS::Mock.nameservers
    }
    response
  else
    response.status = 400
    response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>DelegationSetNotAvailable</Code><Message>Amazon Route 53 allows some duplication, but Amazon Route 53 has a maximum threshold of duplicated domains. This error is generated when you reach that threshold. In this case, the error indicates that too many hosted zones with the given domain name exist. If you want to create a hosted zone and Amazon Route 53 generates this error, contact Customer Support.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestID></Response>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
end

#dataObject



54
55
56
# File 'lib/fog/aws/dns.rb', line 54

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

#get_hosted_zone(zone_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aws/requests/dns/get_hosted_zone.rb', line 42

def get_hosted_zone(zone_id)
  response = Excon::Response.new
  if (zone = self.data[:zones][zone_id])
    response.status = 200
    response.body = {
      'HostedZone' => {
        'Id' => zone[:id],
        'Name' => zone[:name],
        'CallerReference' => zone[:reference],
        'Comment' => zone[:comment]
      },
      'NameServers' => Fog::AWS::Mock.nameservers
    }
    response
  else
    response.status = 404
    response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>NoSuchHostedZone</Code><Message>A hosted zone with the specified hosted zone ID does not exist.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestID></Response>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
end

#list_hosted_zones(options = {}) ⇒ 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/aws/requests/dns/list_hosted_zones.rb', line 55

def list_hosted_zones(options = {})

  if options[:max_items].nil?
    maxitems = 100
  else
    maxitems = options[:max_items]
  end

  if options[:marker].nil?
    start = 0
  else
    start = self.data[:zones].find_index {|z| z[:id] == options[:marker]}
  end

  zones     = self.data[:zones].values[start, maxitems]
  next_zone = self.data[:zones].values[start + maxitems]
  truncated = !next_zone.nil?

  response = Excon::Response.new
  response.status = 200
  response.body = {
    'HostedZones' => zones.map do |z|
      {
        'Id' => z[:id],
        'Name' => z[:name],
        'CallerReference' => z[:reference],
        'Comment' => z[:comment],
      }
    end,
    'Marker' => options[:marker].to_s,
    'MaxItems' => options[:max_items].to_s,
    'IsTruncated' => truncated.to_s
  }

  if truncated
    response.body['NextMarker'] = next_zone[:id]
  end

  response
end

#reset_dataObject



58
59
60
# File 'lib/fog/aws/dns.rb', line 58

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

#setup_credentials(options) ⇒ Object



66
67
68
# File 'lib/fog/aws/dns.rb', line 66

def setup_credentials(options)
  @aws_access_key_id  = options[:aws_access_key_id]
end

#signature(params) ⇒ Object



62
63
64
# File 'lib/fog/aws/dns.rb', line 62

def signature(params)
  "foo"
end