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

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/dns.rb,
lib/fog/aws/requests/dns/get_change.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/delete_hosted_zone.rb,
lib/fog/aws/requests/dns/list_resource_record_sets.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.



50
51
52
53
54
# File 'lib/fog/aws/dns.rb', line 50

def initialize(options={})
  @use_iam_profile = options[:use_iam_profile]
  setup_credentials(options)
  @region             = options[:region]
end

Class Method Details

.dataObject



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

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 => {},
        :changes => {}
      }
    end
  end
end

.resetObject



46
47
48
# File 'lib/fog/aws/dns.rb', line 46

def self.reset
  @data = nil
end

Instance Method Details

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



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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/fog/aws/requests/dns/change_resource_record_sets.rb', line 153

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_id = Fog::AWS::Mock.change_id
    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?
          # raise change.to_s if change[:resource_records].nil?
          zone[:records][change[:type]][change[:name]] =
          if change[:alias_target]
            record = {
              :alias_target => change[:alias_target]
            }
          else
            record = {
              :ttl => change[:ttl].to_s,
            }
          end
          zone[:records][change[:type]][change[:name]] = {
            :change_id => change_id,
            :resource_records => change[:resource_records] || [],
            :name => change[:name],
            :type => change[:type]
          }.merge(record)
        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?
      change = {
        :id => change_id,
        :status => 'PENDING',
        :submitted_at => Time.now.utc.iso8601
      }
      self.data[:changes][change[:id]] = change
      response.body = {
        'Id' => change[:id],
        'Status' => change[:status],
        'SubmittedAt' => change[:submitted_at]
      }
      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



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
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/aws/requests/dns/create_hosted_zone.rb', line 57

def create_hosted_zone(name, options = {})
  # Append a trailing period to the name if absent.
  name = name + "." unless name.end_with?(".")

  response = Excon::Response.new
  if list_hosted_zones.body['HostedZones'].select {|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 => {}
    }
    change = {
      :id => Fog::AWS::Mock.change_id,
      :status => 'PENDING',
      :submitted_at => Time.now.utc.iso8601
    }
    self.data[:changes][change[:id]] = change
    response.body = {
      'HostedZone' => {
        'Id' => zone_id,
        'Name' => name,
        'CallerReference' => caller_ref,
        'Comment' => options[:comment]
      },
      'ChangeInfo' => {
        'Id' => change[:id],
        'Status' => change[:status],
        'SubmittedAt' => change[:submitted_at]
      },
      '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



56
57
58
# File 'lib/fog/aws/dns.rb', line 56

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

#delete_hosted_zone(zone_id) ⇒ Object



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
# File 'lib/fog/aws/requests/dns/delete_hosted_zone.rb', line 37

def delete_hosted_zone(zone_id)
  response = Excon::Response.new
  key = [zone_id, "/hostedzone/#{zone_id}"].find{|k| !self.data[:zones][k].nil?}
  if key
    change = {
      :id => Fog::AWS::Mock.change_id,
      :status => 'INSYNC',
      :submitted_at => Time.now.utc.iso8601
    }
    self.data[:changes][change[:id]] = change
    response.status = 200
    response.body = {
      'ChangeInfo' => {
        'Id' => change[:id],
        'Status' => change[:status],
        'SubmittedAt' => change[:submitted_at]
      }
    }
    self.data[:zones].delete(key)
    response
  else
    response.status = 404
    response.body = "<?xml version=\"1.0\"?><ErrorResponse xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Error><Type>Sender</Type><Code>NoSuchHostedZone</Code><Message>The specified hosted zone does not exist.</Message></Error><RequestId>#{Fog::AWS::Mock.request_id}</RequestId></ErrorResponse>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
end

#get_change(change_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/aws/requests/dns/get_change.rb', line 34

def get_change(change_id)
  response = Excon::Response.new
  # find the record with matching change_id
  # records = data[:zones].values.map{|z| z[:records].values.map{|r| r.values}}.flatten
  change = self.data[:changes][change_id]

  if change
    response.status = 200
     = Time.parse(change[:submitted_at])
    response.body = {
      'Id' => change[:id],
      # set as insync after some time
      'Status' => ( + Fog::Mock.delay) < Time.now ? 'INSYNC' : change[:status],
      'SubmittedAt' => change[:submitted_at]
    }
    response
  else
    response.status = 404
    response.body = "<?xml version=\"1.0\"?><ErrorResponse xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Error><Type>Sender</Type><Code>NoSuchChange</Code><Message>Could not find resource with ID: #{change_id}</Message></Error><RequestId>#{Fog::AWS::Mock.request_id}</RequestId></ErrorResponse>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
end

#get_hosted_zone(zone_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/aws/requests/dns/get_hosted_zone.rb', line 38

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\"?><ErrorResponse xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Error><Type>Sender</Type><Code>NoSuchHostedZone</Code><Message>The specified hosted zone does not exist.</Message></Error><RequestId>#{Fog::AWS::Mock.request_id}</RequestId></ErrorResponse>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
end

#list_hosted_zones(options = {}) ⇒ Object



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
83
84
# File 'lib/fog/aws/requests/dns/list_hosted_zones.rb', line 50

def list_hosted_zones(options = {})
  maxitems = [options[:max_items]||100,100].min

  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' => maxitems,
    'IsTruncated' => truncated
  }

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

  response
end

#list_resource_record_sets(zone_id, options = {}) ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/aws/requests/dns/list_resource_record_sets.rb', line 61

def list_resource_record_sets(zone_id, options = {})
  maxitems = [options[:max_items]||100,100].min

  response = Excon::Response.new

  zone = self.data[:zones][zone_id]
  if zone.nil?
    response.status = 404
    response.body = "<?xml version=\"1.0\"?>\n<ErrorResponse xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\"><Error><Type>Sender</Type><Code>NoSuchHostedZone</Code><Message>No hosted zone found with ID: #{zone_id}</Message></Error><RequestId>#{Fog::AWS::Mock.request_id}</RequestId></ErrorResponse>"
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end

  records = if options[:type]
    records_type = zone[:records][options[:type]]
    records_type.values if records_type
  else
    zone[:records].values.map{|r| r.values}.flatten
  end

  records ||= []

  # sort for pagination
  records.sort! { |a,b| a[:name].gsub(zone[:name],"") <=> b[:name].gsub(zone[:name],"") }

  if options[:name]
    name = options[:name].gsub(zone[:name],"")
    records = records.select{|r| r[:name].gsub(zone[:name],"") >= name }
    require 'pp'
  end

  next_record  = records[maxitems]
  records      = records[0, maxitems]
  truncated    = !next_record.nil?

  response.status = 200
  response.body = {
    'ResourceRecordSets' => records.map do |r|
      if r[:alias_target]
        record = {
          'AliasTarget' => {
            'HostedZoneId' => r[:alias_target][:hosted_zone_id],
            'DNSName' => r[:alias_target][:dns_name]
          }
        }
      else
        record = {
          'TTL' => r[:ttl]
        }
      end
      {
        'ResourceRecords' => r[:resource_records],
        'Name' => r[:name],
        'Type' => r[:type]
      }.merge(record)
    end,
    'MaxItems' => maxitems,
    'IsTruncated' => truncated
  }

  if truncated
    response.body['NextRecordName'] = next_record[:name]
    response.body['NextRecordType'] = next_record[:type]
  end

  response
end

#reset_dataObject



60
61
62
# File 'lib/fog/aws/dns.rb', line 60

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

#setup_credentials(options) ⇒ Object



68
69
70
# File 'lib/fog/aws/dns.rb', line 68

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

#signature(params) ⇒ Object



64
65
66
# File 'lib/fog/aws/dns.rb', line 64

def signature(params)
  "foo"
end