Class: Route53::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accesskey, secret, api = '2010-10-01', endpoint = 'https://route53.amazonaws.com/', verbose = false) ⇒ Connection

Returns a new instance of Connection.



22
23
24
25
26
27
28
29
# File 'lib/route53.rb', line 22

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

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



18
19
20
# File 'lib/route53.rb', line 18

def api
  @api
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



17
18
19
# File 'lib/route53.rb', line 17

def base_url
  @base_url
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



19
20
21
# File 'lib/route53.rb', line 19

def endpoint
  @endpoint
end

#verboseObject (readonly)

Returns the value of attribute verbose.



20
21
22
# File 'lib/route53.rb', line 20

def verbose
  @verbose
end

Instance Method Details

#get_dateObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/route53.rb', line 88

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")
    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



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
# File 'lib/route53.rb', line 54

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 = Hpricot::XML(resp.raw_data)
    elements = zone_list.search("HostedZone")
    elements.each do |e|
      zones.push(Zone.new(e.search("Name").first.innerText,
                          e.search("Id").first.innerText,
                          self))
    end
    truncated = (zone_list.search("IsTruncated").first.innerText == "true") if truncated
    query = ["marker="+zone_list.search("NextMarker").first.innerText] 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
    end
    return nil
  end
  return zones
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/route53.rb', line 31

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")
  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, raw_resp = http.send_request(type,uri.path+"?"+(uri.query.nil? ? "" : uri.query),data,headers)
  #puts "Resp:"+resp.to_s if @verbose
  #puts "XML_RESP:"+raw_resp if @verbose
  return AWSResponse.new(raw_resp,self)
end