Class: Bolt::PuppetDB::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(config:, instances: {}, default: nil, project: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • config (Hash)

    A map of default PuppetDB configuration.

  • instances (Hash) (defaults to: {})

    A map of configuration for named PuppetDB instances.

  • default (String) (defaults to: nil)

    The name of PuppetDB instance to use as the default.

  • project (String) (defaults to: nil)

    The path to the Bolt project.



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.

Parameters:

  • certnames (Array) (defaults to: [])

    The list of certnames to retrieve fact values for.

  • facts (Array) (defaults to: [])

    The list of facts to retrive.

  • instance (String) (defaults to: nil)

    The name of the PuppetDB instance.



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.

Parameters:

  • certnames (Array)

    The list of certnames to retrieve facts for.

  • instance (String) (defaults to: nil)

    The name of the PuppetDB instance.



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.

Parameters:

  • name (String) (defaults to: nil)

    The name of the PuppetDB instance.

Returns:



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.

Parameters:

  • query (String)

    The query to send to PuppetDB.

  • path (String) (defaults to: nil)

    The API path to append to the query URL.

  • instance (String) (defaults to: nil)

    The name of the PuppetDB instance.



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.

Parameters:

  • query (String)

    The PDB query.

  • instance (String) (defaults to: nil)

    The name of 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.

Parameters:

  • command (String)

    The command to invoke.

  • version (Integer)

    The version of the command to invoke.

  • payload (Hash)

    The payload to send with the command.

  • instance (String) (defaults to: nil)

    The name of the PuppetDB instance.



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