commit fc2ea9572bce030cc3fa9939734b0dda054e6677 Author: Dan Bode Date: Thu Oct 20 11:57:45 2011 -0700 initial commit This commit just adds support for the ability to query classes, nodes, and groups out of the dashboard. diff --git a/lib/puppet/application/dashboard.rb b/lib/puppet/application/dashboard.rb new file mode 100644 index 0000000..481ec9b --- /dev/null +++ b/lib/puppet/application/dashboard.rb @@ -0,0 +1,5 @@ +require 'puppet/face' +require 'puppet/application/face_base' + +class Puppet::Application::Dashboard < Puppet::Application::FaceBase +end diff --git a/lib/puppet/dashboard/classifier.rb b/lib/puppet/dashboard/classifier.rb new file mode 100644 index 0000000..e68774a --- /dev/null +++ b/lib/puppet/dashboard/classifier.rb @@ -0,0 +1,36 @@ +require 'puppet' +require 'puppet/network/http_pool' +module Puppet::Dashboard + class Classifier + + def self.connection(options) + @connection ||= Puppet::Dashboard::Classifier.new(options, false) + end + + def initialize(options, use_ssl=false) + @http_connection = Puppet::Network::HttpPool.http_instance(options[:enc_server], options[:enc_port]) + # Workaround for the fact that Dashboard is typically insecure. + @http_connection.use_ssl = use_ssl + @headers = { 'Content-Type' => 'application/json' } + end + + # list expects a return of 200 + def list(type, action) + response = @http_connection.get("/#{type}.json", @headers ) + nodes = handle_json_response(response, action) + end + + def handle_json_response(response, action, expected_code='200') + if response.code == expected_code + Puppet.notice "#{action} ... Done" + PSON.parse response.body + else + # I should probably raise an exception! + Puppet.warning "#{action} ... Failed" + Puppet.info("Body: #{response.body}") + Puppet.warning "Server responded with a #{response.code} status" + raise Puppet::Error, "Could not: #{action}, got #{response.code} expected #{expected_code}" + end + end + end +end diff --git a/lib/puppet/face/dashboard.rb b/lib/puppet/face/dashboard.rb new file mode 100644 index 0000000..0ff7fe2 --- /dev/null +++ b/lib/puppet/face/dashboard.rb @@ -0,0 +1,37 @@ +require 'puppet/face' +require 'puppet/dashboard/classifier' +Puppet::Face.define(:dashboard, '0.0.1') do + option '--enc-server=' do + summary 'The External Node Classifier URL.' + description <<-EOT + The URL of the External Node Classifier. + This currently only supports the Dashboard + as an external node classifier. + EOT + default_to do + Puppet[:server] + end + end + + option '--enc-port=' do + summary 'The External Node Classifier Port' + description <<-EOT + The port of the External Node Classifier. + This currently only supports the Dashboard + as an external node classifier. + EOT + default_to do + 3000 + end + end + + action 'list' do + summary 'list all of a certain type from the dashboard, this currently supports the ability to list: classes, nodes, and groups' + when_invoked do |type, options| + type_map = {'classes' => 'node_classes', 'nodes' => 'nodes', 'groups' => 'node_groups'} + connection = Puppet::Dashboard::Classifier.connection(options) + connection.list(type_map[type], "Listing #{type}") + end + end +end +