Skip to content

Commit

Permalink
[zerigo|dns] first rough pass at models
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Dec 23, 2010
1 parent be64214 commit a641545
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 12 deletions.
24 changes: 13 additions & 11 deletions lib/fog/zerigo/dns.rb
Expand Up @@ -5,25 +5,27 @@ class DNS < Fog::Service
requires :zerigo_email, :zerigo_token
recognizes :timeout, :persistent

# model_path 'fog/zerigo/models/dns'
# model :server
# collection :servers
model_path 'fog/zerigo/models/dns'
model :record
collection :records
model :zone
collection :zones

request_path 'fog/zerigo/requests/dns'
request :list_zones
request :count_hosts
request :count_zones
request :get_zone
request :get_zone_stats
request :create_host
request :create_zone
request :update_zone
request :delete_host
request :delete_zone
request :list_hosts
request :find_hosts
request :count_hosts
request :get_host
request :create_host
request :get_zone
request :get_zone_stats
request :list_zones
request :list_hosts
request :update_host
request :delete_host
request :update_zone

class Mock

Expand Down
54 changes: 54 additions & 0 deletions lib/fog/zerigo/models/dns/record.rb
@@ -0,0 +1,54 @@
require 'fog/core/model'

module Fog
module Zerigo
class DNS

class Record < Fog::Model

identity :id

attribute :created_at, :aliases => 'created-at'
attribute :ip, :aliases => 'data'
attribute :domain, :aliases => 'fqdn'
attribute :name, :aliases => 'hostname'
attribute :notes
attribute :priority
attribute :ttl
attribute :type, :aliases => 'host-type'
attribute :updated_at, :aliases => 'updated-at'
attribute :zone_id, :aliases => 'zone-id'

def destroy
requires :identity
connection.delete_host(identity)
true
end

def zone
@zone
end

def save
requires :zone, :type, :ip
options = {}
options[:hostname] = name if name
options[:notes] = notes if notes
options[:priority] = priority if priority
options[:ttl] = ttl if ttl
data = connection.create_host(@zone.id, type, ip, options)
merge_attributes(data.body)
true
end

private

def zone=(new_zone)
@zone = new_zone
end

end

end
end
end
41 changes: 41 additions & 0 deletions lib/fog/zerigo/models/dns/records.rb
@@ -0,0 +1,41 @@
require 'fog/core/collection'
require 'fog/zerigo/models/dns/record'

module Fog
module Zerigo
class DNS

class Records < Fog::Collection

attribute :zone

model Fog::Zerigo::DNS::Record

def all
requires :zone
parent = zone.collection.get(zone.identity)
if parent
merge_attributes(parent.records.attributes)
load(parent.records.map {|record| record.attributes})
else
nil
end
end

def get(record_id)
data = connection.get_host(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
82 changes: 82 additions & 0 deletions lib/fog/zerigo/models/dns/zone.rb
@@ -0,0 +1,82 @@
require 'fog/core/model'
require 'fog/zerigo/models/dns/records'

module Fog
module Zerigo
class DNS

class Zone < Fog::Model

identity :id

attribute :created_at, :aliases => 'created-at'
attribute :domain
attribute :ttl, :aliases => 'default-ttl'
attribute :type, :aliases => 'ns-type'
attribute :updated_at, :aliases => 'updated-at'

# <custom-nameservers>ns1.example.com,ns2.example.com</custom-nameservers>
# <custom-ns type="boolean">true</custom-ns>
# <hostmaster>dnsadmin@example.com</hostmaster>
# <notes nil="true"/>
# <ns1 nil="true"/>
# <nx-ttl nil="true"></nx-ttl>
# <slave-nameservers nil="true"/>
# <tag-list>one two</tag-list>
# <hosts-count>1</hosts-count>

def initialize(attributes={})
self.type ||= 'pri_sec'
self.ttl ||= 3600
super
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
[
'a.ns.zerigo.net',
'b.ns.zerigo.net',
'c.ns.zerigo.net',
'd.ns.zerigo.net',
'e.ns.zerigo.net'
]
end

def save
requires :domain, :type, :ttl
options = {}
# * options<~Hash> - optional paramaters
# * ns1<~String> - required if ns_type == sec
# * nx_ttl<~Integer> -
# * slave_nameservers<~String> - required if ns_type == pri
# * axfr_ips<~String> - comma-separated list of IPs or IP blocks allowed to perform AXFRs
# * custom_nameservers<~String> - comma-separated list of custom nameservers
# * custom_ns<~String> - indicates if vanity (custom) nameservers are enabled for this domain
# * hostmaster<~String> - email of the DNS administrator or hostmaster
# * notes<~String> - notes about the domain
# * restrict_axfr<~String> - indicates if AXFR transfers should be restricted to IPs in axfr-ips
# * tag_list<~String> - List of all tags associated with this domain
data = connection.create_zone(domain, ttl, type, options)
merge_attributes(data.body)
true
end

end

end
end
end
30 changes: 30 additions & 0 deletions lib/fog/zerigo/models/dns/zones.rb
@@ -0,0 +1,30 @@
require 'fog/core/collection'
require 'fog/zerigo/models/dns/zone'

module Fog
module Zerigo
class DNS

class Zones < Fog::Collection

model Fog::Zerigo::DNS::Zone

def all
data = connection.list_zones.body['zones']
load(data)
end

def get(zone_id)
data = connection.get_zone(zone_id).body
zone = new(data)
zone.records.load(data['hosts'])
zone
rescue Excon::Errors::Forbidden
nil
end

end

end
end
end
2 changes: 1 addition & 1 deletion lib/fog/zerigo/requests/dns/create_host.rb
Expand Up @@ -31,7 +31,7 @@ class Real
# * 'updated-at'<~String>
# * 'zone-id'<~String>
# * 'status'<~Integer> - 201 if successful
def create_host( zone_id, host_type, data, options = {} )
def create_host(zone_id, host_type, data, options = {})

optional_tags= ''
options.each { |option, value|
Expand Down

0 comments on commit a641545

Please sign in to comment.