~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to lib/puppet/parser/ast/resourcedef.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'puppet/parser/ast/branch'
2
 
 
3
 
# Any normal puppet object declaration.  Can result in a class or a 
4
 
# component, in addition to builtin types.
5
 
class Puppet::Parser::AST
6
 
class ResourceDef < AST::Branch
7
 
    attr_accessor :title, :type, :exported, :virtual
8
 
    attr_reader :params
9
 
 
10
 
    # probably not used at all
11
 
    def []=(index,obj)
12
 
        @params[index] = obj
13
 
    end
14
 
 
15
 
    # probably not used at all
16
 
    def [](index)
17
 
        return @params[index]
18
 
    end
19
 
 
20
 
    # Iterate across all of our children.
21
 
    def each
22
 
        [@type,@title,@params].flatten.each { |param|
23
 
            #Puppet.debug("yielding param %s" % param)
24
 
            yield param
25
 
        }
26
 
    end
27
 
 
28
 
    # Does not actually return an object; instead sets an object
29
 
    # in the current scope.
30
 
    def evaluate(hash)
31
 
        scope = hash[:scope]
32
 
        @scope = scope
33
 
        hash = {}
34
 
 
35
 
        # Get our type and name.
36
 
        objtype = @type
37
 
 
38
 
        # Disable definition inheritance, for now.  8/27/06, luke
39
 
        #if objtype == "super"
40
 
        #    objtype = supertype()
41
 
        #    @subtype = true
42
 
        #else
43
 
            @subtype = false
44
 
        #end
45
 
 
46
 
        # Evaluate all of the specified params.
47
 
        paramobjects = @params.collect { |param|
48
 
            param.safeevaluate(:scope => scope)
49
 
        }
50
 
 
51
 
        # Now collect info from our parent.
52
 
        parentname = nil
53
 
        if @subtype
54
 
            parentname = supersetup(hash)
55
 
        end
56
 
 
57
 
        objtitles = nil
58
 
        # Determine our name if we have one.
59
 
        if self.title
60
 
            objtitles = @title.safeevaluate(:scope => scope)
61
 
            # it's easier to always use an array, even for only one name
62
 
            unless objtitles.is_a?(Array)
63
 
                objtitles = [objtitles]
64
 
            end
65
 
        else
66
 
            if parentname
67
 
                objtitles = [parentname]
68
 
            else
69
 
                # See if they specified the name as a parameter instead of
70
 
                # as a normal name (i.e., before the colon).
71
 
                unless object # we're a builtin
72
 
                    if objclass = Puppet::Type.type(objtype)
73
 
                        namevar = objclass.namevar
74
 
 
75
 
                        tmp = hash["name"] || hash[namevar.to_s] 
76
 
 
77
 
                        if tmp
78
 
                            objtitles = [tmp]
79
 
                        end
80
 
                    else
81
 
                        # This isn't grammatically legal.
82
 
                        raise Puppet::ParseError, "Got a resource with no title"
83
 
                    end
84
 
                end
85
 
            end
86
 
        end
87
 
 
88
 
        # This is where our implicit iteration takes place; if someone
89
 
        # passed an array as the name, then we act just like the called us
90
 
        # many times.
91
 
        objtitles.collect { |objtitle|
92
 
            exceptwrap :type => Puppet::ParseError do
93
 
                obj = Puppet::Parser::Resource.new(
94
 
                    :type => objtype,
95
 
                    :title => objtitle,
96
 
                    :params => paramobjects,
97
 
                    :file => @file,
98
 
                    :line => @line,
99
 
                    :exported => self.exported || scope.exported,
100
 
                    :virtual => self.virtual,
101
 
                    :source => scope.source,
102
 
                    :scope => scope
103
 
                )
104
 
 
105
 
                # And then store the resource in the scope.
106
 
                # XXX At some point, we need to switch all of this to return
107
 
                # objects instead of storing them like this.
108
 
                scope.setresource(obj)
109
 
                obj
110
 
            end
111
 
        }.reject { |obj| obj.nil? }
112
 
    end
113
 
 
114
 
    # Create our ResourceDef.  Handles type checking for us.
115
 
    def initialize(hash)
116
 
        @checked = false
117
 
        super
118
 
 
119
 
        #self.typecheck(@type.value)
120
 
    end
121
 
 
122
 
    # Set the parameters for our object.
123
 
    def params=(params)
124
 
        if params.is_a?(AST::ASTArray)
125
 
            @params = params
126
 
        else
127
 
            @params = AST::ASTArray.new(
128
 
                :line => params.line,
129
 
                :file => params.file,
130
 
                :children => [params]
131
 
            )
132
 
        end
133
 
    end
134
 
 
135
 
    def supercomp
136
 
        unless defined? @supercomp
137
 
            if @scope and comp = @scope.inside
138
 
                @supercomp = comp
139
 
            else
140
 
                error = Puppet::ParseError.new(
141
 
                    "'super' is only valid within definitions"
142
 
                )
143
 
                error.line = self.line
144
 
                error.file = self.file
145
 
                raise error
146
 
            end
147
 
        end
148
 
        @supercomp
149
 
    end
150
 
 
151
 
    # Take all of the arguments of our parent and add them into our own,
152
 
    # without overriding anything.
153
 
    def supersetup(hash)
154
 
        comp = supercomp()
155
 
 
156
 
        # Now check each of the arguments from the parent.
157
 
        comp.arguments.each do |name, value|
158
 
            unless hash.has_key? name
159
 
                hash[name] = value
160
 
            end
161
 
        end
162
 
 
163
 
        # Return the parent name, so it can be used if appropriate.
164
 
        return comp.name
165
 
    end
166
 
 
167
 
    # Retrieve our supertype.
168
 
    def supertype
169
 
        unless defined? @supertype
170
 
            if parent = supercomp.parentclass
171
 
                @supertype = parent
172
 
            else
173
 
                error = Puppet::ParseError.new(
174
 
                    "%s does not have a parent class" % comp.type
175
 
                )
176
 
                error.line = self.line
177
 
                error.file = self.file
178
 
                raise error
179
 
            end
180
 
        end
181
 
        @supertype
182
 
    end
183
 
 
184
 
    # Print this object out.
185
 
    def tree(indent = 0)
186
 
        return [
187
 
            @type.tree(indent + 1),
188
 
            @title.tree(indent + 1),
189
 
            ((@@indline * indent) + self.typewrap(self.pin)),
190
 
            @params.collect { |param|
191
 
                begin
192
 
                    param.tree(indent + 1)
193
 
                rescue NoMethodError => detail
194
 
                    Puppet.err @params.inspect
195
 
                    error = Puppet::DevError.new(
196
 
                        "failed to tree a %s" % self.class
197
 
                    )
198
 
                    error.set_backtrace detail.backtrace
199
 
                    raise error
200
 
                end
201
 
            }.join("\n")
202
 
        ].join("\n")
203
 
    end
204
 
 
205
 
    def to_s
206
 
        return "%s => { %s }" % [@title,
207
 
            @params.collect { |param|
208
 
                param.to_s
209
 
            }.join("\n")
210
 
        ]
211
 
    end
212
 
end
213
 
end
214
 
 
215
 
# $Id: resourcedef.rb 1726 2006-10-04 18:24:24Z luke $