class Facter::EC2::Metadata
Constants
- DEFAULT_URI
Public Class Methods
new(uri = DEFAULT_URI)
click to toggle source
# File lib/facter/ec2/rest.rb, line 55 def initialize(uri = DEFAULT_URI) @baseurl = uri end
Public Instance Methods
fetch(path = '')
click to toggle source
# File lib/facter/ec2/rest.rb, line 59 def fetch(path = '') results = {} keys = fetch_endpoint(path) keys.each do |key| next if FILTERED_KEYS.include? key if key.match(%r[/$]) # 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 85 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/i 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 rescue Timeout::Error => e Facter.log_exception(e, "Failed to fetch ec2 uri #{uri}: #{e.message}") return nil end
Private Instance Methods
parse_results(body)
click to toggle source
# File lib/facter/ec2/rest.rb, line 106 def parse_results(body) lines = body.split("\n") lines.map do |line| if (match = line.match(/^(\d+)=.*$/)) # Metadata arrays are formatted like '<index>=<associated key>/', so # we need to extract the index from that output. "#{match[1]}/" else line end end end