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 _cache query target_mapping instance].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
- #hook_descriptions ⇒ 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 24 25 26 |
# File 'lib/bolt/plugin/puppetdb.rb', line 19 def initialize(config:, context:) @puppetdb_client = Bolt::PuppetDB::Client.new(default: config.delete('default'), instances: config.delete('instances') || {}, config: config, project: context.boltdir) @logger = Bolt::Logger.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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/bolt/plugin/puppetdb.rb', line 46 def fact_path(raw_fact) fact_path = raw_fact.split(".") fact_path = fact_path.map do |segment| # Turn it into an integer if we can Integer(segment) rescue ArgumentError # Otherwise return the value segment end 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 |
#hook_descriptions ⇒ Object
36 37 38 39 40 |
# File 'lib/bolt/plugin/puppetdb.rb', line 36 def hook_descriptions { resolve_reference: 'Query PuppetDB for a group of targets.' } end |
#hooks ⇒ Object
32 33 34 |
# File 'lib/bolt/plugin/puppetdb.rb', line 32 def hooks hook_descriptions.keys end |
#name ⇒ Object
28 29 30 |
# File 'lib/bolt/plugin/puppetdb.rb', line 28 def name 'puppetdb' end |
#resolve_facts(config, certname, target_data) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bolt/plugin/puppetdb.rb', line 103 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bolt/plugin/puppetdb.rb', line 64 def resolve_reference(opts) targets = @puppetdb_client.query_certnames(opts['query'], opts['instance']) 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. Don't gather certname since we already # have that and it's not a fact. if value.is_a?(String) && value != 'certname' facts << fact_path(value) end value end facts.uniq! # Returns {'mycertname' => [{'path' => ['nested', 'fact'], 'value' => val'}], ... } fact_values = @puppetdb_client.fact_values(targets, facts, opts['instance']) 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 |