Class: Bolt::PuppetDB::Client
- Inherits:
-
Object
- Object
- Bolt::PuppetDB::Client
- Defined in:
- lib/bolt/puppetdb/client.rb
Instance Method Summary collapse
-
#fact_values(certnames = [], facts = [], instance = nil) ⇒ Object
Retrive fact values for a list of nodes.
-
#facts_for_node(certnames, instance = nil) ⇒ Object
Retrieve facts from PuppetDB for a list of nodes.
-
#initialize(config:, instances: {}, default: nil, project: nil) ⇒ Client
constructor
A new instance of Client.
-
#instance(name = nil) ⇒ Bolt::PuppetDB::Instance
Selects the PuppetDB instance to connect to.
-
#make_query(query, path = nil, instance = nil) ⇒ Object
Sends a query to PuppetDB.
-
#query_certnames(query, instance = nil) ⇒ Object
Queries certnames from the PuppetDB instance.
-
#send_command(command, version, payload, instance = nil) ⇒ Object
Sends a command to PuppetDB using the commands API.
Constructor Details
#initialize(config:, instances: {}, default: nil, project: nil) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bolt/puppetdb/client.rb', line 15 def initialize(config:, instances: {}, default: nil, project: nil) @logger = Bolt::Logger.logger(self) @instances = instances.transform_values do |instance_config| Bolt::PuppetDB::Instance.new(config: instance_config, project: project) end @default_instance = if default validate_instance(default) @instances[default] else Bolt::PuppetDB::Instance.new(config: config, project: project, load_defaults: true) end end |
Instance Method Details
#fact_values(certnames = [], facts = [], instance = nil) ⇒ Object
Retrive fact values for a list of nodes.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bolt/puppetdb/client.rb', line 109 def fact_values(certnames = [], facts = [], instance = nil) return {} if certnames.empty? || facts.empty? certnames.uniq! name_query = certnames.each_slice(100).map { |slice| ["in", "certname", ["array", slice]] } name_query.unshift("or") facts_query = facts.map { |f| ["=", "path", f] } facts_query.unshift("or") query = ['and', name_query, facts_query] @logger.debug("Querying certnames") result = make_query(query, 'fact-contents', instance) result.map! { |h| h.delete_if { |k, _v| %w[environment name].include?(k) } } result.group_by { |c| c['certname'] } end |
#facts_for_node(certnames, instance = nil) ⇒ Object
Retrieve facts from PuppetDB for a list of nodes.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bolt/puppetdb/client.rb', line 88 def facts_for_node(certnames, instance = nil) return {} if certnames.empty? || certnames.nil? certnames.uniq! name_query = certnames.map { |c| ["=", "certname", c] } name_query.insert(0, "or") @logger.debug("Querying certnames") result = make_query(name_query, 'inventory', instance) result&.each_with_object({}) do |node, coll| coll[node['certname']] = node['facts'] end end |
#instance(name = nil) ⇒ Bolt::PuppetDB::Instance
Selects the PuppetDB instance to connect to. If an instance is not specified, the default instance is used.
55 56 57 58 59 60 61 62 |
# File 'lib/bolt/puppetdb/client.rb', line 55 def instance(name = nil) if name validate_instance(name) @instances[name] else @default_instance end end |
#make_query(query, path = nil, instance = nil) ⇒ Object
Sends a query to PuppetDB.
146 147 148 149 150 |
# File 'lib/bolt/puppetdb/client.rb', line 146 def make_query(query, path = nil, instance = nil) with_instance(instance) do |pdb| pdb.make_query(query, path) end end |
#query_certnames(query, instance = nil) ⇒ Object
Queries certnames from the PuppetDB instance.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bolt/puppetdb/client.rb', line 69 def query_certnames(query, instance = nil) return [] unless query @logger.debug("Querying certnames") results = make_query(query, nil, instance) if results&.first && !results.first&.key?('certname') fields = results.first&.keys raise Bolt::PuppetDBError, "Query results did not contain a 'certname' field: got #{fields.join(', ')}" end results&.map { |result| result['certname'] }&.uniq end |
#send_command(command, version, payload, instance = nil) ⇒ Object
Sends a command to PuppetDB using the commands API.
134 135 136 137 138 |
# File 'lib/bolt/puppetdb/client.rb', line 134 def send_command(command, version, payload, instance = nil) with_instance(instance) do |pdb| pdb.send_command(command, version, payload) end end |