Skip to content

Commit facb5cb

Browse files
authored
feat: add cloud workflows quickstart (GoogleCloudPlatform#5200)
## Description Adds Cloud Workflows quickstart. Tested on Cloud Shell: ``` GOOGLE_CLOUD_PROJECT=my-project python3 main.py ``` ## Checklist - [x] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md) - [x] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file) - [x] **Tests** pass: `nox -s py-3.6` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [x] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [x] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - workflows.googleapis.com - workflowexecutions.googleapis.com - [x] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [x] Please **merge** this PR for me once it is approved. - [x] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.github/CODEOWNERS) with the codeowners for this sample --- Test: ``` cd ~/github/googlecloudplatform/python-docs-samples/ python3 -m venv my-venv source my-venv/bin/activate cd workflows/cloud-client pip3 install -r requirements-test.txt cd .. GOOGLE_CLOUD_PROJECT=serverless-com-demo nox -s py-3.7 -- cloud-client/main_test.py ``` Local execution: ``` GOOGLE_CLOUD_PROJECT=serverless-com-demo python3 cloud-client/main.py ```
1 parent 0182811 commit facb5cb

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@
6868
/translate/**/*.py @telpirion @sirtorry @GoogleCloudPlatform/python-samples-owners
6969
/video/**/*.py @telpirion @sirtorry @GoogleCloudPlatform/python-samples-owners
7070
/vision/**/*.py @telpirion @sirtorry @GoogleCloudPlatform/python-samples-owners
71+
/workflows/**/*.py @grant @GoogleCloudPlatform/python-samples-owners

workflows/cloud-client/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Cloud Workflows Quickstart – Python
4+
5+
This sample shows how to execute a Cloud Workflow and wait for the workflow execution results using the Python client libraries.
6+
7+
## Setup
8+
9+
1. Deploy the workflow, `myFirstWorkflow`:
10+
11+
1. Copy the YAML from this file: https://github.com/GoogleCloudPlatform/workflows-samples/blob/main/src/myFirstWorkflow.workflows.yaml
12+
1. Paste the YAML into a file called `myFirstWorkflow.workflows.yaml`.
13+
1. Run the command: `gcloud workflows deploy myFirstWorkflow --source myFirstWorkflow.workflows.yaml`
14+
15+
## Run the Quickstart
16+
17+
Install [`pip`][pip] and [`virtualenv`][virtualenv] if you do not already have them.
18+
19+
You may want to refer to the [`Python Development Environment Setup Guide`][setup] for Google Cloud Platform for instructions.
20+
21+
1. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
22+
23+
```sh
24+
virtualenv env
25+
source env/bin/activate
26+
```
27+
28+
1. Install the dependencies needed to run the samples.
29+
30+
```sh
31+
pip install -r requirements.txt
32+
```
33+
34+
1. Start the application, setting your project name in an environment variable, `GOOGLE_CLOUD_PROJECT`:
35+
36+
```sh
37+
export GOOGLE_CLOUD_PROJECT=your-project-id
38+
python main.py
39+
```
40+
41+
1. Observe the results:
42+
43+
In stdout, you should see a JSON response from your workflow like the following:
44+
45+
```json
46+
["Wednesday","Wednesday Night Wars","Wednesday 13","Wednesday Addams","Wednesday Campanella","Wednesdayite","Wednesday Martin","Wednesday Campanella discography","Wednesday Night Hockey (American TV program)","Wednesday Morning, 3 A.M."]
47+
```
48+
49+
[prereq]: ../README.md#prerequisities
50+
[setup]: https://cloud.google.com/python/setup
51+
[pip]: https://pip.pypa.io/
52+
[virtualenv]: https://virtualenv.pypa.io/

workflows/cloud-client/main.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
18+
def execute_workflow(
19+
project, location="us-central1", workflow="myFirstWorkflow"
20+
):
21+
"""Execute a workflow and print the execution results."""
22+
# [START workflows_api_quickstart]
23+
import time
24+
25+
from google.cloud import workflows_v1beta
26+
from google.cloud.workflows import executions_v1beta
27+
from google.cloud.workflows.executions_v1beta.types import executions
28+
29+
# TODO(developer): Uncomment these lines and replace with your values.
30+
# project = 'my-project-id'
31+
# location = 'us- central1'
32+
# workflow = 'myFirstWorkflow'
33+
34+
if not project:
35+
raise Exception('GOOGLE_CLOUD_PROJECT env var is required.')
36+
37+
# Set up API clients.
38+
execution_client = executions_v1beta.ExecutionsClient()
39+
workflows_client = workflows_v1beta.WorkflowsClient()
40+
41+
# Construct the fully qualified location path.
42+
parent = workflows_client.workflow_path(project, location, workflow)
43+
44+
# Execute the workflow.
45+
response = execution_client.create_execution(request={"parent": parent})
46+
print(f"Created execution: {response.name}")
47+
48+
# Wait for execution to finish, then print results.
49+
execution_finished = False
50+
backoff_delay = 1 # Start wait with delay of 1 second
51+
print('Poll every second for result...')
52+
while (not execution_finished):
53+
execution = execution_client.get_execution(request={"name": response.name})
54+
execution_finished = execution.state != executions.Execution.State.ACTIVE
55+
56+
# If we haven't seen the result yet, wait a second.
57+
if not execution_finished:
58+
print('- Waiting for results...')
59+
time.sleep(backoff_delay)
60+
backoff_delay *= 2 # Double the delay to provide exponential backoff.
61+
else:
62+
print(f'Execution finished with state: {execution.state.name}')
63+
print(execution.result)
64+
return execution.result
65+
# [END workflows_api_quickstart]
66+
67+
68+
if __name__ == "__main__":
69+
project = os.environ.get('GOOGLE_CLOUD_PROJECT')
70+
execute_workflow(project=project)

workflows/cloud-client/main_test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from google.cloud import workflows_v1beta
18+
19+
import main
20+
21+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
LOCATION = "us-central1"
23+
WORKFLOW_ID = "myFirstWorkflow"
24+
25+
26+
def test_workflow_execution():
27+
assert PROJECT != ""
28+
29+
if not workflow_exists():
30+
workflow_file = open("myFirstWorkflow.workflows.yaml", "r").read()
31+
32+
workflows_client = workflows_v1beta.WorkflowsClient()
33+
workflows_client.create_workflow(request={
34+
# Manually construct the location
35+
# https://github.com/googleapis/python-workflows/issues/21
36+
"parent": f'projects/{PROJECT}/locations/{LOCATION}',
37+
"workflow_id": WORKFLOW_ID,
38+
"workflow": {
39+
"name": WORKFLOW_ID,
40+
"source_contents": workflow_file
41+
}
42+
})
43+
44+
result = main.execute_workflow(PROJECT)
45+
assert len(result) > 0
46+
47+
48+
def workflow_exists():
49+
"""Returns True if the workflow exists in this project
50+
"""
51+
try:
52+
workflows_client = workflows_v1beta.WorkflowsClient()
53+
workflow_name = workflows_client.workflow_path(PROJECT, LOCATION, WORKFLOW_ID)
54+
workflows_client.get_workflow(request={"name": workflow_name})
55+
return True
56+
except Exception as e:
57+
print(f"Workflow doesn't exist: {e}")
58+
return False
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
- getCurrentTime:
16+
call: http.get
17+
args:
18+
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
19+
result: currentTime
20+
- readWikipedia:
21+
call: http.get
22+
args:
23+
url: https://en.wikipedia.org/w/api.php
24+
query:
25+
action: opensearch
26+
search: ${currentTime.body.dayOfTheWeek}
27+
result: wikiResult
28+
- returnResult:
29+
return: ${wikiResult.body[1]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==6.2.1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-workflows==0.2.0

0 commit comments

Comments
 (0)