mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Implemented mocks for Zerigo.
This commit is contained in:
parent
09b2266371
commit
25562a42b4
16 changed files with 352 additions and 16 deletions
|
@ -34,7 +34,7 @@ module Fog
|
|||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, key|
|
||||
hash[key] = {}
|
||||
hash[key] = key == :zones ? [] : {}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -48,13 +48,24 @@ module Fog
|
|||
end
|
||||
|
||||
def data
|
||||
self.class.data[@zerigo_email]
|
||||
self.class.data
|
||||
end
|
||||
|
||||
def reset_data
|
||||
self.class.data.delete(@zerigo_email)
|
||||
self.class.reset
|
||||
end
|
||||
|
||||
def find_by_zone_id(zone_id)
|
||||
self.data[:zones].find { |z| z['id'] == zone_id }
|
||||
end
|
||||
|
||||
def find_by_domain(domain)
|
||||
self.data[:zones].find { |z| z['domain'] == domain }
|
||||
end
|
||||
|
||||
def find_host(host_id)
|
||||
self.data[:zones].collect { |z| z['hosts'].find { |h| h['id'] == host_id } }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
|
|
|
@ -14,8 +14,8 @@ module Fog
|
|||
load(data)
|
||||
end
|
||||
|
||||
def get(zone_id)
|
||||
data = connection.get_zone(zone_id).body
|
||||
def get(zone_id_or_domain)
|
||||
data = connection.get_zone(zone_id_or_domain).body
|
||||
zone = new(data)
|
||||
zone.records.load(data['hosts'])
|
||||
zone
|
||||
|
|
|
@ -13,7 +13,7 @@ module Fog
|
|||
# * body<~Hash>
|
||||
# * 'count'<~Integer>
|
||||
# * 'status'<~Integer> - 200 indicates success
|
||||
def count_hosts( zone_id)
|
||||
def count_hosts(zone_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
|
@ -23,6 +23,25 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def count_hosts(zone_id)
|
||||
zone = find_by_zone_id(zone_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if zone
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'count' => zone['hosts'].size
|
||||
}
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,7 +13,7 @@ module Fog
|
|||
# * body<~Hash>
|
||||
# * 'count'<~Integer>
|
||||
# * 'status'<~Integer> - 200 indicates success
|
||||
def count_zones()
|
||||
def count_zones
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
|
@ -23,6 +23,17 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def count_zones
|
||||
response = Excon::Response.new
|
||||
|
||||
response.status = 200
|
||||
response.body = { 'count' => self.data[:zones].size }
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ module Fog
|
|||
# * zone_id<~Integer>
|
||||
# * host_type<~String>
|
||||
# * data<~String>
|
||||
# * options<~Hash> - optional paramaters
|
||||
# * options<~Hash> - optional parameters
|
||||
# * hostname<~String> - Note: normally this is set/required!!
|
||||
# * notes<~String>
|
||||
# * priority<~Integer> - Note: required for MX or SRV records
|
||||
|
@ -57,6 +57,72 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def valid_host_types
|
||||
%w[A AAAA CNAME GEO MX NS SPF SRV TXT URL PTR CNAME NS]
|
||||
end
|
||||
|
||||
def create_host(zone_id, host_type, data, options = {})
|
||||
zone = find_by_zone_id(zone_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
# Handle error cases.
|
||||
|
||||
# Zone doesn't exist.
|
||||
unless zone
|
||||
response.status = 404
|
||||
return response
|
||||
end
|
||||
|
||||
# Bad host type.
|
||||
unless valid_host_types.include?(host_type)
|
||||
response.status = 422
|
||||
response.body = {
|
||||
'errors' => [
|
||||
'error' => 'Host type is not included in the list'
|
||||
]
|
||||
}
|
||||
|
||||
return response
|
||||
end
|
||||
|
||||
# Missing or bad priority value for MX or SRV records.
|
||||
if %w[MX SRV].include?(host_type) && options['priority'].to_s !~ /\d+/
|
||||
response.status = 422
|
||||
response.body = {
|
||||
'errors' => [
|
||||
'error' => 'Priority is not a number'
|
||||
]
|
||||
}
|
||||
|
||||
return response
|
||||
end
|
||||
|
||||
# Successful case.
|
||||
now = Time.now
|
||||
host = {
|
||||
'id' => rand(10000000),
|
||||
'fqdn' => options[:hostname] ? "#{options[:hostname]}.#{zone['domain']}" : zone['domain'],
|
||||
'data' => data,
|
||||
'hostname' => options[:hostname],
|
||||
'ttl' => options[:ttl].to_i,
|
||||
'host-type' => host_type,
|
||||
'created-at' => now,
|
||||
'updated-at' => now,
|
||||
'notes' => options[:notes],
|
||||
'priority' => options[:priority].to_i,
|
||||
'zone-id' => zone_id
|
||||
}
|
||||
|
||||
zone['hosts'] << host
|
||||
response.status = 201
|
||||
response.body = host
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -46,7 +46,7 @@ module Fog
|
|||
# * 'restrict-axfr'<~String>
|
||||
# * 'status'<~Integer> - 201 if successful
|
||||
|
||||
def create_zone( domain, default_ttl, ns_type, options = {})
|
||||
def create_zone(domain, default_ttl, ns_type, options = {})
|
||||
|
||||
optional_tags= ''
|
||||
options.each { |option, value|
|
||||
|
@ -84,6 +84,49 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def create_zone(domain, default_ttl, ns_type, options = {})
|
||||
now = Time.now
|
||||
zone = {
|
||||
'id' => rand(10000000),
|
||||
'domain' => domain,
|
||||
'created-at' => now,
|
||||
'updated-at' => now,
|
||||
'ns1' => options[:ns1],
|
||||
'nx-ttl' => options[:nx_ttl].to_i,
|
||||
'default-ttl' => default_ttl.to_i,
|
||||
'ns-type' => ns_type,
|
||||
'hosts' => options[:hosts] || [],
|
||||
'hosts-count' => (options[:hosts] || []).size,
|
||||
'notes' => options[:notes],
|
||||
'slave-nameservers' => options[:slave_nameservers],
|
||||
'tag-list' => options[:tag_list]
|
||||
}
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if self.data[:zones].any? {|z| z['domain'] == zone['domain'] }
|
||||
response.status = 422
|
||||
response.body = {
|
||||
'errors' => [
|
||||
'error' => 'Domain is already associated to another account',
|
||||
'error' => 'Domain already exists. If it was just deleted, wait a minute and try again'
|
||||
]
|
||||
}
|
||||
|
||||
else
|
||||
self.data[:zones] << zone
|
||||
|
||||
response.status = 201
|
||||
response.headers = { 'Location' => "http://ns.zerigo.com/api/1.1/zones/#{zone['id']}" }
|
||||
response.body = zone['hosts'].empty? ? zone.merge(:hosts => nil) : zone # Zerigo returns nil, not an empty list only on the create command.
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,6 +19,25 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def delete_host(host_id)
|
||||
host = find_host(host_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if host
|
||||
zone = find_by_zone_id(host['zone-id'])
|
||||
zone['hosts'].delete(host)
|
||||
|
||||
response.status = 200
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,6 +20,23 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def delete_zone(zone_id)
|
||||
zone = find_by_zone_id(zone_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if zone
|
||||
self.data[:zones].delete(zone)
|
||||
response.status = 200
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,7 +29,7 @@ module Fog
|
|||
# * 'zone-id'<~String>
|
||||
# * 'status'<~Integer> - 200 indicated success
|
||||
#
|
||||
def find_hosts( fqdn, zone_id = nil)
|
||||
def find_hosts(fqdn, zone_id = nil)
|
||||
if zone_id.nil?
|
||||
#look for matching host across all zones
|
||||
request(
|
||||
|
@ -50,6 +50,27 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def find_hosts(fqdn, zone_id = nil)
|
||||
response = Excon::Response.new
|
||||
|
||||
zone = find_by_zone_id(zone_id)
|
||||
if zone_id && !zone
|
||||
response.status = 404
|
||||
else
|
||||
hosts = zone ? zone['hosts'].select { |z| z['fqdn'] == fqdn } :
|
||||
self.data[:zones].collect { |z| z['hosts'].find { |h| h['fqdn'] == fqdn } }.compact
|
||||
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'hosts' => hosts
|
||||
}
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,7 +24,7 @@ module Fog
|
|||
# * 'updated-at'<~String>
|
||||
# * 'zone-id'<~String>
|
||||
# * 'status'<~Integer> - 200 indicates success
|
||||
def get_host( host_id)
|
||||
def get_host(host_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
|
@ -34,6 +34,23 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def get_host(host_id)
|
||||
host = find_host(host_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if host
|
||||
response.status = 200
|
||||
response.body = host
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -34,16 +34,33 @@ module Fog
|
|||
# * 'restrict-axfr'<~String>
|
||||
# * 'status'<~Integer> - 200 indicates success
|
||||
|
||||
def get_zone(zone)
|
||||
def get_zone(zone_id_or_domain)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::DNS::Zerigo::GetZone.new,
|
||||
:path => "/api/1.1/zones/#{zone}.xml"
|
||||
:path => "/api/1.1/zones/#{zone_id_or_domain}.xml"
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def get_zone(zone_id_or_domain)
|
||||
zone = find_by_zone_id(zone_id_or_domain) || find_by_domain(zone_id_or_domain)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if zone
|
||||
response.status = 200
|
||||
response.body = zone
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,6 +31,29 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def get_zone_stats(zone_id)
|
||||
zone = find_by_zone_id(zone_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if zone
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'id' => zone,
|
||||
'domain' => zone['domain'],
|
||||
'period-begin' => zone['created-at'].strftime("%F"),
|
||||
'period-end' => Date.today.to_s,
|
||||
'queries' => 0
|
||||
}
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,7 +25,7 @@ module Fog
|
|||
# * 'updated-at'<~String>
|
||||
# * 'zone-id'<~String>
|
||||
# * 'status'<~Integer> - 200 indicates success
|
||||
def list_hosts( zone_id)
|
||||
def list_hosts(zone_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
|
@ -35,6 +35,25 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def list_hosts(zone_id)
|
||||
zone = find_by_zone_id(zone_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if zone
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'hosts' => zone['hosts']
|
||||
}
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -40,6 +40,21 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
|
||||
def list_zones
|
||||
response = Excon::Response.new
|
||||
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'zones' => self.data[:zones]
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,7 +18,7 @@ module Fog
|
|||
# * response<~Excon::Response>:
|
||||
# * 'status'<~Integer> - 200 for success
|
||||
#
|
||||
def update_host( host_id, options = {})
|
||||
def update_host(host_id, options = {})
|
||||
|
||||
optional_tags= ''
|
||||
options.each { |option, value|
|
||||
|
@ -47,6 +47,25 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def update_host(host_id, options = {})
|
||||
host = find_host(host_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if host
|
||||
options.each { |k, v| host[k.to_s] = v } # Deal with symbols in requests but strings in responses.
|
||||
host['updated-at'] = Time.now
|
||||
|
||||
response.status = 200
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,7 +24,7 @@ module Fog
|
|||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * 'status'<~Integer> - 200 for success
|
||||
def update_zone( zone_id, options = {})
|
||||
def update_zone(zone_id, options = {})
|
||||
|
||||
optional_tags= ''
|
||||
options.each { |option, value|
|
||||
|
@ -65,6 +65,25 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock # :nodoc:all
|
||||
def update_zone(zone_id, options = {})
|
||||
zone = find_by_zone_id(zone_id)
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
if zone
|
||||
options.each { |k, v| zone[k.to_s] = v } # Deal with symbols in requests but strings in responses.
|
||||
zone['updated-at'] = Time.now
|
||||
|
||||
response.status = 200
|
||||
else
|
||||
response.status = 404
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue