Skip to content

Commit

Permalink
[aws|dns] record(s) models
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Jan 3, 2011
1 parent c5583a0 commit 15832b0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/fog/aws/dns.rb
Expand Up @@ -6,6 +6,8 @@ class DNS < Fog::Service
recognizes :host, :path, :port, :scheme, :version, :persistent

model_path 'fog/aws/models/dns'
model :record
collection :records
model :zone
collection :zones

Expand Down
64 changes: 64 additions & 0 deletions 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
47 changes: 47 additions & 0 deletions 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
12 changes: 6 additions & 6 deletions lib/fog/aws/models/dns/zone.rb
Expand Up @@ -22,12 +22,12 @@ def destroy
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
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/aws/models/dns/zones.rb
Expand Up @@ -22,7 +22,7 @@ def all(options = {})
def get(zone_id)
data = connection.get_hosted_zone(zone_id).body
new(data)
rescue Excon::Errors::Forbidden
rescue Excon::Errors::BadRequest
nil
end

Expand Down

0 comments on commit 15832b0

Please sign in to comment.