From a641545902d7c9e105546a848f4f065c83ec30ce Mon Sep 17 00:00:00 2001 From: geemus Date: Wed, 22 Dec 2010 20:36:53 -0800 Subject: [PATCH] [zerigo|dns] first rough pass at models --- lib/fog/zerigo/dns.rb | 24 ++++--- lib/fog/zerigo/models/dns/record.rb | 54 ++++++++++++++ lib/fog/zerigo/models/dns/records.rb | 41 +++++++++++ lib/fog/zerigo/models/dns/zone.rb | 82 ++++++++++++++++++++++ lib/fog/zerigo/models/dns/zones.rb | 30 ++++++++ lib/fog/zerigo/requests/dns/create_host.rb | 2 +- 6 files changed, 221 insertions(+), 12 deletions(-) create mode 100644 lib/fog/zerigo/models/dns/record.rb create mode 100644 lib/fog/zerigo/models/dns/records.rb create mode 100644 lib/fog/zerigo/models/dns/zone.rb create mode 100644 lib/fog/zerigo/models/dns/zones.rb diff --git a/lib/fog/zerigo/dns.rb b/lib/fog/zerigo/dns.rb index c893d5555c..7dcccb7a66 100644 --- a/lib/fog/zerigo/dns.rb +++ b/lib/fog/zerigo/dns.rb @@ -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 diff --git a/lib/fog/zerigo/models/dns/record.rb b/lib/fog/zerigo/models/dns/record.rb new file mode 100644 index 0000000000..a3229a4d01 --- /dev/null +++ b/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 diff --git a/lib/fog/zerigo/models/dns/records.rb b/lib/fog/zerigo/models/dns/records.rb new file mode 100644 index 0000000000..d61061e300 --- /dev/null +++ b/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 diff --git a/lib/fog/zerigo/models/dns/zone.rb b/lib/fog/zerigo/models/dns/zone.rb new file mode 100644 index 0000000000..86d9b7c78d --- /dev/null +++ b/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' + + # ns1.example.com,ns2.example.com + # true + # dnsadmin@example.com + # + # + # + # + # one two + # 1 + + 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 diff --git a/lib/fog/zerigo/models/dns/zones.rb b/lib/fog/zerigo/models/dns/zones.rb new file mode 100644 index 0000000000..0981f12cfa --- /dev/null +++ b/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 diff --git a/lib/fog/zerigo/requests/dns/create_host.rb b/lib/fog/zerigo/requests/dns/create_host.rb index 2534708df9..bb053bb5f1 100644 --- a/lib/fog/zerigo/requests/dns/create_host.rb +++ b/lib/fog/zerigo/requests/dns/create_host.rb @@ -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|