Browse files

[slicehost|dns] give dns its own namespace/service

  • Loading branch information...
1 parent 8d7733c commit 8b3081ad103136f5777bba397d69876272e80dfb @geemus geemus committed Dec 21, 2010
View
1 lib/fog/slicehost.rb
@@ -10,6 +10,7 @@ module Slicehost
service_path 'fog/slicehost'
service :compute
+ service :dns
def self.new(attributes = {})
location = caller.first
View
2 lib/fog/slicehost/bin.rb
@@ -5,6 +5,8 @@ def class_for(key)
case key
when :compute, :slices
Fog::Slicehost::Compute
+ when :dns
+ Fog::Slicehost::DNS
else
raise ArgumentError, "Unrecognized service: #{key}"
end
View
8 lib/fog/slicehost/compute.rb
@@ -24,14 +24,6 @@ class Compute < Fog::Service
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
View
84 lib/fog/slicehost/dns.rb
@@ -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
View
2 ...licehost/parsers/compute/create_record.rb → ...og/slicehost/parsers/dns/create_record.rb
@@ -1,7 +1,7 @@
module Fog
module Parsers
module Slicehost
- module Compute
+ module DNS
class CreateRecord < Fog::Parsers::Base
View
2 .../slicehost/parsers/compute/create_zone.rb → lib/fog/slicehost/parsers/dns/create_zone.rb
@@ -1,7 +1,7 @@
module Fog
module Parsers
module Slicehost
- module Compute
+ module DNS
class CreateZone < Fog::Parsers::Base
View
2 ...g/slicehost/parsers/compute/get_record.rb → lib/fog/slicehost/parsers/dns/get_record.rb
@@ -1,7 +1,7 @@
module Fog
module Parsers
module Slicehost
- module Compute
+ module DNS
class GetRecord < Fog::Parsers::Base
View
2 .../slicehost/parsers/compute/get_records.rb → lib/fog/slicehost/parsers/dns/get_records.rb
@@ -1,7 +1,7 @@
module Fog
module Parsers
module Slicehost
- module Compute
+ module DNS
class GetRecords < Fog::Parsers::Base
View
2 ...fog/slicehost/parsers/compute/get_zone.rb → lib/fog/slicehost/parsers/dns/get_zone.rb
@@ -1,7 +1,7 @@
module Fog
module Parsers
module Slicehost
- module Compute
+ module DNS
class GetZone < Fog::Parsers::Base
View
2 ...og/slicehost/parsers/compute/get_zones.rb → lib/fog/slicehost/parsers/dns/get_zones.rb
@@ -1,7 +1,7 @@
module Fog
module Parsers
module Slicehost
- module Compute
+ module DNS
class GetZones < Fog::Parsers::Base
View
75 lib/fog/slicehost/requests/compute/dns_manager.rb
@@ -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
View
16 lib/fog/slicehost/requests/compute/mock.rb
@@ -1,16 +0,0 @@
-module Fog
- module Slicehost
- class Compute
-
- class Mock
-
- def initialize
- @slices = []
- @zones = []
- end
-
- end
-
- end
- end
-end
View
6 ...icehost/requests/compute/create_record.rb → ...g/slicehost/requests/dns/create_record.rb
@@ -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 @@ def create_record( record_type, zone_id, name, data, options = {})
: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
View
6 ...slicehost/requests/compute/create_zone.rb → ...fog/slicehost/requests/dns/create_zone.rb
@@ -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 @@ def create_zone(origin, options = {})
: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
View
2 ...icehost/requests/compute/delete_record.rb → ...g/slicehost/requests/dns/delete_record.rb
@@ -1,6 +1,6 @@
module Fog
module Slicehost
- class Compute
+ class DNS
class Real
# Delete a record from the specified DNS zone
View
2 ...slicehost/requests/compute/delete_zone.rb → ...fog/slicehost/requests/dns/delete_zone.rb
@@ -1,6 +1,6 @@
module Fog
module Slicehost
- class Compute
+ class DNS
class Real
# Delete a zone from Slicehost's DNS
View
6 .../slicehost/requests/compute/get_record.rb → lib/fog/slicehost/requests/dns/get_record.rb
@@ -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 @@ def get_record( record_id)
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
View
6 ...slicehost/requests/compute/get_records.rb → ...fog/slicehost/requests/dns/get_records.rb
@@ -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 @@ def get_records
request(
:expects => 200,
:method => 'GET',
- :parser => Fog::Parsers::Slicehost::Compute::GetRecords.new,
+ :parser => Fog::Parsers::Slicehost::DNS::GetRecords.new,
:path => "records.xml"
)
end
View
6 ...og/slicehost/requests/compute/get_zone.rb → lib/fog/slicehost/requests/dns/get_zone.rb
@@ -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 @@ def get_zone(zone_id)
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
View
6 ...g/slicehost/requests/compute/get_zones.rb → lib/fog/slicehost/requests/dns/get_zones.rb
@@ -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 @@ def get_zones
request(
:expects => 200,
:method => 'GET',
- :parser => Fog::Parsers::Slicehost::Compute::GetZones.new,
+ :parser => Fog::Parsers::Slicehost::DNS::GetZones.new,
:path => 'zones.xml'
)
end
View
32 ...s/slicehost/requests/compute/dns_tests.rb → ...slicehost/requests/dns_tests/dns_tests.rb
@@ -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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
result= false
- response = Slicehost[:compute].get_records()
+ response = Slicehost[:dns].get_records()
if response.status == 200
records = response.body['records']
@@ -239,7 +239,7 @@ def generate_unique_domain( with_trailing_dot = false)
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 @@ def generate_unique_domain( with_trailing_dot = false)
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

0 comments on commit 8b3081a

Please sign in to comment.