Class: Puppet::Parser::Resource

Inherits:
Resource show all
Includes:
FileCollection::Lookup, YamlTrimmer, Resource::TypeCollectionHelper, Util, Util::Errors, Util::Logging, Util::MethodHelper, Util::Tagging
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 inherited from Resource

Resource::ATTRIBUTES, Resource::Reference

Instance Attribute Summary collapse

Attributes included from FileCollection::Lookup

#file_index, #line

Attributes inherited from Resource

#file, #line, #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

#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

activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, 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 included from FileCollection::Lookup

#file, #file=, #file_collection

Methods inherited from Resource

#==, #builtin?, #builtin_type?, #each, #environment=, from_pson, #include?, #inspect, #key_attributes, #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

#indirects

Constructor Details

#initialize(*args) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (ArgumentError)


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

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.



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

def catalog
  @catalog
end

#evaluatedObject

Returns the value of attribute evaluated.



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

def evaluated
  @evaluated
end

#exportedObject

Returns the value of attribute exported.



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

def exported
  @exported
end

#overrideObject

Returns the value of attribute override.



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

def override
  @override
end

#parametersObject (readonly)

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#rails_idObject

Returns the value of attribute rails_id.



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

def rails_id
  @rails_id
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#translatedObject

Returns the value of attribute translated.



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

def translated
  @translated
end

#virtualObject

Returns the value of attribute virtual.



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

def virtual
  @virtual
end

Class Method Details

.relationship_parameter?(name) ⇒ Boolean

Determine whether the provided parameter name is a relationship parameter.

Returns:

  • (Boolean)


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

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



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

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

#[]=(param, value) ⇒ Object



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

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.



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

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



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

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

#environmentObject



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

def environment
  scope.environment
end

#evaluateObject

Retrieve the associated definition and evaluate it.



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

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

    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)


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

def evaluated?;  !!@evaluated;  end

#finishObject

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



109
110
111
112
113
114
115
116
# File 'lib/puppet/parser/resource.rb', line 109

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)


119
120
121
# File 'lib/puppet/parser/resource.rb', line 119

def finished?
  @finished
end

#isomorphic?Boolean

Is this resource modeling an isomorphic resource type?

Returns:

  • (Boolean)


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

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.



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

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)


154
155
156
# File 'lib/puppet/parser/resource.rb', line 154

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



158
159
160
# File 'lib/puppet/parser/resource.rb', line 158

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

#override?Boolean

Returns:

  • (Boolean)


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

def override?;   !!@override;   end

#pathObject

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



163
164
165
# File 'lib/puppet/parser/resource.rb', line 163

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.



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

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



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

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.



242
243
244
# File 'lib/puppet/parser/resource.rb', line 242

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.



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
230
231
# File 'lib/puppet/parser/resource.rb', line 198

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.



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

def to_trans
  return nil if virtual?

  to_resource.to_trans
end

#translated?Boolean

Set up some boolean test methods

Returns:

  • (Boolean)


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

def translated?; !!@translated; end