This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit f6716dc8aa4b52955537c8d576191acee2bf5948
Author: Alex Heneveld <a...@cloudsoft.io>
AuthorDate: Fri May 31 10:53:52 2024 +0100

    allow output from previous step to be accessible in subworkflow
---
 .../apache/brooklyn/camp/brooklyn/WorkflowYamlTest.java  | 16 ++++++++++++++++
 .../core/workflow/WorkflowExpressionResolution.java      |  3 ++-
 .../brooklyn/core/workflow/steps/CustomWorkflowStep.java |  1 +
 .../core/workflow/steps/flow/SubWorkflowStep.java        | 10 +++++++++-
 4 files changed, 28 insertions(+), 2 deletions(-)

diff --git 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WorkflowYamlTest.java
 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WorkflowYamlTest.java
index 02f9517fd7..7b3ea599a2 100644
--- 
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WorkflowYamlTest.java
+++ 
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WorkflowYamlTest.java
@@ -1639,4 +1639,20 @@ public class WorkflowYamlTest extends AbstractYamlTest {
         Asserts.assertEquals(((Map)result).get("exit_code"), 0);
     }
 
+    @Test
+    public void testOutputAccessibleInSubWorkflow() throws Exception {
+        Application app = createAndStartApplication(
+                "services:",
+                "- type: " + BasicEntity.class.getName(),
+                "  id: child");
+
+        WorkflowExecutionContext x = WorkflowBasicTest.runWorkflow(app, 
Strings.lines(
+                "steps:",
+                "  - transform value { a: 1 } | type map",
+                "  - type: subworkflow",
+                "    steps:",
+                "      - return ${output.a}-${a}"
+        ), "test");
+        Asserts.assertEquals(x.getTask(true).get().getUnchecked(), "1-1");
+    }
 }
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java
index 633ddbfc2b..90a99de0e3 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java
@@ -187,7 +187,8 @@ public class WorkflowExpressionResolution {
                 if (context.currentStepInstance!=null && 
context.currentStepInstance.getOutput() !=null) return 
TemplateProcessor.wrapAsTemplateModel(context.currentStepInstance.getOutput());
                 Object previousStepOutput = context.getPreviousStepOutput();
                 if (previousStepOutput!=null) return 
TemplateProcessor.wrapAsTemplateModel(previousStepOutput);
-                return ifNoMatches();
+                ////if no output in scope then continue to items below, eg 
consider variables
+                // return ifNoMatches();
             }
 
             Maybe candidate = Maybe.absent();
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/CustomWorkflowStep.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/CustomWorkflowStep.java
index 237472eec8..259ec06df9 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/CustomWorkflowStep.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/CustomWorkflowStep.java
@@ -462,6 +462,7 @@ public class CustomWorkflowStep extends 
WorkflowStepDefinition implements Workfl
         return returnValue;
     }
 
+    /** resolves the variables to be reduced, and returns them; stored by the 
caller and returned at end if appropriate */
     protected Map 
initializeReducingVariables(WorkflowStepInstanceExecutionContext context, 
Map<String, Object> reducing) {
         return 
context.resolve(WorkflowExpressionResolution.WorkflowExpressionStage.STEP_INPUT,
 reducing, Map.class);
     }
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/SubWorkflowStep.java
 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/SubWorkflowStep.java
index 3ce6c3336b..7d0735e211 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/SubWorkflowStep.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/SubWorkflowStep.java
@@ -76,7 +76,15 @@ public class SubWorkflowStep extends CustomWorkflowStep {
     @Override
     protected Map 
initializeReducingVariables(WorkflowStepInstanceExecutionContext context, 
Map<String, Object> reducing) {
         context.isLocalSubworkflow = true;
-        return super.initializeReducingVariables(context, 
context.getWorkflowExectionContext().getWorkflowScratchVariables());
+        MutableMap<String, Object> allVarsInScope = 
MutableMap.copyOf(context.getWorkflowExectionContext().getWorkflowScratchVariables());
+        // make output visible
+        allVarsInScope.add("output", 
context.getWorkflowExectionContext().getPreviousStepOutput());
+        if (context.getWorkflowExectionContext().getPreviousStepOutput() 
instanceof Map) {
+            
allVarsInScope.add((Map)context.getWorkflowExectionContext().getPreviousStepOutput());
+        }
+        allVarsInScope.add(reducing);
+        // but see getReducingWorkflowVarsFromLastStep -- note that for a 
subworkflow, reduced vars are not returned
+        return super.initializeReducingVariables(context, allVarsInScope);
     }
 
     @Override

Reply via email to