Class: Puppet::Parser::Resource
- Includes:
- Util, Util::Errors, Util::Logging
- Defined in:
- lib/puppet/parser/resource.rb
Overview
The primary difference between this class and its parent is that this class has rules on who can set parameters
Defined Under Namespace
Classes: Param
Constant Summary
Constants included from Util
Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE
Constants included from Util::POSIX
Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS
Constants included from Util::SymbolicFileMode
Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit
Constants inherited from Resource
Resource::ATTRIBUTES, Resource::CLASS_STRING, Resource::COMPILABLE_TYPE_STRING, Resource::DEFINED_TYPE_STRING, Resource::EMPTY_ARRAY, Resource::EMPTY_HASH, Resource::PCORE_TYPE_KEY, Resource::TYPE_CLASS, Resource::TYPE_NODE, Resource::UNKNOWN_TYPE_STRING, Resource::VALUE_KEY
Constants included from Indirector
Constants included from Util::Tagging
Instance Attribute Summary collapse
-
#catalog ⇒ Object
Returns the value of attribute catalog.
-
#collector_id ⇒ Object
Returns the value of attribute collector_id.
-
#evaluated ⇒ Object
Returns the value of attribute evaluated.
-
#exported ⇒ Object
Returns the value of attribute exported.
-
#file ⇒ Object
Returns the value of attribute file.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#line ⇒ Object
Returns the value of attribute line.
-
#override ⇒ Object
Returns the value of attribute override.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#source ⇒ Object
Returns the value of attribute source.
-
#translated ⇒ Object
Returns the value of attribute translated.
-
#virtual ⇒ Object
Returns the value of attribute virtual.
Attributes inherited from Resource
#sensitive_parameters, #strict, #title, #type, #validate_parameters
Class Method Summary collapse
-
.relationship_parameter?(name) ⇒ Boolean
Determine whether the provided parameter name is a relationship parameter.
Instance Method Summary collapse
- #[](param) ⇒ Object
-
#add_edge_to_stage ⇒ Object
Process the stage metaparameter for a class.
- #eachparam ⇒ Object
- #environment ⇒ Object
-
#evaluate ⇒ Object
Retrieve the associated definition and evaluate it.
- #evaluated? ⇒ Boolean
-
#finish(do_validate = true) ⇒ Object
private
Do any finishing work on this object, called before storage/translation.
-
#finish_evaluation ⇒ Object
private
Finish the evaluation by assigning defaults and scope tags.
-
#finished? ⇒ Boolean
Has this resource already been finished?.
-
#initialize(type, title, attributes, with_defaults = true) ⇒ Resource
constructor
A new instance of Resource.
-
#isomorphic? ⇒ Boolean
Is this resource modeling an isomorphic resource type?.
-
#merge(resource) ⇒ Object
Merge an override resource in.
- #name ⇒ Object
- #offset ⇒ Object
- #override? ⇒ Boolean
- #pos ⇒ Object
-
#raw_tagged?(tag_array) ⇒ Boolean
Answers if this resource is tagged with at least one of the tags given in downcased string form.
-
#set_parameter(param, value = nil) ⇒ Object
(also: #[]=)
Define a parameter in our resource.
- #to_hash ⇒ Object
-
#to_ral ⇒ Object
Convert this resource to a RAL resource.
-
#translated? ⇒ Boolean
Set up some boolean test methods.
Methods included from Util::Logging
#clear_deprecation_warnings, #debug, #deprecation_warning, #format_backtrace, #format_exception, #get_deprecation_offender, #log_and_raise, #log_deprecations_to_file, #log_exception, #puppet_deprecation_warning, #send_log, setup_facter_logging!, #warn_once
Methods included from Util::Errors
#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail
Methods included from Util
absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::SymbolicFileMode
#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Methods inherited from Resource
#==, #builtin?, #builtin_type?, #class?, #copy_as_resource, #each, #environment=, from_data_hash, #include?, #initialize_from_hash, #inspect, #key_attributes, #prune_parameters, #ref, #resolve, resource_type, #resource_type, #resource_type=, #stage?, #to_data_hash, #to_hiera_hash, #to_hierayaml, to_kind, #to_manifest, #to_ref, #to_s, type_and_title, #uniqueness_key, #valid_parameter?, #validate_parameter, value_to_json_data, #yaml_property_munge
Methods included from Indirector
Methods included from Util::PsychSupport
Methods included from Util::Tagging
#merge_into, #merge_tags_from, #set_tags, #tag, #tag_if_valid, #tagged?, #tags, #tags=, #valid_tag?
Constructor Details
#initialize(type, title, attributes, with_defaults = true) ⇒ Resource
Returns a new instance of Resource.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/puppet/parser/resource.rb', line 124 def initialize(type, title, attributes, with_defaults = true) raise ArgumentError, _('Resources require a hash as last argument') unless attributes.is_a? Hash raise ArgumentError, _('Resources require a scope') unless attributes[:scope] super(type, title, attributes) @source ||= scope.source if with_defaults scope.lookupdefaults(self.type).each_pair do |name, param| unless @parameters.include?(name) self.debug "Adding default for #{name}" param = param.dup @parameters[name] = param tag(*param.value) if param.name == :tag end end end end |
Instance Attribute Details
#catalog ⇒ Object
Returns the value of attribute catalog.
15 16 17 |
# File 'lib/puppet/parser/resource.rb', line 15 def catalog @catalog end |
#collector_id ⇒ Object
Returns the value of attribute collector_id.
14 15 16 |
# File 'lib/puppet/parser/resource.rb', line 14 def collector_id @collector_id end |
#evaluated ⇒ Object
Returns the value of attribute evaluated.
15 16 17 |
# File 'lib/puppet/parser/resource.rb', line 15 def evaluated @evaluated end |
#exported ⇒ Object
Returns the value of attribute exported.
18 19 20 |
# File 'lib/puppet/parser/resource.rb', line 18 def exported @exported end |
#file ⇒ Object
Returns the value of attribute file.
16 17 18 |
# File 'lib/puppet/parser/resource.rb', line 16 def file @file end |
#kind ⇒ Object
Returns the value of attribute kind.
16 17 18 |
# File 'lib/puppet/parser/resource.rb', line 16 def kind @kind end |
#line ⇒ Object
Returns the value of attribute line.
16 17 18 |
# File 'lib/puppet/parser/resource.rb', line 16 def line @line end |
#override ⇒ Object
Returns the value of attribute override.
15 16 17 |
# File 'lib/puppet/parser/resource.rb', line 15 def override @override end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
18 19 20 |
# File 'lib/puppet/parser/resource.rb', line 18 def parameters @parameters end |
#scope ⇒ Object
Returns the value of attribute scope.
14 15 16 |
# File 'lib/puppet/parser/resource.rb', line 14 def scope @scope end |
#source ⇒ Object
Returns the value of attribute source.
14 15 16 |
# File 'lib/puppet/parser/resource.rb', line 14 def source @source end |
#translated ⇒ Object
Returns the value of attribute translated.
15 16 17 |
# File 'lib/puppet/parser/resource.rb', line 15 def translated @translated end |
#virtual ⇒ Object
Returns the value of attribute virtual.
15 16 17 |
# File 'lib/puppet/parser/resource.rb', line 15 def virtual @virtual end |
Class Method Details
.relationship_parameter?(name) ⇒ Boolean
Determine whether the provided parameter name is a relationship parameter.
21 22 23 24 |
# File 'lib/puppet/parser/resource.rb', line 21 def self.relationship_parameter?(name) @relationship_names ||= Puppet::Type.relationship_params.collect { |p| p.name } @relationship_names.include?(name) end |
Instance Method Details
#[](param) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/puppet/parser/resource.rb', line 31 def [](param) param = param.intern if param == :title return self.title end if @parameters.has_key?(param) @parameters[param].value else nil end end |
#add_edge_to_stage ⇒ Object
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.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/puppet/parser/resource.rb', line 56 def add_edge_to_stage return unless self.class? stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main) unless stage raise ArgumentError, _("Could not find stage %{stage} specified by %{resource}") % { stage: self[:stage] || :main, resource: self } end self[:stage] ||= stage.title unless stage.title == :main catalog.add_edge(stage, self) end |
#eachparam ⇒ Object
43 44 45 46 47 |
# File 'lib/puppet/parser/resource.rb', line 43 def eachparam @parameters.each do |name, param| yield param end end |
#environment ⇒ Object
49 50 51 |
# File 'lib/puppet/parser/resource.rb', line 49 def environment scope.environment end |
#evaluate ⇒ Object
Retrieve the associated definition and evaluate it.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppet/parser/resource.rb', line 69 def evaluate return if evaluated? Puppet::Util::Profiler.profile(_("Evaluated resource %{res}") % { res: self }, [:compiler, :evaluate_resource, self]) do @evaluated = true if builtin_type? devfail "Cannot evaluate a builtin type (#{type})" elsif resource_type.nil? self.fail "Cannot find definition #{type}" else finish_evaluation() # do not finish completely (as that destroys Sensitive data) resource_type.evaluate_code(self) end end end |
#evaluated? ⇒ Boolean
29 |
# File 'lib/puppet/parser/resource.rb', line 29 def evaluated?; !!@evaluated; end |
#finish(do_validate = true) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
111 112 113 114 115 116 117 |
# File 'lib/puppet/parser/resource.rb', line 111 def finish(do_validate = true) return if finished? @finished = true finish_evaluation replace_sensitive_data validate if do_validate end |
#finish_evaluation ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Finish the evaluation by assigning defaults and scope tags
98 99 100 101 102 |
# File 'lib/puppet/parser/resource.rb', line 98 def finish_evaluation return if @evaluation_finished @evaluation_finished = true end |
#finished? ⇒ Boolean
Has this resource already been finished?
120 121 122 |
# File 'lib/puppet/parser/resource.rb', line 120 def finished? @finished end |
#isomorphic? ⇒ Boolean
Is this resource modeling an isomorphic resource type?
145 146 147 148 149 150 151 |
# File 'lib/puppet/parser/resource.rb', line 145 def isomorphic? if builtin_type? return resource_type.isomorphic? else return true end end |
#merge(resource) ⇒ Object
Merge an override resource in. This will throw exceptions if any overrides aren't allowed.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/puppet/parser/resource.rb', line 155 def merge(resource) # Test the resource scope, to make sure the resource is even allowed # to override. unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source) raise Puppet::ParseError.new(_("Only subclasses can override parameters"), resource.file, resource.line) end if evaluated? error_location_str = Puppet::Util::Errors.error_location(file, line) msg = if error_location_str.empty? _('Attempt to override an already evaluated resource with new values') else _('Attempt to override an already evaluated resource, defined at %{error_location}, with new values') % { error_location: error_location_str } end strict = Puppet[:strict] unless strict == :off if strict == :error raise Puppet::ParseError.new(msg, resource.file, resource.line) else msg += Puppet::Util::Errors.error_location_with_space(resource.file, resource.line) Puppet.warning(msg) end end end # Some of these might fail, but they'll fail in the way we want. resource.parameters.each do |name, param| override_parameter(param) end end |
#name ⇒ Object
186 187 188 |
# File 'lib/puppet/parser/resource.rb', line 186 def name self[:name] || self.title end |
#offset ⇒ Object
254 255 256 |
# File 'lib/puppet/parser/resource.rb', line 254 def offset nil end |
#override? ⇒ Boolean
28 |
# File 'lib/puppet/parser/resource.rb', line 28 def override?; !!@override; end |
#pos ⇒ Object
258 259 260 |
# File 'lib/puppet/parser/resource.rb', line 258 def pos nil end |
#raw_tagged?(tag_array) ⇒ Boolean
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.
250 251 252 |
# File 'lib/puppet/parser/resource.rb', line 250 def raw_tagged?(tag_array) super || ((scope_resource = scope.resource) && !scope_resource.equal?(self) && scope_resource.raw_tagged?(tag_array)) end |
#set_parameter(param, value = nil) ⇒ Object Also known as: []=
Define a parameter in our resource. if we ever receive a parameter named 'tag', set the resource tags with its value.
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/puppet/parser/resource.rb', line 196 def set_parameter(param, value = nil) if ! param.is_a?(Puppet::Parser::Resource::Param) param = param.name if param.is_a?(Puppet::Pops::Resource::Param) param = Puppet::Parser::Resource::Param.new( :name => param, :value => value, :source => self.source ) end tag(*param.value) if param.name == :tag # And store it in our parameter hash. @parameters[param.name] = param end |
#to_hash ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/puppet/parser/resource.rb', line 211 def to_hash parse_title.merge(@parameters.reduce({}) do |result, (_, param)| value = param.value value = (:undef == value) ? nil : value unless value.nil? case param.name when :before, :subscribe, :notify, :require if value.is_a?(Array) value = value.flatten.reject {|v| v.nil? || :undef == v } end result[param.name] = value else result[param.name] = value end end result end) end |
#to_ral ⇒ Object
Convert this resource to a RAL resource.
232 233 234 |
# File 'lib/puppet/parser/resource.rb', line 232 def to_ral copy_as_resource.to_ral end |
#translated? ⇒ Boolean
Set up some boolean test methods
27 |
# File 'lib/puppet/parser/resource.rb', line 27 def translated?; !!@translated; end |