Permalink
Browse files

initial model work

  • Loading branch information...
1 parent 3a8382f commit 37af71d76ad6e677a7431bb1fb852eaa7f902f3c @dje dje committed Feb 25, 2011
View
@@ -3,8 +3,15 @@ module DNSimple
class DNS < Fog::Service
requires :dnsimple_email, :dnsimple_password
+ recognizes :host, :path, :port, :scheme, :persistent
recognizes :provider # remove post deprecation
+ model_path 'fog/dns/models/dnsimple'
+ model :record
+ collection :records
+ model :zone
+ collection :zones
+
request_path 'fog/dns/requests/dnsimple'
request :list_domains
request :create_domain
@@ -16,7 +23,32 @@ class DNS < Fog::Service
request :delete_record
class Mock
- # TODO
+
+ 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={})
+ unless options.delete(:provider)
+ location = caller.first
+ warning = "[yellow][WARN] Fog::DNS::DNSimple.new is deprecated, use Fog::DNS.new(:provider => 'DNSimple') instead[/]"
+ warning << " [light_black](" << location << ")[/] "
+ Formatador.display_line(warning)
+ end
+
+ @dnsimple_email = options[:dnsimple_email]
+ @dnsimple_password = options[:dnsimple_password]
+ @data = self.class.data[@dnsimple_email]
+ @data = self.class.data[@dnsimple_password]
+ end
end
class Real
@@ -36,7 +68,7 @@ def initialize(options={})
@host = options[:host] || "test.dnsimple.com"
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
- @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
+ @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@@ -0,0 +1,55 @@
+require 'fog/core/model'
+
+module Fog
+ module DNSimple
+ class DNS
+
+ class Record < Fog::Model
+
+ identity :id
+
+ attribute :name
+ attribute :ip, :aliases => "content"
+ attribute :ttl
+ attribute :created_at
+ attribute :updated_at
+ attribute :zone_id, :aliases => "domain_id"
+ attribute :type, :aliases => "record_type"
+ attribute :priority, :aliases => "prio"
+
+ def initialize(attributes={})
+ self.ttl ||= 3600
+ super
+ end
+
+ def destroy
+ requires :identity
+ connection.delete_record(identity)
+ true
+ end
+
+ def zone
+ @zone
+ end
+
+ def save
+ requires :name, :type, :ip
+ options = {}
+ options[:prio] = priority if priority
+ options[:ttl] = ttl if ttl
+ connection.create_record(zone.id, name, type, ip, options)
+ merge_attributes(data.body)
+ true
+ end
+
+ private
+
+ def zone=(new_zone)
+ @zone = new_zone
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,38 @@
+require 'fog/core/collection'
+require 'fog/dns/models/dnsimple/record'
+
+module Fog
+ module DNSimple
+ class DNS
+
+ class Records < Fog::Collection
+
+ attribute :zone
+
+ model Fog::DNSimple::DNS::Record
+
+ def all
+ requires :zone
+ data = connection.list_records(zone.id).body
+ end
+
+ def get(record_id)
+ all.each do |record|
+ if record["record"]["id"] == record_id
+ return new(record)
+ end
+ end
+
+ nil
+ end
+
+ def new(attributes = {})
+ requires :zone
+ super({ :zone => zone }.merge!(attributes))
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,50 @@
+require 'fog/core/model'
+require 'fog/dns/models/dnsimple/records'
+
+module Fog
+ module DNSimple
+ class DNS
+
+ class Zone < Fog::Model
+
+ identity :id
+
+ attribute :domain, :aliases => "name"
+ attribute :created_at
+ attribute :updated_at
+
+ def destroy
+ requires :identity
+ connection.delete_domain(identity)
+ true
+ end
+
+ def records
+ @records ||= begin
+ Fog::DNSimple::DNS::Records.new(
+ :zone => self,
+ :connection => connection
+ )
+ end
+ end
+
+ def nameservers
+ [
+ "ns1.dnsimple.com",
+ "ns2.dnsimple.com",
+ "ns3.dnsimple.com",
+ "ns4.dnsimple.com",
+ ]
+ end
+
+ def save
+ requires :domain
+ data = connection.create_domain(domain).body
+ true
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,30 @@
+require 'fog/core/collection'
+require 'fog/dns/models/dnsimple/zone'
+
+module Fog
+ module DNSimple
+ class DNS
+
+ class Zones < Fog::Collection
+
+ model Fog::DNSimple::DNS::Zone
+
+ def all
+ data = connection.list_domains.body
+ load(data)
+ end
+
+ def get(zone_id)
+ data = connection.get_domain(zone_id).body["domain"]
+ zone = new(data)
+ zone.records.load(data["record"])
+ zone
+ rescue Fog::Service::NotFound
+ nil
+ end
+
+ end
+
+ end
+ end
+end
@@ -115,7 +115,6 @@ def generate_unique_domain( with_trailing_dot = false)
result
end
-
test("delete domain") do
pending if Fog.mocking?

0 comments on commit 37af71d

Please sign in to comment.