summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/indirection.rb
blob: 67b8a9d4855c8d49bdd6bd890d3c460c447a73f1 (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
# An actual indirection.
class Puppet::Indirector::Indirection
    @@indirections = []

    # Clear all cached termini from all indirections.
    def self.clear_cache
        @@indirections.each { |ind| ind.clear_cache }
    end
    
    attr_accessor :name, :termini
    attr_reader :to

    # Clear our cached list of termini.
    # This is only used for testing.
    def clear_cache
        @termini.clear
    end

    # This is only used for testing.
    def delete
        @@indirections.delete(self) if @@indirections.include?(self)
    end

    def initialize(name, options = {})
        @name = name
        options.each do |name, value|
            begin
                send(name.to_s + "=", value)
            rescue NoMethodError
                raise ArgumentError, "%s is not a valid Indirection parameter" % name
            end
        end
        @termini = {}
        @@indirections << self
    end

    # Return the singleton terminus for this indirection.
    def terminus(name = nil)
        # Get the name of the terminus.
        unless name
            unless param_name = self.to
                raise ArgumentError, "You must specify an indirection terminus for indirection %s" % self.name
            end
            name = Puppet[param_name]
            name = name.intern if name.is_a?(String)
        end
        
        unless @termini[name]
            @termini[name] = make_terminus(name)
        end
        @termini[name]
    end

    # Validate the parameter.  This requires that indirecting
    # classes require 'puppet/defaults', because of ordering issues,
    # but it makes problems much easier to debug.
    def to=(param_name)
        unless Puppet.config.valid?(param_name)
            raise ArgumentError, "Configuration parameter '%s' for indirection '%s' does not exist'" % [param_name, self.name]
        end
        @to = param_name
    end

    private

    # Create a new terminus instance.
    def make_terminus(name)
        # Load our terminus class.
        unless klass = Puppet::Indirector.terminus(self.name, name)
            raise ArgumentError, "Could not find terminus %s for indirection %s" % [name, self.name]
        end
        return klass.new
    end
end