Class: Bolt::Plugin::Puppetdb
- Inherits:
-
Object
- Object
- Bolt::Plugin::Puppetdb
- 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 query target_mapping].freeze
Instance Method Summary collapse
- #fact_path(raw_fact) ⇒ Object
- #hooks ⇒ Object
-
#initialize(pdb_client) ⇒ Puppetdb
constructor
A new instance of Puppetdb.
- #name ⇒ Object
- #resolve_facts(config, certname, target_data) ⇒ Object
- #resolve_reference(opts) ⇒ Object
- #warn_missing_fact(certname, fact) ⇒ Object
Constructor Details
#initialize(pdb_client) ⇒ Puppetdb
Returns a new instance of Puppetdb.
17 18 19 20 |
# File 'lib/bolt/plugin/puppetdb.rb', line 17 def initialize(pdb_client) @puppetdb_client = pdb_client @logger = Logging.logger[self] end |
Instance Method Details
#fact_path(raw_fact) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bolt/plugin/puppetdb.rb', line 34 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 |
#hooks ⇒ Object
26 27 28 |
# File 'lib/bolt/plugin/puppetdb.rb', line 26 def hooks [:resolve_reference] end |
#name ⇒ Object
22 23 24 |
# File 'lib/bolt/plugin/puppetdb.rb', line 22 def name 'puppetdb' end |
#resolve_facts(config, certname, target_data) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bolt/plugin/puppetdb.rb', line 83 def resolve_facts(config, certname, target_data) Bolt::Util.walk_vals(config) do |value| if value.is_a?(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 elsif value.is_a?(Array) || value.is_a?(Hash) value else raise FactLookupError.new(value, "fact lookups must be a string") end end end |
#resolve_reference(opts) ⇒ Object
45 46 47 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 |
# File 'lib/bolt/plugin/puppetdb.rb', line 45 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
30 31 32 |
# File 'lib/bolt/plugin/puppetdb.rb', line 30 def warn_missing_fact(certname, fact) @logger.warn("Could not find fact #{fact} for node #{certname}") end |