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
-
#facts_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
50 51 52 |
# File 'lib/puppetdb/parser_helper.rb', line 50 def self.extract(*field, query) ['extract', field.collect(&:to_s), query] end |
Instance Method Details
#facts_hash(facts) ⇒ Hash
Turn an array of facts into a hash of nodes containing facts
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/puppetdb/parser_helper.rb', line 38 def facts_hash(facts) facts.reduce({}) do |ret, fact| if ret.include? fact['certname'] ret[fact['certname']][fact['name']] = fact['value'] else ret[fact['certname']] = { fact['name'] => fact['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 |
# 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| ['=', 'name', f] }] 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 |