Class: Puppet::Parser::Resource

Inherits:
Resource show all
Includes:
YamlTrimmer, Util, Util::Errors, Util::Logging, Util::MethodHelper
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 YamlTrimmer

YamlTrimmer::REMOVE

Constants included from Util::Logging

Util::Logging::FILE_AND_LINE, Util::Logging::FILE_NO_LINE, Util::Logging::MM, Util::Logging::NO_FILE_LINE

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

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::TYPE_CLASS, Resource::TYPE_NODE, Resource::TYPE_SITE, Resource::YAML_ATTRIBUTES

Constants included from Indirector

Indirector::BadNameRegexp

Constants included from Util::Tagging

Util::Tagging::ValidTagRegex

Instance Attribute Summary collapse

Attributes inherited from Resource

#sensitive_parameters, #strict, #title, #type, #validate_parameters

Class Method Summary collapse

Instance Method Summary collapse

Methods included from YamlTrimmer

#to_yaml_properties

Methods included from Util::Logging

#clear_deprecation_warnings, #debug, #deprecation_warning, #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, #exceptwrap, #fail

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods inherited from Resource

#==, #builtin?, #builtin_type?, #class?, #copy_as_resource, #each, #environment=, #export, from_data_hash, #include?, #inspect, #is_application_component?, #is_capability?, #is_json_type?, #key_attributes, #prune_parameters, #ref, #resolve, resource_type, #resource_type, #resource_type=, #set_default_parameters, #stage?, #to_data_hash, #to_hierayaml, #to_manifest, #to_ref, #to_s, #to_yaml_properties, type_and_title, #uniqueness_key, #valid_parameter?, #validate_complete, #validate_parameter, value_to_pson_data, #yaml_property_munge

Methods included from Indirector

configure_routes, #indirects

Methods included from Util::Tagging

#merge_into, #merge_tags, #set_tags, #tag, #tag_if_valid, #tagged?, #tags, #tags=

Constructor Details

#initialize(type, title, attributes) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
# File 'lib/puppet/parser/resource.rb', line 127

def initialize(type, title, attributes)
  raise ArgumentError, 'Resources require a hash as last argument' unless attributes.is_a? Hash
  raise ArgumentError, 'Resources require a scope' unless attributes[:scope]
  super

  @source ||= scope.source
end

Instance Attribute Details

#catalogObject



18
19
20
# File 'lib/puppet/parser/resource.rb', line 18

def catalog
  @catalog
end

#collector_idObject



17
18
19
# File 'lib/puppet/parser/resource.rb', line 17

def collector_id
  @collector_id
end

#evaluatedObject



18
19
20
# File 'lib/puppet/parser/resource.rb', line 18

def evaluated
  @evaluated
end

#exportedObject



21
22
23
# File 'lib/puppet/parser/resource.rb', line 21

def exported
  @exported
end

#fileObject



19
20
21
# File 'lib/puppet/parser/resource.rb', line 19

def file
  @file
end

#lineObject



19
20
21
# File 'lib/puppet/parser/resource.rb', line 19

def line
  @line
end

#overrideObject



18
19
20
# File 'lib/puppet/parser/resource.rb', line 18

def override
  @override
end

#parametersObject (readonly)



21
22
23
# File 'lib/puppet/parser/resource.rb', line 21

def parameters
  @parameters
end

#scopeObject



17
18
19
# File 'lib/puppet/parser/resource.rb', line 17

def scope
  @scope
end

#sourceObject



17
18
19
# File 'lib/puppet/parser/resource.rb', line 17

def source
  @source
end

#translatedObject



18
19
20
# File 'lib/puppet/parser/resource.rb', line 18

def translated
  @translated
end

#virtualObject



18
19
20
# File 'lib/puppet/parser/resource.rb', line 18

def virtual
  @virtual
end

Class Method Details

.relationship_parameter?(name) ⇒ Boolean

Determine whether the provided parameter name is a relationship parameter.

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/puppet/parser/resource.rb', line 24

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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet/parser/resource.rb', line 34

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_stageObject

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/parser/resource.rb', line 59

def add_edge_to_stage
  return unless self.class?

  unless stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main)
    raise ArgumentError, "Could not find stage #{self[:stage] || :main} specified by #{self}"
  end

  self[:stage] ||= stage.title unless stage.title == :main
  catalog.add_edge(stage, self)
end

#add_parameters_from_consumeObject

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.

Fills resource params from a capability

This backs ‘consumes => Sql



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/puppet/parser/resource.rb', line 239

def add_parameters_from_consume
  return if self[:consume].nil?

  map = {}
  [ self[:consume] ].flatten.map do |ref|
    # Assert that the ref really is a resource reference
    raise Puppet::Error, "Invalid consume in #{self.ref}: #{ref} is not a resource" unless ref.is_a?(Puppet::Resource)

    # Resolve references
    t = ref.type
    t = Puppet::Pops::Evaluator::Runtime3ResourceSupport.find_resource_type(scope, t) unless t == 'class' || t == 'node'
    cap = catalog.resource(t, ref.title)
    if cap.nil?
      raise "Resource #{ref} could not be found; it might not have been produced yet"
    end

    # Ensure that the found resource is a capability resource
    raise Puppet::Error, "Invalid consume in #{ref}: #{cap} is not a capability resource" unless cap.resource_type.is_capability?
    cap
  end.each do |cns|
    # Establish mappings
    blueprint = resource_type.consumes.find do |bp|
      bp[:capability] == cns.type
    end
    # @todo lutter 2015-08-03: catch this earlier, can we do this during
    # static analysis ?
    raise "Resource #{self} tries to consume #{cns} but no 'consumes' mapping exists for #{self.resource_type} and #{cns.type}" unless blueprint

    # setup scope that has, for each attr of cns, a binding to cns[attr]
    scope.with_global_scope do |global_scope|
      cns_scope = global_scope.newscope(:source => self, :resource => self)
      cns.to_hash.each { |name, value| cns_scope[name.to_s] = value }

      # evaluate mappings in that scope
      resource_type.arguments.keys.each do |name|
        if expr = blueprint[:mappings][name]
          # Explicit mapping
          value = expr.safeevaluate(cns_scope)
        else
          value = cns[name]
        end
        unless value.nil?
          # @todo lutter 2015-07-01: this should be caught by the checker
          # much earlier. We consume several capres, at least two of which
          # want to map to the same parameter (PUP-5080)
          raise "Attempt to reassign attribute '#{name}' in '#{self}' caused by multiple consumed mappings to the same attribute" if map[name]
          map[name] = value
        end
      end
    end
  end

  map.each { |name, value| self[name] = value if self[name].nil? }
end

#eachparamObject



46
47
48
49
50
# File 'lib/puppet/parser/resource.rb', line 46

def eachparam
  @parameters.each do |name, param|
    yield param
  end
end

#environmentObject



52
53
54
# File 'lib/puppet/parser/resource.rb', line 52

def environment
  scope.environment
end

#evaluateObject

Retrieve the associated definition and evaluate it.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet/parser/resource.rb', line 71

def evaluate
  return if evaluated?
  Puppet::Util::Profiler.profile("Evaluated resource #{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

Returns:

  • (Boolean)


32
# File 'lib/puppet/parser/resource.rb', line 32

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.

Parameters:

  • do_validate (Boolean) (defaults to: true)

    true if validation should be performed



114
115
116
117
118
119
120
# File 'lib/puppet/parser/resource.rb', line 114

def finish(do_validate = true)
  return if finished?
  @finished = true
  finish_evaluation
  replace_sensitive_data
  validate if do_validate
end

#finish_evaluationObject

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



100
101
102
103
104
105
# File 'lib/puppet/parser/resource.rb', line 100

def finish_evaluation
  return if @evaluation_finished
  add_defaults
  add_scope_tags
  @evaluation_finished = true
end

#finished?Boolean

Has this resource already been finished?

Returns:

  • (Boolean)


123
124
125
# File 'lib/puppet/parser/resource.rb', line 123

def finished?
  @finished
end

#is_unevaluated_consumer?Boolean

Returns:

  • (Boolean)


144
145
146
147
# File 'lib/puppet/parser/resource.rb', line 144

def is_unevaluated_consumer?
  # We don't declare a new variable here just to test. Saves memory
  instance_variable_defined?(:@unevaluated_consumer)
end

#isomorphic?Boolean

Is this resource modeling an isomorphic resource type?

Returns:

  • (Boolean)


136
137
138
139
140
141
142
# File 'lib/puppet/parser/resource.rb', line 136

def isomorphic?
  if builtin_type?
    return resource_type.isomorphic?
  else
    return true
  end
end

#mark_unevaluated_consumerObject



149
150
151
# File 'lib/puppet/parser/resource.rb', line 149

def mark_unevaluated_consumer
  @unevaluated_consumer = true
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
# 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
  # 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

#nameObject



167
168
169
# File 'lib/puppet/parser/resource.rb', line 167

def name
  self[:name] || self.title
end

#offsetObject



294
295
296
# File 'lib/puppet/parser/resource.rb', line 294

def offset
  nil
end

#override?Boolean

Returns:

  • (Boolean)


31
# File 'lib/puppet/parser/resource.rb', line 31

def override?;   !!@override;   end

#posObject



298
299
300
# File 'lib/puppet/parser/resource.rb', line 298

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.

Parameters:

  • tag_array (Array[String])

    list tags to look for

Returns:

  • (Boolean)

    true if this instance is tagged with at least one of the provided tags



231
232
233
# File 'lib/puppet/parser/resource.rb', line 231

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.



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/puppet/parser/resource.rb', line 177

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_hashObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/puppet/parser/resource.rb', line 192

def to_hash
  @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_ralObject

Convert this resource to a RAL resource.



213
214
215
# File 'lib/puppet/parser/resource.rb', line 213

def to_ral
  copy_as_resource.to_ral
end

#translated?Boolean

Set up some boolean test methods

Returns:

  • (Boolean)


30
# File 'lib/puppet/parser/resource.rb', line 30

def translated?; !!@translated; end