Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ public DoTaskBuilder openapi(String name, Consumer<CallOpenAPITaskBuilder> items
this.listBuilder().openapi(name, itemsConfigurer);
return this;
}

@Override
public DoTaskBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
this.listBuilder().workflow(name, itemsConfigurer);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,17 @@ public TaskItemListBuilder openapi(

return addTaskItem(new TaskItem(name, task));
}

@Override
public TaskItemListBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
name = defaultNameAndRequireConfig(name, itemsConfigurer);

final WorkflowTaskBuilder workflowTaskBuilder = new WorkflowTaskBuilder();
itemsConfigurer.accept(workflowTaskBuilder);

final Task task = new Task();
task.setRunTask(workflowTaskBuilder.build());

return addTaskItem(new TaskItem(name, task));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec;

import io.serverlessworkflow.api.types.RunTask;
import io.serverlessworkflow.api.types.RunTaskConfigurationUnion;
import io.serverlessworkflow.api.types.RunWorkflow;
import io.serverlessworkflow.api.types.SubflowConfiguration;
import io.serverlessworkflow.fluent.spec.spi.WorkflowTaskFluent;

public class WorkflowTaskBuilder extends TaskBaseBuilder<WorkflowTaskBuilder>
implements WorkflowTaskFluent<WorkflowTaskBuilder> {

private final RunTask task;
private final RunWorkflow configuration;
private final SubflowConfiguration workflow;

WorkflowTaskBuilder() {
this.task = new RunTask();
this.configuration = new RunWorkflow();
this.workflow = new SubflowConfiguration();
this.configuration.setWorkflow(this.workflow);
this.task.setRun(new RunTaskConfigurationUnion().withRunWorkflow(this.configuration));
this.setTask(task);
}

@Override
public WorkflowTaskBuilder self() {
return this;
}

public RunWorkflow config() {
return this.configuration;
}

public SubflowConfiguration workflow() {
return this.workflow;
}

public RunTask build() {
return this.task;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.configurers;

import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
import java.util.function.Consumer;

@FunctionalInterface
public interface WorkflowConfigurer extends Consumer<WorkflowTaskBuilder> {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.serverlessworkflow.fluent.spec.configurers.TasksConfigurer;
import io.serverlessworkflow.fluent.spec.configurers.TryCatchConfigurer;
import io.serverlessworkflow.fluent.spec.configurers.TryConfigurer;
import io.serverlessworkflow.fluent.spec.configurers.WorkflowConfigurer;
import io.serverlessworkflow.types.Errors;
import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -102,6 +103,18 @@ public static CallOpenAPISpec openapi() {
return new CallOpenAPISpec();
}

public static WorkflowSpec workflow(String namespace, String name, String version) {
return new WorkflowSpec().namespace(namespace).name(name).version(version);
}

public static WorkflowSpec workflow(String namespace, String name) {
return new WorkflowSpec().namespace(namespace).name(name);
}

public static WorkflowSpec workflow() {
return new WorkflowSpec();
}

/**
* Convenience for defining a {@code use} block with a single secret.
*
Expand Down Expand Up @@ -623,6 +636,10 @@ public static TasksConfigurer call(CallOpenAPIConfigurer configurer) {
return list -> list.openapi(configurer);
}

public static TasksConfigurer workflow(WorkflowConfigurer configurer) {
return list -> list.workflow(configurer);
}

/**
* Create a {@link TasksConfigurer} that adds a {@code set} task using a low-level configurer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.dsl;

import io.serverlessworkflow.api.types.RunTaskConfiguration;
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
import io.serverlessworkflow.fluent.spec.configurers.WorkflowConfigurer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public final class WorkflowSpec implements WorkflowConfigurer {

private final List<Consumer<WorkflowTaskBuilder>> steps = new ArrayList<>();

public WorkflowSpec namespace(String namespace) {
steps.add(b -> b.namespace(namespace));
return this;
}

public WorkflowSpec name(String name) {
steps.add(b -> b.name(name));
return this;
}

public WorkflowSpec version(String version) {
steps.add(b -> b.version(version));
return this;
}

public WorkflowSpec input(Map<String, Object> input) {
steps.add(b -> b.input(input));
return this;
}

public WorkflowSpec input(String key, Object value) {
steps.add(b -> b.input(key, value));
return this;
}

public WorkflowSpec await(boolean await) {
steps.add(b -> b.await(await));
return this;
}

public WorkflowSpec returnType(RunTaskConfiguration.ProcessReturnType returnType) {
steps.add(b -> b.returnType(returnType));
return this;
}

@Override
public void accept(WorkflowTaskBuilder builder) {
for (var s : steps) {
s.accept(builder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.serverlessworkflow.fluent.spec.SwitchTaskBuilder;
import io.serverlessworkflow.fluent.spec.TaskItemListBuilder;
import io.serverlessworkflow.fluent.spec.TryTaskBuilder;
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;

/**
* Documents the exposed fluent `do` DSL.
Expand All @@ -44,4 +45,5 @@ public interface DoFluent<T>
ForkFluent<ForkTaskBuilder, T>,
ListenFluent<ListenTaskBuilder, T>,
RaiseFluent<RaiseTaskBuilder, T>,
CallOpenAPIFluent<CallOpenAPITaskBuilder, T> {}
CallOpenAPIFluent<CallOpenAPITaskBuilder, T>,
WorkflowFluent<WorkflowTaskBuilder, T> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.spi;

import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
import java.util.UUID;
import java.util.function.Consumer;

public interface WorkflowFluent<SELF extends TaskBaseBuilder<SELF>, LIST> {

LIST workflow(String name, Consumer<SELF> itemsConfigurer);

default LIST workflow(Consumer<SELF> itemsConfigurer) {
return this.workflow(UUID.randomUUID().toString(), itemsConfigurer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.spi;

import io.serverlessworkflow.api.types.RunTaskConfiguration;
import io.serverlessworkflow.api.types.SubflowInput;
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
import java.util.Map;

public interface WorkflowTaskFluent<SELF extends TaskBaseBuilder<SELF>> {

SELF self();

default SELF namespace(String namespace) {
((WorkflowTaskBuilder) this.self()).workflow().setNamespace(namespace);
return self();
}

default SELF name(String name) {
((WorkflowTaskBuilder) this.self()).workflow().setName(name);
return self();
}

default SELF version(String version) {
((WorkflowTaskBuilder) this.self()).workflow().setVersion(version);
return self();
}

default SELF input(Map<String, Object> input) {
final SubflowInput subflowInput = new SubflowInput();
input.forEach(subflowInput::setAdditionalProperty);
((WorkflowTaskBuilder) this.self()).workflow().setInput(subflowInput);
return self();
}

default SELF input(String key, Object value) {
final WorkflowTaskBuilder builder = (WorkflowTaskBuilder) this.self();
if (builder.workflow().getInput() == null) {
builder.workflow().setInput(new SubflowInput());
}
builder.workflow().getInput().setAdditionalProperty(key, value);
return self();
}

default SELF await(boolean await) {
((WorkflowTaskBuilder) this.self()).config().setAwait(await);
return self();
}

default SELF returnType(RunTaskConfiguration.ProcessReturnType returnType) {
((WorkflowTaskBuilder) this.self()).config().setReturn(returnType);
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.serverlessworkflow.api.types.OneEventConsumptionStrategy;
import io.serverlessworkflow.api.types.RetryLimitAttempt;
import io.serverlessworkflow.api.types.RetryPolicy;
import io.serverlessworkflow.api.types.RunTaskConfiguration;
import io.serverlessworkflow.api.types.SetTask;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.api.types.TryTask;
Expand Down Expand Up @@ -517,4 +518,51 @@ void testDoTaskCallHTTPRedirectAndOutput() {
call.getWith().getOutput(),
"Output should be overridden");
}

@Test
void testDoTaskRunWorkflow() {
Workflow wf =
WorkflowBuilder.workflow("parentFlow")
.tasks(
d ->
d.workflow(
"runChild",
w ->
w.namespace("org.acme")
.name("childFlow")
.version("1.0.0")
.input(Map.of("id", 42, "region", "us-east"))
.await(false)
.returnType(RunTaskConfiguration.ProcessReturnType.NONE)))
.build();

var runTask = wf.getDo().get(0).getTask().getRunTask();
assertNotNull(runTask, "RunTask should be present");
assertNotNull(runTask.getRun(), "RunTask configuration should be present");
assertNotNull(runTask.getRun().getRunWorkflow(), "RunWorkflow should be selected");
assertEquals("org.acme", runTask.getRun().getRunWorkflow().getWorkflow().getNamespace());
assertEquals("childFlow", runTask.getRun().getRunWorkflow().getWorkflow().getName());
assertEquals("1.0.0", runTask.getRun().getRunWorkflow().getWorkflow().getVersion());
assertEquals(
42,
runTask
.getRun()
.getRunWorkflow()
.getWorkflow()
.getInput()
.getAdditionalProperties()
.get("id"));
assertEquals(
"us-east",
runTask
.getRun()
.getRunWorkflow()
.getWorkflow()
.getInput()
.getAdditionalProperties()
.get("region"));
assertEquals(
RunTaskConfiguration.ProcessReturnType.NONE, runTask.getRun().getRunWorkflow().getReturn());
assertEquals(false, runTask.getRun().getRunWorkflow().isAwait());
}
}
Loading
Loading