Skip to content

Commit e419fcf

Browse files
authored
fix: update Cloud Workflows sample (GoogleCloudPlatform#9836)
* fix: update Cloud Workflows sample * Update state * fix lint * update workflow
1 parent 8fe1db1 commit e419fcf

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

workflows/cloud-client/main.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def execute_workflow(
2222
# [START workflows_api_quickstart]
2323
import time
2424

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
25+
from google.cloud import workflows_v1
26+
from google.cloud.workflows import executions_v1
27+
from google.cloud.workflows.executions_v1.types import executions
2828

2929
# TODO(developer): Uncomment these lines and replace with your values.
3030
# project = 'my-project-id'
@@ -35,8 +35,8 @@ def execute_workflow(
3535
raise Exception('GOOGLE_CLOUD_PROJECT env var is required.')
3636

3737
# Set up API clients.
38-
execution_client = executions_v1beta.ExecutionsClient()
39-
workflows_client = workflows_v1beta.WorkflowsClient()
38+
execution_client = executions_v1.ExecutionsClient()
39+
workflows_client = workflows_v1.WorkflowsClient()
4040

4141
# Construct the fully qualified location path.
4242
parent = workflows_client.workflow_path(project, location, workflow)
@@ -50,18 +50,20 @@ def execute_workflow(
5050
backoff_delay = 1 # Start wait with delay of 1 second
5151
print('Poll every second for result...')
5252
while (not execution_finished):
53-
execution = execution_client.get_execution(request={"name": response.name})
53+
execution = execution_client.get_execution(
54+
request={"name": response.name})
5455
execution_finished = execution.state != executions.Execution.State.ACTIVE
5556

5657
# If we haven't seen the result yet, wait a second.
5758
if not execution_finished:
5859
print('- Waiting for results...')
5960
time.sleep(backoff_delay)
60-
backoff_delay *= 2 # Double the delay to provide exponential backoff.
61+
# Double the delay to provide exponential backoff.
62+
backoff_delay *= 2
6163
else:
6264
print(f'Execution finished with state: {execution.state.name}')
63-
print(execution.result)
64-
return execution.result
65+
print(f'Execution results: {execution.result}')
66+
return execution
6567
# [END workflows_api_quickstart]
6668

6769

workflows/cloud-client/main_test.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
import os
1616

17-
from google.cloud import workflows_v1beta
17+
from google.cloud import workflows_v1
18+
from google.cloud.workflows.executions_v1.types import executions
1819

1920
import main
2021

@@ -29,7 +30,7 @@ def test_workflow_execution():
2930
if not workflow_exists():
3031
workflow_file = open("myFirstWorkflow.workflows.yaml", "r").read()
3132

32-
workflows_client = workflows_v1beta.WorkflowsClient()
33+
workflows_client = workflows_v1.WorkflowsClient()
3334
workflows_client.create_workflow(request={
3435
# Manually construct the location
3536
# https://github.com/googleapis/python-workflows/issues/21
@@ -42,15 +43,17 @@ def test_workflow_execution():
4243
})
4344

4445
result = main.execute_workflow(PROJECT)
45-
assert len(result) > 0
46+
assert result.state == executions.Execution.State.SUCCEEDED
47+
assert len(result.result) > 0
4648

4749

4850
def workflow_exists():
4951
"""Returns True if the workflow exists in this project
5052
"""
5153
try:
52-
workflows_client = workflows_v1beta.WorkflowsClient()
53-
workflow_name = workflows_client.workflow_path(PROJECT, LOCATION, WORKFLOW_ID)
54+
workflows_client = workflows_v1.WorkflowsClient()
55+
workflow_name = workflows_client.workflow_path(
56+
PROJECT, LOCATION, WORKFLOW_ID)
5457
workflows_client.get_workflow(request={"name": workflow_name})
5558
return True
5659
except Exception as e:

workflows/cloud-client/myFirstWorkflow.workflows.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
- getCurrentTime:
1616
call: http.get
1717
args:
18-
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
18+
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
1919
result: currentTime
2020
- readWikipedia:
2121
call: http.get
2222
args:
2323
url: https://en.wikipedia.org/w/api.php
2424
query:
2525
action: opensearch
26-
search: ${currentTime.body.dayOfTheWeek}
26+
search: ${currentTime.body.dayOfWeek}
2727
result: wikiResult
2828
- returnResult:
2929
return: ${wikiResult.body[1]}

0 commit comments

Comments
 (0)