Method: Bolt::PuppetDB::Client#send_command

Defined in:
lib/bolt/puppetdb/client.rb

#send_command(command, version, payload) ⇒ Object

Sends a command to PuppetDB using version 1 of the commands API. puppet.com/docs/puppetdb/latest/api/command/v1/commands.html

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.

Returns:

  • A UUID identifying the submitted command.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/bolt/puppetdb/client.rb', line 106

def send_command(command, version, payload)
  command = command.dup.force_encoding('utf-8')
  body    = JSON.generate(payload)

  # PDB requires the following query parameters to the POST request.
  # Error early if there's no certname, as PDB does not return a
  # message indicating it's required.
  unless payload['certname']
    raise Bolt::Error.new(
      "Payload must include 'certname', unable to invoke command.",
      'bolt/pdb-command'
    )
  end

  url = uri.tap do |u|
    u.path         = 'pdb/cmd/v1'
    u.query_values = { 'command'  => command,
                       'version'  => version,
                       'certname' => payload['certname'] }
  end

  # Send the command to PDB
  begin
    @logger.debug("Sending PuppetDB command '#{command}' to #{url}")
    response = http_client.post(url.to_s, body: body, header: headers)
  rescue StandardError => e
    raise Bolt::PuppetDBFailoverError, "Failed to invoke PuppetDB command: #{e}"
  end

  @logger.debug("Got response code #{response.code} from PuppetDB")
  if response.code != 200
    raise Bolt::PuppetDBError, "Failed to invoke PuppetDB command: #{response.body}"
  end

  # Return the UUID string from the response body
  begin
    JSON.parse(response.body).fetch('uuid', nil)
  rescue JSON::ParserError
    raise Bolt::PuppetDBError, "Unable to parse response as JSON: #{response.body}"
  end
rescue Bolt::PuppetDBFailoverError => e
  @logger.error("Request to puppetdb at #{@current_url} failed with #{e}.")
  reject_url
  send_command(command, version, payload)
end