module Puppet::Util::Puppetdb

Public Class Methods

config() click to toggle source
# File lib/puppet/util/puppetdb.rb, line 27
def self.config
  @config ||= Puppet::Util::Puppetdb::Config.load
  @config
end
log_x_deprecation_header(response) click to toggle source

@api private

# File lib/puppet/util/puppetdb.rb, line 113
def log_x_deprecation_header(response)
  if warning = response['x-deprecation']
    Puppet.deprecation_warning "Deprecation from PuppetDB: #{warning}"
  end
end
query_puppetdb(query) click to toggle source

Query PuppetDB.

@param query [String, Array] The PQL or AST query for PuppetDB @return [Array<Hash>]

# File lib/puppet/util/puppetdb.rb, line 72
def self.query_puppetdb(query)
  Puppet::Util::Profiler.profile("Submitted query '#{query}'", [:puppetdb, :query, query]) do
    headers = { "Accept" => "application/json",
                "Content-Type" => "application/json; charset=UTF-8" }
    response = Puppet::Util::Puppetdb::Http.action("/pdb/query/v4", :query) do |http_instance, path|
      http_instance.post(path, { 'query' => query }.to_json, headers)
    end
    JSON.parse(response.body)
  end
end
to_bool(value) click to toggle source

Convert a value (usually a string) to a boolean

# File lib/puppet/util/puppetdb.rb, line 42
def self.to_bool(value)
  case value
  when true, "true"; return true
  when false, "false"; return false
  else
    raise ArgumentError.new("invalid value for Boolean: \"#{val}\"")
  end
end
to_wire_time(time) click to toggle source

Given an instance of ruby's Time class, this method converts it to a String that conforms to PuppetDB's wire format for representing a date/time.

# File lib/puppet/util/puppetdb.rb, line 34
def self.to_wire_time(time)
  # The current implementation simply calls iso8601, but having this method
  # allows us to change that in the future if needed w/o being forced to
  # update all of the date objects elsewhere in the code.
  time.iso8601(9)
end

Public Instance Methods

config() click to toggle source

@api private

# File lib/puppet/util/puppetdb.rb, line 108
def config
  Puppet::Util::Puppetdb.config
end
profile(message, metric_id, &block) click to toggle source

Profile a block of code and log the time it took to execute.

This outputs logs entries to the Puppet masters logging destination providing the time it took, a message describing the profiled code and a leaf location marking where the profile method was called in the profiled hierachy.

@param message [String] A description of the profiled event @param metric_id [Array] A list of strings making up the ID of a metric to profile @param block [Block] The segment of code to profile @api public

# File lib/puppet/util/puppetdb.rb, line 94
def profile(message, metric_id, &block)
  message = "PuppetDB: " + message
  arity = Puppet::Util::Profiler.method(:profile).arity
  case arity
  when 1
    Puppet::Util::Profiler.profile(message, &block)
  when 2, -2
    Puppet::Util::Profiler.profile(message, metric_id, &block)
  end
end
submit_command(certname, payload, command_name, version) click to toggle source

Submit a command to PuppetDB.

@param certname [String] The certname this command operates on @param payload [String] payload @param command_name [String] name of command @param version [Number] version number of command @return [Hash <String, String>]

# File lib/puppet/util/puppetdb.rb, line 60
def submit_command(certname, payload, command_name, version)
  profile("Submitted command '#{command_name}' version '#{version}'",
          [:puppetdb, :command, :submit, command_name, version]) do
    command = Puppet::Util::Puppetdb::Command.new(command_name, version, certname, payload)
    command.submit
  end
end

Private Instance Methods

log_x_deprecation_header(response) click to toggle source

@api private

# File lib/puppet/util/puppetdb.rb, line 113
def log_x_deprecation_header(response)
  if warning = response['x-deprecation']
    Puppet.deprecation_warning "Deprecation from PuppetDB: #{warning}"
  end
end