Module: Puppet::Util::Puppetdb

Included in:
Node::Facts::Puppetdb, Node::Puppetdb, Resource::Catalog::Puppetdb, Resource::Puppetdb, Command
Defined in:
lib/puppet/util/puppetdb/char_encoding.rb,
lib/puppet/util/puppetdb/command_names.rb,
lib/puppet/util/puppetdb/config.rb,
lib/puppet/util/puppetdb/http.rb,
lib/puppet/util/puppetdb/atom.rb,
lib/puppet/util/puppetdb.rb

Defined Under Namespace

Modules: CharEncoding, CommandNames Classes: Atom, Command, CommandSubmissionError, Config, Http, InventorySearchError, NotFoundError, SoftWriteFailError

Public instance methods collapse

Private instance methods collapse

Class Method Summary collapse

Class Method Details

.configObject



27
28
29
30
# File 'lib/puppet/util/puppetdb.rb', line 27

def self.config
  @config ||= Puppet::Util::Puppetdb::Config.load
  @config
end

.log_x_deprecation_header(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



113
114
115
116
117
# 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) ⇒ Array<Hash>

Query PuppetDB.

Parameters:

  • query (String, Array)

    The PQL or AST query for PuppetDB

Returns:

  • (Array<Hash>)


72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object

Convert a value (usually a string) to a boolean



42
43
44
45
46
47
48
49
# 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) ⇒ Object

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.



34
35
36
37
38
39
# 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

Instance Method Details

#configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
# File 'lib/puppet/util/puppetdb.rb', line 108

def config
  Puppet::Util::Puppetdb.config
end

#profile(message, metric_id, &block) ⇒ Object

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.

Parameters:

  • message (String)

    A description of the profiled event

  • metric_id (Array)

    A list of strings making up the ID of a metric to profile

  • block (Block)

    The segment of code to profile



94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Hash <String, String>

Submit a command to PuppetDB.

Parameters:

  • certname (String)

    The certname this command operates on

  • payload (String)

    payload

  • command_name (String)

    name of command

  • version (Number)

    version number of command

Returns:

  • (Hash <String, String>)


60
61
62
63
64
65
66
# 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