Skip to content

Commit d0f51e4

Browse files
committed
New Disks Samples. Secondary + Secondary region
1 parent d8ee104 commit d0f51e4

File tree

7 files changed

+495
-0
lines changed

7 files changed

+495
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
# https://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_secondary_disk>
24+
def create_secondary_disk(
25+
primary_disk_name: str,
26+
primary_disk_project: str,
27+
primary_disk_zone: str,
28+
secondary_disk_name: str,
29+
secondary_disk_project: str,
30+
secondary_disk_zone: str,
31+
disk_size_gb: int,
32+
disk_type: str = "pd-ssd",
33+
) -> compute_v1.Disk:
34+
"""Create a secondary disk with a primary disk as a source.
35+
Args:
36+
primary_disk_name (str): The name of the primary disk.
37+
primary_disk_project (str): The project of the primary disk.
38+
primary_disk_zone (str): The location of the primary disk.
39+
secondary_disk_name (str): The name of the secondary disk.
40+
secondary_disk_project (str): The project of the secondary disk.
41+
secondary_disk_zone (str): The location of the secondary disk.
42+
disk_size_gb (int): The size of the disk in GB. Should be the same as the primary disk.
43+
disk_type (str): The type of the disk. Must be one of pd-ssd or pd-balanced.
44+
"""
45+
disk_client = compute_v1.DisksClient()
46+
disk = compute_v1.Disk()
47+
disk.name = secondary_disk_name
48+
disk.size_gb = disk_size_gb
49+
disk.type = f"zones/{primary_disk_zone}/diskTypes/{disk_type}"
50+
disk.async_primary_disk = compute_v1.DiskAsyncReplication(
51+
disk=f"projects/{primary_disk_project}/zones/{primary_disk_zone}/disks/{primary_disk_name}"
52+
)
53+
54+
operation = disk_client.insert(
55+
project=secondary_disk_project, zone=secondary_disk_zone, disk_resource=disk
56+
)
57+
wait_for_extended_operation(operation, "create_secondary_disk")
58+
59+
secondary_disk = disk_client.get(
60+
project=secondary_disk_project,
61+
zone=secondary_disk_zone,
62+
disk=secondary_disk_name,
63+
)
64+
return secondary_disk
65+
66+
67+
# </INGREDIENT>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
# https://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_secondary_region_disk>
24+
def create_secondary_region_disk(
25+
primary_disk_name: str,
26+
primary_disk_project: str,
27+
primary_disk_region: str,
28+
secondary_disk_name: str,
29+
secondary_disk_project: str,
30+
secondary_disk_region: str,
31+
disk_size_gb: int,
32+
disk_type: str = "pd-ssd",
33+
) -> compute_v1.Disk:
34+
"""Create a secondary disk in replica zones with a primary region disk as a source .
35+
Args:
36+
primary_disk_name (str): The name of the primary disk.
37+
primary_disk_project (str): The project of the primary disk.
38+
primary_disk_region (str): The location of the primary disk.
39+
secondary_disk_name (str): The name of the secondary disk.
40+
secondary_disk_project (str): The project of the secondary disk.
41+
secondary_disk_region (str): The location of the secondary disk.
42+
disk_size_gb (int): The size of the disk in GB. Should be the same as the primary disk.
43+
disk_type (str): The type of the disk. Must be one of pd-ssd or pd-balanced.
44+
"""
45+
disk_client = compute_v1.RegionDisksClient()
46+
disk = compute_v1.Disk()
47+
disk.name = secondary_disk_name
48+
disk.size_gb = disk_size_gb
49+
disk.type = f"regions/{primary_disk_region}/diskTypes/{disk_type}"
50+
disk.async_primary_disk = compute_v1.DiskAsyncReplication(
51+
disk=f"projects/{primary_disk_project}/regions/{primary_disk_region}/disks/{primary_disk_name}"
52+
)
53+
54+
# Set the replica zones for the secondary disk. By default, in b and c zones.
55+
disk.replica_zones = [
56+
f"zones/{secondary_disk_region}-b",
57+
f"zones/{secondary_disk_region}-c",
58+
]
59+
60+
operation = disk_client.insert(
61+
project=secondary_disk_project,
62+
region=secondary_disk_region,
63+
disk_resource=disk,
64+
)
65+
wait_for_extended_operation(operation, "create_secondary_region_disk")
66+
secondary_disk = disk_client.get(
67+
project=secondary_disk_project,
68+
region=secondary_disk_region,
69+
disk=secondary_disk_name,
70+
)
71+
return secondary_disk
72+
73+
74+
# </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_create_secondary>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_secondary_disk />
22+
23+
# </REGION compute_disk_create_secondary>
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_create_secondary_regional>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_secondary_region_disk />
22+
23+
# </REGION compute_disk_create_secondary_regional>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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_create_secondary]
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_secondary_disk(
81+
primary_disk_name: str,
82+
primary_disk_project: str,
83+
primary_disk_zone: str,
84+
secondary_disk_name: str,
85+
secondary_disk_project: str,
86+
secondary_disk_zone: str,
87+
disk_size_gb: int,
88+
disk_type: str = "pd-ssd",
89+
) -> compute_v1.Disk:
90+
"""Create a secondary disk with a primary disk as a source.
91+
Args:
92+
primary_disk_name (str): The name of the primary disk.
93+
primary_disk_project (str): The project of the primary disk.
94+
primary_disk_zone (str): The location of the primary disk.
95+
secondary_disk_name (str): The name of the secondary disk.
96+
secondary_disk_project (str): The project of the secondary disk.
97+
secondary_disk_zone (str): The location of the secondary disk.
98+
disk_size_gb (int): The size of the disk in GB. Should be the same as the primary disk.
99+
disk_type (str): The type of the disk. Must be one of pd-ssd or pd-balanced.
100+
"""
101+
disk_client = compute_v1.DisksClient()
102+
disk = compute_v1.Disk()
103+
disk.name = secondary_disk_name
104+
disk.size_gb = disk_size_gb
105+
disk.type = f"zones/{primary_disk_zone}/diskTypes/{disk_type}"
106+
disk.async_primary_disk = compute_v1.DiskAsyncReplication(
107+
disk=f"projects/{primary_disk_project}/zones/{primary_disk_zone}/disks/{primary_disk_name}"
108+
)
109+
110+
operation = disk_client.insert(
111+
project=secondary_disk_project, zone=secondary_disk_zone, disk_resource=disk
112+
)
113+
wait_for_extended_operation(operation, "create_secondary_disk")
114+
115+
secondary_disk = disk_client.get(
116+
project=secondary_disk_project,
117+
zone=secondary_disk_zone,
118+
disk=secondary_disk_name,
119+
)
120+
return secondary_disk
121+
122+
123+
# [END compute_disk_create_secondary]

0 commit comments

Comments
 (0)