Browse Source

Improve generate functions handling

Change-Id: I84d8aec905d71b2dbebdb3b5c05454c279e71c09
Dmitry Ilyin 2 years ago
parent
commit
41966d861d
1 changed files with 26 additions and 13 deletions
  1. 26
    13
      lib/noop/task/helpers.rb

+ 26
- 13
lib/noop/task/helpers.rb View File

@@ -77,26 +77,39 @@ module Noop
77 77
     # @param context [RSpec::ExampleGroup] The 'self' of the RSpec example group
78 78
     # @return <Lambda>
79 79
     def create_ral_catalog(context)
80
-      catalog = context.subject
80
+      catalog = context.catalog
81 81
       catalog = catalog.call if catalog.is_a? Proc
82 82
       ral_catalog = catalog.to_ral
83
-      generate_functions = [:generate, :eval_generate]
84
-
85 83
       ral_catalog.resources.each do |resource|
86
-        generate_functions.each do |function_name|
87
-          next unless resource.respond_to? function_name
88
-          generated = resource.send function_name
89
-          next unless generated.is_a? Array
90
-          generated.each do |generated_resource|
91
-            next unless generated_resource.is_a? Puppet::Type
92
-            ral_catalog.add_resource generated_resource
93
-          end
94
-        end
84
+        generate_additional_resources ral_catalog, resource
95 85
       end
96
-
97 86
       lambda { ral_catalog }
98 87
     end
99 88
 
89
+    # If the resources has one of the generate function
90
+    # run it and add the generated resources to the catalog
91
+    # if they are not there already. Run generate functions
92
+    # recursively for the generated resources too.
93
+    # @param catalog [Puppet::Catalog]
94
+    # @param resource [Puppet::Type]
95
+    def generate_additional_resources(catalog, resource)
96
+      generate_functions = [:generate, :eval_generate]
97
+      generate_functions.each do |function_name|
98
+        next unless resource.respond_to? function_name
99
+        debug "Resource: #{resource} run: #{function_name}"
100
+        generated = resource.send function_name
101
+        next unless generated.is_a? Array
102
+        generated.each do |generated_resource|
103
+          next unless generated_resource.is_a? Puppet::Type
104
+          next if catalog.resource generated_resource.ref
105
+          debug "Add resource: #{generated_resource} to the catalog"
106
+          catalog.add_resource generated_resource
107
+          generate_additional_resources catalog, generated_resource
108
+        end
109
+      end
110
+      catalog
111
+    end
112
+
100 113
     # Check if the currently running spec is the given one
101 114
     # or one of the given ones if an array is provided
102 115
     # @param spec [String, Array<String>]

Loading…
Cancel
Save