Module: Jerakia::Scope::Puppetdb

Defined in:
lib/jerakia/scope/puppetdb.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Raises:


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jerakia/scope/puppetdb.rb', line 4

def create
  yaml_file = request.scope_options['file'] || './jerakia_scope.yaml'
  puppetdb_host = request.scope_options['puppetdb_host'] || 'localhost'
  puppetdb_port = request.scope_options['puppetdb_port'] || 8080
  puppetdb_api  = request.scope_options['puppetdb_api'] || 4
  node = request.scope_options['node']

  raise Jerakia::Error, "Must pass the option node to the puppetdb scope handler" unless node

  connection_opts = {
    :host => puppetdb_host,
    :port => puppetdb_port,
    :output => 'json',
    :ignore_404 => true
  }.merge(request.scope_options['puppetdb_http_opts'] || {})

  puppetdb_con = LookupHttp.new(connection_opts)
  
  case puppetdb_api
  when 4
    path = "/pdb/query/v4/nodes/#{node}/facts"
  else
    raise Jerakia::Error, "Unsupported PuppetDB API version, #{puppetdb_api}"
  end

  Jerakia.log.debug("Sending HTTP query to PuppetDB #{puppetdb_host}:#{puppetdb_port} at path #{path}")

  response = puppetdb_con.get_parsed(path)

  raise Jerakia::Error, "PuppetDB returned no data for node #{node}" unless response.is_a?(Array)

  response.each { |r| value[r['name'].to_sym] =  r['value'] }
end