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, Validator Classes: ActiveRecord, Catalog, Ral, Rest, Status, StoreConfigs, 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]

Constants included from Indirector

Indirector::BadNameRegexp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Pson

pson_create

Methods included from Indirector

configure_routes, 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.



178
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
# File 'lib/puppet/resource.rb', line 178

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.



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

def catalog
  @catalog
end

#exportedObject

Returns the value of attribute exported.



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

def exported
  @exported
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

#strictObject

Returns the value of attribute strict.



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

def strict
  @strict
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#validate_parametersObject

Returns the value of attribute validate_parameters.



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

def validate_parameters
  @validate_parameters
end

#virtualObject

Returns the value of attribute virtual.



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

def virtual
  @virtual
end

Class Method Details

.from_pson(pson) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.from_pson(pson)
  raise ArgumentError, "No resource type provided in serialized data" unless type = pson['type']
  raise ArgumentError, "No resource title provided in serialized 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
end

.value_to_pson_data(value) ⇒ Object



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

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



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

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.



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

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

#[]=(param, value) ⇒ Object

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



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

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

#builtin?Boolean

Compatibility method.



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

def builtin?
  builtin_type?
end

#builtin_type?Boolean

Is this a builtin resource type?



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

def builtin_type?
  resource_type.is_a?(Class)
end

#eachObject

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



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

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.



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

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

#environment=(env) ⇒ Object



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

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

#include?(parameter) ⇒ Boolean



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

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

#inspectObject



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

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

#key_attributesObject



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

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

#nameObject



334
335
336
337
338
339
# File 'lib/puppet/resource.rb', line 334

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

#prune_parameters(options = {}) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/puppet/resource.rb', line 353

def prune_parameters(options = {})
  properties = resource_type.properties.map(&:name)

  dup.collect do |attribute, value|
    if value.to_s.empty? or Array(value).empty?
      delete(attribute)
    elsif value.to_s == "absent" and attribute.to_s != "ensure"
      delete(attribute)
    end

    parameters_to_include = options[:parameters_to_include] || []
    delete(attribute) unless properties.include?(attribute) || parameters_to_include.include?(attribute)
  end
  self
end

#refObject



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

def ref
  to_s
end

#resolveObject

Find our resource.



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

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

#resource_typeObject



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

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.



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

def to_hash
  parse_title.merge parameters
end

#to_manifestObject

Convert our resource to Puppet code.



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

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]
    "  %-#{attr_max}s => %s,\n" % [k, Puppet::Parameter.format_value_for_display(v)]
  }.join

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

#to_pson(*args) ⇒ Object



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

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

#to_pson_data_hashObject



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

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.



278
279
280
281
282
283
284
# File 'lib/puppet/resource.rb', line 278

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



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

def to_ref
  ref
end

#to_resourceObject



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

def to_resource
  self
end

#to_sObject



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

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

#to_transObject

Translate our object to a backward-compatible transportable object.



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/puppet/resource.rb', line 287

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



300
301
302
# File 'lib/puppet/resource.rb', line 300

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

#to_transobjectObject

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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/puppet/resource.rb', line 305

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



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

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



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

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

#validate_parameter(name) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#yaml_property_munge(x) ⇒ Object



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

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