Class: Helpers::PuppetDB

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ PuppetDB

Returns a new instance of PuppetDB.



8
9
10
11
# File 'lib/helpers/puppetdb.rb', line 8

def initialize(host, port)
  @puppetdb_host = host
  @puppetdb_port = port
end

Instance Method Details

#get_facts(node = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/helpers/puppetdb.rb', line 29

def get_facts(node=nil)
  if node
    fact_endpoint = "http://#{@puppetdb_host}:#{@puppetdb_port}/v3/nodes/#{node}/facts"
  else
    fact_endpoint = "http://#{@puppetdb_host}:#{@puppetdb_port}/v3/facts"
  end
  
  uri = URI.parse(fact_endpoint)
  request = Net::HTTP::Get.new(uri.path)
  request.add_field('Accept', 'application/json')
  http_client = Net::HTTP.new(uri.host, uri.port)
  begin
    response = http_client.request(request)
    response['Content-Type'] = 'application/json'
    if response.code == '200'
      facts = JSON.parse(response.body)
    else
      facts = []
    end

    return facts
  rescue Timeout::Error

  end
end

#get_nodesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/helpers/puppetdb.rb', line 13

def get_nodes
  uri = URI.parse( "http://#{@puppetdb_host}:#{@puppetdb_port}/v3/nodes" )
  request = Net::HTTP::Get.new(uri.path)
  request.add_field('Accept', 'application/json')
  http_client = Net::HTTP.new(uri.host, uri.port)
  response = http_client.request(request)
  response['Content-Type'] = 'application/yaml'
  if response.code == '200'
    nodes = JSON.parse(response.body)
  else
    nodes = []
  end

  return nodes
end