|
| 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 __future__ import annotations |
| 17 | + |
| 18 | +import sys |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +from google.api_core.extended_operation import ExtendedOperation |
| 22 | +from google.cloud import compute_v1 |
| 23 | + |
| 24 | + |
| 25 | +def wait_for_extended_operation( |
| 26 | + operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300 |
| 27 | +) -> Any: |
| 28 | + """ |
| 29 | + Waits for the extended (long-running) operation to complete. |
| 30 | + If the operation is successful, it will return its result. |
| 31 | + If the operation ends with an error, an exception will be raised. |
| 32 | + If there were any warnings during the execution of the operation |
| 33 | + they will be printed to sys.stderr. |
| 34 | + Args: |
| 35 | + operation: a long-running operation you want to wait on. |
| 36 | + verbose_name: (optional) a more verbose name of the operation, |
| 37 | + used only during error and warning reporting. |
| 38 | + timeout: how long (in seconds) to wait for operation to finish. |
| 39 | + If None, wait indefinitely. |
| 40 | + Returns: |
| 41 | + Whatever the operation.result() returns. |
| 42 | + Raises: |
| 43 | + This method will raise the exception received from `operation.exception()` |
| 44 | + or RuntimeError if there is no exception set, but there is an `error_code` |
| 45 | + set for the `operation`. |
| 46 | + In case of an operation taking longer than `timeout` seconds to complete, |
| 47 | + a `concurrent.futures.TimeoutError` will be raised. |
| 48 | + """ |
| 49 | + result = operation.result(timeout=timeout) |
| 50 | + |
| 51 | + if operation.error_code: |
| 52 | + print( |
| 53 | + f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", |
| 54 | + file=sys.stderr, |
| 55 | + flush=True, |
| 56 | + ) |
| 57 | + print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True) |
| 58 | + raise operation.exception() or RuntimeError(operation.error_message) |
| 59 | + |
| 60 | + if operation.warnings: |
| 61 | + print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True) |
| 62 | + for warning in operation.warnings: |
| 63 | + print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True) |
| 64 | + |
| 65 | + return result |
| 66 | + |
| 67 | + |
| 68 | +def create_secondary_custom_disk( |
| 69 | + primary_disk_name: str, |
| 70 | + primary_disk_project: str, |
| 71 | + primary_disk_zone: str, |
| 72 | + secondary_disk_name: str, |
| 73 | + secondary_disk_project: str, |
| 74 | + secondary_disk_zone: str, |
| 75 | + disk_size_gb: int, |
| 76 | + disk_type: str = "pd-ssd", |
| 77 | + |
| 78 | +) -> compute_v1.Disk: |
| 79 | + """Creates a custom secondary disk whose properties differ from the primary disk. |
| 80 | + Args: |
| 81 | + primary_disk_name (str): The name of the primary disk. |
| 82 | + primary_disk_project (str): The project of the primary disk. |
| 83 | + primary_disk_zone (str): The location of the primary disk. |
| 84 | + secondary_disk_name (str): The name of the secondary disk. |
| 85 | + secondary_disk_project (str): The project of the secondary disk. |
| 86 | + secondary_disk_zone (str): The location of the secondary disk. |
| 87 | + disk_size_gb (int): The size of the disk in GB. Should be the same as the primary disk. |
| 88 | + disk_type (str): The type of the disk. Must be one of pd-ssd or pd-balanced. |
| 89 | + """ |
| 90 | + disk_client = compute_v1.DisksClient() |
| 91 | + disk = compute_v1.Disk() |
| 92 | + disk.name = secondary_disk_name |
| 93 | + disk.size_gb = disk_size_gb |
| 94 | + disk.type = f"zones/{primary_disk_zone}/diskTypes/{disk_type}" |
| 95 | + disk.async_primary_disk = compute_v1.DiskAsyncReplication( |
| 96 | + disk=f"projects/{primary_disk_project}/zones/{primary_disk_zone}/disks/{primary_disk_name}" |
| 97 | + ) |
| 98 | + |
| 99 | + # Specify additional guest OS features for the secondary disk |
| 100 | + # Set to one or more of the following values: - VIRTIO_SCSI_MULTIQUEUE - WINDOWS - MULTI_IP_SUBNET |
| 101 | + # - UEFI_COMPATIBLE - GVNIC - SEV_CAPABLE - SUSPEND_RESUME_COMPATIBLE |
| 102 | + # - SEV_LIVE_MIGRATABLE_V2 - SEV_SNP_CAPABLE - TDX_CAPABLE - IDPF |
| 103 | + disk.guest_os_features = [compute_v1.GuestOsFeature(type="MULTI_IP_SUBNET")] |
| 104 | + |
| 105 | + # Assign additional labels to the secondary disk |
| 106 | + disk.labels = { |
| 107 | + "source-disk": primary_disk_name, |
| 108 | + "secondary-disk-for-replication": "true", |
| 109 | + } |
| 110 | + |
| 111 | + operation = disk_client.insert( |
| 112 | + project=secondary_disk_project, zone=secondary_disk_zone, disk_resource=disk |
| 113 | + ) |
| 114 | + wait_for_extended_operation(operation, "create_secondary_disk") |
| 115 | + |
| 116 | + secondary_disk = disk_client.get( |
| 117 | + project=secondary_disk_project, |
| 118 | + zone=secondary_disk_zone, |
| 119 | + disk=secondary_disk_name, |
| 120 | + ) |
| 121 | + return secondary_disk |
| 122 | + |
| 123 | + |
| 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"]) |
0 commit comments