Skip to content

Commit aaabd2d

Browse files
feat: Create regional replicated Sample (GoogleCloudPlatform#12982)
* Created regional replicated disk Sample. Updated tests.
1 parent ff10417 commit aaabd2d

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 create_replicated_disk>
24+
def create_regional_replicated_disk(
25+
project_id,
26+
region,
27+
disk_name,
28+
size_gb,
29+
disk_type: str = "pd-ssd",
30+
) -> compute_v1.Disk:
31+
"""Creates a synchronously replicated disk in a region across two zones.
32+
Args:
33+
project_id (str): The ID of the Google Cloud project.
34+
region (str): The region where the disk will be created.
35+
disk_name (str): The name of the disk.
36+
size_gb (int): The size of the disk in gigabytes.
37+
disk_type (str): The type of the disk. Default is 'pd-ssd'.
38+
Returns:
39+
compute_v1.Disk: The created disk object.
40+
"""
41+
disk = compute_v1.Disk()
42+
disk.name = disk_name
43+
44+
# You can specify the zones where the disk will be replicated.
45+
disk.replica_zones = [
46+
f"zones/{region}-a",
47+
f"zones/{region}-b",
48+
]
49+
disk.size_gb = size_gb
50+
disk.type = f"regions/{region}/diskTypes/{disk_type}"
51+
52+
client = compute_v1.RegionDisksClient()
53+
operation = client.insert(project=project_id, region=region, disk_resource=disk)
54+
55+
wait_for_extended_operation(operation, "Replicated disk creation")
56+
57+
return client.get(project=project_id, region=region, disk=disk_name)
58+
59+
60+
# </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_regional_replicated>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_replicated_disk />
22+
23+
# </REGION compute_disk_regional_replicated>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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_regional_replicated]
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 create_regional_replicated_disk(
81+
project_id,
82+
region,
83+
disk_name,
84+
size_gb,
85+
disk_type: str = "pd-ssd",
86+
) -> compute_v1.Disk:
87+
"""Creates a synchronously replicated disk in a region across two zones.
88+
Args:
89+
project_id (str): The ID of the Google Cloud project.
90+
region (str): The region where the disk will be created.
91+
disk_name (str): The name of the disk.
92+
size_gb (int): The size of the disk in gigabytes.
93+
disk_type (str): The type of the disk. Default is 'pd-ssd'.
94+
Returns:
95+
compute_v1.Disk: The created disk object.
96+
"""
97+
disk = compute_v1.Disk()
98+
disk.name = disk_name
99+
100+
# You can specify the zones where the disk will be replicated.
101+
disk.replica_zones = [
102+
f"zones/{region}-a",
103+
f"zones/{region}-b",
104+
]
105+
disk.size_gb = size_gb
106+
disk.type = f"regions/{region}/diskTypes/{disk_type}"
107+
108+
client = compute_v1.RegionDisksClient()
109+
operation = client.insert(project=project_id, region=region, disk_resource=disk)
110+
111+
wait_for_extended_operation(operation, "Replicated disk creation")
112+
113+
return client.get(project=project_id, region=region, disk=disk_name)
114+
115+
116+
# [END compute_disk_regional_replicated]

compute/client_library/snippets/tests/test_disks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from ..disks.create_hyperdisk_from_pool import create_hyperdisk_from_pool
2929
from ..disks.create_hyperdisk_storage_pool import create_hyperdisk_storage_pool
3030
from ..disks.create_kms_encrypted_disk import create_kms_encrypted_disk
31+
from ..disks.create_replicated_disk import create_regional_replicated_disk
3132
from ..disks.create_secondary_custom import create_secondary_custom_disk
3233
from ..disks.create_secondary_disk import create_secondary_disk
3334
from ..disks.create_secondary_region_disk import create_secondary_region_disk
@@ -447,6 +448,17 @@ def test_create_custom_secondary_disk(
447448
assert disk.labels["source-disk"] == test_empty_pd_balanced_disk.name
448449

449450

451+
def test_create_replicated_disk(autodelete_regional_disk_name):
452+
disk = create_regional_replicated_disk(
453+
project_id=PROJECT,
454+
region=REGION_SECONDARY,
455+
disk_name=autodelete_regional_disk_name,
456+
size_gb=DISK_SIZE,
457+
)
458+
assert f"{PROJECT}/zones/{REGION_SECONDARY}-" in disk.replica_zones[0]
459+
assert f"{PROJECT}/zones/{REGION_SECONDARY}-" in disk.replica_zones[1]
460+
461+
450462
def test_start_stop_region_replication(
451463
autodelete_regional_blank_disk, autodelete_regional_disk_name
452464
):

0 commit comments

Comments
 (0)