Skip to content

Commit 04fdc19

Browse files
committed
Fix file generation
1 parent 27c735f commit 04fdc19

File tree

3 files changed

+111
-18
lines changed

3 files changed

+111
-18
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
from google.cloud import compute_v1
17+
18+
19+
# <INGREDIENT create_secondary_custom_disk>
20+
def create_secondary_custom_disk(
21+
primary_disk_name: str,
22+
primary_disk_project: str,
23+
primary_disk_zone: str,
24+
secondary_disk_name: str,
25+
secondary_disk_project: str,
26+
secondary_disk_zone: str,
27+
disk_size_gb: int,
28+
disk_type: str = "pd-ssd",
29+
) -> compute_v1.Disk:
30+
"""Creates a custom secondary disk whose properties differ from the primary disk.
31+
Args:
32+
primary_disk_name (str): The name of the primary disk.
33+
primary_disk_project (str): The project of the primary disk.
34+
primary_disk_zone (str): The location of the primary disk.
35+
secondary_disk_name (str): The name of the secondary disk.
36+
secondary_disk_project (str): The project of the secondary disk.
37+
secondary_disk_zone (str): The location of the secondary disk.
38+
disk_size_gb (int): The size of the disk in GB. Should be the same as the primary disk.
39+
disk_type (str): The type of the disk. Must be one of pd-ssd or pd-balanced.
40+
"""
41+
disk_client = compute_v1.DisksClient()
42+
disk = compute_v1.Disk()
43+
disk.name = secondary_disk_name
44+
disk.size_gb = disk_size_gb
45+
disk.type = f"zones/{primary_disk_zone}/diskTypes/{disk_type}"
46+
disk.async_primary_disk = compute_v1.DiskAsyncReplication(
47+
disk=f"projects/{primary_disk_project}/zones/{primary_disk_zone}/disks/{primary_disk_name}"
48+
)
49+
50+
# Specify additional guest OS features for the secondary disk
51+
# Set to one or more of the following values: - VIRTIO_SCSI_MULTIQUEUE - WINDOWS - MULTI_IP_SUBNET
52+
# - UEFI_COMPATIBLE - GVNIC - SEV_CAPABLE - SUSPEND_RESUME_COMPATIBLE
53+
# - SEV_LIVE_MIGRATABLE_V2 - SEV_SNP_CAPABLE - TDX_CAPABLE - IDPF
54+
disk.guest_os_features = [compute_v1.GuestOsFeature(type="MULTI_IP_SUBNET")]
55+
56+
# Assign additional labels to the secondary disk
57+
disk.labels = {
58+
"source-disk": primary_disk_name,
59+
"secondary-disk-for-replication": "true",
60+
}
61+
62+
operation = disk_client.insert(
63+
project=secondary_disk_project, zone=secondary_disk_zone, disk_resource=disk
64+
)
65+
wait_for_extended_operation(operation, "create_secondary_disk")
66+
67+
secondary_disk = disk_client.get(
68+
project=secondary_disk_project,
69+
zone=secondary_disk_zone,
70+
disk=secondary_disk_name,
71+
)
72+
return secondary_disk
73+
74+
75+
# </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_custom>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_secondary_custom_disk />
22+
23+
# </REGION compute_disk_create_secondary_custom>

compute/client_library/snippets/disks/create_secondary_custom.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
# limitations under the License.
1414
# flake8: noqa
1515

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_custom]
1623
from __future__ import annotations
1724

1825
import sys
@@ -27,22 +34,27 @@ def wait_for_extended_operation(
2734
) -> Any:
2835
"""
2936
Waits for the extended (long-running) operation to complete.
37+
3038
If the operation is successful, it will return its result.
3139
If the operation ends with an error, an exception will be raised.
3240
If there were any warnings during the execution of the operation
3341
they will be printed to sys.stderr.
42+
3443
Args:
3544
operation: a long-running operation you want to wait on.
3645
verbose_name: (optional) a more verbose name of the operation,
3746
used only during error and warning reporting.
3847
timeout: how long (in seconds) to wait for operation to finish.
3948
If None, wait indefinitely.
49+
4050
Returns:
4151
Whatever the operation.result() returns.
52+
4253
Raises:
4354
This method will raise the exception received from `operation.exception()`
4455
or RuntimeError if there is no exception set, but there is an `error_code`
4556
set for the `operation`.
57+
4658
In case of an operation taking longer than `timeout` seconds to complete,
4759
a `concurrent.futures.TimeoutError` will be raised.
4860
"""
@@ -74,7 +86,6 @@ def create_secondary_custom_disk(
7486
secondary_disk_zone: str,
7587
disk_size_gb: int,
7688
disk_type: str = "pd-ssd",
77-
7889
) -> compute_v1.Disk:
7990
"""Creates a custom secondary disk whose properties differ from the primary disk.
8091
Args:
@@ -121,20 +132,4 @@ def create_secondary_custom_disk(
121132
return secondary_disk
122133

123134

124-
if __name__ == "__main__":
125-
import os
126-
127-
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
128-
a = create_secondary_custom_disk(
129-
primary_disk_name="disk-1",
130-
primary_disk_project=PROJECT_ID,
131-
primary_disk_zone="europe-west2-c",
132-
secondary_disk_name="secondary-disk-name6",
133-
secondary_disk_project=PROJECT_ID,
134-
secondary_disk_zone="europe-west1-c",
135-
disk_size_gb=100,
136-
disk_type="pd-ssd",
137-
)
138-
print(a.labels)
139-
print(a.labels["secondary-disk-for-replication"])
140-
print(a.labels["source-disk"])
135+
# [END compute_disk_create_secondary_custom]

0 commit comments

Comments
 (0)