Skip to content

Commit 2b8170c

Browse files
fix(workflows): Fix Quickstart for Cloud Client (#13063)
* fix(workflows): Fix Quickstart for Cloud Client - Add __main__ to call execute_workflow() when running `python main.py` - Remove redundant default arguments LOCATION and WORKFLOW_ID (as we have them in main.py and main_test.py as constants) - Apply style recomendations to pass linter check. * fix(workflows): fix blank line with whitespace * fix(workflows): add default values for environment variables To be compatible with documentation in https://cloud.google.com/workflows/docs/execute-workflow-client-libraries#run_the_sample
1 parent b14a1d5 commit 2b8170c

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

workflows/cloud-client/main.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,28 @@
1313
# limitations under the License.
1414

1515
# [START workflows_api_quickstart]
16+
import os
1617
import time
1718

1819
from google.cloud import workflows_v1
1920
from google.cloud.workflows import executions_v1
2021
from google.cloud.workflows.executions_v1 import Execution
2122
from google.cloud.workflows.executions_v1.types import executions
2223

24+
PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
25+
LOCATION = os.getenv("LOCATION", "us-central1")
26+
WORKFLOW_ID = os.getenv("WORKFLOW", "myFirstWorkflow")
2327

24-
def execute_workflow(
25-
project: str, location: str = "us-central1", workflow: str = "myFirstWorkflow"
26-
) -> Execution:
28+
29+
def execute_workflow(project: str, location: str, workflow: str) -> Execution:
2730
"""Execute a workflow and print the execution results.
2831
29-
A workflow consists of a series of steps described using the Workflows syntax, and can be written in either YAML or JSON.
32+
A workflow consists of a series of steps described
33+
using the Workflows syntax, and can be written in either YAML or JSON.
3034
3135
Args:
32-
project: The Google Cloud project id which contains the workflow to execute.
36+
project: The Google Cloud project id
37+
which contains the workflow to execute.
3338
location: The location for the workflow
3439
workflow: The ID of the workflow to execute.
3540
@@ -41,6 +46,7 @@ def execute_workflow(
4146
execution_client = executions_v1.ExecutionsClient()
4247
workflows_client = workflows_v1.WorkflowsClient()
4348
# [END workflows_api_quickstart_client_libraries]
49+
4450
# [START workflows_api_quickstart_execution]
4551
# Construct the fully qualified location path.
4652
parent = workflows_client.workflow_path(project, location, workflow)
@@ -54,7 +60,9 @@ def execute_workflow(
5460
backoff_delay = 1 # Start wait with delay of 1 second
5561
print("Poll for result...")
5662
while not execution_finished:
57-
execution = execution_client.get_execution(request={"name": response.name})
63+
execution = execution_client.get_execution(
64+
request={"name": response.name}
65+
)
5866
execution_finished = execution.state != executions.Execution.State.ACTIVE
5967

6068
# If we haven't seen the result yet, wait a second.
@@ -70,4 +78,7 @@ def execute_workflow(
7078
# [END workflows_api_quickstart_execution]
7179

7280

81+
if __name__ == "__main__":
82+
assert PROJECT, "'GOOGLE_CLOUD_PROJECT' environment variable not set."
83+
execute_workflow(PROJECT, LOCATION, WORKFLOW_ID)
7384
# [END workflows_api_quickstart]

workflows/cloud-client/main_test.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
import main
2121

22-
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
2323
LOCATION = "us-central1"
2424
WORKFLOW_ID = "myFirstWorkflow"
2525

2626

2727
def test_workflow_execution() -> None:
28-
assert PROJECT != ""
28+
assert PROJECT, "'GOOGLE_CLOUD_PROJECT' environment variable not set."
2929

3030
if not workflow_exists():
3131
workflow_file = open("myFirstWorkflow.workflows.yaml").read()
@@ -37,20 +37,25 @@ def test_workflow_execution() -> None:
3737
# https://github.com/googleapis/python-workflows/issues/21
3838
"parent": f"projects/{PROJECT}/locations/{LOCATION}",
3939
"workflow_id": WORKFLOW_ID,
40-
"workflow": {"name": WORKFLOW_ID, "source_contents": workflow_file},
40+
"workflow": {
41+
"name": WORKFLOW_ID,
42+
"source_contents": workflow_file
43+
},
4144
}
4245
)
4346

44-
result = main.execute_workflow(PROJECT)
47+
result = main.execute_workflow(PROJECT, LOCATION, WORKFLOW_ID)
4548
assert result.state == executions.Execution.State.SUCCEEDED
46-
assert len(result.result) > 0
49+
assert result.result # Result not empty
4750

4851

4952
def workflow_exists() -> bool:
50-
"""Returns True if the workflow exists in this project"""
53+
"""Returns True if the workflow exists in this project."""
5154
try:
5255
workflows_client = workflows_v1.WorkflowsClient()
53-
workflow_name = workflows_client.workflow_path(PROJECT, LOCATION, WORKFLOW_ID)
56+
workflow_name = workflows_client.workflow_path(
57+
PROJECT, LOCATION, WORKFLOW_ID
58+
)
5459
workflows_client.get_workflow(request={"name": workflow_name})
5560
return True
5661
except Exception as e:

0 commit comments

Comments
 (0)