1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #967 from avalanche123/mock-aws-route53

Mock aws route53
This commit is contained in:
Wesley Beary 2012-06-08 11:46:46 -07:00
commit 5d4df4d8aa
6 changed files with 189 additions and 3 deletions

View file

@ -249,6 +249,20 @@ module Fog
def self.subnet_id
"subnet-#{Fog::Mock.random_hex(8)}"
end
def self.zone_id
"zone-#{Fog::Mock.random_hex(8)}"
end
def self.change_id
"change-#{Fog::Mock.random_hex(8)}"
end
def self.nameservers
[
'ns-2048.awsdns-64.com',
'ns-2049.awsdns-65.net',
'ns-2050.awsdns-66.org',
'ns-2051.awsdns-67.co.uk'
]
end
def self.key_id(length=21)
#Probably close enough

View file

@ -29,7 +29,11 @@ module Fog
@data ||= Hash.new do |hash, region|
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:buckets => {}
:buckets => {},
:limits => {
:duplicate_domains => 5
},
:zones => {}
}
end
end

View file

@ -29,7 +29,7 @@ module Fog
# * 'Id'<~String> - The ID of the request
# * 'Status'<~String> - status of the request - PENDING | INSYNC
# * 'SubmittedAt'<~String> - The date and time the change was made
# * status<~Integer> - 201 when successful
# * status<~Integer> - 200 when successful
#
# ==== Examples
#
@ -131,6 +131,61 @@ module Fog
end
end
class Mock
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
end
end
end
end

View file

@ -54,6 +54,51 @@ module Fog
end
end
class Mock
require 'time'
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
end
end
end
end

View file

@ -20,7 +20,7 @@ module Fog
# * 'Comment'<~String> -
# * 'NameServers'<~Array>
# * 'NameServer'<~String>
# * status<~Integer> - 201 when successful
# * status<~Integer> - 200 when successful
def get_hosted_zone(zone_id)
# AWS methods return zone_ids that looks like '/hostedzone/id'. Let the caller either use
@ -37,6 +37,29 @@ module Fog
end
end
class Mock
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
end
end
end
end

View file

@ -49,6 +49,51 @@ module Fog
end
end
class Mock
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[:zone_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[:zone_id]
end
response
end
end
end
end
end