Class: Puppet::Parser::Resource

Inherits:
Resource show all
Includes:
YamlTrimmer, Resource::TypeCollectionHelper, Util, Util::Errors, Util::Logging, Util::MethodHelper, Util::Tagging
Defined in:
lib/vendor/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

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Constants inherited from Resource

Resource::ATTRIBUTES, Resource::Reference

Constants included from Indirector

Indirector::BadNameRegexp

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

#tag, #tagged?, #tags, #tags=

Methods included from Util::Logging

#clear_deprecation_warnings, #deprecation_warning, #send_log

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?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

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

Methods included from Resource::TypeCollectionHelper

#known_resource_types

Methods inherited from Resource

#==, #builtin?, #builtin_type?, #each, #environment=, from_pson, #include?, #inspect, #key_attributes, #prune_parameters, #ref, #resolve, #resource_type, #to_manifest, #to_pson, #to_pson_data_hash, #to_ref, #to_s, #to_trans_ref, #to_transobject, #uniqueness_key, #valid_parameter?, #validate_parameter, value_to_pson_data, #yaml_property_munge

Methods included from Util::Pson

#pson_create

Methods included from Indirector

configure_routes, #indirects

Constructor Details

#initialize(*args) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (ArgumentError)


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

def initialize(*args)
  raise ArgumentError, "Resources require a scope" unless args.last[:scope]
  super

  @source ||= scope.source
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



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

def catalog
  @catalog
end

#collector_idObject

Returns the value of attribute collector_id.



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

def collector_id
  @collector_id
end

#evaluatedObject

Returns the value of attribute evaluated.



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

def evaluated
  @evaluated
end

#exportedObject

Returns the value of attribute exported.



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

def exported
  @exported
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

#overrideObject

Returns the value of attribute override.



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

def override
  @override
end

#parametersObject (readonly)

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#translatedObject

Returns the value of attribute translated.



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

def translated
  @translated
end

#virtualObject

Returns the value of attribute virtual.



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

def virtual
  @virtual
end

Class Method Details

.relationship_parameter?(name) ⇒ Boolean

Determine whether the provided parameter name is a relationship parameter.

Returns:

  • (Boolean)


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

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



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

def [](param)
  param = param.intern
  if param == :title
    return self.title
  end
  if @parameters.has_key?(param)
    @parameters[param].value
  else
    nil
  end
end

#[]=(param, value) ⇒ Object



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

def []=(param, value)
  set_parameter(param, value)
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.



67
68
69
70
71
72
73
74
75
76
# File 'lib/vendor/puppet/parser/resource.rb', line 67

def add_edge_to_stage
  return unless self.type.to_s.downcase == "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

#eachparamObject



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

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

#environmentObject



60
61
62
# File 'lib/vendor/puppet/parser/resource.rb', line 60

def environment
  scope.environment
end

#evaluateObject

Retrieve the associated definition and evaluate it.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vendor/puppet/parser/resource.rb', line 79

def evaluate
  return if evaluated?
  @evaluated = true
  if klass = resource_type and ! builtin_type?
    finish
    evaluated_code = klass.evaluate_code(self)

    return evaluated_code
  elsif builtin?
    devfail "Cannot evaluate a builtin type (#{type})"
  else
    self.fail "Cannot find definition #{type}"
  end
end

#evaluated?Boolean

Returns:

  • (Boolean)


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

def evaluated?;  !!@evaluated;  end

#finishObject

Do any finishing work on this object, called before evaluation or before storage/translation.



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

def finish
  return if finished?
  @finished = true
  add_defaults
  add_metaparams
  add_scope_tags
  validate
end

#finished?Boolean

Has this resource already been finished?

Returns:

  • (Boolean)


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

def finished?
  @finished
end

#isomorphic?Boolean

Is this resource modeling an isomorphic resource type?

Returns:

  • (Boolean)


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

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.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/vendor/puppet/parser/resource.rb', line 139

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

#metaparam_compatibility_mode?Boolean

Unless we’re running >= 0.25, we’re in compat mode.

Returns:

  • (Boolean)


152
153
154
# File 'lib/vendor/puppet/parser/resource.rb', line 152

def metaparam_compatibility_mode?
  ! (catalog and ver = (catalog.client_version||'0.0.0').split(".") and (ver[0] > "0" or ver[1].to_i >= 25))
end

#nameObject



156
157
158
# File 'lib/vendor/puppet/parser/resource.rb', line 156

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

#override?Boolean

Returns:

  • (Boolean)


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

def override?;   !!@override;   end

#pathObject

A temporary occasion, until I get paths in the scopes figured out.



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

def path
  to_s
end

#set_parameter(param, value = nil) ⇒ Object

Define a parameter in our resource. if we ever receive a parameter named ‘tag’, set the resource tags with its value.



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

def set_parameter(param, value = nil)
  if ! value.nil?
    param = Puppet::Parser::Resource::Param.new(
      :name => param, :value => value, :source => self.source
    )
  elsif ! param.is_a?(Puppet::Parser::Resource::Param)
    raise ArgumentError, "Must pass a parameter or all necessary values"
  end

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

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

#to_hashObject



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

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

#to_ralObject

Convert this resource to a RAL resource. We hackishly go via the transportable stuff.



240
241
242
# File 'lib/vendor/puppet/parser/resource.rb', line 240

def to_ral
  to_resource.to_ral
end

#to_resourceObject

Create a Puppet::Resource instance from this parser resource. We plan, at some point, on not needing to do this conversion, but it’s sufficient for now.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/vendor/puppet/parser/resource.rb', line 196

def to_resource
  result = Puppet::Resource.new(type, title)

  to_hash.each do |p, v|
    if v.is_a?(Puppet::Resource)
      v = Puppet::Resource.new(v.type, v.title)
    elsif v.is_a?(Array)
      # flatten resource references arrays
      v = v.flatten if v.flatten.find { |av| av.is_a?(Puppet::Resource) }
      v = v.collect do |av|
        av = Puppet::Resource.new(av.type, av.title) if av.is_a?(Puppet::Resource)
        av
      end
    end

    # If the value is an array with only one value, then
    # convert it to a single value.  This is largely so that
    # the database interaction doesn't have to worry about
    # whether it returns an array or a string.
    result[p] = if v.is_a?(Array) and v.length == 1
      v[0]
        else
          v
            end
  end

  result.file = self.file
  result.line = self.line
  result.exported = self.exported
  result.virtual = self.virtual
  result.tag(*self.tags)

  result
end

#to_transObject

Translate our object to a transportable object.



232
233
234
235
236
# File 'lib/vendor/puppet/parser/resource.rb', line 232

def to_trans
  return nil if virtual?

  to_resource.to_trans
end

#translated?Boolean

Set up some boolean test methods

Returns:

  • (Boolean)


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

def translated?; !!@translated; end