Sha256: caf6d743472a73dfb3d2488933e3ebad52ca9854daa5e2b1a74c1c48951e8a5c

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require 'bolt/error'

# Get a single target from inventory if it exists, otherwise create a new Target.
#
# **NOTE:** Calling `get_target` inside an `apply` block with a
# version 2 inventory creates a new Target object.
# `get_target('all')` returns an empty array.
# **NOTE:** Only compatible with inventory v2
Puppet::Functions.create_function(:get_target) do
  # @param name A Target name.
  # @return A single target, either new or from inventory.
  # @example Create a new Target from a URI
  #   get_target('winrm://host2:54321')
  # @example Get an existing Target from inventory
  #   get_target('existing-target')
  dispatch :get_target do
    param 'Boltlib::TargetSpec', :name
    return_type 'Target'
  end

  def get_target(name)
    inventory = Puppet.lookup(:bolt_inventory)
    # Bolt executor not expected when invoked from apply block
    executor = Puppet.lookup(:bolt_executor) { nil }
    executor&.report_function_call(self.class.name)

    unless inventory.version > 1
      raise Puppet::ParseErrorWithIssue
        .from_issue_and_stack(Bolt::PAL::Issues::UNSUPPORTED_INVENTORY_VERSION, action: 'get_target')
    end

    inventory.get_target(name)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bolt-1.42.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.41.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.40.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.39.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.38.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.37.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.36.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.35.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.34.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.33.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb
bolt-1.32.0 bolt-modules/boltlib/lib/puppet/functions/get_target.rb