Skip to content

Commit 1132a4e

Browse files
Created new Samples for start/stop Disk Asynchronous Replication. Updated tests. (#12805)
1 parent 4ecefa0 commit 1132a4e

File tree

7 files changed

+458
-0
lines changed

7 files changed

+458
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
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
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT start_disk_replication>
24+
def start_disk_replication(
25+
project_id: str,
26+
primary_disk_location: str,
27+
primary_disk_name: str,
28+
secondary_disk_location: str,
29+
secondary_disk_name: str,
30+
) -> bool:
31+
"""Starts the asynchronous replication of a primary disk to a secondary disk.
32+
Args:
33+
project_id (str): The ID of the Google Cloud project.
34+
primary_disk_location (str): The location of the primary disk, either a zone or a region.
35+
primary_disk_name (str): The name of the primary disk.
36+
secondary_disk_location (str): The location of the secondary disk, either a zone or a region.
37+
secondary_disk_name (str): The name of the secondary disk.
38+
Returns:
39+
bool: True if the replication was successfully started.
40+
"""
41+
# Check if the primary disk location is a region or a zone.
42+
if primary_disk_location[-1].isdigit():
43+
region_client = compute_v1.RegionDisksClient()
44+
request_resource = compute_v1.RegionDisksStartAsyncReplicationRequest(
45+
async_secondary_disk=f"projects/{project_id}/regions/{secondary_disk_location}/disks/{secondary_disk_name}"
46+
)
47+
operation = region_client.start_async_replication(
48+
project=project_id,
49+
region=primary_disk_location,
50+
disk=primary_disk_name,
51+
region_disks_start_async_replication_request_resource=request_resource,
52+
)
53+
else:
54+
client = compute_v1.DisksClient()
55+
request_resource = compute_v1.DisksStartAsyncReplicationRequest(
56+
async_secondary_disk=f"zones/{secondary_disk_location}/disks/{secondary_disk_name}"
57+
)
58+
operation = client.start_async_replication(
59+
project=project_id,
60+
zone=primary_disk_location,
61+
disk=primary_disk_name,
62+
disks_start_async_replication_request_resource=request_resource,
63+
)
64+
wait_for_extended_operation(operation, verbose_name="replication operation")
65+
print(f"Replication for disk {primary_disk_name} started.")
66+
return True
67+
68+
69+
# </INGREDIENT>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
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
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT stop_disk_replication>
24+
def stop_disk_replication(
25+
project_id: str, primary_disk_location: str, primary_disk_name: str
26+
) -> bool:
27+
"""
28+
Stops the asynchronous replication of a disk.
29+
Args:
30+
project_id (str): The ID of the Google Cloud project.
31+
primary_disk_location (str): The location of the primary disk, either a zone or a region.
32+
primary_disk_name (str): The name of the primary disk.
33+
Returns:
34+
bool: True if the replication was successfully stopped.
35+
"""
36+
# Check if the primary disk is in a region or a zone
37+
if primary_disk_location[-1].isdigit():
38+
region_client = compute_v1.RegionDisksClient()
39+
operation = region_client.stop_async_replication(
40+
project=project_id, region=primary_disk_location, disk=primary_disk_name
41+
)
42+
else:
43+
zone_client = compute_v1.DisksClient()
44+
operation = zone_client.stop_async_replication(
45+
project=project_id, zone=primary_disk_location, disk=primary_disk_name
46+
)
47+
48+
wait_for_extended_operation(operation, verbose_name="replication operation")
49+
print(f"Replication for disk {primary_disk_name} stopped.")
50+
return True
51+
52+
53+
# </INGREDIENT>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
# <REGION compute_disk_start_replication>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT start_disk_replication />
22+
23+
# </REGION compute_disk_start_replication>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
# <REGION compute_disk_stop_replication>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT stop_disk_replication />
22+
23+
# </REGION compute_disk_stop_replication>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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_disk_start_replication]
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 start_disk_replication(
81+
project_id: str,
82+
primary_disk_location: str,
83+
primary_disk_name: str,
84+
secondary_disk_location: str,
85+
secondary_disk_name: str,
86+
) -> bool:
87+
"""Starts the asynchronous replication of a primary disk to a secondary disk.
88+
Args:
89+
project_id (str): The ID of the Google Cloud project.
90+
primary_disk_location (str): The location of the primary disk, either a zone or a region.
91+
primary_disk_name (str): The name of the primary disk.
92+
secondary_disk_location (str): The location of the secondary disk, either a zone or a region.
93+
secondary_disk_name (str): The name of the secondary disk.
94+
Returns:
95+
bool: True if the replication was successfully started.
96+
"""
97+
# Check if the primary disk location is a region or a zone.
98+
if primary_disk_location[-1].isdigit():
99+
region_client = compute_v1.RegionDisksClient()
100+
request_resource = compute_v1.RegionDisksStartAsyncReplicationRequest(
101+
async_secondary_disk=f"projects/{project_id}/regions/{secondary_disk_location}/disks/{secondary_disk_name}"
102+
)
103+
operation = region_client.start_async_replication(
104+
project=project_id,
105+
region=primary_disk_location,
106+
disk=primary_disk_name,
107+
region_disks_start_async_replication_request_resource=request_resource,
108+
)
109+
else:
110+
client = compute_v1.DisksClient()
111+
request_resource = compute_v1.DisksStartAsyncReplicationRequest(
112+
async_secondary_disk=f"zones/{secondary_disk_location}/disks/{secondary_disk_name}"
113+
)
114+
operation = client.start_async_replication(
115+
project=project_id,
116+
zone=primary_disk_location,
117+
disk=primary_disk_name,
118+
disks_start_async_replication_request_resource=request_resource,
119+
)
120+
wait_for_extended_operation(operation, verbose_name="replication operation")
121+
print(f"Replication for disk {primary_disk_name} started.")
122+
return True
123+
124+
125+
# [END compute_disk_start_replication]

0 commit comments

Comments
 (0)