summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-05-02 14:31:54 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-05-02 15:34:19 -0700
commit86c6ec24f387fc70abc333fc4ac974b06b3ec80a (patch)
treef0ec5e0d43f115b16dab5ec2568a9369436c7425 /lib/puppet/indirector
parentc63e9c2394a30fe653908cd15967218d90fa34d6 (diff)
downloadpuppet-86c6ec24f387fc70abc333fc4ac974b06b3ec80a.tar.gz
puppet-86c6ec24f387fc70abc333fc4ac974b06b3ec80a.tar.xz
puppet-86c6ec24f387fc70abc333fc4ac974b06b3ec80a.zip
maint: move the indirector face base out of puppet/face
We used to shove the base class Puppet::Face::Indirector next to the actual faces; this made a bunch of things, including testing, confusing. Instead, move it away into the indirector where it lives with the rest of the indirector related things. Reviewed-By: Nick Lewis <nick@puppetlabs.com>
Diffstat (limited to 'lib/puppet/indirector')
-rw-r--r--lib/puppet/indirector/face.rb94
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/puppet/indirector/face.rb b/lib/puppet/indirector/face.rb
new file mode 100644
index 000000000..0fd44dfea
--- /dev/null
+++ b/lib/puppet/indirector/face.rb
@@ -0,0 +1,94 @@
+require 'puppet/face'
+
+class Puppet::Indirector::Face < Puppet::Face
+ option "--terminus TERMINUS" do
+ description %q{
+REVISIT: You can select a terminus, which has some bigger effect
+that we should describe in this file somehow.
+}.strip
+
+ before_action do |action, args, options|
+ set_terminus(options[:terminus])
+ end
+
+ after_action do |action, args, options|
+ indirection.reset_terminus_class
+ end
+ end
+
+ def self.indirections
+ Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort
+ end
+
+ def self.terminus_classes(indirection)
+ Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort
+ end
+
+ def call_indirection_method(method, key, options)
+ begin
+ result = indirection.__send__(method, key, options)
+ rescue => detail
+ puts detail.backtrace if Puppet[:trace]
+ raise "Could not call '#{method}' on '#{indirection_name}': #{detail}"
+ end
+
+ return result
+ end
+
+ action :destroy do
+ when_invoked { |key, options| call_indirection_method(:destroy, key, options) }
+ end
+
+ action :find do
+ when_invoked { |key, options| call_indirection_method(:find, key, options) }
+ end
+
+ action :save do
+ when_invoked { |key, options| call_indirection_method(:save, key, options) }
+ end
+
+ action :search do
+ when_invoked { |key, options| call_indirection_method(:search, key, options) }
+ end
+
+ # Print the configuration for the current terminus class
+ action :info do
+ when_invoked do |*args|
+ if t = indirection.terminus_class
+ puts "Run mode '#{Puppet.run_mode.name}': #{t}"
+ else
+ $stderr.puts "No default terminus class for run mode '#{Puppet.run_mode.name}'"
+ end
+ end
+ end
+
+ attr_accessor :from
+
+ def indirection_name
+ @indirection_name || name.to_sym
+ end
+
+ # Here's your opportunity to override the indirection name. By default it
+ # will be the same name as the face.
+ def set_indirection_name(name)
+ @indirection_name = name
+ end
+
+ # Return an indirection associated with a face, if one exists;
+ # One usually does.
+ def indirection
+ unless @indirection
+ @indirection = Puppet::Indirector::Indirection.instance(indirection_name)
+ @indirection or raise "Could not find terminus for #{indirection_name}"
+ end
+ @indirection
+ end
+
+ def set_terminus(from)
+ begin
+ indirection.terminus_class = from
+ rescue => detail
+ raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{self.class.terminus_classes(indirection.name).join(", ") }"
+ end
+ end
+end