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 Attribute Summary collapse
-
#puppetdb_client ⇒ Object
readonly
Returns the value of attribute puppetdb_client.
Instance Method Summary collapse
- #fact_path(raw_fact) ⇒ Object
- #hooks ⇒ Object
-
#initialize(config:, context:) ⇒ 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(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 = Logging.logger[self] end |
Instance Attribute Details
#puppetdb_client ⇒ Object (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 |
#hooks ⇒ Object
29 30 31 |
# File 'lib/bolt/plugin/puppetdb.rb', line 29 def hooks [:resolve_reference] end |
#name ⇒ Object
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 |
# File 'lib/bolt/plugin/puppetdb.rb', line 86 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
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) @logger.warn("Could not find fact #{fact} for node #{certname}") end |