Sha256: a38b0c23a5fb298abb8eedb086f35262116158bd867e6e329225d7888a4259fe

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'bolt/error'

# Returns a hash of certname to facts objects for each matched Target.  This
# functions accepts an array of certnames and returns a hash of target
# certnames and their corresponding facts hash.
#
# * 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
  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

    begin
      puppetdb_client.facts_for_node(certnames)
    rescue StandardError => e
      raise Bolt::CLIError, "Could not retrieve targets from PuppetDB: #{e}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bolt-0.20.2 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.20.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.19.1 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.19.0 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb
bolt-0.18.2 bolt-modules/boltlib/lib/puppet/functions/puppetdb_fact.rb