Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #14 from nanliu/tb/poolmember

Improvements to puppetdb query
  • Loading branch information...
commit 51f13d0e6c806c6dd7bfb7ebb28496521ca0bf6f 2 parents d80abb3 + ae93660
@ripienaar authored
View
23 lib/puppet/parser/functions/query_facts.rb
@@ -0,0 +1,23 @@
+Puppet::Parser::Functions.newfunction(:query_facts, :type => :rvalue, :doc => <<-EOT
+
+ accepts two arguments, a query used to discover nodes, and a filter used to
+ specify the facts that should be returned from those hosts.
+
+ The query specified should conform to the following format:
+ (Type[title] and fact_name<operator>fact_value) or ...
+ Package[mysql-server] and cluster_id=my_first_cluster
+
+ The filter provided should be a comma delimited list of fact names.
+
+ The result is a hash that maps the name of the nodes to a hash of facts that
+ contains the filters specified.
+
+EOT
+) do |args|
+ filter, query = args
+ query ||= ''
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'puppetdb'))
+ # check that the filter is only one resource
+ raise(Puppet::Error, 'Must specify at least one argument (fact to query for)') unless filter
+ result = PuppetDB.new.query_facts(:query => query, :filter => filter)
+end
View
4 lib/puppet/parser/functions/query_nodes.rb
@@ -11,8 +11,8 @@
EOT
) do |args|
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'puppetdb'))
query, filter = args
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'puppetdb'))
raise(Puppet::Error, 'Query is a required parameter') unless query
- PuppetDB.new.query_nodes(:query => query, :filter => filter, :only_active => true)
+ PuppetDB.new.query_nodes(:query => query, :filter => filter)
end
View
28 lib/puppetX/puppetdb_query.rb
@@ -4,18 +4,38 @@
#
module PuppetX
class Puppetdb_query
+ def self.setting
+ begin
+ require 'puppet'
+ require 'puppet/util/puppetdb'
+ host = Puppet::Util::Puppetdb.server || 'localhost'
+ port = 8080
+ #port = Puppet::Util::Puppetdb.port || 8080
+ rescue Exception => e
+ Puppet.debug(e.message)
+ host = 'localhost'
+ port = 8080
+ end
+
+ Puppet.debug(host)
+ Puppet.debug(port)
+
+ { :host => host,
+ :port => port }
+ end
def self.add_puppetdb_host_option(action)
action.option '--puppetdb_host PUPPETDB' do
summary "Host running PuppetDB. "
- default_to { 'localhost' }
+
+ default_to { PuppetX::Puppetdb_query.setting[:host] }
end
end
def self.add_puppetdb_port_option(action)
action.option '--puppetdb_port PORT' do
summary 'Port PuppetDB is running on'
- default_to { 8080 }
+ default_to { PuppetX::Puppetdb_query.setting[:port] }
end
end
@@ -42,8 +62,8 @@ def self.add_only_active_option(action)
# set all of the common defaults to the options
def self.set_common_defaults(options)
{
- :puppetdb_host => 'localhost',
- :puppetdb_port => '8080',
+ :puppetdb_host => PuppetX::Puppetdb_query.setting[:host],
+ :puppetdb_port => PuppetX::Puppetdb_query.setting[:port],
:query => ''
}.merge(options)
end
View
28 lib/puppetdb.rb
@@ -5,7 +5,6 @@ class PuppetDB
require File.join(libdir, 'puppetdb/util')
require File.join(libdir, 'puppetX/puppetdb_query')
require File.join(libdir, 'puppetdb/matcher')
- require 'json'
require 'net/http'
require 'net/https'
@@ -174,7 +173,7 @@ def parse_statement(statement)
statement = statement.to_a.flatten.last
if statement =~ /^([\w:]+)\[(.+)\]$/
- resource_type = $1.capitalize
+ resource_type = $1
resource_name = $2
if resource_name.start_with?('"') or resource_name.start_with?("'")
@@ -182,6 +181,7 @@ def parse_statement(statement)
end
# in puppetdb class names are all capitalized but resource named arent
+ resource_type = resource_type.split("::").map{|c| c.capitalize}.join("::")
resource_name = resource_name.split("::").map{|c| c.capitalize}.join("::") if resource_type == "Class"
return {"resources" => ["and", ["=", "type", resource_type], ["=", "title", resource_name]]}
@@ -191,34 +191,32 @@ def parse_statement(statement)
end
def query_puppetdb(host, port, query)
+ require 'puppet/network/http_pool'
http = Net::HTTP.new(host, port)
#http.use_ssl = true
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- if query == :empty
-
- resp, data = http.get("/nodes", {"accept" => "application/json"})
- return JSON.parse(data)
+ headers = { "Accept" => "application/json" }
+ if query == :empty
+ resp, data = http.get("/nodes", headers)
+ return PSON.parse(data)
else
+ type = query.keys.first
- type = query.keys.first
- headers = {"accept" => "application/json"}
-
- puts 'querying db'
case type
when "resources"
- query = "/resources?query=%s" % URI.escape(query[type].to_json)
+ query = "/resources?query=%s" % URI.escape(query[type].to_pson)
resp, data = http.get(query, headers)
- return JSON.parse(data)
+ return PSON.parse(data)
when "nodes"
- query = "/nodes?query=%s" % URI.escape(query[type].to_json)
+ query = "/nodes?query=%s" % URI.escape(query[type].to_pson)
resp, data = http.get(query, headers)
- return JSON.parse(data)
+ return PSON.parse(data)
when "facts"
query = "/facts/#{query[type]}"
resp, data = http.get(query, headers)
- return JSON.parse(data)
+ return PSON.parse(data)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.