|
| 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> |
0 commit comments