Class: Puppet::Resource

Inherits:
Object show all
Extended by:
Indirector, Util::Pson
Includes:
Enumerable, TypeCollectionHelper, Util::Tagging
Defined in:
lib/puppet/resource.rb,
lib/puppet/resource/status.rb

Overview

The simplest resource class. Eventually it will function as the base class for all resource-like behaviour.

Direct Known Subclasses

Parser::Resource

Defined Under Namespace

Modules: TypeCollectionHelper Classes: Catalog, Ral, Rest, Status, Type, TypeCollection

Constant Summary collapse

Reference =

This stub class is only needed for serialization compatibility with 0.25.x. Specifically, it exists to provide a compatibility API when using YAML serialized objects loaded from StoreConfigs.

Puppet::Resource
ATTRIBUTES =
[:file, :line, :exported]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Pson

pson_create

Methods included from Indirector

indirects

Methods included from TypeCollectionHelper

#known_resource_types

Methods included from Util::Tagging

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

Constructor Details

#initialize(type, title = nil, attributes = {}) ⇒ Resource

Create our resource.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/puppet/resource.rb', line 179

def initialize(type, title = nil, attributes = {})
  @parameters = {}

  # Set things like strictness first.
  attributes.each do |attr, value|
    next if attr == :parameters
    send(attr.to_s + "=", value)
  end

  @type, @title = extract_type_and_title(type, title)

  @type = munge_type_name(@type)

  if @type == "Class"
    @title = :main if @title == ""
    @title = munge_type_name(@title)
  end

  if params = attributes[:parameters]
    extract_parameters(params)
  end

  tag(self.type)
  tag(self.title) if valid_tag?(self.title)

  @reference = self # for serialization compatibility with 0.25.x
  if strict? and ! resource_type
    if @type == 'Class'
      raise ArgumentError, "Could not find declared class #{title}"
    else
      raise ArgumentError, "Invalid resource type #{type}"
    end
  end
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



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

def catalog
  @catalog
end

#exportedObject

Returns the value of attribute exported.



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

def exported
  @exported
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

#strictObject

Returns the value of attribute strict.



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

def strict
  @strict
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#validate_parametersObject

Returns the value of attribute validate_parameters.



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

def validate_parameters
  @validate_parameters
end

#virtualObject

Returns the value of attribute virtual.



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

def virtual
  @virtual
end

Class Method Details

.from_pson(pson) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/resource.rb', line 29

def self.from_pson(pson)
  raise ArgumentError, "No resource type provided in pson data" unless type = pson['type']
  raise ArgumentError, "No resource title provided in pson data" unless title = pson['title']

  resource = new(type, title)

  if params = pson['parameters']
    params.each { |param, value| resource[param] = value }
  end

  if tags = pson['tags']
    tags.each { |tag| resource.tag(tag) }
  end

  ATTRIBUTES.each do |a|
    if value = pson[a.to_s]
      resource.send(a.to_s + "=", value)
    end
  end

  resource.exported ||= false

  resource
end

.value_to_pson_data(value) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/puppet/resource.rb', line 82

def self.value_to_pson_data(value)
  if value.is_a? Array
    value.map{|v| value_to_pson_data(v) }
  elsif value.is_a? Puppet::Resource
    value.to_s
  else
    value
  end
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  return false unless other.respond_to?(:title) and self.type == other.type and self.title == other.title

  return false unless to_hash == other.to_hash
  true
end

#[](param) ⇒ Object

Return a given parameter’s value. Converts all passed names to lower-case symbols.



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

def [](param)
  parameters[parameter_name(param)]
end

#[]=(param, value) ⇒ Object

Set a given parameter. Converts all passed names to lower-case symbols.



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

def []=(param, value)
  validate_parameter(param) if validate_parameters
  parameters[parameter_name(param)] = value
end

#builtin?Boolean

Compatibility method.

Returns:

  • (Boolean)


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

def builtin?
  builtin_type?
end

#builtin_type?Boolean

Is this a builtin resource type?

Returns:

  • (Boolean)


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

def builtin_type?
  resource_type.is_a?(Class)
end

#eachObject

Iterate over each param/value pair, as required for Enumerable.



147
148
149
# File 'lib/puppet/resource.rb', line 147

def each
  parameters.each { |p,v| yield p, v }
end

#environmentObject

These two methods are extracted into a Helper module, but file load order prevents me from including them in the class, and I had weird behaviour (i.e., sometimes it didn’t work) when I directly extended each resource with the helper.



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

def environment
  Puppet::Node::Environment.new(@environment)
end

#environment=(env) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/puppet/resource.rb', line 164

def environment=(env)
  if env.is_a?(String) or env.is_a?(Symbol)
    @environment = env
  else
    @environment = env.name
  end
end

#include?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/puppet/resource.rb', line 151

def include?(parameter)
  super || parameters.keys.include?( parameter_name(parameter) )
end

#inspectObject



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

def inspect
  "#{@type}[#{@title}]#{to_hash.inspect}"
end

#key_attributesObject



249
250
251
# File 'lib/puppet/resource.rb', line 249

def key_attributes
  return(resource_type.respond_to? :key_attributes) ? resource_type.key_attributes : [:name]
end

#nameObject



339
340
341
342
343
344
# File 'lib/puppet/resource.rb', line 339

def name
  # this is potential namespace conflict
  # between the notion of an "indirector name"
  # and a "resource name"
  [ type, title ].join('/')
end

#refObject



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

def ref
  to_s
end

#resolveObject

Find our resource.



219
220
221
# File 'lib/puppet/resource.rb', line 219

def resolve
  return(catalog ? catalog.resource(to_s) : nil)
end

#resource_typeObject



223
224
225
226
227
228
229
230
# File 'lib/puppet/resource.rb', line 223

def resource_type
  case type
  when "Class"; known_resource_types.hostclass(title == :main ? "" : title)
  when "Node"; known_resource_types.node(title)
  else
    Puppet::Type.type(type.to_s.downcase.to_sym) || known_resource_types.definition(type)
  end
end

#to_hashObject

Produce a simple hash of our parameters.



233
234
235
# File 'lib/puppet/resource.rb', line 233

def to_hash
  parse_title.merge parameters
end

#to_manifestObject

Convert our resource to Puppet code.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/puppet/resource.rb', line 254

def to_manifest
  # Collect list of attributes to align => and move ensure first
  attr = parameters.keys
  attr_max = attr.inject(0) { |max,k| k.to_s.length > max ? k.to_s.length : max }

  attr.sort!
  if attr.first != :ensure  && attr.include?(:ensure)
    attr.delete(:ensure)
    attr.unshift(:ensure)
  end

  attributes = attr.collect { |k|
    v = parameters[k]
    if v.is_a? Array
      "  %-#{attr_max}s => %s,\n" % [ k, "[\'#{v.join("', '")}\']" ]
    else
      "  %-#{attr_max}s => %s,\n" % [ k, "\'#{v}\'" ]
    end
  }

  "%s { '%s':\n%s}" % [self.type.to_s.downcase, self.title, attributes]
end

#to_pson(*args) ⇒ Object



104
105
106
# File 'lib/puppet/resource.rb', line 104

def to_pson(*args)
  to_pson_data_hash.to_pson(*args)
end

#to_pson_data_hashObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/resource.rb', line 58

def to_pson_data_hash
  data = ([:type, :title, :tags] + ATTRIBUTES).inject({}) do |hash, param|
    next hash unless value = self.send(param)
    hash[param.to_s] = value
    hash
  end

  data["exported"] ||= false

  params = self.to_hash.inject({}) do |hash, ary|
    param, value = ary

    # Don't duplicate the title as the namevar
    next hash if param == namevar and value == title

    hash[param] = Puppet::Resource.value_to_pson_data(value)
    hash
  end

  data["parameters"] = params unless params.empty?

  data
end

#to_ralObject

Convert our resource to a RAL resource instance. Creates component instances for resource types that don’t exist.



283
284
285
286
287
288
289
# File 'lib/puppet/resource.rb', line 283

def to_ral
  if typeklass = Puppet::Type.type(self.type)
    return typeklass.new(self)
  else
    return Puppet::Type::Component.new(self)
  end
end

#to_refObject



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

def to_ref
  ref
end

#to_resourceObject



346
347
348
# File 'lib/puppet/resource.rb', line 346

def to_resource
  self
end

#to_sObject



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

def to_s
  "#{type}[#{title}]"
end

#to_transObject

Translate our object to a backward-compatible transportable object.



292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/puppet/resource.rb', line 292

def to_trans
  if builtin_type? and type.downcase.to_s != "stage"
    result = to_transobject
  else
    result = to_transbucket
  end

  result.file = self.file
  result.line = self.line

  result
end

#to_trans_refObject



305
306
307
# File 'lib/puppet/resource.rb', line 305

def to_trans_ref
  [type.to_s, title.to_s]
end

#to_transobjectObject

Create an old-style TransObject instance, for builtin resource types.



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/puppet/resource.rb', line 310

def to_transobject
  # Now convert to a transobject
  result = Puppet::TransObject.new(title, type)
  to_hash.each do |p, v|
    if v.is_a?(Puppet::Resource)
      v = v.to_trans_ref
    elsif v.is_a?(Array)
      v = v.collect { |av|
        av = av.to_trans_ref if av.is_a?(Puppet::Resource)
        av
      }
    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.to_s] = if v.is_a?(Array) and v.length == 1
      v[0]
        else
          v
            end
  end

  result.tags = self.tags

  result
end

#uniqueness_keyObject



241
242
243
244
245
246
247
# File 'lib/puppet/resource.rb', line 241

def uniqueness_key
  # Temporary kludge to deal with inconsistant use patters
  h = self.to_hash
  h[namevar] ||= h[:name]
  h[:name]   ||= h[namevar]
  h.values_at(*key_attributes.sort_by { |k| k.to_s })
end

#valid_parameter?(name) ⇒ Boolean

Returns:

  • (Boolean)


350
351
352
# File 'lib/puppet/resource.rb', line 350

def valid_parameter?(name)
  resource_type.valid_parameter?(name)
end

#validate_parameter(name) ⇒ Object

Raises:

  • (ArgumentError)


354
355
356
# File 'lib/puppet/resource.rb', line 354

def validate_parameter(name)
  raise ArgumentError, "Invalid parameter #{name}" unless valid_parameter?(name)
end

#yaml_property_munge(x) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/puppet/resource.rb', line 92

def yaml_property_munge(x)
  case x
  when Hash
    x.inject({}) { |h,kv|
      k,v = kv
      h[k] = self.class.value_to_pson_data(v)
      h
    }
  else self.class.value_to_pson_data(x)
  end
end