Module: PuppetDB::ParserHelper
- Defined in:
- lib/puppetdb/parser_helper.rb
Class Method Summary collapse
-
.extract(*field, query) ⇒ Object
Turn a query into one for only certain fields.
Instance Method Summary collapse
-
#extract_nested_fact(fact_hashes, keys) ⇒ Object
Take an array of hashes of fact hashes and get a nested value from each of them.
-
#facts_hash(fact_hash, facts) ⇒ Hash
Turn an array of facts into a hash of nodes containing facts.
-
#facts_query(query, facts = nil) ⇒ Array
Create a query for facts on nodes matching a query string.
-
#parse(query, endpoint = :nodes) ⇒ Array
Parse a query string into a PuppetDB query.
Class Method Details
.extract(*field, query) ⇒ Object
Turn a query into one for only certain fields
97 98 99 |
# File 'lib/puppetdb/parser_helper.rb', line 97 def self.extract(*field, query) ['extract', field.collect(&:to_s), query] end |
Instance Method Details
#extract_nested_fact(fact_hashes, keys) ⇒ Object
Take an array of hashes of fact hashes and get a nested value from each of them.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/puppetdb/parser_helper.rb', line 81 def extract_nested_fact(fact_hashes, keys) fact_hashes.map do |fact_hash| hash = fact_hash['value'] # Traverse the hash, setting `hash` equal to the next level deep each step keys[0..-2].each do |key| hash = hash.fetch(key, {}) end # Lookup the final key. This will convert to nil if we've been defaulting # to empty hash beforehand. hash[keys.last] end end |
#facts_hash(fact_hash, facts) ⇒ Hash
Turn an array of facts into a hash of nodes containing facts
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/puppetdb/parser_helper.rb', line 45 def facts_hash(fact_hash, facts) fact_hash.reduce({}) do |ret, fact| # Array#include? only matches on values of the same type, so if we find # a matching string, it's not a nested query. name, value = if facts.include?(fact['name']) || facts == [:all] [fact['name'], fact['value']] else # Find the set of keys where the first value is the fact name nested_keys = facts.select do |x| x.is_a?(Array) && x.first == fact['name'] end.flatten # Join all the key names together with an underscore to give # us a unique name, and then send all the keys but the fact # name (which was already queried out) to extract_nested_fact [ nested_keys.join("_"), extract_nested_fact([fact], nested_keys[1..-1]).first ] end if ret.include? fact['certname'] ret[fact['certname']][name] = value else ret[fact['certname']] = { name => value } end ret end end |
#facts_query(query, facts = nil) ⇒ Array
Create a query for facts on nodes matching a query string
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/puppetdb/parser_helper.rb', line 20 def facts_query(query, facts = nil) nodequery = parse(query, :facts) if facts.nil? nodequery else factquery = ['or', *facts.collect { |f| if (f =~ /^\/(.+)\/$/) ['~', 'name', f.scan(/^\/(.+)\/$/).last.first] else ['=', 'name', f] end }] if nodequery ['and', nodequery, factquery] else factquery end end end |
#parse(query, endpoint = :nodes) ⇒ Array
Parse a query string into a PuppetDB query
9 10 11 12 13 |
# File 'lib/puppetdb/parser_helper.rb', line 9 def parse(query, endpoint = :nodes) if query = scan_str(query) query.optimize.evaluate [endpoint] end end |