Browse Source

Merge "Improve generate functions handling"

changes/05/322205/3
Jenkins 3 years ago
parent
commit
4db6b799b5
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

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

Loading…
Cancel
Save