Permalink
Browse files

Merge branch 'dje'

  • Loading branch information...
2 parents 478d7ac + daf0f5e commit d7f214705e98d4b58d02ecb2de3e52b202d8339e @geemus geemus committed Feb 25, 2011
View
@@ -67,3 +67,4 @@ def collections
require 'fog/bin/terremark'
require 'fog/bin/voxel'
require 'fog/bin/zerigo'
+require 'fog/bin/dnsimple'
View
@@ -0,0 +1,30 @@
+class DNSimple < Fog::Bin
+ class << self
+
+ def class_for(key)
+ case key
+ when :dns
+ Fog::DNSimple::DNS
+ else
+ raise ArgumentError, "Unrecognized service: #{key}"
+ end
+ end
+
+ def [](service)
+ @@connections ||= Hash.new do |hash, key|
+ hash[key] = case key
+ when :dns
+ Fog::DNS.new(:provider => 'DNSimple')
+ else
+ raise ArgumentError, "Unrecognized service: #{key.inspect}"
+ end
+ end
+ @@connections[service]
+ end
+
+ def services
+ Fog::DNSimple.services
+ end
+
+ end
+end
@@ -79,6 +79,8 @@ def self.missing_credentials
:voxel_api_secret:
:zerigo_email:
:zerigo_token:
+ :dnsimple_email:
+ :dnsimple_password:
#
# End of Fog Credentials File
#######################################################
View
@@ -19,6 +19,9 @@ def self.new(attributes)
when 'Zerigo'
require 'fog/dns/zerigo'
Fog::Zerigo::DNS.new(attributes)
+ when 'DNSimple'
+ require 'fog/dns/dnsimple'
+ Fog::DNSimple::DNS.new(attributes)
else
raise ArgumentError.new("#{provider} is not a recognized dns provider")
end
View
@@ -0,0 +1,95 @@
+module Fog
+ 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
+ request :get_domain
+ request :delete_domain
+ request :create_record
+ request :list_records
+ request :update_record
+ request :delete_record
+
+ 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={})
+ 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
+
+ def initialize(options={})
+ unless options.delete(:provider)
+ location = caller.first
+ warning = "[yellow][WARN] Fog::DNSimple::DNS.new is deprecated, use Fog::DNS.new(:provider => 'DNSimple') instead[/]"
+ warning << " [light_black](" << location << ")[/] "
+ Formatador.display_line(warning)
+ end
+
+ require 'json'
+
+ @dnsimple_email = options[:dnsimple_email]
+ @dnsimple_password = options[:dnsimple_password]
+ @host = options[:host] || "dnsimple.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] ||= {}
+ key = "#{@dnsimple_email}:#{@dnsimple_password}"
+ params[:headers].merge!({ "Authorization" => "Basic " + Base64.encode64(key).chomp,
+ "Accept" => "application/json",
+ "Content-Type" => "application/json" })
+
+ response = @connection.request(params.merge!({:host => @host}))
+
+ unless response.body.empty?
+ response.body = JSON.parse(response.body)
+ end
+ response
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,54 @@
+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
+ connection.delete_record(zone.domain, identity)
+ true
+ end
+
+ def zone
+ @zone
+ end
+
+ def save
+ requires :name, :type, :ip
+ options = {}
+ options[:prio] = priority if priority
+ options[:ttl] = ttl if ttl
+ data = connection.create_record(zone.domain, name, type, ip, options)
+ merge_attributes(data.body["record"])
+ 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
+ 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["domain"]
+ merge_attributes(data)
+ true
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,28 @@
+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"]
+ new(data)
+ rescue Excon::Errors::NotFound
+ nil
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,27 @@
+module Fog
+ module DNSimple
+ class DNS
+ class Real
+
+ # Create a single domain in DNSimple in your account.
+ # ==== Parameters
+ # * name<~String> - domain name to host (ie example.com)
+ #
+ # ==== Returns
+ # * response<~Excon::Response>:
+ # * body<~Hash>:
+ # * 'name'<~String>
+ def create_domain(name)
+ body = { "domain" => { "name" => name } }
+ request(
+ :body => body.to_json,
+ :expects => 201,
+ :method => 'POST',
+ :path => '/domains'
+ )
+ end
+
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit d7f2147

Please sign in to comment.