summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/resource/reference.rb
blob: cf7e997d8705898d1cbc490f482086ca598db1f0 (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
# A reference to a resource.  Mostly just the type and title.
require 'puppet/resource_reference'
require 'puppet/file_collection/lookup'

# A reference to a resource.  Mostly just the type and title.
class Puppet::Parser::Resource::Reference < Puppet::Resource::Reference
    include Puppet::FileCollection::Lookup
    include Puppet::Util::MethodHelper
    include Puppet::Util::Errors

    attr_accessor :builtin, :file, :line, :scope

    # Are we a builtin type?
    def builtin?
        unless defined? @builtin
            if builtintype()
                @builtin = true
            else
                @builtin = false
            end
        end

        @builtin
    end

    def builtintype
        if t = Puppet::Type.type(self.type.downcase) and t.name != :component
            t
        else
            nil
        end
    end

    # Return the defined type for our obj.  This can return classes,
    # definitions or nodes.
    def definedtype
        unless defined? @definedtype
            case self.type
            when "Class" # look for host classes
                if self.title == :main
                    tmp = @scope.findclass("")
                else
                    unless tmp = @scope.parser.classes[self.title]
                        fail Puppet::ParseError, "Could not find class '%s'" % self.title
                    end
                end
            when "Node" # look for node definitions
                unless tmp = @scope.parser.nodes[self.title]
                    fail Puppet::ParseError, "Could not find node '%s'" % self.title
                end
            else # normal definitions
                # The resource type is capitalized, so we have to downcase.  Really,
                # we should have a better interface for finding these, but eh.
                tmp = @scope.parser.definitions[self.type.downcase]
            end

            if tmp
                @definedtype = tmp
            else
                fail Puppet::ParseError, "Could not find resource type '%s'" % self.type
            end
        end

        @definedtype
    end

    def initialize(hash)
        set_options(hash)
        requiredopts(:type, :title)
    end

    def to_ref
        # We have to return different cases to provide backward compatibility
        # from 0.24.x to 0.23.x.
        if builtin?
            return [type.to_s.downcase, title.to_s]
        else
            return [type.to_s, title.to_s]
        end
    end

    def typeclass
        unless defined? @typeclass
            if tmp = builtintype || definedtype
                @typeclass = tmp
            else
                fail Puppet::ParseError, "Could not find type %s" % self.type
            end
        end

        @typeclass
    end
end