Class: PuppetDB::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetdb/model/base.rb

Direct Known Subclasses

Fact, Inventory, Node, Resource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Parses objects from PuppetDB

The parser expects an array of hashes where each item in the array is parsed into a separate object. The keys of the hashes are turned into getters of the resulting object returning the associated values.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/puppetdb/model/base.rb', line 9

def initialize(attributes)
  attributes.each do |attribute_name, attribute_value|
    # this will create an `attr_reader` for each attribute passed
    self.class.send(:define_method, attribute_name.to_sym) do
      instance_variable_get("@#{attribute_name}")
    end

    # set an instance variable for each attribute like `@certname`
    instance_variable_set("@#{attribute_name}", attribute_value)
  end
end

Class Method Details

.clientPuppetDB::Client

Return the PuppetDB Client

Returns:

  • (PuppetDB::Client)

    instance



33
34
35
# File 'lib/puppetdb/model/base.rb', line 33

def self.client
  @@client
end

.client=(client) ⇒ Object

Saves the PuppetDB client into a class variable which is shared among all object created from this class

Parameters:

  • client (PuppetDB::Client)


24
25
26
27
28
# File 'lib/puppetdb/model/base.rb', line 24

def self.client=(client)
  # rubocop:disable Style/ClassVars
  @@client = client
  # rubocop:enable Style/ClassVars
end

.get(object:, regexp: false, **filters) ⇒ Object

Get object from Puppet DB

Pass a list of key: val arguments as filters for the query.

Examples:

Get exported Resources

PuppetDB::Model::Base.get(object: 'resources', exported: true)

Regexp search Resources by title

PuppetDB::Model::Resource.get(object: 'resources', title: '^foo.*bar', regexp: true)

Parameters:

  • regexp (Boolean) (defaults to: false)

    Set this to true for regular expression matching

  • object (String)

    The name of the object from The PuppetDB



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppetdb/model/base.rb', line 59

def self.get(object:, regexp: false, **filters)
  # use strict comparator by default, because this is less performance intensive
  comparator = regexp ? '~' : '='

  filters = filters.map do |key, value|
    # Boolean values doesn't support regexp operator(~) and must not be escaped
    if [TrueClass, FalseClass].include?(value.class)
      "#{key} = #{value}"
    else
      "#{key} #{comparator} #{value.to_s.dump}"
    end
  end

  query("#{object} {#{filters.join(' and ')}}")
end

.query(query) ⇒ Object

Query the Puppet DB an build new Objects from the result

Parameters:

  • query (String)

    The PQL query to execute



40
41
42
# File 'lib/puppetdb/model/base.rb', line 40

def self.query(query)
  request(query).map { |object| new(object) }
end

.request(query) ⇒ Object



44
45
46
# File 'lib/puppetdb/model/base.rb', line 44

def self.request(query)
  client.request('', query).data
end