Sha256: a520fa4edf0fd0bfb4be1ed1cd7549f6c7824e2cc9edea355deca1e66c3e77e7

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

# Helper methods for the parser, included in the parser class
require 'puppetdb'
module PuppetDB::ParserHelper
  # Parse a query string into a PuppetDB query
  #
  # @param query [String] the query string to parse
  # @param endpoint [Symbol] the endpoint for which the query should be evaluated
  # @return [Array] the PuppetDB query
  def parse(query, endpoint = :nodes)
    if query = scan_str(query)
      query.optimize.evaluate [endpoint]
    end
  end

  # Create a query for facts on nodes matching a query string
  #
  # @param query [String] the query string to parse
  # @param facts [Array] an array of facts to get
  # @return [Array] the PuppetDB query
  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

  # Turn an array of facts into a hash of nodes containing facts
  #
  # @param facts [Array] fact values
  # @return [Hash] nodes as keys containing a hash of facts as value
  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

  # Turn a query into one for only certain fields
  def self.extract(*field, query)
    ['extract', field.collect(&:to_s), query]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-puppetdb-2.1.1 lib/puppetdb/parser_helper.rb
ruby-puppetdb-2.1.0 lib/puppetdb/parser_helper.rb