mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[slicehost|dns] give dns its own namespace/service
This commit is contained in:
parent
8d7733c239
commit
8b3081ad10
21 changed files with 129 additions and 141 deletions
|
@ -10,6 +10,7 @@ module Fog
|
|||
|
||||
service_path 'fog/slicehost'
|
||||
service :compute
|
||||
service :dns
|
||||
|
||||
def self.new(attributes = {})
|
||||
location = caller.first
|
||||
|
|
|
@ -5,6 +5,8 @@ class Slicehost < Fog::Bin
|
|||
case key
|
||||
when :compute, :slices
|
||||
Fog::Slicehost::Compute
|
||||
when :dns
|
||||
Fog::Slicehost::DNS
|
||||
else
|
||||
raise ArgumentError, "Unrecognized service: #{key}"
|
||||
end
|
||||
|
|
|
@ -24,14 +24,6 @@ module Fog
|
|||
request :get_slice
|
||||
request :get_slices
|
||||
request :reboot_slice
|
||||
request :create_zone
|
||||
request :delete_zone
|
||||
request :get_zones
|
||||
request :get_zone
|
||||
request :create_record
|
||||
request :delete_record
|
||||
request :get_records
|
||||
request :get_record
|
||||
|
||||
class Mock
|
||||
|
||||
|
|
84
lib/fog/slicehost/dns.rb
Normal file
84
lib/fog/slicehost/dns.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class DNS < Fog::Service
|
||||
|
||||
requires :slicehost_password
|
||||
recognizes :host, :port, :scheme, :persistent
|
||||
|
||||
model_path 'fog/slicehost/models/dns'
|
||||
|
||||
request_path 'fog/slicehost/requests/dns'
|
||||
request :create_record
|
||||
request :create_zone
|
||||
request :delete_record
|
||||
request :delete_zone
|
||||
request :get_record
|
||||
request :get_records
|
||||
request :get_zone
|
||||
request :get_zones
|
||||
|
||||
class Mock
|
||||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, key|
|
||||
hash[key] = {}
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset_data(keys=data.keys)
|
||||
for key in [*keys]
|
||||
data.delete(key)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(options={})
|
||||
@slicehost_password = options[:slicehost_password]
|
||||
@data = self.class.data[@slicehost_password]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Real
|
||||
|
||||
def initialize(options={})
|
||||
@slicehost_password = options[:slicehost_password]
|
||||
@host = options[:host] || "api.slicehost.com"
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
||||
end
|
||||
|
||||
def reload
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
def request(params)
|
||||
params[:headers] ||= {}
|
||||
params[:headers].merge!({
|
||||
'Authorization' => "Basic #{Base64.encode64(@slicehost_password).delete("\r\n")}"
|
||||
})
|
||||
case params[:method]
|
||||
when 'DELETE', 'GET', 'HEAD'
|
||||
params[:headers]['Accept'] = 'application/xml'
|
||||
when 'POST', 'PUT'
|
||||
params[:headers]['Content-Type'] = 'application/xml'
|
||||
end
|
||||
|
||||
begin
|
||||
response = @connection.request(params.merge!({:host => @host}))
|
||||
rescue Excon::Errors::HTTPStatusError => error
|
||||
raise case error
|
||||
when Excon::Errors::NotFound
|
||||
Fog::Slicehost::DNS::NotFound.slurp(error)
|
||||
else
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Slicehost
|
||||
module Compute
|
||||
module DNS
|
||||
|
||||
class CreateRecord < Fog::Parsers::Base
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Slicehost
|
||||
module Compute
|
||||
module DNS
|
||||
|
||||
class CreateZone < Fog::Parsers::Base
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Slicehost
|
||||
module Compute
|
||||
module DNS
|
||||
|
||||
class GetRecord < Fog::Parsers::Base
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Slicehost
|
||||
module Compute
|
||||
module DNS
|
||||
|
||||
class GetRecords < Fog::Parsers::Base
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Slicehost
|
||||
module Compute
|
||||
module DNS
|
||||
|
||||
class GetZone < Fog::Parsers::Base
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Slicehost
|
||||
module Compute
|
||||
module DNS
|
||||
|
||||
class GetZones < Fog::Parsers::Base
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
|
||||
# notes: slicehost finds records by id - need to check if others do the same (for zones & records)
|
||||
# linode uses IDs
|
||||
#
|
||||
|
||||
# array won't work because ID will change when delete items
|
||||
# hash - but need to generate a unique number - counter
|
||||
|
||||
class DnsManager
|
||||
|
||||
def initialize
|
||||
@zones = {}
|
||||
@zone_id_counter= 0
|
||||
@record_id_counter= 0
|
||||
end
|
||||
|
||||
# add domain to list of zones and return a zone id. note, only domain name is manatory. any
|
||||
# other desired fields can be included using options parameter
|
||||
def create_zone( domain, options)
|
||||
|
||||
#see if domain already has zone
|
||||
zone_id= 0
|
||||
@zone.each { |id, zone|
|
||||
if domain.casecmp zone[:domain]
|
||||
zone_id= id
|
||||
break
|
||||
end
|
||||
}
|
||||
#if did not find, get a new zone ID
|
||||
if zone_id == 0
|
||||
zone_id= get_zone_id
|
||||
|
||||
#add fields to zone
|
||||
zone = { :domain => domain }
|
||||
options.each { |option, value|
|
||||
zone[option] = value
|
||||
}
|
||||
|
||||
#add zone to dns manager
|
||||
@zones[zone_id] = zone
|
||||
|
||||
zone_id
|
||||
end
|
||||
|
||||
# update an existing zone with new data. any field can be updated included domain name
|
||||
def update_zone( zone_id, domain, options)
|
||||
#build zone hash - merge?
|
||||
zone = {}
|
||||
@zones[zone_id] = zone
|
||||
end
|
||||
|
||||
# remove a zone from the dns manager
|
||||
def delete_zone( zone_id)
|
||||
@zones.delete( zone_id)
|
||||
end
|
||||
|
||||
# get
|
||||
def get_zone( zone_id)
|
||||
@zones[zone_id]
|
||||
end
|
||||
|
||||
|
||||
#----------------------
|
||||
|
||||
def get_zone_id
|
||||
@zone_id_counter+=1
|
||||
end
|
||||
private :get_zone_id
|
||||
|
||||
def get_record_id
|
||||
@record_id_counter+=1
|
||||
end
|
||||
private :get_record_id
|
||||
|
||||
end
|
|
@ -1,16 +0,0 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
|
||||
class Mock
|
||||
|
||||
def initialize
|
||||
@slices = []
|
||||
@zones = []
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
require 'fog/slicehost/parsers/compute/create_record'
|
||||
require 'fog/slicehost/parsers/dns/create_record'
|
||||
|
||||
# Create a new record in a DNS zone - or update an existing one
|
||||
# ==== Parameters
|
||||
|
@ -42,7 +42,7 @@ module Fog
|
|||
:body => %Q{<?xml version="1.0" encoding="UTF-8"?><record><record_type>#{record_type}</record_type><zone_id type="integer">#{zone_id}</zone_id><name>#{name}</name><data>#{data}</data>#{optional_tags}</record>},
|
||||
:expects => 201,
|
||||
:method => 'POST',
|
||||
:parser => Fog::Parsers::Slicehost::Compute::CreateRecord.new,
|
||||
:parser => Fog::Parsers::Slicehost::DNS::CreateRecord.new,
|
||||
:path => 'records.xml'
|
||||
)
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
require 'fog/slicehost/parsers/compute/create_zone'
|
||||
require 'fog/slicehost/parsers/dns/create_zone'
|
||||
|
||||
# Create a new zone for Slicehost's DNS servers to serve/host
|
||||
# ==== Parameters
|
||||
|
@ -35,7 +35,7 @@ module Fog
|
|||
:body => %Q{<?xml version="1.0" encoding="UTF-8"?><zone><origin>#{origin}</origin>#{optional_tags}</zone>},
|
||||
:expects => 201,
|
||||
:method => 'POST',
|
||||
:parser => Fog::Parsers::Slicehost::Compute::CreateZone.new,
|
||||
:parser => Fog::Parsers::Slicehost::DNS::CreateZone.new,
|
||||
:path => 'zones.xml'
|
||||
)
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
# Delete a record from the specified DNS zone
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
# Delete a zone from Slicehost's DNS
|
|
@ -1,9 +1,9 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
require 'fog/slicehost/parsers/compute/get_record'
|
||||
require 'fog/slicehost/parsers/dns/get_record'
|
||||
|
||||
# Get an individual DNS record from the specified zone
|
||||
#
|
||||
|
@ -21,7 +21,7 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Slicehost::Compute::GetRecords.new,
|
||||
:parser => Fog::Parsers::Slicehost::DNS::GetRecords.new,
|
||||
:path => "records/#{record_id}.xml"
|
||||
)
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
require 'fog/slicehost/parsers/compute/get_records'
|
||||
require 'fog/slicehost/parsers/dns/get_records'
|
||||
|
||||
# Get all the DNS records across all the DNS zones for this account
|
||||
#
|
||||
|
@ -22,7 +22,7 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Slicehost::Compute::GetRecords.new,
|
||||
:parser => Fog::Parsers::Slicehost::DNS::GetRecords.new,
|
||||
:path => "records.xml"
|
||||
)
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
require 'fog/slicehost/parsers/compute/get_zone'
|
||||
require 'fog/slicehost/parsers/dns/get_zone'
|
||||
|
||||
# Get details of a DNS zone
|
||||
#
|
||||
|
@ -21,7 +21,7 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Slicehost::Compute::GetZone.new,
|
||||
:parser => Fog::Parsers::Slicehost::DNS::GetZone.new,
|
||||
:path => "/zones/#{zone_id}.xml"
|
||||
)
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
module Fog
|
||||
module Slicehost
|
||||
class Compute
|
||||
class DNS
|
||||
class Real
|
||||
|
||||
require 'fog/slicehost/parsers/compute/get_zones'
|
||||
require 'fog/slicehost/parsers/dns/get_zones'
|
||||
|
||||
# Get list of all DNS zones hosted on Slicehost (for this account)
|
||||
#
|
||||
|
@ -19,7 +19,7 @@ module Fog
|
|||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Slicehost::Compute::GetZones.new,
|
||||
:parser => Fog::Parsers::Slicehost::DNS::GetZones.new,
|
||||
:path => 'zones.xml'
|
||||
)
|
||||
end
|
|
@ -1,4 +1,4 @@
|
|||
Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
||||
Shindo.tests('Slicehost::dns | DNS requests', ['slicehost', 'dns']) do
|
||||
|
||||
@domain = ''
|
||||
@new_zones = []
|
||||
|
@ -22,7 +22,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
pending if Fog.mocking?
|
||||
|
||||
@org_zone_count= 0
|
||||
response = Slicehost[:compute].get_zones()
|
||||
response = Slicehost[:dns].get_zones()
|
||||
if response.status == 200
|
||||
zones = response.body['zones']
|
||||
@org_zone_count = zones.count
|
||||
|
@ -35,7 +35,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
pending if Fog.mocking?
|
||||
|
||||
domain = generate_unique_domain( true)
|
||||
response = Slicehost[:compute].create_zone(domain)
|
||||
response = Slicehost[:dns].create_zone(domain)
|
||||
if response.status == 201
|
||||
zone_id = response.body['id']
|
||||
@new_zones << zone_id
|
||||
|
@ -49,7 +49,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
options = { :ttl => 1800, :active => 'N' }
|
||||
@domain= generate_unique_domain( true)
|
||||
response = Slicehost[:compute].create_zone( @domain, options)
|
||||
response = Slicehost[:dns].create_zone( @domain, options)
|
||||
if response.status == 201
|
||||
@zone_id = response.body['id']
|
||||
@new_zones << @zone_id
|
||||
|
@ -63,7 +63,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
result= false
|
||||
|
||||
response = Slicehost[:compute].get_zone( @zone_id)
|
||||
response = Slicehost[:dns].get_zone( @zone_id)
|
||||
if response.status == 200
|
||||
zone = response.body
|
||||
if (zone['origin'] == @domain) and (zone['ttl'] == 1800) and
|
||||
|
@ -80,7 +80,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
result= false
|
||||
|
||||
response = Slicehost[:compute].get_zones()
|
||||
response = Slicehost[:dns].get_zones()
|
||||
if response.status == 200
|
||||
zones = response.body['zones']
|
||||
if (@org_zone_count+2) == zones.count
|
||||
|
@ -96,7 +96,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
result= false
|
||||
|
||||
response = Slicehost[:compute].get_zones()
|
||||
response = Slicehost[:dns].get_zones()
|
||||
if response.status == 200
|
||||
zones = response.body['zones']
|
||||
zones.each { |zone|
|
||||
|
@ -120,7 +120,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
host= 'www.' + @domain
|
||||
zone_id= @new_zones[1]
|
||||
response = Slicehost[:compute].create_record( 'A', zone_id, host, '1.2.3.4')
|
||||
response = Slicehost[:dns].create_record( 'A', zone_id, host, '1.2.3.4')
|
||||
if response.status == 201
|
||||
record_id = response.body['id']
|
||||
@new_records << record_id
|
||||
|
@ -135,7 +135,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
host= 'ftp.' + @domain
|
||||
zone_id= @new_zones[1]
|
||||
options = { :ttl => 3600, :active => 'N'}
|
||||
response = Slicehost[:compute].create_record( 'A', zone_id, host, '1.2.3.4', options)
|
||||
response = Slicehost[:dns].create_record( 'A', zone_id, host, '1.2.3.4', options)
|
||||
if response.status == 201
|
||||
record_id = response.body['id']
|
||||
@new_records << record_id
|
||||
|
@ -148,7 +148,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
pending if Fog.mocking?
|
||||
|
||||
zone_id= @new_zones[1]
|
||||
response = Slicehost[:compute].create_record( 'CNAME', zone_id, 'mail', @domain)
|
||||
response = Slicehost[:dns].create_record( 'CNAME', zone_id, 'mail', @domain)
|
||||
if response.status == 201
|
||||
record_id = response.body['id']
|
||||
@new_records << record_id
|
||||
|
@ -163,7 +163,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
ns_domain = 'ns.' + @domain
|
||||
zone_id= @new_zones[1]
|
||||
options = { :ttl => 3600, :active => 'N'}
|
||||
response = Slicehost[:compute].create_record( 'NS', zone_id, @domain, ns_domain, options)
|
||||
response = Slicehost[:dns].create_record( 'NS', zone_id, @domain, ns_domain, options)
|
||||
if response.status == 201
|
||||
record_id = response.body['id']
|
||||
@new_records << record_id
|
||||
|
@ -178,7 +178,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
mail_domain = 'mail.' + @domain
|
||||
zone_id= @new_zones[1]
|
||||
options = { :ttl => 3600, :active => 'N', :aux => '10'}
|
||||
response = Slicehost[:compute].create_record( 'MX', zone_id, @domain, mail_domain, options)
|
||||
response = Slicehost[:dns].create_record( 'MX', zone_id, @domain, mail_domain, options)
|
||||
if response.status == 201
|
||||
@record_id = response.body['id']
|
||||
@new_records << @record_id
|
||||
|
@ -192,7 +192,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
result= false
|
||||
|
||||
response = Slicehost[:compute].get_record(@record_id)
|
||||
response = Slicehost[:dns].get_record(@record_id)
|
||||
if response.status == 200
|
||||
mail_domain = 'mail.' + @domain
|
||||
record = response.body['records'][0]
|
||||
|
@ -211,7 +211,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
result= false
|
||||
|
||||
response = Slicehost[:compute].get_records()
|
||||
response = Slicehost[:dns].get_records()
|
||||
if response.status == 200
|
||||
records = response.body['records']
|
||||
|
||||
|
@ -239,7 +239,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
|
||||
result= true
|
||||
@new_records.each { |record_id|
|
||||
response = Slicehost[:compute].delete_record( record_id)
|
||||
response = Slicehost[:dns].delete_record( record_id)
|
||||
if response.status != 200
|
||||
result= false;
|
||||
end
|
||||
|
@ -253,7 +253,7 @@ Shindo.tests('Slicehost::Compute | DNS requests', ['slicehost', 'dns']) do
|
|||
result= true
|
||||
|
||||
@new_zones.each { |zone_id|
|
||||
response = Slicehost[:compute].delete_zone( zone_id)
|
||||
response = Slicehost[:dns].delete_zone( zone_id)
|
||||
if response.status != 200
|
||||
result= false;
|
||||
end
|
Loading…
Add table
Reference in a new issue