Module: PuppetDB::ParserHelper

Defined in:
lib/puppetdb/parser_helper.rb

Instance Method Summary collapse

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