Class: Bolt::Plugin::PuppetConnectData

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

Constant Summary collapse

INPUT_DATA_VAR =
'PUPPET_CONNECT_INPUT_DATA'

Instance Method Summary collapse

Constructor Details

#initialize(context:, **_opts) ⇒ PuppetConnectData

Returns a new instance of PuppetConnectData.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 8

def initialize(context:, **_opts)
  if ENV.key?(INPUT_DATA_VAR)
    # The user provided input data that they will copy-paste into the Puppet Connect UI
    # for inventory syncing. This environment variable will likely be set when invoking a
    # general "test Puppet Connect input data" command. That command tests that parsing
    # the inventory with the given input data results in connectable targets. Part of
    # that requires validating that the input data contains all of the referenced keys,
    # which is what this plugin will do in validate_resolve_reference.
    @input_data_path = ENV[INPUT_DATA_VAR]
    data_path = @input_data_path
  else
    # The user is using this plugin during a regular Bolt invocation, so fetch the (minimal)
    # required data from the default location. This data should typically be non-autoloadable
    # secrets like WinRM passwords.
    #
    # Note that any unspecified keys will be resolved to nil.
    data_path = File.join(context.boltdir, 'puppet_connect_data.yaml')
  end

  @data = Bolt::Util.read_optional_yaml_hash(
    data_path,
    File.basename(data_path)
  )

  if @input_data_path
    # Validate that the data does not contain any plugin-reference
    # values
    @data.each do |key, toplevel_value|
      # Use walk_vals to check for nested plugin references
      Bolt::Util.walk_vals(toplevel_value) do |current_value|
        if current_value.is_a?(Hash) && current_value.key?('_plugin')
          raise invalid_input_data_err("the #{key} key's value contains a plugin reference")
        end
        current_value
      end
    end
  end
end

Instance Method Details

#hook_descriptionsObject



55
56
57
58
59
60
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 55

def hook_descriptions
  {
    resolve_reference: nil,
    validate_resolve_reference: nil
  }
end

#hooksObject



51
52
53
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 51

def hooks
  hook_descriptions.keys
end

#invalid_input_data_err(msg) ⇒ Object



79
80
81
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 79

def invalid_input_data_err(msg)
  Bolt::ValidationError.new("invalid input data #{@input_data_path}: #{msg}")
end

#nameObject



47
48
49
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 47

def name
  'puppet_connect_data'
end

#resolve_reference(opts) ⇒ Object



62
63
64
65
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 62

def resolve_reference(opts)
  key = opts['key']
  @data[key]
end

#validate_resolve_reference(opts) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bolt/plugin/puppet_connect_data.rb', line 67

def validate_resolve_reference(opts)
  unless opts['key']
    raise Bolt::ValidationError,
          "puppet_connect_data plugin requires that 'key' be specified"
  end
  if @input_data_path && !@data.key?(opts['key'])
    # Input data for Puppet Connect was provided and opts['key'] does not have a
    # value specified. Raise an error for this case.
    raise invalid_input_data_err("a value for the #{opts['key']} key is not specified")
  end
end