Class: Puppet::Resource
Overview
The simplest resource class. Eventually it will function as the base class for all resource-like behaviour.
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]
- YAML_ATTRIBUTES =
[:@file, :@line, :@exported, :@type, :@title, :@tags, :@parameters]
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
#known_resource_types
#tag, #tagged?, #tags, #tags=
Constructor Details
#initialize(type, title = nil, attributes = {}) ⇒ Resource
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
232
233
234
235
236
237
238
|
# File 'lib/puppet/resource.rb', line 205
def initialize(type, title = nil, attributes = {})
@parameters = {}
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 self.class?
@title = :main if @title == ""
@title = munge_type_name(@title)
end
if params = attributes[:parameters]
(params)
end
tag(self.type)
tag(self.title) if valid_tag?(self.title)
@reference = self if strict? and ! resource_type
if self.class?
raise ArgumentError, "Could not find declared class #{title}"
else
raise ArgumentError, "Invalid resource type #{type}"
end
end
end
|
Instance Attribute Details
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def catalog
@catalog
end
|
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def exported
@exported
end
|
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def file
@file
end
|
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def line
@line
end
|
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def strict
@strict
end
|
24
25
26
|
# File 'lib/puppet/resource.rb', line 24
def title
@title
end
|
24
25
26
|
# File 'lib/puppet/resource.rb', line 24
def type
@type
end
|
#validate_parameters ⇒ Object
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def validate_parameters
@validate_parameters
end
|
23
24
25
|
# File 'lib/puppet/resource.rb', line 23
def virtual
@virtual
end
|
Class Method Details
.from_pson(pson) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/puppet/resource.rb', line 32
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
88
89
90
91
92
93
94
95
96
|
# File 'lib/puppet/resource.rb', line 88
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
147
148
149
150
151
152
|
# File 'lib/puppet/resource.rb', line 147
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.
143
144
145
|
# File 'lib/puppet/resource.rb', line 143
def [](param)
parameters[parameter_name(param)]
end
|
#[]=(param, value) ⇒ Object
Set a given parameter. Converts all passed names to lower-case symbols.
136
137
138
139
|
# File 'lib/puppet/resource.rb', line 136
def []=(param, value)
validate_parameter(param) if validate_parameters
parameters[parameter_name(param)] = value
end
|
#builtin? ⇒ Boolean
155
156
157
|
# File 'lib/puppet/resource.rb', line 155
def builtin?
builtin_type?
end
|
#builtin_type? ⇒ Boolean
Is this a builtin resource type?
160
161
162
|
# File 'lib/puppet/resource.rb', line 160
def builtin_type?
resource_type.is_a?(Class)
end
|
#class? ⇒ Boolean
196
197
198
|
# File 'lib/puppet/resource.rb', line 196
def class?
@is_class ||= @type == "Class"
end
|
#copy_as_resource ⇒ Object
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
# File 'lib/puppet/resource.rb', line 392
def copy_as_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)
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
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
|
Iterate over each param/value pair, as required for Enumerable.
165
166
167
|
# File 'lib/puppet/resource.rb', line 165
def each
parameters.each { |p,v| yield p, v }
end
|
#environment ⇒ Object
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.
#environment=(env) ⇒ Object
182
183
184
185
186
187
188
|
# File 'lib/puppet/resource.rb', line 182
def environment=(env)
if env.is_a?(String) or env.is_a?(Symbol)
@environment = env
else
@environment = env.name
end
end
|
#include?(parameter) ⇒ Boolean
169
170
171
|
# File 'lib/puppet/resource.rb', line 169
def include?(parameter)
super || parameters.keys.include?( parameter_name(parameter) )
end
|
55
56
57
|
# File 'lib/puppet/resource.rb', line 55
def inspect
"#{@type}[#{@title}]#{to_hash.inspect}"
end
|
#key_attributes ⇒ Object
275
276
277
|
# File 'lib/puppet/resource.rb', line 275
def key_attributes
resource_type.respond_to?(:key_attributes) ? resource_type.key_attributes : [:name]
end
|
313
314
315
316
317
318
|
# File 'lib/puppet/resource.rb', line 313
def name
[ type, title ].join('/')
end
|
#prune_parameters(options = {}) ⇒ Object
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
# File 'lib/puppet/resource.rb', line 448
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
|
240
241
242
|
# File 'lib/puppet/resource.rb', line 240
def ref
to_s
end
|
245
246
247
|
# File 'lib/puppet/resource.rb', line 245
def resolve
catalog ? catalog.resource(to_s) : nil
end
|
#resource_type ⇒ Object
249
250
251
252
253
254
255
256
|
# File 'lib/puppet/resource.rb', line 249
def resource_type
@rstype ||= case type
when "Class"; known_resource_types.hostclass(title == :main ? "" : title)
when "Node"; known_resource_types.node(title)
else
Puppet::Type.type(type) || known_resource_types.definition(type)
end
end
|
#set_default_parameters(scope) ⇒ Object
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/puppet/resource.rb', line 369
def set_default_parameters(scope)
return [] unless resource_type and resource_type.respond_to?(:arguments)
unless is_a?(Puppet::Parser::Resource)
fail Puppet::DevError, "Cannot evaluate default parameters for #{self} - not a parser resource"
end
missing_arguments.collect do |param, default|
external_value = lookup_external_default_for(param, scope)
if external_value.nil? && default.nil?
next
elsif external_value.nil?
value = default.safeevaluate(scope)
else
value = external_value
end
self[param.to_sym] = value
param
end.compact
end
|
#stage? ⇒ Boolean
200
201
202
|
# File 'lib/puppet/resource.rb', line 200
def stage?
@is_stage ||= @type.to_s.downcase == "stage"
end
|
#to_data_hash ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/puppet/resource.rb', line 59
def to_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
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
|
Produce a simple hash of our parameters.
259
260
261
|
# File 'lib/puppet/resource.rb', line 259
def to_hash
parse_title.merge parameters
end
|
#to_manifest ⇒ Object
Convert our resource to Puppet code.
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/puppet/resource.rb', line 280
def to_manifest
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
122
123
124
|
# File 'lib/puppet/resource.rb', line 122
def to_pson(*args)
to_data_hash.to_pson(*args)
end
|
#to_pson_data_hash ⇒ Object
This doesn’t include document type as it is part of a catalog
84
85
86
|
# File 'lib/puppet/resource.rb', line 84
def to_pson_data_hash
to_data_hash
end
|
Convert our resource to a RAL resource instance. Creates component instances for resource types that don’t exist.
305
306
307
308
309
310
311
|
# File 'lib/puppet/resource.rb', line 305
def to_ral
if typeklass = Puppet::Type.type(self.type)
return typeklass.new(self)
else
return Puppet::Type::Component.new(self)
end
end
|
299
300
301
|
# File 'lib/puppet/resource.rb', line 299
def to_ref
ref
end
|
263
264
265
|
# File 'lib/puppet/resource.rb', line 263
def to_s
"#{type}[#{title}]"
end
|
#to_yaml_properties ⇒ Array<Symbol>
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.
Explicitly list the instance variables that should be serialized when converting to YAML.
118
119
120
|
# File 'lib/puppet/resource.rb', line 118
def to_yaml_properties
YAML_ATTRIBUTES & super
end
|
#uniqueness_key ⇒ Object
267
268
269
270
271
272
273
|
# File 'lib/puppet/resource.rb', line 267
def uniqueness_key
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
427
428
429
|
# File 'lib/puppet/resource.rb', line 427
def valid_parameter?(name)
resource_type.valid_parameter?(name)
end
|
#validate_complete ⇒ Object
Verify that all required arguments are either present or have been provided with defaults. Must be called after ‘set_default_parameters’. We can’t join the methods because Type#set_parameters needs specifically ordered behavior.
435
436
437
438
439
440
441
442
|
# File 'lib/puppet/resource.rb', line 435
def validate_complete
return unless resource_type and resource_type.respond_to?(:arguments)
resource_type.arguments.each do |param, default|
param = param.to_sym
fail Puppet::ParseError, "Must pass #{param} to #{self}" unless parameters.include?(param)
end
end
|
#validate_parameter(name) ⇒ Object
444
445
446
|
# File 'lib/puppet/resource.rb', line 444
def validate_parameter(name)
raise ArgumentError, "Invalid parameter #{name}" unless valid_parameter?(name)
end
|
#yaml_property_munge(x) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/puppet/resource.rb', line 98
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
|