Class: Route53::Zone

Inherits:
Object
  • Object
show all
Defined in:
lib/route53/zone.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, host_url, conn) ⇒ Zone

Returns a new instance of Zone.



8
9
10
11
12
13
14
15
# File 'lib/route53/zone.rb', line 8

def initialize(name,host_url,conn)
  @name = name
  unless @name.end_with?(".")
    @name += "."
  end
  @host_url = host_url
  @conn = conn
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



6
7
8
# File 'lib/route53/zone.rb', line 6

def conn
  @conn
end

#host_urlObject (readonly)

Returns the value of attribute host_url.



3
4
5
# File 'lib/route53/zone.rb', line 3

def host_url
  @host_url
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/route53/zone.rb', line 4

def name
  @name
end

#recordsObject (readonly)

Returns the value of attribute records.



5
6
7
# File 'lib/route53/zone.rb', line 5

def records
  @records
end

Instance Method Details

#create_zone(comment = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/route53/zone.rb', line 28

def create_zone(comment = nil)
  xml_str = ""
  xml = Builder::XmlMarkup.new(:target=>xml_str, :indent=>2)
  xml.instruct!
  xml.CreateHostedZoneRequest(:xmlns => @conn.endpoint+'doc/'+@conn.api+'/') { |create|
    create.Name(@name)
    # AWS lists this as required
    # "unique string that identifies the request and that
    # allows failed CreateHostedZone requests to be retried without the risk of executing the operation twice."
    # Just going to pass a random string instead.
    create.CallerReference(rand(2**32).to_s(16))
    create.HostedZoneConfig { |conf|
      conf.Comment(comment)
    }
  }
  #puts "XML:\n#{xml_str}" if @conn.verbose
  resp = @conn.request(@conn.base_url + "/hostedzone","POST",xml_str)
  resp_xml = Nokogiri::XML(resp.raw_data)
  @host_url = resp_xml.search("HostedZone").first.search("Id").first.inner_text
  return resp
end

#delete_zoneObject



24
25
26
# File 'lib/route53/zone.rb', line 24

def delete_zone
  @conn.request(@conn.base_url + @host_url,"DELETE")
end

#gen_change_xml(change_list, comment = nil) ⇒ Object

When deleting a record an optional value is available to specify just a single value within a recordset like an MX record Takes an array of [:action => , :record => ] where action is either CREATE or DELETE and record is a DNSRecord



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/route53/zone.rb', line 103

def gen_change_xml(change_list,comment=nil)
  #Get zone list and pick zone that matches most ending chars

  xml_str = ""
  xml = Builder::XmlMarkup.new(:target=>xml_str, :indent=>2)
  xml.instruct!
  xml.ChangeResourceRecordSetsRequest(:xmlns => @conn.endpoint+'doc/'+@conn.api+'/') { |req|
    req.ChangeBatch { |batch|
      batch.Comment(comment) unless comment.nil?
      batch.Changes { |changes|
        change_list.each { |change_item|
          change_item[:record].gen_change_xml(changes,change_item[:action])
        }
      }
    }
  }
  #puts "XML:\n#{xml_str}" if @conn.verbose
  return xml_str
end

#get_records(type = "ANY") ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/route53/zone.rb', line 50

def get_records(type="ANY")
  return nil if host_url.nil?

  truncated = true
  query = []
  dom_records = []
  while truncated
    resp = @conn.request(@conn.base_url+@host_url+"/rrset?"+query.join("&"))
    if resp.error?
      return nil
    end
    zone_file = Nokogiri::XML(resp.raw_data)
    records = zone_file.search("ResourceRecordSet")

    records.each do |record|
      #puts "Name:"+record.search("Name").first.inner_text if @conn.verbose
      #puts "Type:"+record.search("Type").first.inner_text if @conn.verbose
      #puts "TTL:"+record.search("TTL").first.inner_text if @conn.verbose
      #record.search("Value").each do |val|
      #  #puts "Val:"+val.inner_text if @conn.verbose
      #end
      zone_apex_records = record.search("HostedZoneId")
      values = record.search("Value").map { |val| val.inner_text }
      values << record.search("DNSName").first.inner_text unless zone_apex_records.empty?
      weight_records = record.search("Weight")
      ident_records = record.search("SetIdentifier")
      dom_records.push(DNSRecord.new(record.search("Name").first.inner_text,
                    record.search("Type").first.inner_text,
                    ((record.search("TTL").first.nil? ? '' : record.search("TTL").first.inner_text) if zone_apex_records.empty?),
                    values,
                    self,
                    (zone_apex_records.first.inner_text unless zone_apex_records.empty?),
                    (weight_records.first.inner_text unless weight_records.empty?),
                    (ident_records.first.inner_text unless ident_records.empty?)
                    ))
    end

    truncated = (zone_file.search("IsTruncated").first.inner_text == "true")
    if truncated
      next_name = zone_file.search("NextRecordName").first.inner_text
      next_type = zone_file.search("NextRecordType").first.inner_text
      query = ["name="+next_name,"type="+next_type]
    end
  end
  @records = dom_records
  if type != 'ANY'
    return dom_records.select { |r| r.type == type }
  end
  return dom_records
end

#nameserversObject



17
18
19
20
21
22
# File 'lib/route53/zone.rb', line 17

def nameservers
  return @nameservers if @nameservers
  response = Nokogiri::XML(@conn.request(@conn.base_url + @host_url).to_s)
  @nameservers = response.search("NameServer").map(&:inner_text)
  @nameservers
end

#perform_actions(change_list, comment = nil) ⇒ Object

For modifying multiple or single records within a single transaction



124
125
126
127
# File 'lib/route53/zone.rb', line 124

def perform_actions(change_list,comment=nil)
  xml_str = gen_change_xml(change_list,comment)
  @conn.request(@conn.base_url + @host_url+"/rrset","POST",xml_str)
end

#to_sObject



130
131
132
# File 'lib/route53/zone.rb', line 130

def to_s
  return "#{@name} #{@host_url}"
end