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 00916809142c56926f55b65ae967748e2039dd22 Author: Alex Heneveld <a...@cloudsoft.io> AuthorDate: Thu Feb 22 10:23:26 2024 +0000 foreach takes 'do step' optionally --- .../core/workflow/steps/flow/ForeachWorkflowStep.java | 11 +++++++++-- .../workflow/WorkflowNestedAndCustomExtensionTest.java | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/ForeachWorkflowStep.java b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/ForeachWorkflowStep.java index e4a511ce0f..0d205be016 100644 --- a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/ForeachWorkflowStep.java +++ b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/flow/ForeachWorkflowStep.java @@ -21,13 +21,14 @@ package org.apache.brooklyn.core.workflow.steps.flow; import org.apache.brooklyn.core.workflow.WorkflowExecutionContext; import org.apache.brooklyn.core.workflow.WorkflowStepInstanceExecutionContext; import org.apache.brooklyn.core.workflow.steps.CustomWorkflowStep; +import org.apache.brooklyn.util.collections.MutableList; import org.apache.brooklyn.util.collections.MutableMap; import java.util.Map; public class ForeachWorkflowStep extends CustomWorkflowStep { - public static final String SHORTHAND = "${target_var_name} [ \" in \" ${target...} ]"; + public static final String SHORTHAND = "${target_var_name} [ \" in \" ${target...} [ \" do \" ${step...} ] ]"; public static final String SHORTHAND_TYPE_NAME_DEFAULT = "foreach"; @@ -43,10 +44,16 @@ public class ForeachWorkflowStep extends CustomWorkflowStep { @Override public void populateFromShorthand(String value) { if (input==null) input = MutableMap.of(); - populateFromShorthandTemplate(SHORTHAND, value); + populateFromShorthandTemplate(SHORTHAND, value, true, true); if (input.containsKey("target")) target = input.remove("target"); target_var_name = input.remove("target_var_name"); + + if (input.containsKey("step")) { + Object step = input.remove("step"); + if (this.steps!=null) throw new IllegalArgumentException("Cannot set step in shorthand as it is set elsewhere"); + this.steps = MutableList.of(step); + } } protected Iterable checkTarget(Object targetR) { diff --git a/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowNestedAndCustomExtensionTest.java b/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowNestedAndCustomExtensionTest.java index 20f59d5165..245cdd6143 100644 --- a/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowNestedAndCustomExtensionTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowNestedAndCustomExtensionTest.java @@ -910,11 +910,11 @@ public class WorkflowNestedAndCustomExtensionTest extends RebindTestFixture<Test } @Test - public void testForeachCondition() throws Exception { + public void testForeachConditionAndShorthandStep() throws Exception { Object output = invokeWorkflowStepsWithLogging(MutableList.of( "let list L = [ a, b, c ]", - MutableMap.of("step", "foreach item in ${L}", - "steps", MutableList.of("return ${item}"), + MutableMap.of("step", "foreach item in ${L} do return ${item}", + //"steps", MutableList.of("return ${item}"), "condition", MutableMap.of("any", MutableList.of( "a", MutableMap.of("target", MutableList.of("x", "c"), @@ -924,4 +924,14 @@ public class WorkflowNestedAndCustomExtensionTest extends RebindTestFixture<Test )))))); Asserts.assertEquals(output, MutableList.of("a", "c")); } + + @Test + public void testForeachStepsDuplication() throws Exception { + Asserts.assertFailsWith(() -> invokeWorkflowStepsWithLogging(MutableList.of( + "let list L = [ a, b, c ]", + MutableMap.of("step", "foreach item in ${L} do return 1", + "steps", MutableList.of("return 0") + ))), + Asserts.expectedFailureContainsIgnoreCase("cannot set step", "shorthand", "elsewhere")); + } }