Skip to content

Commit 0607b4b

Browse files
authored
Add workflows sample (GoogleCloudPlatform#5821)
Fixes #issue > It's a good idea to open an issue first for discussion. - [ ] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [ ] `pom.xml` parent set to latest `shared-configuration` - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [ ] **Tests** pass: `mvn clean verify` **required** - [ ] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [ ] Please **merge** this PR for me once it is approved.
1 parent ec01ca9 commit 0607b4b

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed

workflows/cloud-client/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Cloud Workflows Quickstart
2+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dlp&page=editor&open_in_editor=samples/snippets/README.md">
3+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
4+
5+
The [Workflows API](https://cloud.google.com/workflows/docs/) provides programmatic access to execute serverless workflows that link series of serverless tasks together in an order you define.
6+
7+
## Setup
8+
- A Google Cloud project with billing enabled
9+
- [Enable](https://console.cloud.google.com/launcher/details/google/workflows.googleapis.com) the DLP API.
10+
- [Create a service account](https://cloud.google.com/docs/authentication/getting-started)
11+
and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to the downloaded credentials file.
12+
13+
## Snippets
14+
Run the tests via:
15+
```
16+
mvn clean verify
17+
```
18+

workflows/cloud-client/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
Copyright 2021 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.example.workflows</groupId>
20+
<artifactId>workflows-quickstart</artifactId>
21+
<packaging>jar</packaging>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.23</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
</properties>
38+
39+
<!-- [START workflows_api_quickstart_dependencies] -->
40+
<!-- Using libraries-bom to manage versions.
41+
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
42+
<dependencyManagement>
43+
<dependencies>
44+
<dependency>
45+
<groupId>com.google.cloud</groupId>
46+
<artifactId>libraries-bom</artifactId>
47+
<version>20.6.0</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
</dependencies>
52+
</dependencyManagement>
53+
54+
<dependencies>
55+
<dependency>
56+
<groupId>com.google.cloud</groupId>
57+
<artifactId>google-cloud-workflows</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.cloud</groupId>
61+
<artifactId>google-cloud-workflow-executions</artifactId>
62+
<version>0.2.1</version>
63+
</dependency>
64+
<!-- [END workflows_api_quickstart_dependencies] -->
65+
66+
<!-- Test dependencies -->
67+
<dependency>
68+
<groupId>junit</groupId>
69+
<artifactId>junit</artifactId>
70+
<version>4.13.2</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.google.truth</groupId>
75+
<artifactId>truth</artifactId>
76+
<version>1.1.2</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<!-- [START workflows_api_quickstart_dependencies] -->
80+
</dependencies>
81+
<!-- [END workflows_api_quickstart_dependencies] -->
82+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.workflows;
18+
19+
// [START workflows_api_quickstart]
20+
21+
// Imports the Google Cloud client library
22+
23+
import com.google.cloud.workflows.executions.v1.CreateExecutionRequest;
24+
import com.google.cloud.workflows.executions.v1.Execution;
25+
import com.google.cloud.workflows.executions.v1.ExecutionsClient;
26+
import com.google.cloud.workflows.executions.v1.WorkflowName;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
public class WorkflowsQuickstart {
31+
32+
public static void main(String... args)
33+
throws IOException, InterruptedException, ExecutionException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "your-project-id";
36+
String location = "us-central1";
37+
String workflow = "myFirstWorkflow";
38+
workflowsQuickstart(projectId, location, workflow);
39+
}
40+
41+
private static volatile boolean finished;
42+
43+
public static void workflowsQuickstart(String projectId, String location, String workflow)
44+
throws IOException, InterruptedException, ExecutionException {
45+
// Initialize client that will be used to send requests. This client only needs
46+
// to be created once, and can be reused for multiple requests. After completing all of your
47+
// requests, call the "close" method on the client to safely clean up any remaining background
48+
// resources.
49+
try (ExecutionsClient executionsClient = ExecutionsClient.create()) {
50+
// Construct the fully qualified location path.
51+
WorkflowName parent = WorkflowName.of(projectId, location, workflow);
52+
53+
// Creates the execution object.
54+
CreateExecutionRequest request =
55+
CreateExecutionRequest.newBuilder()
56+
.setParent(parent.toString())
57+
.setExecution(Execution.newBuilder().build())
58+
.build();
59+
Execution response = executionsClient.createExecution(request);
60+
61+
String executionName = response.getName();
62+
System.out.printf("Created execution: %s%n", executionName);
63+
64+
long backoffTime = 0;
65+
long backoffDelay = 1_000; // Start wait with delay of 1,000 ms
66+
final long backoffTimeout = 10 * 60 * 1_000; // Time out at 10 minutes
67+
System.out.println("Poll for results...");
68+
69+
// Wait for execution to finish, then print results.
70+
while (!finished && backoffTime < backoffTimeout) {
71+
Execution execution = executionsClient.getExecution(executionName);
72+
finished = execution.getState() != Execution.State.ACTIVE;
73+
74+
// If we haven't seen the results yet, wait.
75+
if (!finished) {
76+
System.out.println("- Waiting for results");
77+
Thread.sleep(backoffDelay);
78+
backoffTime += backoffDelay;
79+
backoffDelay *= 2; // Double the delay to provide exponential backoff.
80+
} else {
81+
System.out.println("Execution finished with state: " + execution.getState().name());
82+
System.out.println("Execution results: " + execution.getResult());
83+
}
84+
}
85+
}
86+
}
87+
}
88+
// [END workflows_api_quickstart]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.workflows;
18+
19+
import static com.example.workflows.WorkflowsQuickstart.workflowsQuickstart;
20+
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.assertFalse;
22+
23+
import com.google.api.client.util.Strings;
24+
import com.google.cloud.workflows.v1.LocationName;
25+
import com.google.cloud.workflows.v1.Workflow;
26+
import com.google.cloud.workflows.v1.WorkflowName;
27+
import com.google.cloud.workflows.v1.WorkflowsClient;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.PrintStream;
31+
import java.nio.file.Files;
32+
import java.nio.file.Paths;
33+
import java.util.UUID;
34+
import java.util.concurrent.ExecutionException;
35+
import org.junit.AfterClass;
36+
import org.junit.BeforeClass;
37+
import org.junit.Test;
38+
39+
public class WorkflowsQuickstartTest {
40+
41+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
42+
private static final String LOCATION_ID = "us-central1";
43+
private static final String WORKFLOW_ID = "java-quickstart-" + UUID.randomUUID().toString();
44+
private static WorkflowsClient workflowsClient;
45+
private static ByteArrayOutputStream stdOut;
46+
47+
@BeforeClass
48+
public static void beforeAll() throws Exception {
49+
assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
50+
// Deploy the workflow
51+
deployWorkflow(PROJECT_ID, LOCATION_ID, WORKFLOW_ID);
52+
stdOut = new ByteArrayOutputStream();
53+
System.setOut(new PrintStream(stdOut));
54+
}
55+
56+
@AfterClass
57+
public static void afterAll() throws Exception {
58+
deleteWorkflow(PROJECT_ID, LOCATION_ID, WORKFLOW_ID);
59+
60+
stdOut = null;
61+
System.setOut(null);
62+
}
63+
64+
@Test
65+
public void testQuickstart() throws IOException, InterruptedException, ExecutionException {
66+
// Run the workflow we deployed
67+
workflowsQuickstart(PROJECT_ID, LOCATION_ID, WORKFLOW_ID);
68+
assertThat(stdOut.toString()).contains("Execution results:");
69+
}
70+
71+
private static void deployWorkflow(String projectId, String location, String workflowId)
72+
throws IOException, InterruptedException, ExecutionException {
73+
74+
if (workflowsClient == null) {
75+
workflowsClient = WorkflowsClient.create();
76+
}
77+
LocationName parent = LocationName.of(projectId, location);
78+
79+
String source =
80+
new String(
81+
Files.readAllBytes(
82+
Paths.get("src/test/java/com/example/workflows/resources/source.yaml")));
83+
Workflow workflow = Workflow.newBuilder().setSourceContents(source).build();
84+
85+
// Deploy workflow
86+
workflowsClient.createWorkflowAsync(parent, workflow, workflowId).get();
87+
}
88+
89+
public static void deleteWorkflow(String projectId, String location, String workflowId)
90+
throws IOException {
91+
if (workflowsClient == null) {
92+
workflowsClient = WorkflowsClient.create();
93+
}
94+
workflowsClient.deleteWorkflowAsync(WorkflowName.of(PROJECT_ID, LOCATION_ID, WORKFLOW_ID));
95+
}
96+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- getCurrentTime:
2+
call: http.get
3+
args:
4+
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
5+
result: currentTime
6+
- readWikipedia:
7+
call: http.get
8+
args:
9+
url: https://en.wikipedia.org/w/api.php
10+
query:
11+
action: opensearch
12+
search: ${currentTime.body.dayOfTheWeek}
13+
result: wikiResult
14+
- returnResult:
15+
return: ${wikiResult.body[1]}

0 commit comments

Comments
 (0)