lib/hieracles/puppetdb/request.rb in hieracles-0.2.2 vs lib/hieracles/puppetdb/request.rb in hieracles-0.3.0
- old
+ new
@@ -25,20 +25,57 @@
alias_method :node_fact, :node_facts
def node_resources(fqdn, *args)
resp = @client.request("nodes/#{fqdn}/resources")
field = args.shift
- resp.data = resp.data.reduce({}) do |a, d|
- if !field || Regexp.new(field).match(d['title'])
- a[d['title']] = d
+ make_hashed resp
+ end
+ alias_method :node_res, :node_resources
+
+ def facts(label, value)
+ resp = @client.request("facts/#{label}/#{value}")
+ extract_names resp
+ end
+
+ def same(name, fqdn)
+ r = @client.request("nodes/#{fqdn}/facts/#{name}")
+ if r.data.length > 0
+ value = r.data[0]['value']
+ facts name, value
+ else
+ r
+ end
+ end
+
+ def factnames()
+ @client.request("fact-names")
+ end
+
+ def resources(*args)
+ query = Query.new args
+ make_hashed @client.request("resources", query.elements)
+ end
+ alias_method :res, :resources
+
+ private
+
+ def extract_names(resp, label = 'certname')
+ resp.data = resp.data.reduce([]) do |a, d|
+ a << d[label]
+ a
+ end.sort
+ resp
+ end
+
+ def make_hashed(resp, label = 'title', filter = nil)
+ resp.data = resp.data.reduce({}) do |a, d|
+ if !filter || Regexp.new(filter).match(d[label])
+ a[d[label]] = d
end
a
end
resp
end
- alias_method :node_res, :node_resources
-
-
end
end
end