Class: Puppet::Parser::Resource

Inherits:
Resource show all
Includes:
YamlTrimmer, Resource::TypeCollectionHelper, 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::YAML_ATTRIBUTES

Constants included from Indirector

Indirector::BadNameRegexp

Constants included from Util::Tagging

Util::Tagging::ValidTagRegex

Instance Attribute Summary collapse

Attributes inherited from Resource

#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 included from Resource::TypeCollectionHelper

#known_resource_types

Methods inherited from Resource

#==, #builtin?, #builtin_type?, #class?, #copy_as_resource, #each, #environment=, #export, from_data_hash, #include?, #inspect, #is_application_component?, #is_capability?, #key_attributes, #prune_parameters, #ref, #resolve, #resource_type, #resource_type=, #set_default_parameters, #stage?, #to_data_hash, #to_hierayaml, #to_manifest, #to_ref, #to_s, #to_yaml_properties, #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)


120
121
122
123
124
125
126
# File 'lib/puppet/parser/resource.rb', line 120

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



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

def catalog
  @catalog
end

#collector_idObject



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

def collector_id
  @collector_id
end

#evaluatedObject



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

def evaluated
  @evaluated
end

#exportedObject



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

def exported
  @exported
end

#fileObject



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

def file
  @file
end

#lineObject



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

def line
  @line
end

#overrideObject



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

def override
  @override
end

#parametersObject (readonly)



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

def parameters
  @parameters
end

#scopeObject



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

def scope
  @scope
end

#sourceObject



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

def source
  @source
end

#translatedObject



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

def translated
  @translated
end

#virtualObject



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

def virtual
  @virtual
end

Class Method Details

.relationship_parameter?(name) ⇒ Boolean

Determine whether the provided parameter name is a relationship parameter.

Returns:

  • (Boolean)


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

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet/parser/resource.rb', line 37

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.



62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/parser/resource.rb', line 62

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



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
# File 'lib/puppet/parser/resource.rb', line 220

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
    cap = catalog.resource(ref.type, 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



49
50
51
52
53
# File 'lib/puppet/parser/resource.rb', line 49

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

#environmentObject



55
56
57
# File 'lib/puppet/parser/resource.rb', line 55

def environment
  scope.environment
end

#evaluateObject

Retrieve the associated definition and evaluate it.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/parser/resource.rb', line 74

def evaluate
  return if evaluated?
  Puppet::Util::Profiler.profile("Evaluated resource #{self}", [:compiler, :evaluate_resource, self]) do
    @evaluated = true
    if builtin?
      devfail "Cannot evaluate a builtin type (#{type})"
    elsif resource_type.nil?
      self.fail "Cannot find definition #{type}"
    else
      finish(false) # Call finish but do not validate
      resource_type.evaluate_code(self)
    end
  end
end

#evaluated?Boolean

Returns:

  • (Boolean)


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

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 evaluation or 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



107
108
109
110
111
112
113
# File 'lib/puppet/parser/resource.rb', line 107

def finish(do_validate = true)
  return if finished?
  @finished = true
  add_defaults
  add_scope_tags
  validate if do_validate
end

#finished?Boolean

Has this resource already been finished?

Returns:

  • (Boolean)


116
117
118
# File 'lib/puppet/parser/resource.rb', line 116

def finished?
  @finished
end

#is_unevaluated_consumer?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#mark_unevaluated_consumerObject



142
143
144
# File 'lib/puppet/parser/resource.rb', line 142

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.



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/puppet/parser/resource.rb', line 148

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.line, resource.file)
  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



160
161
162
# File 'lib/puppet/parser/resource.rb', line 160

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

#offsetObject



273
274
275
# File 'lib/puppet/parser/resource.rb', line 273

def offset
  nil
end

#override?Boolean

Returns:

  • (Boolean)


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

def override?;   !!@override;   end

#posObject



277
278
279
# File 'lib/puppet/parser/resource.rb', line 277

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



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

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.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/puppet/parser/resource.rb', line 170

def set_parameter(param, value = nil)
  if ! param.is_a?(Puppet::Parser::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



184
185
186
187
188
189
190
191
# File 'lib/puppet/parser/resource.rb', line 184

def to_hash
  @parameters.inject({}) do |hash, ary|
    param = ary[1]
    # Skip "undef" and nil values.
    hash[param.name] = param.value if param.value != :undef && !param.value.nil?
    hash
  end
end

#to_ralObject

Convert this resource to a RAL resource.



194
195
196
# File 'lib/puppet/parser/resource.rb', line 194

def to_ral
  copy_as_resource.to_ral
end

#translated?Boolean

Set up some boolean test methods

Returns:

  • (Boolean)


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

def translated?; !!@translated; end