@@ -11,7 +11,7 @@ class Puppet::Resource
11
11
include Puppet ::Util ::PsychSupport
12
12
13
13
include Enumerable
14
- attr_accessor :file , :line , :catalog , :exported , :virtual , :strict
14
+ attr_accessor :file , :line , :catalog , :exported , :virtual , :strict , :kind
15
15
attr_reader :type , :title , :parameters
16
16
17
17
# @!attribute [rw] sensitive_parameters
@@ -29,11 +29,16 @@ class Puppet::Resource
29
29
EMPTY_ARRAY = [ ] . freeze
30
30
EMPTY_HASH = { } . freeze
31
31
32
- ATTRIBUTES = [ :file , :line , :exported ] . freeze
32
+ ATTRIBUTES = [ :file , :line , :exported , :kind ] . freeze
33
33
TYPE_CLASS = 'Class' . freeze
34
34
TYPE_NODE = 'Node' . freeze
35
35
TYPE_SITE = 'Site' . freeze
36
36
37
+ CLASS_STRING = 'class' . freeze
38
+ DEFINED_TYPE_STRING = 'defined_type' . freeze
39
+ COMPILABLE_TYPE_STRING = 'compilable_type' . freeze
40
+ UNKNOWN_TYPE_STRING = 'unknown' . freeze
41
+
37
42
PCORE_TYPE_KEY = '__ptype' . freeze
38
43
VALUE_KEY = 'value' . freeze
39
44
@@ -194,6 +199,18 @@ def builtin_type?
194
199
resource_type . is_a? ( Puppet ::CompilableResourceType )
195
200
end
196
201
202
+ def self . to_kind ( resource_type )
203
+ if resource_type == CLASS_STRING
204
+ CLASS_STRING
205
+ elsif resource_type . is_a? ( Puppet ::Resource ::Type ) && resource_type . type == :definition
206
+ DEFINED_TYPE_STRING
207
+ elsif resource_type . is_a? ( Puppet ::CompilableResourceType )
208
+ COMPILABLE_TYPE_STRING
209
+ else
210
+ UNKNOWN_TYPE_STRING
211
+ end
212
+ end
213
+
197
214
# Iterate over each param/value pair, as required for Enumerable.
198
215
def each
199
216
parameters . each { |p , v | yield p , v }
@@ -248,6 +265,7 @@ def initialize(type, title = nil, attributes = EMPTY_HASH)
248
265
src = type
249
266
self . file = src . file
250
267
self . line = src . line
268
+ self . kind = src . kind
251
269
self . exported = src . exported
252
270
self . virtual = src . virtual
253
271
self . set_tags ( src )
@@ -310,6 +328,7 @@ def initialize(type, title = nil, attributes = EMPTY_HASH)
310
328
311
329
rt = resource_type
312
330
331
+ self . kind = self . class . to_kind ( rt ) unless kind
313
332
if strict? && rt . nil?
314
333
if self . class?
315
334
raise ArgumentError , _ ( "Could not find declared class %{title}" ) % { title : title }
@@ -493,10 +512,24 @@ def to_ref
493
512
ref
494
513
end
495
514
496
- # Convert our resource to a RAL resource instance. Creates component
497
- # instances for resource types that don't exist.
515
+ # Convert our resource to a RAL resource instance. Creates component
516
+ # instances for resource types that are not of a compilable_type kind. In case
517
+ # the resource doesn’t exist and it’s compilable_type kind, raise an error.
518
+ # There are certain cases where a resource won't be in a catalog, such as
519
+ # when we create a resource directly by using Puppet::Resource.new(...), so we
520
+ # must check its kind before deciding whether the catalog format is of an older
521
+ # version or not.
498
522
def to_ral
499
- typeklass = Puppet ::Type . type ( self . type ) || Puppet ::Type . type ( :component )
523
+ if self . kind == COMPILABLE_TYPE_STRING
524
+ typeklass = Puppet ::Type . type ( self . type )
525
+ elsif self . catalog && self . catalog . catalog_format >= 2
526
+ typeklass = Puppet ::Type . type ( :component )
527
+ else
528
+ typeklass = Puppet ::Type . type ( self . type ) || Puppet ::Type . type ( :component )
529
+ end
530
+
531
+ raise ( Puppet ::Error , "Resource type '#{ self . type } ' was not found" ) unless typeklass
532
+
500
533
typeklass . new ( self )
501
534
end
502
535
0 commit comments