Module: PuppetDB::ParserHelper

Defined in:
modules/puppetdbquery/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



100
101
102
# File 'modules/puppetdbquery/lib/puppetdb/parser_helper.rb', line 100

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.

Parameters:

  • fact_hashes (Array)

    an array of hashes of fact hashes

  • keys (Array)

    an array of keys to dig into the hash



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'modules/puppetdbquery/lib/puppetdb/parser_helper.rb', line 84

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

Parameters:

  • fact_hash (Array)

    fact values

  • facts (Array)

    fact names

Returns:

  • (Hash)

    nodes as keys containing a hash of facts as value



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
74
75
76
# File 'modules/puppetdbquery/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] ||
                    # in case a regex pattern is used in the facts query
                    facts.index{ |factname| factname =~ /^\/(.+)\/$/ && Regexp.new(factname.match(/^\/(.+)\/$/)[1]).match(fact['name']) }

                    [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

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
33
34
35
36
37
38
# File 'modules/puppetdbquery/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

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 'modules/puppetdbquery/lib/puppetdb/parser_helper.rb', line 9

def parse(query, endpoint = :nodes)
  if query = scan_str(query)
    query.optimize.evaluate [endpoint]
  end
end