Class: PuppetDB::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/puppetdb/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}, query_api_version = 4, command_api_version = 1) ⇒ Client

Returns a new instance of Client.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#logger=(value) ⇒ Object (writeonly)

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



23
24
25
# File 'lib/puppetdb/client.rb', line 23

def logger=(value)
  @logger = value
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



22
23
24
# File 'lib/puppetdb/client.rb', line 22

def use_ssl
  @use_ssl
end

Instance Method Details

#command(command, payload, version) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



30
31
32
# File 'lib/puppetdb/client.rb', line 30

def debug(msg)
  @logger.debug(msg) if @logger
end

#hash_includes?(hash, *sought_keys) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# 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) ⇒ Object

Raises:



65
66
67
68
69
# 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 = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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