summaryrefslogtreecommitdiffstats
path: root/lib/puppet/faces/indirector.rb
blob: 7e4e0f00fcbd69fa664384c78a67c42cfda7493c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'puppet'
require 'puppet/faces'

class Puppet::Faces::Indirector < Puppet::Faces
  option "--terminus TERMINUS" do
    desc "REVISIT: You can select a terminus, which has some bigger effect
that we should describe in this file somehow."
  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, *args)
    options = args.last
    options.has_key?(:terminus) and set_terminus(options[:terminus])

    begin
      result = indirection.__send__(method, *args)
    rescue => detail
      puts detail.backtrace if Puppet[:trace]
      raise "Could not call '#{method}' on '#{indirection_name}': #{detail}"
    end

    indirection.reset_terminus_class
    return result
  end

  action :destroy do
    when_invoked { |*args| call_indirection_method(:destroy, *args) }
  end

  action :find do
    when_invoked { |*args| call_indirection_method(:find, *args) }
  end

  action :save do
    when_invoked { |*args| call_indirection_method(:save, *args) }
  end

  action :search do
    when_invoked { |*args| call_indirection_method(:search, *args) }
  end

  # Print the configuration for the current terminus class
  action :info do
    when_invoked do |*args|
      options = args.pop
      options.has_key?(:terminus) and set_terminus(options[:terminus])

      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

      indirection.reset_terminus_class
    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