|
| 1 | +# Copyright 2022 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 | +import sys |
| 20 | + |
| 21 | +from google.cloud import compute_v1 |
| 22 | + |
| 23 | + |
| 24 | +# <INGREDIENT create_empty_disk> |
| 25 | +def create_empty_disk( |
| 26 | + project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int |
| 27 | +) -> compute_v1.Disk: |
| 28 | + """ |
| 29 | + Creates a new empty disk in a project in given zone. |
| 30 | +
|
| 31 | + Args: |
| 32 | + project_id: project ID or project number of the Cloud project you want to use. |
| 33 | + zone: name of the zone in which you want to create the disk. |
| 34 | + disk_name: name of the disk you want to create. |
| 35 | + disk_type: the type of disk you want to create. This value uses the following format: |
| 36 | + "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". |
| 37 | + For example: "zones/us-west3-b/diskTypes/pd-ssd" |
| 38 | + disk_size_gb: size of the new disk in gigabytes |
| 39 | +
|
| 40 | + Returns: |
| 41 | + An unattached Disk instance. |
| 42 | + """ |
| 43 | + disk = compute_v1.Disk() |
| 44 | + disk.size_gb = disk_size_gb |
| 45 | + disk.name = disk_name |
| 46 | + disk.zone = zone |
| 47 | + disk.type_ = disk_type |
| 48 | + |
| 49 | + disk_client = compute_v1.DisksClient() |
| 50 | + operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk) |
| 51 | + operation_client = compute_v1.ZoneOperationsClient() |
| 52 | + operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name) |
| 53 | + |
| 54 | + if operation.error: |
| 55 | + print("Error during disk creation:", operation.error, file=sys.stderr) |
| 56 | + raise RuntimeError(operation.error) |
| 57 | + if operation.warnings: |
| 58 | + print("Warnings during disk creation:\n", file=sys.stderr) |
| 59 | + for warning in operation.warnings: |
| 60 | + print(f" - {warning.code}: {warning.message}", file=sys.stderr) |
| 61 | + |
| 62 | + return disk_client.get(project=project_id, zone=zone, disk=disk.name) |
| 63 | +# </INGREDIENT> |
0 commit comments