Class: Puppet::Parser::Resource

Inherits:
Resource show all
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

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

configure_routes, #indirects

Methods included from Util::PsychSupport

#encode_with, #init_with

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.

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/puppet/parser/resource.rb', line 129

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|
      next if @parameters.include?(name)

      debug "Adding default for #{name}"

      param = param.dup
      @parameters[name] = param
      tag(*param.value) if param.name == :tag
    end
  end
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



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

def catalog
  @catalog
end

#collector_idObject

Returns the value of attribute collector_id.



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

def collector_id
  @collector_id
end

#evaluatedObject

Returns the value of attribute evaluated.



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

def evaluated
  @evaluated
end

#exportedObject

Returns the value of attribute exported.



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

def exported
  @exported
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#kindObject

Returns the value of attribute kind.



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

def kind
  @kind
end

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

#overrideObject

Returns the value of attribute override.



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

def override
  @override
end

#parametersObject (readonly)

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#translatedObject

Returns the value of attribute translated.



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

def translated
  @translated
end

#virtualObject

Returns the value of attribute virtual.



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

def virtual
  @virtual
end

Class Method Details

.relationship_parameter?(name) ⇒ Boolean

Determine whether the provided parameter name is a relationship parameter.

Returns:

  • (Boolean)


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

def self.relationship_parameter?(name)
  @relationship_names ||= Puppet::Type.relationship_params.collect(&:name)
  @relationship_names.include?(name)
end

Instance Method Details

#[](param) ⇒ Object



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

def [](param)
  param = param.intern
  if param == :title
    return 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
69
# File 'lib/puppet/parser/resource.rb', line 59

def add_edge_to_stage
  return unless 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

#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.



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

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

Returns:

  • (Boolean)


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

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



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

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



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

def finish_evaluation
  return if @evaluation_finished

  add_scope_tags
  @evaluation_finished = true
end

#finished?Boolean

Has this resource already been finished?

Returns:

  • (Boolean)


125
126
127
# File 'lib/puppet/parser/resource.rb', line 125

def finished?
  @finished
end

#isomorphic?Boolean

Is this resource modeling an isomorphic resource type?

Returns:

  • (Boolean)


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

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

#merge(resource) ⇒ Object

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/puppet/parser/resource.rb', line 161

def merge(resource)
  # Test the resource scope, to make sure the resource is even allowed
  # to override.
  unless source.equal?(resource.source) || resource.source.child_of?(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

#nameObject



192
193
194
# File 'lib/puppet/parser/resource.rb', line 192

def name
  self[:name] || title
end

#offsetObject



258
259
260
# File 'lib/puppet/parser/resource.rb', line 258

def offset
  nil
end

#override?Boolean

Returns:

  • (Boolean)


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

def override?;   !!@override;   end

#posObject



262
263
264
# File 'lib/puppet/parser/resource.rb', line 262

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



254
255
256
# File 'lib/puppet/parser/resource.rb', line 254

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.



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/puppet/parser/resource.rb', line 202

def set_parameter(param, value = nil)
  unless 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 => source
    )
  end

  tag(*param.value) if param.name == :tag

  # And store it in our parameter hash.
  @parameters[param.name] = param
end

#to_hashObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/puppet/parser/resource.rb', line 217

def to_hash
  parse_title.merge(@parameters.each_with_object({}) do |(_, param), result|
    value = param.value
    value = (:undef == value) ? nil : value

    next if 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
    else
    end
    result[param.name] = value
  end)
end

#to_ralObject

Convert this resource to a RAL resource.



236
237
238
# File 'lib/puppet/parser/resource.rb', line 236

def to_ral
  copy_as_resource.to_ral
end

#translated?Boolean

Set up some boolean test methods

Returns:

  • (Boolean)


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

def translated?; !!@translated; end