From 15832b0eb7e71cd1a9536d4f207052982619e0b2 Mon Sep 17 00:00:00 2001 From: geemus Date: Mon, 3 Jan 2011 14:45:50 -0800 Subject: [PATCH] [aws|dns] record(s) models --- lib/fog/aws/dns.rb | 2 + lib/fog/aws/models/dns/record.rb | 64 +++++++++++++++++++++++++++++++ lib/fog/aws/models/dns/records.rb | 47 +++++++++++++++++++++++ lib/fog/aws/models/dns/zone.rb | 12 +++--- lib/fog/aws/models/dns/zones.rb | 2 +- 5 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 lib/fog/aws/models/dns/record.rb create mode 100644 lib/fog/aws/models/dns/records.rb diff --git a/lib/fog/aws/dns.rb b/lib/fog/aws/dns.rb index 647c6b06b..09a9a553c 100644 --- a/lib/fog/aws/dns.rb +++ b/lib/fog/aws/dns.rb @@ -6,6 +6,8 @@ module Fog recognizes :host, :path, :port, :scheme, :version, :persistent model_path 'fog/aws/models/dns' + model :record + collection :records model :zone collection :zones diff --git a/lib/fog/aws/models/dns/record.rb b/lib/fog/aws/models/dns/record.rb new file mode 100644 index 000000000..dc746e3aa --- /dev/null +++ b/lib/fog/aws/models/dns/record.rb @@ -0,0 +1,64 @@ +require 'fog/core/model' + +module Fog + module AWS + class DNS + + class Record < Fog::Model + + identity :id, :aliases => ['Id'] + + attribute :ip, :aliases => ['ResourceRecords'] + attribute :name, :aliases => ['Name'] + attribute :ttl, :aliases => ['TTL'] + attribute :type, :aliases => ['Type'] + attribute :status, :aliases => ['Status'] + attribute :created_at, :aliases => ['SubmittedAt'] + + def initialize(attributes={}) + self.ttl ||= 3600 + super + end + + def destroy + requires :ip, :name, :ttl, :type, :zone + options = { + :action => 'DELETE', + :name => name, + :resource_records => [*ip], + :ttl => ttl, + :type => type + } + connection.change_resource_record_sets(zone.id, [options]) + true + end + + def zone + @zone + end + + def save + requires :ip, :name, :ttl, :type, :zone + options = { + :action => 'CREATE', + :name => name, + :resource_records => [*ip], + :ttl => ttl, + :type => type + } + data = connection.change_resource_record_sets(zone.id, [options]).body + merge_attributes(data) + true + end + + private + + def zone=(new_zone) + @zone = new_zone + end + + end + + end + end +end diff --git a/lib/fog/aws/models/dns/records.rb b/lib/fog/aws/models/dns/records.rb new file mode 100644 index 000000000..b32a4b919 --- /dev/null +++ b/lib/fog/aws/models/dns/records.rb @@ -0,0 +1,47 @@ +require 'fog/core/collection' +require 'fog/aws/models/dns/record' + +module Fog + module AWS + class DNS + + class Records < Fog::Collection + + attribute :is_truncated, :aliases => ['IsTruncated'] + attribute :max_items, :aliases => ['MaxItems'] + attribute :name + attribute :next_record_name, :aliases => ['NextRecordName'] + attribute :next_record_type, :aliases => ['NextRecordType'] + attribute :type + + attribute :zone + + model Fog::AWS::DNS::Record + + def all(options = {}) + requires :zone + options['MaxItems'] ||= max_items + options['Name'] ||= name + options['Type'] ||= type + data = connection.list_resource_record_sets(zone.id, options).body + merge_attributes(data.reject {|key, value| !['IsTruncated', 'MaxItems', 'NextRecordName', 'NextRecordType'].include?(key)}) + load(data['ResourceRecordSets']) + end + + def get(record_id) + data = connection.get_change(record_id).body + new(data) + rescue Excon::Errors::BadRequest + nil + end + + def new(attributes = {}) + requires :zone + super({ :zone => zone }.merge!(attributes)) + end + + end + + end + end +end diff --git a/lib/fog/aws/models/dns/zone.rb b/lib/fog/aws/models/dns/zone.rb index 339b1d855..56097f3f1 100644 --- a/lib/fog/aws/models/dns/zone.rb +++ b/lib/fog/aws/models/dns/zone.rb @@ -22,12 +22,12 @@ module Fog end def records - # @records ||= begin - # Fog::Linode::DNS::Records.new( - # :zone => self, - # :connection => connection - # ) - # end + @records ||= begin + Fog::AWS::DNS::Records.new( + :zone => self, + :connection => connection + ) + end end def save diff --git a/lib/fog/aws/models/dns/zones.rb b/lib/fog/aws/models/dns/zones.rb index fbc7e4de2..6de512828 100644 --- a/lib/fog/aws/models/dns/zones.rb +++ b/lib/fog/aws/models/dns/zones.rb @@ -22,7 +22,7 @@ module Fog def get(zone_id) data = connection.get_hosted_zone(zone_id).body new(data) - rescue Excon::Errors::Forbidden + rescue Excon::Errors::BadRequest nil end