Class Puppet::Parser::Resource
In: lib/puppet/parser/resource.rb
Parent: Object

A resource that we‘re managing. This handles making sure that only subclasses can set parameters.

Methods

Included Modules

Puppet::Util Puppet::Util::MethodHelper Puppet::Util::Errors Puppet::Util::Logging Puppet::Util::Tagging

Attributes

evaluated  [R] 
exported  [R] 
file  [RW] 
line  [RW] 
override  [RW] 
params  [R] 
rails_id  [RW] 
scope  [RW] 
source  [RW] 
translated  [RW] 
virtual  [RW] 

Public Class methods

[Source]

     # File lib/puppet/parser/resource.rb, line 98
 98:     def initialize(options)
 99:         # Set all of the options we can.
100:         options.each do |option, value|
101:             if respond_to?(option.to_s + "=")
102:                 send(option.to_s + "=", value)
103:                 options.delete(option)
104:             end
105:         end
106: 
107:         unless self.scope
108:             raise ArgumentError, "Resources require a scope"
109:         end
110:         @source ||= scope.source
111: 
112:         options = symbolize_options(options)
113: 
114:         # Set up our reference.
115:         if type = options[:type] and title = options[:title]
116:             options.delete(:type)
117:             options.delete(:title)
118:         else
119:             raise ArgumentError, "Resources require a type and title"
120:         end
121: 
122:         @ref = Reference.new(:type => type, :title => title, :scope => self.scope)
123: 
124:         @params = {}
125: 
126:         # Define all of the parameters
127:         if params = options[:params]
128:             options.delete(:params)
129:             params.each do |param|
130:                 set_parameter(param)
131:             end
132:         end
133: 
134:         # Throw an exception if we've got any arguments left to set.
135:         unless options.empty?
136:             raise ArgumentError, "Resources do not accept %s" % options.keys.collect { |k| k.to_s }.join(", ")
137:         end
138: 
139:         tag(@ref.type)
140:         tag(@ref.title) if valid_tag?(@ref.title.to_s)
141:     end

Determine whether the provided parameter name is a relationship parameter.

[Source]

    # File lib/puppet/parser/resource.rb, line 19
19:     def self.relationship_parameter?(name)
20:         unless defined?(@relationship_names)
21:             @relationship_names = Puppet::Type.relationship_params.collect { |p| p.name }
22:         end
23:         @relationship_names.include?(name)
24:     end

Public Instance methods

[Source]

    # File lib/puppet/parser/resource.rb, line 41
41:     def [](param)
42:         param = symbolize(param)
43:         if param == :title
44:             return self.title
45:         end
46:         if @params.has_key?(param)
47:             @params[param].value
48:         else
49:             nil
50:         end
51:     end

[Source]

    # File lib/puppet/parser/resource.rb, line 53
53:     def builtin=(bool)
54:         @ref.builtin = bool
55:     end

Retrieve the associated definition and evaluate it.

[Source]

    # File lib/puppet/parser/resource.rb, line 58
58:     def evaluate
59:         if klass = @ref.definedtype
60:             finish()
61:             return klass.evaluate_code(self)
62:         elsif builtin?
63:             devfail "Cannot evaluate a builtin type"
64:         else
65:             self.fail "Cannot find definition %s" % self.type
66:         end
67:     ensure
68:         @evaluated = true
69:     end

Mark this resource as both exported and virtual, or remove the exported mark.

[Source]

    # File lib/puppet/parser/resource.rb, line 73
73:     def exported=(value)
74:         if value
75:             @virtual = true
76:             @exported = value
77:         else
78:             @exported = value
79:         end
80:     end

Do any finishing work on this object, called before evaluation or before storage/translation.

[Source]

    # File lib/puppet/parser/resource.rb, line 84
84:     def finish
85:         return if finished?
86:         @finished = true
87:         add_defaults()
88:         add_metaparams()
89:         add_scope_tags()
90:         validate()
91:     end

Has this resource already been finished?

[Source]

    # File lib/puppet/parser/resource.rb, line 94
94:     def finished?
95:         defined?(@finished) and @finished
96:     end

Is this resource modeling an isomorphic resource type?

[Source]

     # File lib/puppet/parser/resource.rb, line 144
144:     def isomorphic?
145:         if builtin?
146:             return @ref.builtintype.isomorphic?
147:         else
148:             return true
149:         end
150:     end

Merge an override resource in. This will throw exceptions if any overrides aren‘t allowed.

[Source]

     # File lib/puppet/parser/resource.rb, line 154
154:     def merge(resource)
155:         # Test the resource scope, to make sure the resource is even allowed
156:         # to override.
157:         unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source)
158:             raise Puppet::ParseError.new("Only subclasses can override parameters", resource.line, resource.file)
159:         end
160:         # Some of these might fail, but they'll fail in the way we want.
161:         resource.params.each do |name, param|
162:             override_parameter(param)
163:         end
164:     end

Modify this resource in the Rails database. Poor design, yo.

[Source]

     # File lib/puppet/parser/resource.rb, line 167
167:     def modify_rails(db_resource)
168:         args = rails_args
169:         args.each do |param, value|
170:             db_resource[param] = value unless db_resource[param] == value
171:         end
172: 
173:         # Handle file specially
174:         if (self.file and  
175:             (!db_resource.file or db_resource.file != self.file))
176:             db_resource.file = self.file
177:         end
178:         
179:         updated_params = @params.inject({}) do |hash, ary|
180:             hash[ary[0].to_s] = ary[1]
181:             hash
182:         end
183:         
184:         db_resource.ar_hash_merge(db_resource.get_params_hash(db_resource.param_values), updated_params,
185:                                   :create => Proc.new { |name, parameter|
186:                                       parameter.to_rails(db_resource)
187:                                   }, :delete => Proc.new { |values|
188:                                       values.each { |value| db_resource.param_values.delete(value) }
189:                                   }, :modify => Proc.new { |db, mem|
190:                                       mem.modify_rails_values(db)
191:                                   })
192:         
193:         updated_tags = tags.inject({}) { |hash, tag| 
194:             hash[tag] = tag
195:             hash
196:         }
197:             
198:         db_resource.ar_hash_merge(db_resource.get_tag_hash(), 
199:                                   updated_tags,
200:                                   :create => Proc.new { |name, tag|
201:                                       db_resource.add_resource_tag(name)
202:                                   }, :delete => Proc.new { |tag|
203:                                       db_resource.resource_tags.delete(tag)
204:                                   }, :modify => Proc.new { |db, mem|
205:                                       # nothing here
206:                                   })
207:     end

Return the resource name, or the title if no name was specified.

[Source]

     # File lib/puppet/parser/resource.rb, line 211
211:     def name
212:         unless defined? @name
213:             @name = self[:name] || self.title
214:         end
215:         @name
216:     end

This significantly reduces the number of calls to Puppet.[].

[Source]

     # File lib/puppet/parser/resource.rb, line 219
219:     def paramcheck?
220:         unless defined? @@paramcheck
221:             @@paramcheck = Puppet[:paramcheck]
222:         end
223:         @@paramcheck
224:     end

A temporary occasion, until I get paths in the scopes figured out.

[Source]

     # File lib/puppet/parser/resource.rb, line 227
227:     def path
228:         to_s
229:     end

Return the short version of our name.

[Source]

     # File lib/puppet/parser/resource.rb, line 232
232:     def ref
233:         @ref.to_s
234:     end

Define a parameter in our resource.

[Source]

     # File lib/puppet/parser/resource.rb, line 237
237:     def set_parameter(param, value = nil)
238:         if value
239:             param = Puppet::Parser::Resource::Param.new(
240:                 :name => param, :value => value, :source => self.source
241:             )
242:         elsif ! param.is_a?(Puppet::Parser::Resource::Param)
243:             raise ArgumentError, "Must pass a parameter or all necessary values"
244:         end
245: 
246:         # And store it in our parameter hash.
247:         @params[param.name] = param
248:     end

[Source]

     # File lib/puppet/parser/resource.rb, line 250
250:     def to_hash
251:         @params.inject({}) do |hash, ary|
252:             param = ary[1]
253:             # Skip "undef" values.
254:             if param.value != :undef
255:                 hash[param.name] = param.value
256:             end
257:             hash
258:         end
259:     end

Turn our parser resource into a Rails resource.

[Source]

     # File lib/puppet/parser/resource.rb, line 262
262:     def to_rails(host)
263:         args = rails_args
264: 
265:         db_resource = host.resources.build(args)
266: 
267:         # Handle file specially
268:         db_resource.file = self.file
269: 
270:         db_resource.save
271: 
272:         @params.each { |name, param|
273:             param.to_rails(db_resource)
274:         }
275:         
276:         tags.each { |tag| db_resource.add_resource_tag(tag) }
277: 
278:         return db_resource
279:     end

[Source]

     # File lib/puppet/parser/resource.rb, line 281
281:     def to_s
282:         self.ref
283:     end

Translate our object to a transportable object.

[Source]

     # File lib/puppet/parser/resource.rb, line 286
286:     def to_trans
287:         return nil if virtual?
288: 
289:         if builtin?
290:             to_transobject
291:         else
292:             to_transbucket
293:         end
294:     end

[Source]

     # File lib/puppet/parser/resource.rb, line 296
296:     def to_transbucket
297:         bucket = Puppet::TransBucket.new([])
298: 
299:         bucket.type = self.type
300:         bucket.name = self.title
301: 
302:         # TransBuckets don't support parameters, which is why they're being deprecated.
303:         return bucket
304:     end

[Source]

     # File lib/puppet/parser/resource.rb, line 312
312:     def to_transobject
313:         # Now convert to a transobject
314:         obj = Puppet::TransObject.new(@ref.title, @ref.type)
315:         to_hash.each do |p, v|
316:             if v.is_a?(Reference)
317:                 v = v.to_ref
318:             elsif v.is_a?(Array)
319:                 v = v.collect { |av|
320:                     if av.is_a?(Reference)
321:                         av = av.to_ref
322:                     end
323:                     av
324:                 }
325:             end
326: 
327:             # If the value is an array with only one value, then
328:             # convert it to a single value.  This is largely so that
329:             # the database interaction doesn't have to worry about
330:             # whether it returns an array or a string.
331:             obj[p.to_s] = if v.is_a?(Array) and v.length == 1
332:                               v[0]
333:                           else
334:                               v
335:                           end
336:         end
337: 
338:         obj.file = self.file
339:         obj.line = self.line
340: 
341:         obj.tags = self.tags
342: 
343:         return obj
344:     end

Convert this resource to a RAL resource. We hackishly go via the transportable stuff.

[Source]

     # File lib/puppet/parser/resource.rb, line 308
308:     def to_type
309:         to_trans.to_type
310:     end

[Validate]