Skip to content

Commit ebf05af

Browse files
docs(samples): Adding the LRO handling method as a separate file and sample (GoogleCloudPlatform#9104)
* docs(samples): Adding the LRO handling method as a separate file and sample * Renaming the sample region --------- Co-authored-by: Remigiusz Samborski <rsamborski@users.noreply.github.com>
1 parent 3ffcd94 commit ebf05af

File tree

4 files changed

+97
-26
lines changed

4 files changed

+97
-26
lines changed

compute/client_library/recipes/operations/operation_check.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# flake8: noqa
15-
#
16-
# Licensed under the Apache License, Version 2.0 (the "License");
17-
# you may not use this file except in compliance with the License.
18-
# You may obtain a copy of the License at
19-
#
20-
# http://www.apache.org/licenses/LICENSE-2.0
21-
#
22-
# Unless required by applicable law or agreed to in writing, software
23-
# distributed under the License is distributed on an "AS IS" BASIS,
24-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25-
# See the License for the specific language governing permissions and
26-
# limitations under the License.
27-
# flake8: noqa
2815

2916
# <REGION compute_instances_operation_check>
3017
# <IMPORTS/>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2023 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+
# flake8: noqa
15+
16+
# <REGION compute_operation_extended_wait>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation/>
20+
# </REGION compute_operation_extended_wait>

compute/client_library/snippets/operations/operation_check.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# flake8: noqa
15-
#
16-
# Licensed under the Apache License, Version 2.0 (the "License");
17-
# you may not use this file except in compliance with the License.
18-
# You may obtain a copy of the License at
19-
#
20-
# http://www.apache.org/licenses/LICENSE-2.0
21-
#
22-
# Unless required by applicable law or agreed to in writing, software
23-
# distributed under the License is distributed on an "AS IS" BASIS,
24-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25-
# See the License for the specific language governing permissions and
26-
# limitations under the License.
27-
# flake8: noqa
2815

2916

3017
# This file is automatically generated. Please do not modify it directly.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2023 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+
# flake8: noqa
15+
16+
17+
# This file is automatically generated. Please do not modify it directly.
18+
# Find the relevant recipe file in the samples/recipes or samples/ingredients
19+
# directory and apply your changes there.
20+
21+
22+
# [START compute_operation_extended_wait]
23+
import sys
24+
from typing import Any
25+
26+
from google.api_core.extended_operation import ExtendedOperation
27+
28+
29+
def wait_for_extended_operation(
30+
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
31+
) -> Any:
32+
"""
33+
Waits for the extended (long-running) operation to complete.
34+
35+
If the operation is successful, it will return its result.
36+
If the operation ends with an error, an exception will be raised.
37+
If there were any warnings during the execution of the operation
38+
they will be printed to sys.stderr.
39+
40+
Args:
41+
operation: a long-running operation you want to wait on.
42+
verbose_name: (optional) a more verbose name of the operation,
43+
used only during error and warning reporting.
44+
timeout: how long (in seconds) to wait for operation to finish.
45+
If None, wait indefinitely.
46+
47+
Returns:
48+
Whatever the operation.result() returns.
49+
50+
Raises:
51+
This method will raise the exception received from `operation.exception()`
52+
or RuntimeError if there is no exception set, but there is an `error_code`
53+
set for the `operation`.
54+
55+
In case of an operation taking longer than `timeout` seconds to complete,
56+
a `concurrent.futures.TimeoutError` will be raised.
57+
"""
58+
result = operation.result(timeout=timeout)
59+
60+
if operation.error_code:
61+
print(
62+
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
63+
file=sys.stderr,
64+
flush=True,
65+
)
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
67+
raise operation.exception() or RuntimeError(operation.error_message)
68+
69+
if operation.warnings:
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
71+
for warning in operation.warnings:
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
73+
74+
return result
75+
76+
77+
# [END compute_operation_extended_wait]

0 commit comments

Comments
 (0)