Class/Module Index [+]

Quicksearch

Facter::EC2::Metadata

Constants

DEFAULT_URI

Public Class Methods

new(uri = DEFAULT_URI) click to toggle source
# File lib/facter/ec2/rest.rb, line 50
def initialize(uri = DEFAULT_URI)
  @baseurl = uri
end

Public Instance Methods

fetch(path = '') click to toggle source
# File lib/facter/ec2/rest.rb, line 54
def fetch(path = '')
  results = {}

  keys = fetch_endpoint(path)
  keys.each do |key|
    if key.match(%[/$])
      # If a metadata key is suffixed with '/' then it's a general metadata
      # resource, so we have to recursively look up all the keys in the given
      # collection.
      name = key[0..-2]
      results[name] = fetch("#{path}#{key}")
    else
      # This is a simple key/value pair, we can just query the given endpoint
      # and store the results.
      ret = fetch_endpoint("#{path}#{key}")
      results[key] = ret.size > 1 ? ret : ret.first
    end
  end

  results
end
fetch_endpoint(path) click to toggle source

@param path [String] The path relative to the object base url

@return [Array, NilClass]

# File lib/facter/ec2/rest.rb, line 79
def fetch_endpoint(path)
  uri = @baseurl + path
  body = open(uri, :proxy => nil).read
  parse_results(body)
rescue OpenURI::HTTPError => e
  if e.message.match /404 Not Found/
    return nil
  else
    Facter.log_exception(e, "Failed to fetch ec2 uri #{uri}: #{e.message}")
    return nil
  end
rescue *CONNECTION_ERRORS => e
  Facter.log_exception(e, "Failed to fetch ec2 uri #{uri}: #{e.message}")
  return nil
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.