Class: Hieracles::Puppetdb::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(options, version = 3) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hieracles/puppetdb/client.rb', line 8

def initialize(options, version = 3)
  @version = version
  setup_if_ssl(options)
  options['port'] ||= 8080
  options['host'] ||= 'localhost'
  scheme = options['usessl'] ? "https://" : "http://"
  self.class.base_uri(scheme + 
    options['host'] +
    ':' + options['port'].to_s +
    '/v' + @version.to_s())
end

Instance Method Details

#get_request(endpoint, query, opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hieracles/puppetdb/client.rb', line 41

def get_request(endpoint, query, opts)
  path = "/" + endpoint
  if query
    json_query = JSON.dump(query)
    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
    #puts path ; puts filtered_opts ; exit(0)
    self.class.get(path, query: filtered_opts)
  else
    self.class.get(path)
  end
end

#request(endpoint, query = nil, method = 'get', opts = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hieracles/puppetdb/client.rb', line 27

def request(endpoint, query = nil, method = 'get', opts = {})
  ret = send("#{method}_request".to_sym, endpoint, query, opts)
  if ret.code.to_s() =~ /^[4|5]/ or ret.parsed_response.length < 1
    notifications = [ Hieracles::Notification.new('puppetdb', 'No match.', 'error') ]
    Response.new({}, 0, notifications)
  else
    total = ret.headers['X-Records']
    if total.nil?
      total = ret.parsed_response.length
    end
    Response.new(ret.parsed_response, total)
  end
end

#setup_if_ssl(options) ⇒ Object



20
21
22
23
24
25
# File 'lib/hieracles/puppetdb/client.rb', line 20

def setup_if_ssl(options)
  if options['usessl']
    self.class.default_options = {:options => options}
    self.class.connection_adapter(FixSSLConnectionAdapter)
  end
end