Class: Bolt::ApplyTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/apply_target.rb

Constant Summary collapse

ATTRIBUTES =
%i[uri name target_alias config vars facts features
plugin_hooks resources safe_name].freeze
COMPUTED =
%i[host password port protocol user].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_hash, config) ⇒ ApplyTarget

Returns a new instance of ApplyTarget.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bolt/apply_target.rb', line 44

def initialize(target_hash, config)
  ATTRIBUTES.each do |attr|
    instance_variable_set("@#{attr}", target_hash[attr.to_s])
  end

  # Merge the config hash with inventory config
  config = Bolt::Util.deep_merge(config, @config || {})
  transport = config['transport'] || 'ssh'
  t_conf = config['transports'][transport] || {}
  uri_obj = parse_uri(uri)
  @host = uri_obj.hostname || t_conf['host']
  @password = Addressable::URI.unencode_component(uri_obj.password) || t_conf['password']
  @port = uri_obj.port || t_conf['port']
  @protocol = uri_obj.scheme || transport
  @user = Addressable::URI.unencode_component(uri_obj.user) || t_conf['user']
end

Class Method Details

._pcore_init_from_hashObject

rubocop:enable Lint/UnusedMethodArgument


33
34
35
# File 'lib/bolt/apply_target.rb', line 33

def self._pcore_init_from_hash
  raise "ApplyTarget shouldn't be instantiated from a pcore_init class method. How did this get called?"
end

.from_asserted_args(uri = nil, name = nil, safe_name = nil, target_alias = nil, config = nil, facts = nil, vars = nil, features = nil, plugin_hooks = nil, resources = nil) ⇒ Object

Target.new from a plan with just a uri.

Raises:


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bolt/apply_target.rb', line 19

def self.from_asserted_args(uri = nil,
                            name = nil,
                            safe_name = nil,
                            target_alias = nil,
                            config = nil,
                            facts = nil,
                            vars = nil,
                            features = nil,
                            plugin_hooks = nil,
                            resources = nil)
  raise Bolt::Error.new("Target objects cannot be instantiated inside apply blocks", 'bolt/apply-error')
end

.from_asserted_hash(hash) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument Target.new from a plan initialized with a hash

Raises:


14
15
16
# File 'lib/bolt/apply_target.rb', line 14

def self.from_asserted_hash(hash)
  raise Bolt::Error.new("Target objects cannot be instantiated inside apply blocks", 'bolt/apply-error')
end

Instance Method Details

#_pcore_init_from_hash(init_hash) ⇒ Object


37
38
39
40
41
42
# File 'lib/bolt/apply_target.rb', line 37

def _pcore_init_from_hash(init_hash)
  inventory = Puppet.lookup(:bolt_inventory)
  initialize(init_hash, inventory.config_hash)
  inventory.create_apply_target(self)
  self
end

#hashObject


86
87
88
# File 'lib/bolt/apply_target.rb', line 86

def hash
  @name.hash
end

#parse_uri(string) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bolt/apply_target.rb', line 69

def parse_uri(string)
  require 'addressable/uri'
  if string.nil?
    Addressable::URI.new
    # Forbid empty uri
  elsif string.empty?
    raise Bolt::ParseError, "Could not parse target URI: URI is empty string"
  elsif string =~ %r{^[^:]+://}
    Addressable::URI.parse(string)
  else
    # Initialize with an empty scheme to ensure we parse the hostname correctly
    Addressable::URI.parse("//#{string}")
  end
rescue Addressable::URI::InvalidURIError => e
  raise Bolt::ParseError, "Could not parse target URI: #{e.message}"
end

#resource(type, title) ⇒ Object


65
66
67
# File 'lib/bolt/apply_target.rb', line 65

def resource(type, title)
  resources[Bolt::ResourceInstance.format_reference(type, title)]
end

#to_sObject


61
62
63
# File 'lib/bolt/apply_target.rb', line 61

def to_s
  @safe_name
end