Sha256: 93ce832b9c18f9991e0b02711d0be105facdea08f3ea356d39936e037e82514a

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'bolt/error'

# Makes a query to [puppetdb](https://puppet.com/docs/puppetdb/latest/index.html)
# using Bolt's PuppetDB client.
Puppet::Functions.create_function(:puppetdb_query) do
  # rubocop:disable Layout/LineLength
  # Make a query to PuppetDB.
  #
  # @param query A PQL query.
  #   Learn more about [Puppet's query language](https://puppet.com/docs/puppetdb/latest/api/query/tutorial-pql.html), PQL.
  # @return Results of the PuppetDB query.
  # @example Request certnames for all nodes
  #   puppetdb_query('nodes[certname] {}')
  # rubocop:enable Layout/LineLength
  dispatch :make_query do
    param 'Variant[String, Array[Data]]', :query
    return_type 'Array[Data]'
  end

  # rubocop:disable Layout/LineLength
  # Make a query to a named PuppetDB instance.
  #
  # @param query A PQL query.
  #   Learn more about [Puppet's query language](https://puppet.com/docs/puppetdb/latest/api/query/tutorial-pql.html), PQL.
  # @param instance The PuppetDB instance to query.
  # @return Results of the PuppetDB query.
  # @example Request certnames for all nodes using a named PuppetDB instance
  #   puppetdb_query('nodes[certname] {}', 'instance-1')
  # rubocop:enable Layout/LineLength
  dispatch :make_query_with_instance do
    param 'Variant[String, Array[Data]]', :query
    param 'String', :instance
    return_type 'Array[Data]'
  end
  # The query type could be more specific ASTQuery = Array[Variant[String, ASTQuery]]

  def make_query(query)
    make_query_with_instance(query, nil)
  end

  def make_query_with_instance(query, instance)
    puppetdb_client = Puppet.lookup(:bolt_pdb_client)
    # Bolt executor not expected when invoked from apply block
    executor = Puppet.lookup(:bolt_executor) { nil }
    # Send Analytics Report
    executor&.report_function_call(self.class.name)

    puppetdb_client.make_query(query, nil, instance)
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bolt-4.0.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.29.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.28.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.27.4 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.27.2 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.27.1 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.26.2 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.26.1 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.25.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.24.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.23.1 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb
bolt-3.23.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_query.rb