diff --git a/README.markdown b/README.markdown index 49119da..a727ed4 100644 --- a/README.markdown +++ b/README.markdown @@ -159,6 +159,9 @@ If you're using this as a library for your own ruby project you can load it and #If updating values for a record, make sure to includ all other values. Otherwise they will be dropped resp = new_record.update(nil,nil,"600",nil) + #Get operation + record = zones.first.get_record("example.com.") + #Deleting a zone #A zone can't be deleted until all of it's records have been deleted (Except for 1 NS record and 1 SOA record) new_zone.get_records.each do |record| diff --git a/lib/route53.rb b/lib/route53.rb index 63bc02f..d772030 100644 --- a/lib/route53.rb +++ b/lib/route53.rb @@ -147,6 +147,21 @@ def create_zone(comment = nil) return resp end + # gets a record by its DNS name. Note: name should end with a ".". + # E.g., "foo.bar.com." + def get_record(name) + return nil if host_url.nil? + records, _ = get_helper("/rrset?name=#{name}&maxitems=1") + return nil if records.nil? + + record = records.first + if record.nil? || record.name != name + return nil + else + return record + end + end + def get_records(type="ANY") return nil if host_url.nil? @@ -154,44 +169,17 @@ def get_records(type="ANY") query = [] dom_records = [] while truncated - resp = @conn.request(@conn.base_url+@host_url+"/rrset?"+query.join("&")) - if resp.error? - return nil - end - zone_file = Hpricot::XML(resp.raw_data) - records = zone_file.search("ResourceRecordSet") - - records.each do |record| - #puts "Name:"+record.search("Name").first.innerText if @conn.verbose - #puts "Type:"+record.search("Type").first.innerText if @conn.verbose - #puts "TTL:"+record.search("TTL").first.innerText if @conn.verbose - #record.search("Value").each do |val| - # #puts "Val:"+val.innerText if @conn.verbose - #end - zone_apex_records = record.search("HostedZoneId") - values = record.search("Value").map { |val| val.innerText } - values << record.search("DNSName").first.innerText unless zone_apex_records.empty? - weight_records = record.search("Weight") - ident_records = record.search("SetIdentifier") - dom_records.push(DNSRecord.new(record.search("Name").first.innerText, - record.search("Type").first.innerText, - (record.search("TTL").first.innerText if zone_apex_records.empty?), - values, - self, - (zone_apex_records.first.innerText unless zone_apex_records.empty?), - (weight_records.first.innerText unless weight_records.empty?), - (ident_records.first.innerText unless ident_records.empty?) - )) - end + records, zone_file = get_helper("/rrset?"+query.join("&")) + return nil if records.nil? + dom_records += records - truncated = (zone_file.search("IsTruncated").first.innerText == "true") + truncated = zone_file.search("IsTruncated").first.innerText == "true" if truncated next_name = zone_file.search("NextRecordName").first.innerText next_type = zone_file.search("NextRecordType").first.innerText query = ["name="+next_name,"type="+next_type] end end - @records = dom_records if type != 'ANY' return dom_records.select { |r| r.type == type } end @@ -230,6 +218,44 @@ def perform_actions(change_list,comment=nil) def to_s return "#{@name} #{@host_url}" end + + private + + # `url` is absolute, e.g., `/rrset?name=foo` + # + # returns two values: the first is an array of records that were parsed + # from the API response. The second is the zone file data, which contains + # information about whether or not the response is truncated, along with other + # metadata. + # + # This may return a nil record list if the request had an error. + def get_helper(url) + resp = @conn.request(@conn.base_url+@host_url+url) + if resp.error? + return nil, nil + end + zone_file = Hpricot::XML(resp.raw_data) + records = zone_file.search("ResourceRecordSet") + + dom_records = [] + records.each do |record| + zone_apex_records = record.search("HostedZoneId") + values = record.search("Value").map { |val| val.innerText } + values << record.search("DNSName").first.innerText unless zone_apex_records.empty? + weight_records = record.search("Weight") + ident_records = record.search("SetIdentifier") + dom_records.push(DNSRecord.new(record.search("Name").first.innerText, + record.search("Type").first.innerText, + (record.search("TTL").first.innerText if zone_apex_records.empty?), + values, + self, + (zone_apex_records.first.innerText unless zone_apex_records.empty?), + (weight_records.first.innerText unless weight_records.empty?), + (ident_records.first.innerText unless ident_records.empty?) + )) + end + return dom_records, zone_file + end end class AWSResponse diff --git a/lib/route53/version.rb b/lib/route53/version.rb index 6161b68..2c209df 100644 --- a/lib/route53/version.rb +++ b/lib/route53/version.rb @@ -1,3 +1,3 @@ module Route53 - VERSION = "0.2.1" + VERSION = "0.2.2" end