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
Public Class Methods
Puppet::Resource::new
# File lib/puppet/parser/resource.rb 124 def initialize(type, title, attributes, with_defaults = true) 125 raise ArgumentError, _('Resources require a hash as last argument') unless attributes.is_a? Hash 126 raise ArgumentError, _('Resources require a scope') unless attributes[:scope] 127 super(type, title, attributes) 128 129 @source ||= scope.source 130 131 if with_defaults 132 scope.lookupdefaults(self.type).each_pair do |name, param| 133 unless @parameters.include?(name) 134 self.debug "Adding default for #{name}" 135 136 param = param.dup 137 @parameters[name] = param 138 tag(*param.value) if param.name == :tag 139 end 140 end 141 end 142 end
Determine whether the provided parameter name is a relationship parameter.
# File lib/puppet/parser/resource.rb 21 def self.relationship_parameter?(name) 22 @relationship_names ||= Puppet::Type.relationship_params.collect { |p| p.name } 23 @relationship_names.include?(name) 24 end
Public Instance Methods
# File lib/puppet/parser/resource.rb 31 def [](param) 32 param = param.intern 33 if param == :title 34 return self.title 35 end 36 if @parameters.has_key?(param) 37 @parameters[param].value 38 else 39 nil 40 end 41 end
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 56 def add_edge_to_stage 57 return unless self.class? 58 59 stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main) 60 unless stage 61 raise ArgumentError, _("Could not find stage %{stage} specified by %{resource}") % { stage: self[:stage] || :main, resource: self } 62 end 63 64 self[:stage] ||= stage.title unless stage.title == :main 65 catalog.add_edge(stage, self) 66 end
# File lib/puppet/parser/resource.rb 43 def eachparam 44 @parameters.each do |name, param| 45 yield param 46 end 47 end
# File lib/puppet/parser/resource.rb 49 def environment 50 scope.environment 51 end
Retrieve the associated definition and evaluate it.
# File lib/puppet/parser/resource.rb 69 def evaluate 70 return if evaluated? 71 Puppet::Util::Profiler.profile(_("Evaluated resource %{res}") % { res: self }, [:compiler, :evaluate_resource, self]) do 72 @evaluated = true 73 if builtin_type? 74 devfail "Cannot evaluate a builtin type (#{type})" 75 elsif resource_type.nil? 76 self.fail "Cannot find definition #{type}" 77 else 78 finish_evaluation() # do not finish completely (as that destroys Sensitive data) 79 resource_type.evaluate_code(self) 80 end 81 end 82 end
# File lib/puppet/parser/resource.rb 29 def evaluated?; !!@evaluated; end
Mark this resource as both exported and virtual, or remove the exported mark.
# File lib/puppet/parser/resource.rb 86 def exported=(value) 87 if value 88 @virtual = true 89 @exported = value 90 else 91 @exported = value 92 end 93 end
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 111 def finish(do_validate = true) 112 return if finished? 113 @finished = true 114 finish_evaluation 115 replace_sensitive_data 116 validate if do_validate 117 end
Finish the evaluation by assigning defaults and scope tags @api private
# File lib/puppet/parser/resource.rb 98 def finish_evaluation 99 return if @evaluation_finished 100 add_scope_tags 101 @evaluation_finished = true 102 end
Has this resource already been finished?
# File lib/puppet/parser/resource.rb 120 def finished? 121 @finished 122 end
Is this resource modeling an isomorphic resource type?
# File lib/puppet/parser/resource.rb 145 def isomorphic? 146 if builtin_type? 147 return resource_type.isomorphic? 148 else 149 return true 150 end 151 end
Merge an override resource in. This will throw exceptions if any overrides aren't allowed.
# File lib/puppet/parser/resource.rb 155 def merge(resource) 156 # Test the resource scope, to make sure the resource is even allowed 157 # to override. 158 unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source) 159 raise Puppet::ParseError.new(_("Only subclasses can override parameters"), resource.file, resource.line) 160 end 161 162 if evaluated? 163 error_location_str = Puppet::Util::Errors.error_location(file, line) 164 msg = if error_location_str.empty? 165 _('Attempt to override an already evaluated resource with new values') 166 else 167 _('Attempt to override an already evaluated resource, defined at %{error_location}, with new values') % { error_location: error_location_str } 168 end 169 strict = Puppet[:strict] 170 unless strict == :off 171 if strict == :error 172 raise Puppet::ParseError.new(msg, resource.file, resource.line) 173 else 174 msg += Puppet::Util::Errors.error_location_with_space(resource.file, resource.line) 175 Puppet.warning(msg) 176 end 177 end 178 end 179 180 # Some of these might fail, but they'll fail in the way we want. 181 resource.parameters.each do |name, param| 182 override_parameter(param) 183 end 184 end
# File lib/puppet/parser/resource.rb 186 def name 187 self[:name] || self.title 188 end
# File lib/puppet/parser/resource.rb 254 def offset 255 nil 256 end
# File lib/puppet/parser/resource.rb 28 def override?; !!@override; end
# File lib/puppet/parser/resource.rb 258 def pos 259 nil 260 end
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
Puppet::Util::Tagging#raw_tagged?
# File lib/puppet/parser/resource.rb 250 def raw_tagged?(tag_array) 251 super || ((scope_resource = scope.resource) && !scope_resource.equal?(self) && scope_resource.raw_tagged?(tag_array)) 252 end
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 196 def set_parameter(param, value = nil) 197 if ! param.is_a?(Puppet::Parser::Resource::Param) 198 param = param.name if param.is_a?(Puppet::Pops::Resource::Param) 199 param = Puppet::Parser::Resource::Param.new( 200 :name => param, :value => value, :source => self.source 201 ) 202 end 203 204 tag(*param.value) if param.name == :tag 205 206 # And store it in our parameter hash. 207 @parameters[param.name] = param 208 end
# File lib/puppet/parser/resource.rb 211 def to_hash 212 parse_title.merge(@parameters.reduce({}) do |result, (_, param)| 213 value = param.value 214 value = (:undef == value) ? nil : value 215 216 unless value.nil? 217 case param.name 218 when :before, :subscribe, :notify, :require 219 if value.is_a?(Array) 220 value = value.flatten.reject {|v| v.nil? || :undef == v } 221 end 222 result[param.name] = value 223 else 224 result[param.name] = value 225 end 226 end 227 result 228 end) 229 end
Convert this resource to a RAL resource.
# File lib/puppet/parser/resource.rb 232 def to_ral 233 copy_as_resource.to_ral 234 end
Set up some boolean test methods
# File lib/puppet/parser/resource.rb 27 def translated?; !!@translated; end
Private Instance Methods
# File lib/puppet/parser/resource.rb 343 def extract_parameters(params) 344 params.each do |param| 345 # Don't set the same parameter twice 346 self.fail Puppet::ParseError, _("Duplicate parameter '%{param}' for on %{resource}") % { param: param.name, resource: self } if @parameters[param.name] 347 348 set_parameter(param) 349 end 350 end
Accept a parameter from an override.
# File lib/puppet/parser/resource.rb 282 def override_parameter(param) 283 # This can happen if the override is defining a new parameter, rather 284 # than replacing an existing one. 285 current = @parameters[param.name] 286 (set_parameter(param) and return) unless current 287 288 # Parameter is already set - if overriding with a default - simply ignore the setting of the default value 289 return if scope.is_default?(type, param.name, param.value) 290 291 # The parameter is already set. Fail if they're not allowed to override it. 292 unless param.source.child_of?(current.source) || param.source.equal?(current.source) && scope.is_default?(type, param.name, current.value) 293 error_location_str = Puppet::Util::Errors.error_location(current.file, current.line) 294 msg = if current.source.to_s == '' 295 if error_location_str.empty? 296 _("Parameter '%{name}' is already set on %{resource}; cannot redefine") % 297 { name: param.name, resource: ref } 298 else 299 _("Parameter '%{name}' is already set on %{resource} at %{error_location}; cannot redefine") % 300 { name: param.name, resource: ref, error_location: error_location_str } 301 end 302 else 303 if error_location_str.empty? 304 _("Parameter '%{name}' is already set on %{resource} by %{source}; cannot redefine") % 305 { name: param.name, resource: ref, source: current.source.to_s } 306 else 307 _("Parameter '%{name}' is already set on %{resource} by %{source} at %{error_location}; cannot redefine") % 308 { name: param.name, resource: ref, source: current.source.to_s, error_location: error_location_str } 309 end 310 end 311 raise Puppet::ParseError.new(msg, param.file, param.line) 312 end 313 314 # If we've gotten this far, we're allowed to override. 315 316 # Merge with previous value, if the parameter was generated with the +> 317 # syntax. It's important that we use a copy of the new param instance 318 # here, not the old one, and not the original new one, so that the source 319 # is registered correctly for later overrides but the values aren't 320 # implicitly shared when multiple resources are overridden at once (see 321 # ticket #3556). 322 if param.add 323 param = param.dup 324 param.value = [current.value, param.value].flatten 325 end 326 327 set_parameter(param) 328 end
# File lib/puppet/parser/resource.rb 271 def replace_sensitive_data 272 parameters.keys.each do |name| 273 param = parameters[name] 274 if param.value.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive) 275 @sensitive_parameters << name 276 parameters[name] = Puppet::Parser::Resource::Param.from_param(param, param.value.unwrap) 277 end 278 end 279 end
Make sure the resource's parameters are all valid for the type.
# File lib/puppet/parser/resource.rb 331 def validate 332 if builtin_type? 333 begin 334 @parameters.each { |name, value| validate_parameter(name) } 335 rescue => detail 336 self.fail Puppet::ParseError, detail.to_s + " on #{self}", detail 337 end 338 else 339 resource_type.validate_resource(self) 340 end 341 end