Sha256: 344186a9652fb7e16004f1b70695d533bdb12d0d1f86a13228ca5692c9d3f4a5

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'bolt/error'

# Collects facts based on a list of certnames.
#
# * If a node is not found in PuppetDB, it's included in the returned hash with empty facts hash.
# * Otherwise the node is included in the hash with a value that is a hash of it's facts.
Puppet::Functions.create_function(:puppetdb_fact) do
  # @param certnames Array of certnames.
  # @return A hash of certname to facts hash for each matched Target.
  # @example Get facts for nodes
  #   puppetdb_fact(['app.example.com', 'db.example.com'])
  dispatch :puppetdb_fact do
    param 'Array[String]', :certnames
    return_type 'Hash[String, Data]'
  end

  def puppetdb_fact(certnames)
    unless Puppet[:tasks]
      raise Puppet::ParseErrorWithIssue.from_issue_and_stack(
        Puppet::Pops::Issues::TASK_OPERATION_NOT_SUPPORTED_WHEN_COMPILING, operation: 'puppetdb_fact'
      )
    end

    puppetdb_client = Puppet.lookup(:bolt_pdb_client) { nil }
    unless puppetdb_client && Puppet.features.bolt?
      raise Puppet::ParseErrorWithIssue.from_issue_and_stack(
        Puppet::Pops::Issues::TASK_MISSING_BOLT, action: _('query facts from puppetdb')
      )
    end

    executor = Puppet.lookup(:bolt_executor) { nil }
    executor&.report_function_call('puppetdb_fact')

    puppetdb_client.facts_for_node(certnames)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bolt-0.21.2 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.21.1 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.21.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.20.7 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb