lib/onering/plugins/reporter.rb in onering-client-0.2.3 vs lib/onering/plugins/reporter.rb in onering-client-0.2.4
- old
+ new
@@ -186,9 +186,67 @@
return {}
end
+ def get(field, default=nil, options={})
+ if options[:data].is_a?(Hash)
+ _report = options[:data]
+ else
+ _report = self.report(options)
+ end
+
+ # this is kinda ugly
+ # because we don't know which property might have an @-prefix, progressively
+ # search through all of them. first non-null match wins
+ parts = field.split('.')
+
+ # create an array with every component of the path prefixed with the @-symbol, then with
+ # the path as is.
+ #
+ # e.g.: onering report get metrics.disk.block
+ # -> value exists in the inventory as properties.metrics.disk.@block,
+ # but the user shouldn't need to know where that @-prefix is, so...
+ #
+ # Search for all of these, first non-nil value wins:
+ # * properties.metrics.disk.block
+ # * properties.@metrics.disk.block
+ # * properties.metrics.@disk.block
+ # * properties.metrics.disk.@block
+ # * metrics.disk.block
+ #
+ candidates = [(['properties']+parts).join('.')]
+
+ parts.each_index{|ix|
+ candidates << (['properties']+(ix == 0 ? [] : parts[0..(ix-1)]) + ["@#{parts[ix]}"] + parts[ix+1..-1]).join('.')
+ }.flatten()
+
+ rv = nil
+
+ # search for the key using science or something
+ candidates.each do |c|
+ rv = _report.get(c)
+ break unless rv.nil?
+ end
+
+ # if we're still nil by this point, use the fallback value
+ rv = _report.get(field) if rv.nil?
+
+ # attempt to get the value remotely if not found locally
+ if rv.nil? and not options[:local]
+ hid = Onering::Util.fact(:hardwareid)
+
+ if not hid.nil?
+ Onering::Logger.debug("Getting remote value #{field} for asset #{hid}")
+ return Onering::API.new(options[:api]).assets.get_field(hid, field, default)
+ end
+ end
+
+ return default if rv.nil?
+ return rv
+ end
+
+
def _generated_report()
Timeout.timeout((@options[:timeout] || 60).to_i) do
hostname = (Facter.value('fqdn') rescue %x{hostname -f}.strip.chomp)
@_report = {