Module: PuppetDB::ParserHelper

Defined in:
lib/puppetdb/parser_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • facts (Array)

    fact values

Returns:

  • (Hash)

    nodes as keys containing a hash of facts as value



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

Parameters:

  • query (String)

    the query string to parse

  • facts (Array) (defaults to: nil)

    an array of facts to get

Returns:

  • (Array)

    the PuppetDB query



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

Parameters:

  • query (String)

    the query string to parse

  • endpoint (Symbol) (defaults to: :nodes)

    the endpoint for which the query should be evaluated

Returns:

  • (Array)

    the 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