class Puppet::Parser::Resource

The primary difference between this class and its parent is that this class has rules on who can set parameters

Attributes

catalog[RW]
collector_id[RW]
evaluated[RW]
exported[R]
file[RW]
kind[RW]
line[RW]
override[RW]
parameters[R]
scope[RW]
source[RW]
translated[RW]
virtual[RW]

Public Class Methods

new(type, title, attributes, with_defaults = true) click to toggle source
Calls superclass method Puppet::Resource::new
    # File lib/puppet/parser/resource.rb
125 def initialize(type, title, attributes, with_defaults = true)
126   raise ArgumentError, _('Resources require a hash as last argument') unless attributes.is_a? Hash
127   raise ArgumentError, _('Resources require a scope') unless attributes[:scope]
128   super(type, title, attributes)
129 
130   @source ||= scope.source
131 
132   if with_defaults
133     scope.lookupdefaults(self.type).each_pair do |name, param|
134       unless @parameters.include?(name)
135         self.debug "Adding default for #{name}"
136 
137         param = param.dup
138         @parameters[name] = param
139         tag(*param.value) if param.name == :tag
140       end
141     end
142   end
143 end
relationship_parameter?(name) click to toggle source

Determine whether the provided parameter name is a relationship parameter.

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

Public Instance Methods

[](param) click to toggle source
   # File lib/puppet/parser/resource.rb
32 def [](param)
33   param = param.intern
34   if param == :title
35     return self.title
36   end
37   if @parameters.has_key?(param)
38     @parameters[param].value
39   else
40     nil
41   end
42 end
[]=(param, value = nil)
Alias for: set_parameter
add_edge_to_stage() click to toggle source

Process the stage metaparameter for a class. A containment edge is drawn from the class to the stage. The stage for containment defaults to main, if none is specified.

   # File lib/puppet/parser/resource.rb
57 def add_edge_to_stage
58   return unless self.class?
59 
60   stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main)
61   unless stage
62     raise ArgumentError, _("Could not find stage %{stage} specified by %{resource}") % { stage: self[:stage] || :main, resource: self }
63   end
64 
65   self[:stage] ||= stage.title unless stage.title == :main
66   catalog.add_edge(stage, self)
67 end
eachparam() { |param| ... } click to toggle source
   # File lib/puppet/parser/resource.rb
44 def eachparam
45   @parameters.each do |name, param|
46     yield param
47   end
48 end
environment() click to toggle source
   # File lib/puppet/parser/resource.rb
50 def environment
51   scope.environment
52 end
evaluate() click to toggle source

Retrieve the associated definition and evaluate it.

   # File lib/puppet/parser/resource.rb
70 def evaluate
71   return if evaluated?
72   Puppet::Util::Profiler.profile(_("Evaluated resource %{res}") % { res: self }, [:compiler, :evaluate_resource, self]) do
73     @evaluated = true
74     if builtin_type?
75       devfail "Cannot evaluate a builtin type (#{type})"
76     elsif resource_type.nil?
77       self.fail "Cannot find definition #{type}"
78     else
79       finish_evaluation() # do not finish completely (as that destroys Sensitive data)
80       resource_type.evaluate_code(self)
81     end
82   end
83 end
evaluated?() click to toggle source
   # File lib/puppet/parser/resource.rb
30 def evaluated?;  !!@evaluated;  end
exported=(value) click to toggle source

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

   # File lib/puppet/parser/resource.rb
87 def exported=(value)
88   if value
89     @virtual = true
90     @exported = value
91   else
92     @exported = value
93   end
94 end
finish(do_validate = true) click to toggle source

Do any finishing work on this object, called before storage/translation. The method does nothing the second time it is called on the same resource.

@param do_validate [Boolean] true if validation should be performed

@api private

    # File lib/puppet/parser/resource.rb
112 def finish(do_validate = true)
113   return if finished?
114   @finished = true
115   finish_evaluation
116   replace_sensitive_data
117   validate if do_validate
118 end
finish_evaluation() click to toggle source

Finish the evaluation by assigning defaults and scope tags @api private

    # File lib/puppet/parser/resource.rb
 99 def finish_evaluation
100   return if @evaluation_finished
101   add_scope_tags
102   @evaluation_finished = true
103 end
finished?() click to toggle source

Has this resource already been finished?

    # File lib/puppet/parser/resource.rb
121 def finished?
122   @finished
123 end
isomorphic?() click to toggle source

Is this resource modeling an isomorphic resource type?

    # File lib/puppet/parser/resource.rb
146 def isomorphic?
147   if builtin_type?
148     return resource_type.isomorphic?
149   else
150     return true
151   end
152 end
merge(resource) click to toggle source

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

    # File lib/puppet/parser/resource.rb
156 def merge(resource)
157   # Test the resource scope, to make sure the resource is even allowed
158   # to override.
159   unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source)
160     raise Puppet::ParseError.new(_("Only subclasses can override parameters"), resource.file, resource.line)
161   end
162 
163   if evaluated?
164     error_location_str = Puppet::Util::Errors.error_location(file, line)
165     msg = if error_location_str.empty?
166             _('Attempt to override an already evaluated resource with new values')
167           else
168             _('Attempt to override an already evaluated resource, defined at %{error_location}, with new values') % { error_location: error_location_str }
169           end
170     strict = Puppet[:strict]
171     unless strict == :off
172       if strict == :error
173         raise Puppet::ParseError.new(msg, resource.file, resource.line)
174       else
175         msg += Puppet::Util::Errors.error_location_with_space(resource.file, resource.line)
176         Puppet.warning(msg)
177       end
178     end
179   end
180 
181   # Some of these might fail, but they'll fail in the way we want.
182   resource.parameters.each do |name, param|
183     override_parameter(param)
184   end
185 end
name() click to toggle source
    # File lib/puppet/parser/resource.rb
187 def name
188   self[:name] || self.title
189 end
offset() click to toggle source
    # File lib/puppet/parser/resource.rb
255 def offset
256   nil
257 end
override?() click to toggle source
   # File lib/puppet/parser/resource.rb
29 def override?;   !!@override;   end
pos() click to toggle source
    # File lib/puppet/parser/resource.rb
259 def pos
260   nil
261 end
raw_tagged?(tag_array) click to toggle source

Answers if this resource is tagged with at least one of the tags given in downcased string form.

The method is a faster variant of the tagged? method that does no conversion of its arguments.

The match takes into account the tags that a resource will inherit from its container but have not been set yet. It does not take tags set via resource defaults as these will never be set on the resource itself since all resources always have tags that are automatically assigned.

@param tag_array [Array] list tags to look for @return [Boolean] true if this instance is tagged with at least one of the provided tags

Calls superclass method Puppet::Util::Tagging#raw_tagged?
    # File lib/puppet/parser/resource.rb
251 def raw_tagged?(tag_array)
252   super || ((scope_resource = scope.resource) && !scope_resource.equal?(self) && scope_resource.raw_tagged?(tag_array))
253 end
set_parameter(param, value = nil) click to toggle source

Define a parameter in our resource. if we ever receive a parameter named 'tag', set the resource tags with its value.

    # File lib/puppet/parser/resource.rb
197 def set_parameter(param, value = nil)
198   if ! param.is_a?(Puppet::Parser::Resource::Param)
199     param = param.name if param.is_a?(Puppet::Pops::Resource::Param)
200     param = Puppet::Parser::Resource::Param.new(
201       :name => param, :value => value, :source => self.source
202     )
203   end
204 
205   tag(*param.value) if param.name == :tag
206 
207   # And store it in our parameter hash.
208   @parameters[param.name] = param
209 end
Also aliased as: []=
to_hash() click to toggle source
    # File lib/puppet/parser/resource.rb
212 def to_hash
213   parse_title.merge(@parameters.reduce({}) do |result, (_, param)|
214     value = param.value
215     value = (:undef == value) ? nil : value
216 
217     unless value.nil?
218       case param.name
219       when :before, :subscribe, :notify, :require
220         if value.is_a?(Array)
221           value = value.flatten.reject {|v| v.nil? || :undef == v }
222         end
223         result[param.name] = value
224       else
225         result[param.name] = value
226       end
227     end
228     result
229   end)
230 end
to_ral() click to toggle source

Convert this resource to a RAL resource.

    # File lib/puppet/parser/resource.rb
233 def to_ral
234   copy_as_resource.to_ral
235 end
translated?() click to toggle source

Set up some boolean test methods

   # File lib/puppet/parser/resource.rb
28 def translated?; !!@translated; end

Private Instance Methods

add_scope_tags() click to toggle source
    # File lib/puppet/parser/resource.rb
265 def add_scope_tags
266   scope_resource = scope.resource
267   unless scope_resource.nil? || scope_resource.equal?(self)
268     merge_tags_from(scope_resource)
269   end
270 end
extract_parameters(params) click to toggle source
    # File lib/puppet/parser/resource.rb
344 def extract_parameters(params)
345   params.each do |param|
346     # Don't set the same parameter twice
347     self.fail Puppet::ParseError, _("Duplicate parameter '%{param}' for on %{resource}") % { param: param.name, resource: self } if @parameters[param.name]
348 
349     set_parameter(param)
350   end
351 end
override_parameter(param) click to toggle source

Accept a parameter from an override.

    # File lib/puppet/parser/resource.rb
283 def override_parameter(param)
284   # This can happen if the override is defining a new parameter, rather
285   # than replacing an existing one.
286   current = @parameters[param.name]
287   (set_parameter(param) and return) unless current
288 
289   # Parameter is already set - if overriding with a default - simply ignore the setting of the default value
290   return if scope.is_default?(type, param.name, param.value)
291 
292   # The parameter is already set.  Fail if they're not allowed to override it.
293   unless param.source.child_of?(current.source) || param.source.equal?(current.source) && scope.is_default?(type, param.name, current.value)
294     error_location_str = Puppet::Util::Errors.error_location(current.file, current.line)
295     msg = if current.source.to_s == ''
296             if error_location_str.empty?
297               _("Parameter '%{name}' is already set on %{resource}; cannot redefine") %
298                   { name: param.name, resource: ref }
299             else
300               _("Parameter '%{name}' is already set on %{resource} at %{error_location}; cannot redefine") %
301                   { name: param.name, resource: ref, error_location: error_location_str }
302             end
303           else
304             if error_location_str.empty?
305               _("Parameter '%{name}' is already set on %{resource} by %{source}; cannot redefine") %
306                   { name: param.name, resource: ref, source: current.source.to_s }
307             else
308               _("Parameter '%{name}' is already set on %{resource} by %{source} at %{error_location}; cannot redefine") %
309                   { name: param.name, resource: ref, source: current.source.to_s, error_location: error_location_str }
310             end
311           end
312     raise Puppet::ParseError.new(msg, param.file, param.line)
313   end
314 
315   # If we've gotten this far, we're allowed to override.
316 
317   # Merge with previous value, if the parameter was generated with the +>
318   # syntax.  It's important that we use a copy of the new param instance
319   # here, not the old one, and not the original new one, so that the source
320   # is registered correctly for later overrides but the values aren't
321   # implicitly shared when multiple resources are overridden at once (see
322   # ticket #3556).
323   if param.add
324     param = param.dup
325     param.value = [current.value, param.value].flatten
326   end
327 
328   set_parameter(param)
329 end
replace_sensitive_data() click to toggle source
    # File lib/puppet/parser/resource.rb
272 def replace_sensitive_data
273   parameters.keys.each do |name|
274     param = parameters[name]
275     if param.value.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
276       @sensitive_parameters << name
277       parameters[name] = Puppet::Parser::Resource::Param.from_param(param, param.value.unwrap)
278     end
279   end
280 end
validate() click to toggle source

Make sure the resource's parameters are all valid for the type.

    # File lib/puppet/parser/resource.rb
332 def validate
333   if builtin_type?
334     begin
335       @parameters.each { |name, value| validate_parameter(name) }
336     rescue => detail
337       self.fail Puppet::ParseError, detail.to_s + " on #{self}", detail
338     end
339   else
340     resource_type.validate_resource(self)
341   end
342 end