Class: Route53::Zone

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, host_url, conn) ⇒ Zone

Returns a new instance of Zone.



115
116
117
118
119
120
121
122
# File 'lib/route53.rb', line 115

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.



113
114
115
# File 'lib/route53.rb', line 113

def conn
  @conn
end

#host_urlObject (readonly)

Returns the value of attribute host_url.



110
111
112
# File 'lib/route53.rb', line 110

def host_url
  @host_url
end

#nameObject (readonly)

Returns the value of attribute name.



111
112
113
# File 'lib/route53.rb', line 111

def name
  @name
end

#recordsObject (readonly)

Returns the value of attribute records.



112
113
114
# File 'lib/route53.rb', line 112

def records
  @records
end

Instance Method Details

#create_zone(comment = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/route53.rb', line 128

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 = Hpricot::XML(resp.raw_data)
  @host_url = resp_xml.search("HostedZone").first.search("Id").first.innerText
  return resp
end

#delete_zoneObject



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

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/route53.rb', line 194

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/route53.rb', line 150

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 = 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
      dom_records.push(DNSRecord.new(record.search("Name").first.innerText,
                    record.search("Type").first.innerText,
                    record.search("TTL").first.innerText,
                    record.search("Value").map { |val| val.innerText },
                    self))
    end
    
    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
  return dom_records
end

#perform_actions(change_list, comment = nil) ⇒ Object

For modifying multiple or single records within a single transaction



215
216
217
218
# File 'lib/route53.rb', line 215

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



221
222
223
# File 'lib/route53.rb', line 221

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