Skip to content

Commit 74bd9aa

Browse files
committed
Created Stop replication consistency group sample
1 parent 3cd04b0 commit 74bd9aa

File tree

5 files changed

+234
-5
lines changed

5 files changed

+234
-5
lines changed

compute/client_library/ingredients/disks/сonsistency_groups/remove_disk_consistency_group.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
# flake8: noqa
15-
1614

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.
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
2019

2120

2221
from google.cloud import compute_v1
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2024 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+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT stop_replication_in_consistency_group>
25+
def stop_replication_consistency_group(project_id, location, consistency_group_name):
26+
"""
27+
Stops the asynchronous replication for a consistency group.
28+
Args:
29+
project_id (str): The ID of the Google Cloud project.
30+
location (str): The region where the consistency group is located.
31+
consistency_group_id (str): The ID of the consistency group.
32+
Returns:
33+
bool: True if the replication was successfully stopped.
34+
"""
35+
consistency_group = compute_v1.DisksStopGroupAsyncReplicationResource(
36+
resource_policy=f"regions/{location}/resourcePolicies/{consistency_group_name}"
37+
)
38+
region_client = compute_v1.RegionDisksClient()
39+
operation = region_client.stop_group_async_replication(
40+
project=project_id,
41+
region=location,
42+
disks_stop_group_async_replication_resource_resource=consistency_group,
43+
)
44+
wait_for_extended_operation(operation, "Stopping replication for consistency group")
45+
46+
return True
47+
48+
49+
# </INGREDIENT>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2024 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+
# <REGION compute_consistency_group_remove_disk>
18+
# <IMPORTS/>
19+
20+
# <INGREDIENT wait_for_extended_operation />
21+
22+
# <INGREDIENT stop_replication_in_consistency_group />
23+
24+
# </REGION compute_consistency_group_remove_disk>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright 2024 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_consistency_group_remove_disk]
23+
from __future__ import annotations
24+
25+
import sys
26+
from typing import Any
27+
28+
from google.api_core.extended_operation import ExtendedOperation
29+
from google.cloud import compute_v1
30+
31+
32+
def wait_for_extended_operation(
33+
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
34+
) -> Any:
35+
"""
36+
Waits for the extended (long-running) operation to complete.
37+
38+
If the operation is successful, it will return its result.
39+
If the operation ends with an error, an exception will be raised.
40+
If there were any warnings during the execution of the operation
41+
they will be printed to sys.stderr.
42+
43+
Args:
44+
operation: a long-running operation you want to wait on.
45+
verbose_name: (optional) a more verbose name of the operation,
46+
used only during error and warning reporting.
47+
timeout: how long (in seconds) to wait for operation to finish.
48+
If None, wait indefinitely.
49+
50+
Returns:
51+
Whatever the operation.result() returns.
52+
53+
Raises:
54+
This method will raise the exception received from `operation.exception()`
55+
or RuntimeError if there is no exception set, but there is an `error_code`
56+
set for the `operation`.
57+
58+
In case of an operation taking longer than `timeout` seconds to complete,
59+
a `concurrent.futures.TimeoutError` will be raised.
60+
"""
61+
result = operation.result(timeout=timeout)
62+
63+
if operation.error_code:
64+
print(
65+
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
66+
file=sys.stderr,
67+
flush=True,
68+
)
69+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
70+
raise operation.exception() or RuntimeError(operation.error_message)
71+
72+
if operation.warnings:
73+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
74+
for warning in operation.warnings:
75+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
76+
77+
return result
78+
79+
80+
def stop_replication_consistency_group(project_id, location, consistency_group_name):
81+
"""
82+
Stops the asynchronous replication for a consistency group.
83+
Args:
84+
project_id (str): The ID of the Google Cloud project.
85+
location (str): The region where the consistency group is located.
86+
consistency_group_id (str): The ID of the consistency group.
87+
Returns:
88+
bool: True if the replication was successfully stopped.
89+
"""
90+
consistency_group = compute_v1.DisksStopGroupAsyncReplicationResource(
91+
resource_policy=f"regions/{location}/resourcePolicies/{consistency_group_name}"
92+
)
93+
region_client = compute_v1.RegionDisksClient()
94+
operation = region_client.stop_group_async_replication(
95+
project=project_id,
96+
region=location,
97+
disks_stop_group_async_replication_resource_resource=consistency_group,
98+
)
99+
wait_for_extended_operation(operation, "Stopping replication for consistency group")
100+
101+
return True
102+
103+
104+
# [END compute_consistency_group_remove_disk]

compute/client_library/snippets/tests/test_disks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
from ..disks.replication_disk_start import start_disk_replication
3939
from ..disks.replication_disk_stop import stop_disk_replication
4040
from ..disks.resize_disk import resize_disk
41+
from ..disks.сonsistency_groups.add_disk_consistency_group import (
42+
add_disk_consistency_group,
43+
)
44+
from ..disks.сonsistency_groups.create_consistency_group import create_consistency_group
45+
from ..disks.сonsistency_groups.delete_consistency_group import delete_consistency_group
46+
from ..disks.сonsistency_groups.remove_disk_consistency_group import remove_disk_consistency_group
47+
from ..disks.сonsistency_groups.stop_replication_consistency_group import (
48+
stop_replication_consistency_group,
49+
)
4150
from ..images.get import get_image_from_family
4251
from ..instances.create import create_instance, disk_from_image
4352
from ..instances.delete import delete_instance
@@ -499,3 +508,47 @@ def test_start_stop_zone_replication(test_empty_pd_balanced_disk, autodelete_dis
499508
)
500509
# Wait for the replication to stop
501510
time.sleep(20)
511+
512+
513+
def test_stop_replications_in_consistency_group(
514+
autodelete_regional_blank_disk, autodelete_regional_disk_name
515+
):
516+
group_name = "test-consistency-group" + uuid.uuid4().hex[:5]
517+
create_consistency_group(PROJECT, REGION, group_name, "DESCRIPTION")
518+
add_disk_consistency_group(
519+
project_id=PROJECT,
520+
disk_name=autodelete_regional_blank_disk.name,
521+
disk_location=REGION,
522+
consistency_group_name=group_name,
523+
consistency_group_region=REGION,
524+
)
525+
second_disk = create_secondary_region_disk(
526+
autodelete_regional_blank_disk.name,
527+
PROJECT,
528+
REGION,
529+
autodelete_regional_disk_name,
530+
PROJECT,
531+
REGION_SECONDARY,
532+
DISK_SIZE,
533+
)
534+
start_disk_replication(
535+
project_id=PROJECT,
536+
primary_disk_location=REGION,
537+
primary_disk_name=autodelete_regional_blank_disk.name,
538+
secondary_disk_location=REGION_SECONDARY,
539+
secondary_disk_name=second_disk.name,
540+
)
541+
time.sleep(15)
542+
try:
543+
assert stop_replication_consistency_group(PROJECT, REGION, group_name)
544+
finally:
545+
remove_disk_consistency_group(
546+
project_id=PROJECT,
547+
disk_name=autodelete_regional_blank_disk.name,
548+
disk_location=REGION,
549+
consistency_group_name=group_name,
550+
consistency_group_region=REGION,
551+
)
552+
time.sleep(10)
553+
delete_consistency_group(PROJECT, REGION, group_name)
554+

0 commit comments

Comments
 (0)