Permalink
Browse files

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

  • Loading branch information...
1 parent 8d7733c commit 8b3081ad103136f5777bba397d69876272e80dfb @geemus geemus committed Dec 21, 2010
@@ -10,6 +10,7 @@ module Slicehost
service_path 'fog/slicehost'
service :compute
+ service :dns
def self.new(attributes = {})
location = caller.first
@@ -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
@@ -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
@@ -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 @@ 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
@@ -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
@@ -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 @@ 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
@@ -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
@@ -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
Oops, something went wrong. Retry.

0 comments on commit 8b3081a

Please sign in to comment.