Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for custom puppetdb queries as discovery options #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The PuppetDB discovery plugin can be activated either by specifying it in your c
or by using it on the cli

% mco rpc rpcutil ping --dm puppetdb -F operatingsytem=CentOS
% mco rpc rpcutil ping --dm puppetdb --do '["in", "certname", ["extract", "certname", ["select-resources", ["and", ["=", "type", "Apache::Vhost"], ["=", "title", "myvhost"]]]]]'


Other configuration settings that can be tuned depending our your PuppetDB installation are :

Expand Down Expand Up @@ -62,3 +64,9 @@ Connect to a remote PuppetDB server using Kerberos
plugin.discovery.puppetdb.ssl_cert = /etc/mcollective/puppetdb/host1.your.com.cert.pem
plugin.discovery.puppetdb.ssl_private_key = /etc/mcollective/puppetdb/host1.your.com.pem

###Example client implementations

Discover nodes whith a custom puppetdb query

mc.discovery_method = "puppetdb"
mc.discovery_options = ["in", "certname", ["extract", "certname", ["select-resources", ["and", ["=", "type", "Apache::Vhost"], ["=", "title", "myvhost"]]]]].inspect
3 changes: 2 additions & 1 deletion discovery/puppetdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Puppetdb

def self.discover(filter, timeout, limit = 0, client = nil)
require 'mcollective/util/puppetdb_discovery'
Util::PuppetdbDiscovery.new(Config.instance).discover(filter)
options = client.options[:discovery_options]
Util::PuppetdbDiscovery.new(Config.instance).discover(filter,options)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions util/puppetdb_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def initialize(config)
@http = create_http
end

def discover(filter)
def discover(filter,query=nil)
found = []

#if no filters are to be applied, we fetch all the nodes registered in puppetdb
if filter['fact'].empty? && filter['cf_class'].empty? && filter['identity'].empty?
found = node_search
found = node_search(query)
else
found << fact_search(filter['fact']) unless filter['fact'].empty?
found << class_search(filter['cf_class']) unless filter['cf_class'].empty?
Expand Down Expand Up @@ -131,9 +131,9 @@ def identity_search(filter)
hosts
end

# Looks up all the nodes registered in puppetdb without applying any filters
def node_search
JSON.parse(make_request('nodes', nil)).map { |node| node['name'] }
# Looks up all the nodes registered in puppetdb with an optional filter
def node_search(query=nil)
JSON.parse(make_request('nodes', query)).map { |node| node['name'] }
end

def make_request(endpoint, query)
Expand Down