Permalink
Browse files

Allow updates of DNS records. Updates on zones not supported yet.

  • Loading branch information...
1 parent 8c680f0 commit c8c43245490a3b94843c25491614b3b258b80384 @patio11 committed Nov 8, 2011
View
@@ -23,6 +23,7 @@ class Slicehost < Fog::Service
request :get_records
request :get_zone
request :get_zones
+ request :update_record
class Mock
@@ -45,13 +45,17 @@ def zone
end
def save
- raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
+
requires :name, :type, :value, :zone
options = {}
options[:active] = active ? 'Y' : 'N'
options[:aux] = description if description
options[:ttl] = ttl if ttl
- data = connection.create_record(type, zone.id, name, value, options)
+ if identity
+ data = connection.update_record(identity, type, zone.id, name, value, options)
+ else
+ data = connection.create_record(type, zone.id, name, value, options)
+ end
merge_attributes(data.body)
true
end
@@ -0,0 +1,43 @@
+module Fog
+ module DNS
+ class Slicehost
+ class Real
+
+ #require 'fog/slicehost/parsers/dns/update_record'
+
+ # Get an individual DNS record from the specified zone
+ #
+ # ==== Returns
+ # * response<~Excon::Response>:
+ # * body<~Hash>:
+ # * 'record_type'<~String> - type of DNS record to create (A, CNAME, etc)
+ # * 'zone_id'<~Integer> - ID of the zone to update
+ # * 'name'<~String> - host name this DNS record is for
+ # * 'data'<~String> - data for the DNS record (ie for an A record, the IP address)
+ # * 'ttl'<~Integer> - time to live in seconds
+ # * 'active'<~String> - whether this record is active or not ('Y' or 'N')
+ # * 'aux'<~String> - extra data required by the record
+ def update_record(record_id, record_type, zone_id, name, data, options = {})
+ optional_tags= ''
+ options.each { |option, value|
+ case option
+ when :ttl
+ optional_tags+= "<ttl type='integer'>#{value}</ttl>"
+ when :active
+ optional_tags+= "<active>#{value}</active>"
+ when :aux
+ optional_tags+= "<aux>#{value}</aux>"
+ end
+ }
+ request(
+ :body => %Q{<?xml version="1.0" encoding="UTF-8"?><record><record_type>#{record_type}</record_type><zone_id type="integer">#{zone_id}</zone_id><name>#{name}</name><data>#{data}</data>#{optional_tags}</record>},
+ :expects => 200,
+ :method => 'PUT',
+ :path => "records/#{record_id}.xml"
+ )
+ end
+
+ end
+ end
+ end
+end
@@ -221,6 +221,46 @@
result
end
+ test('update record - verify all parameters for one record') do
+ pending if Fog.mocking?
+
+ result = false
+
+ specific_record = nil
+
+ response = Fog::DNS[:slicehost].get_records()
+ if response.status == 200
+ records = response.body['records']
+
+ #find mx record
+ records.each {|record|
+ if (record['record_type'] == 'MX') and (record['name'] == @domain)
+ specific_record = record
+ break
+ end
+ }
+ end
+
+ if (specific_record) #Try to change the TTL for this MX record if we've successfully created it.
+ response = Fog::DNS[:slicehost].update_record(specific_record['id'], specific_record['record_type'], specific_record['zone_id'],
+ specific_record['name'], specific_record['value'], {:ttl => 7200, :active => "N", :aux => "10"})
+
+ mail_domain = 'mail.' + @domain
+
+ records = Fog::DNS[:slicehost].get_record(specific_record['id']).body["records"]
+ record = records[0]
+
+ if (record['record_type'] == 'MX') and (record['name'] == @domain) and
+ (record['value'] == mail_domain) and (record['ttl'] == 7200) and (record['active'] == 'N') and
+ (record['aux'] == "10")
+ result = true
+ end
+
+ end
+
+ result
+ end
+
test("newly created zone returns only records which we added to it, not other records already in account") do
pending if Fog.mocking?

0 comments on commit c8c4324

Please sign in to comment.