-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Created Stop replication consistency group sample (#12985)
* Created Stop replication consistency group sample * Fix lint * Updated dependencies * Updated tests * Test fix * Fixed conflicts and updated comments
- Loading branch information
1 parent
f9fddc3
commit 1a866be
Showing
6 changed files
with
224 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...client_library/ingredients/disks/сonsistency_groups/stop_replication_consistency_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# flake8: noqa | ||
|
||
from google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT stop_replication_in_consistency_group> | ||
def stop_replication_consistency_group(project_id, location, consistency_group_name): | ||
""" | ||
Stops the asynchronous replication for a consistency group. | ||
Args: | ||
project_id (str): The ID of the Google Cloud project. | ||
location (str): The region where the consistency group is located. | ||
consistency_group_id (str): The ID of the consistency group. | ||
Returns: | ||
bool: True if the replication was successfully stopped. | ||
""" | ||
consistency_group = compute_v1.DisksStopGroupAsyncReplicationResource( | ||
resource_policy=f"regions/{location}/resourcePolicies/{consistency_group_name}" | ||
) | ||
region_client = compute_v1.RegionDisksClient() | ||
operation = region_client.stop_group_async_replication( | ||
project=project_id, | ||
region=location, | ||
disks_stop_group_async_replication_resource_resource=consistency_group, | ||
) | ||
wait_for_extended_operation(operation, "Stopping replication for consistency group") | ||
|
||
return True | ||
|
||
|
||
# </INGREDIENT> |
24 changes: 24 additions & 0 deletions
24
...ute/client_library/recipes/disks/сonsistency_groups/stop_replication_consistency_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# flake8: noqa | ||
|
||
|
||
# <REGION compute_consistency_group_stop_replication> | ||
# <IMPORTS/> | ||
|
||
# <INGREDIENT wait_for_extended_operation /> | ||
|
||
# <INGREDIENT stop_replication_in_consistency_group /> | ||
|
||
# </REGION compute_consistency_group_stop_replication> |
104 changes: 104 additions & 0 deletions
104
...te/client_library/snippets/disks/сonsistency_groups/stop_replication_consistency_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# flake8: noqa | ||
|
||
|
||
# This file is automatically generated. Please do not modify it directly. | ||
# Find the relevant recipe file in the samples/recipes or samples/ingredients | ||
# directory and apply your changes there. | ||
|
||
|
||
# [START compute_consistency_group_stop_replication] | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import Any | ||
|
||
from google.api_core.extended_operation import ExtendedOperation | ||
from google.cloud import compute_v1 | ||
|
||
|
||
def wait_for_extended_operation( | ||
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300 | ||
) -> Any: | ||
""" | ||
Waits for the extended (long-running) operation to complete. | ||
If the operation is successful, it will return its result. | ||
If the operation ends with an error, an exception will be raised. | ||
If there were any warnings during the execution of the operation | ||
they will be printed to sys.stderr. | ||
Args: | ||
operation: a long-running operation you want to wait on. | ||
verbose_name: (optional) a more verbose name of the operation, | ||
used only during error and warning reporting. | ||
timeout: how long (in seconds) to wait for operation to finish. | ||
If None, wait indefinitely. | ||
Returns: | ||
Whatever the operation.result() returns. | ||
Raises: | ||
This method will raise the exception received from `operation.exception()` | ||
or RuntimeError if there is no exception set, but there is an `error_code` | ||
set for the `operation`. | ||
In case of an operation taking longer than `timeout` seconds to complete, | ||
a `concurrent.futures.TimeoutError` will be raised. | ||
""" | ||
result = operation.result(timeout=timeout) | ||
|
||
if operation.error_code: | ||
print( | ||
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", | ||
file=sys.stderr, | ||
flush=True, | ||
) | ||
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True) | ||
raise operation.exception() or RuntimeError(operation.error_message) | ||
|
||
if operation.warnings: | ||
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True) | ||
for warning in operation.warnings: | ||
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True) | ||
|
||
return result | ||
|
||
|
||
def stop_replication_consistency_group(project_id, location, consistency_group_name): | ||
""" | ||
Stops the asynchronous replication for a consistency group. | ||
Args: | ||
project_id (str): The ID of the Google Cloud project. | ||
location (str): The region where the consistency group is located. | ||
consistency_group_id (str): The ID of the consistency group. | ||
Returns: | ||
bool: True if the replication was successfully stopped. | ||
""" | ||
consistency_group = compute_v1.DisksStopGroupAsyncReplicationResource( | ||
resource_policy=f"regions/{location}/resourcePolicies/{consistency_group_name}" | ||
) | ||
region_client = compute_v1.RegionDisksClient() | ||
operation = region_client.stop_group_async_replication( | ||
project=project_id, | ||
region=location, | ||
disks_stop_group_async_replication_resource_resource=consistency_group, | ||
) | ||
wait_for_extended_operation(operation, "Stopping replication for consistency group") | ||
|
||
return True | ||
|
||
|
||
# [END compute_consistency_group_stop_replication] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters