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

[slicehost|dns] first pass at models

This commit is contained in:
geemus 2010-12-23 15:36:08 -08:00
parent 0fd90a8fa5
commit 13e8141158
9 changed files with 212 additions and 5 deletions

View file

@ -26,7 +26,7 @@ class Slicehost < Fog::Bin
end
def services
[:compute]
[:compute, :dns]
end
end

View file

@ -6,6 +6,10 @@ module Fog
recognizes :host, :port, :scheme, :persistent
model_path 'fog/slicehost/models/dns'
model :record
collection :records
model :zone
collection :zones
request_path 'fog/slicehost/requests/dns'
request :create_record

View file

@ -0,0 +1,66 @@
require 'fog/core/model'
module Fog
module Slicehost
class DNS
class Record < Fog::Model
identity :id
attribute :active
attribute :ip, :aliases => 'ip'
attribute :name
attribute :notes, :aliases => 'aux'
attribute :ttl
attribute :type, :aliases => 'record_type'
attribute :zone_id
def initialize(attributes={})
self.active ||= true
self.ttl ||= 3600
super
end
def active=(new_active)
attributes[:active] = case new_active
when false, 'N'
false
when true, 'Y'
true
end
end
def destroy
requires :identity
connection.delete_record(identity)
true
end
def zone
@zone
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
requires :ip, :name, :type, :zone
options = {}
options[:active] = active ? 'Y' : 'N'
options[:aux] = notes if notes
options[:ttl] = ttl if ttl
data = connection.create_record(type, zone.id, name, ip, options)
merge_attributes(data.body)
true
end
private
def zone=(new_zone)
@zone = new_zone
end
end
end
end
end

View file

@ -0,0 +1,36 @@
require 'fog/core/collection'
require 'fog/slicehost/models/dns/record'
module Fog
module Slicehost
class DNS
class Records < Fog::Collection
attribute :zone
model Fog::Slicehost::DNS::Record
def all
requires :zone
data = connection.get_records.body['records']
load(data).reject {|record| record.zone_id != zone.id}
end
def get(record_id)
data = connection.get_record(record_id).body
new(data)
rescue Excon::Errors::NotFound
nil
end
def new(attributes = {})
requires :zone
super({ :zone => zone }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,69 @@
require 'fog/core/model'
require 'fog/slicehost/models/dns/records'
module Fog
module Slicehost
class DNS
class Zone < Fog::Model
identity :id
attribute :active
attribute :domain, :aliases => 'origin'
attribute :ttl
def initialize(attributes={})
self.active ||= true
self.ttl ||= 3600
super
end
def active=(new_active)
attributes[:active] = case new_active
when false, 'N'
false
when true, 'Y'
true
end
end
def destroy
requires :identity
connection.delete_zone(identity)
true
end
def records
@records ||= begin
Fog::Zerigo::DNS::Records.new(
:zone => self,
:connection => connection
)
end
end
def nameservers
[
'ns1.slicehost.net',
'ns2.slicehost.net',
'ns3.slicehost.net'
]
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
requires :active, :domain, :ttl
options = {}
options[:active] = active ? 'Y' : 'N'
options[:ttl] = ttl
data = connection.create_zone(domain, options)
merge_attributes(data.body)
true
end
end
end
end
end

View file

@ -0,0 +1,28 @@
require 'fog/core/collection'
require 'fog/slicehost/models/dns/zone'
module Fog
module Slicehost
class DNS
class Zones < Fog::Collection
model Fog::Slicehost::DNS::Zone
def all
data = connection.get_zones.body['zones']
load(data)
end
def get(zone_id)
data = connection.get_zone(zone_id).body
new(data)
rescue Excon::Errors::Forbidden
nil
end
end
end
end
end

View file

@ -24,8 +24,7 @@ module Fog
# * 'data'<~String> - as above
# * 'active'<~String> - as above
# * 'aux'<~String> - as above
def create_record( record_type, zone_id, name, data, options = {})
def create_record(record_type, zone_id, name, data, options = {})
optional_tags= ''
options.each { |option, value|
case option
@ -51,7 +50,7 @@ module Fog
class Mock
def create_record( record_type, zone_id, name, data)
def create_record(record_type, zone_id, name, data)
Fog::Mock.not_implemented
end

View file

@ -17,7 +17,7 @@ module Fog
# * 'ttl'<~Integer> - time to live in seconds
# * 'active'<~String> - whether this record is active or not ('Y' or 'N')
# * 'aux'<~String> - extra data required by the record
def get_record( record_id)
def get_record(record_id)
request(
:expects => 200,
:method => 'GET',

View file

@ -19,6 +19,11 @@ module Fog
attribute :updated_at, :aliases => 'updated-at'
attribute :zone_id, :aliases => 'zone-id'
def initialize(attributes={})
self.ttl ||= 3600
super
end
def destroy
requires :identity
connection.delete_host(identity)