Class: Route53::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accesskey, secret, api = '2012-12-12', endpoint = 'https://route53.amazonaws.com/', verbose = false, ssl_no_verify = false) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(accesskey,secret,api='2012-12-12',endpoint='https://route53.amazonaws.com/',verbose=false,ssl_no_verify=false)
  @accesskey = accesskey
  @secret = secret
  @api = api
  @endpoint = endpoint
  @base_url = endpoint+@api
  @verbose = verbose
  @ssl_no_verify = ssl_no_verify
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



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

def api
  @api
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#get_dateObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/route53/connection.rb', line 75

def get_date
  #return Time.now.utc.rfc2822
  #Cache date for 30 seconds to reduce extra calls
  if @date_stale.nil? || @date_stale < Time.now - 30
    uri = URI(@endpoint)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if uri.scheme == "https"
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if RUBY_VERSION.start_with?("1.8") or @ssl_no_verify
    resp = nil
    puts "Making Date Request" if @verbose
    http.start { |http| resp = http.head('/date') }
    @date = resp['Date']
    @date_stale = Time.now
    puts "Received Date." if @verbose
  end
  return @date
end

#get_zones(name = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/route53/connection.rb', line 41

def get_zones(name = nil)
  truncated = true
  query = []
  zones = []
  while truncated
    if !name.nil? && name.start_with?("/hostedzone/")
      resp = request("#{@base_url}#{name}")
      truncated = false
    else
      resp = request("#{@base_url}/hostedzone?"+query.join("&"))
    end
    return nil if resp.error?
    zone_list = Nokogiri::XML(resp.raw_data)
    elements = zone_list.search("HostedZone")
    elements.each do |e|
      zones.push(Zone.new(e.search("Name").first.inner_text,
                          e.search("Id").first.inner_text,
                          self))
    end
    truncated = (zone_list.search("IsTruncated").first.inner_text == "true") if truncated
    query = ["marker="+zone_list.search("NextMarker").first.inner_text] if truncated
  end
  unless name.nil? || name.start_with?("/hostedzone/")
    name_arr = name.split('.')
    (0 ... name_arr.size).each do |i|
      search_domain = name_arr.last(name_arr.size-i).join('.')+"."
      zone_select = zones.select { |z| z.name == search_domain }
      return zone_select if zone_select.any?
    end
    return nil
  end
  return zones
end

#request(url, type = "GET", data = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/route53/connection.rb', line 18

def request(url,type = "GET",data = nil)
  puts "URL: #{url}" if @verbose
  puts "Type: #{type}" if @verbose
  puts "Req: #{data}" if type != "GET" && @verbose
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if RUBY_VERSION.start_with?("1.8") or @ssl_no_verify
  time = get_date
  hmac = HMAC::SHA256.new(@secret)
  hmac.update(time)
  signature = Base64.encode64(hmac.digest).chomp
  headers = {
    'Date' => time,
    'X-Amzn-Authorization' => "AWS3-HTTPS AWSAccessKeyId=#{@accesskey},Algorithm=HmacSHA256,Signature=#{signature}",
    'Content-Type' => 'text/xml; charset=UTF-8'
  }
  resp = http.send_request(type,uri.path+"?"+(uri.query.nil? ? "" : uri.query),data,headers)
  #puts "Resp:"+resp.to_s if @verbose
  #puts "RespBody: #{resp.body}" if @verbose
  return AWSResponse.new(resp.body,self)
end