class PuppetDB::Client

Attributes

logger[W]
use_ssl[R]

Public Class Methods

new(settings = {}, query_api_version = 4, command_api_version = 1) click to toggle source
# File lib/puppetdb/client.rb, line 34
def initialize(settings = {}, query_api_version = 4, command_api_version = 1)
  config = Config.new(settings, load_files: true)
  @query_api_version = query_api_version
  @command_api_version = command_api_version

  server = config.server
  pem    = config['pem'] || {}
  token  = config.token

  scheme = URI.parse(server).scheme

  unless %w[http https].include? scheme
    error_msg = 'Configuration error: :server must specify a protocol of either http or https'
    raise error_msg
  end

  @use_ssl = scheme == 'https'
  if @use_ssl
    unless pem.empty? || hash_includes?(pem, 'key', 'cert', 'ca_file')
      error_msg = 'Configuration error: https:// specified with pem, but pem is incomplete. It requires cert, key, and ca_file.'
      raise error_msg
    end

    self.class.default_options = { pem: pem, cacert: config['cacert'] }
    self.class.headers('X-Authentication' => token) if token
    self.class.connection_adapter(FixSSLConnectionAdapter)
  end

  self.class.base_uri(server)
end

Public Instance Methods

command(command, payload, version) click to toggle source
# File lib/puppetdb/client.rb, line 102
def command(command, payload, version)
  path = "/pdb/cmd/v#{@command_api_version}"

  query = {
    'command' => command,
    'version' => version,
    'certname' => payload['certname']
  }

  debug("#{path} #{query} #{payload}")

  ret = self.class.post(
    path,
    query: query,
    body: payload.to_json,
    headers: {
      'Accept'       => 'application/json',
      'Content-Type' => 'application/json'
    }
  )
  raise_if_error(ret)

  Response.new(ret.parsed_response)
end
debug(msg) click to toggle source
# File lib/puppetdb/client.rb, line 30
def debug(msg)
  @logger.debug(msg) if @logger
end
hash_includes?(hash, *sought_keys) click to toggle source
# File lib/puppetdb/client.rb, line 25
def hash_includes?(hash, *sought_keys)
  sought_keys.each { |x| return false unless hash.include?(x) }
  true
end
raise_if_error(response) click to toggle source
# File lib/puppetdb/client.rb, line 65
def raise_if_error(response)
  raise UnauthorizedError, response if response.code == 401
  raise ForbiddenError, response if response.code == 403
  raise APIError, response if response.code.to_s =~ %r{^[4|5]}
end
request(endpoint, query, opts = {}) click to toggle source
# File lib/puppetdb/client.rb, line 71
def request(endpoint, query, opts = {})
  path = "/pdb/query/v#{@query_api_version}"
  if endpoint == ''
    # PQL
    json_query = query
  else
    path += "/#{endpoint}"
    query = PuppetDB::Query.maybe_promote(query)
    json_query = query.build
  end

  filtered_opts = { 'query' => json_query }
  opts.each do |k, v|
    if k == :counts_filter
      filtered_opts['counts-filter'] = JSON.dump(v)
    else
      filtered_opts[k.to_s.sub('_', '-')] = v
    end
  end

  debug("#{path} #{json_query} #{opts}")

  ret = self.class.get(path, body: filtered_opts)
  raise_if_error(ret)

  total = ret.headers['X-Records']
  total = ret.parsed_response.length if total.nil?

  Response.new(ret.parsed_response, total)
end