Class: Bolt::Plugin::Puppetdb

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

Defined Under Namespace

Classes: FactLookupError

Constant Summary collapse

TEMPLATE_OPTS =
%w[alias config facts features name uri vars].freeze
PLUGIN_OPTS =
%w[_plugin _cache query target_mapping].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, context:) ⇒ Puppetdb

Returns a new instance of Puppetdb.



19
20
21
22
23
# File 'lib/bolt/plugin/puppetdb.rb', line 19

def initialize(config:, context:)
  pdb_config = Bolt::PuppetDB::Config.load_config(config, context.boltdir)
  @puppetdb_client = Bolt::PuppetDB::Client.new(pdb_config)
  @logger = Bolt::Logger.logger(self)
end

Instance Attribute Details

#puppetdb_clientObject (readonly)

Returns the value of attribute puppetdb_client.



17
18
19
# File 'lib/bolt/plugin/puppetdb.rb', line 17

def puppetdb_client
  @puppetdb_client
end

Instance Method Details

#fact_path(raw_fact) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/bolt/plugin/puppetdb.rb', line 37

def fact_path(raw_fact)
  fact_path = raw_fact.split(".")
  if fact_path[0] == 'facts'
    fact_path.drop(1)
  elsif fact_path == ['certname']
    fact_path
  else
    raise FactLookupError.new(raw_fact, "fact lookups must start with 'facts.'")
  end
end

#hooksObject



29
30
31
# File 'lib/bolt/plugin/puppetdb.rb', line 29

def hooks
  [:resolve_reference]
end

#nameObject



25
26
27
# File 'lib/bolt/plugin/puppetdb.rb', line 25

def name
  'puppetdb'
end

#resolve_facts(config, certname, target_data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bolt/plugin/puppetdb.rb', line 86

def resolve_facts(config, certname, target_data)
  Bolt::Util.walk_vals(config) do |value|
    case value
    when String
      if value == 'certname'
        certname
      else
        data = target_data&.detect { |d| d['path'] == fact_path(value) }
        warn_missing_fact(certname, value) if data.nil?
        # If there's no fact data this will be nil
        data&.fetch('value', nil)
      end
    when Array, Hash
      value
    else
      raise FactLookupError.new(value, "fact lookups must be a string")
    end
  end
end

#resolve_reference(opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bolt/plugin/puppetdb.rb', line 48

def resolve_reference(opts)
  targets = @puppetdb_client.query_certnames(opts['query'])
  facts = []

  template = opts.delete('target_mapping') || {}

  keys = Set.new(TEMPLATE_OPTS) & opts.keys
  unless keys.empty?
    raise Bolt::ValidationError, "PuppetDB plugin expects keys #{keys.to_a} to be set under 'target_mapping'"
  end

  keys = Set.new(opts.keys) - PLUGIN_OPTS
  unless keys.empty?
    raise Bolt::ValidationError, "Unknown keys in PuppetDB plugin: #{keys.to_a}"
  end

  Bolt::Util.walk_vals(template) do |value|
    # This is done in parts instead of in place so that we only need to
    # make one puppetDB query
    if value.is_a?(String)
      facts << fact_path(value)
    end
    value
  end

  facts.uniq!
  # Returns {'mycertname' => [{'path' => ['nested', 'fact'], 'value' => val'}], ... }
  fact_values = @puppetdb_client.fact_values(targets, facts)

  targets.map do |certname|
    target_data = fact_values[certname]
    target = resolve_facts(template, certname, target_data) || {}
    target['uri'] = certname unless target['uri'] || target['name']

    target
  end
end

#warn_missing_fact(certname, fact) ⇒ Object



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

def warn_missing_fact(certname, fact)
  Bolt::Logger.warn("puppetdb_missing_fact", "Could not find fact #{fact} for node #{certname}")
end