|
| 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 | +from typing import Optional |
| 20 | + |
| 21 | +from google.cloud import compute_v1 |
| 22 | + |
| 23 | + |
| 24 | +# <INGREDIENT create_kms_encrypted_disk> |
| 25 | +def create_kms_encrypted_disk(project_id: str, zone: str, disk_name: str, disk_type: str, |
| 26 | + disk_size_gb: int, kms_key_name: str, |
| 27 | + disk_link: Optional[str] = None, image_link: Optional[str] = None) -> compute_v1.Disk: |
| 28 | + """ |
| 29 | + Creates a zonal disk in a project. If you do not provide values for disk_link or image_link, |
| 30 | + an empty disk will be created. |
| 31 | +
|
| 32 | + Args: |
| 33 | + project_id: project ID or project number of the Cloud project you want to use. |
| 34 | + zone: name of the zone in which you want to create the disk. |
| 35 | + disk_name: name of the disk you want to create. |
| 36 | + disk_type: the type of disk you want to create. This value uses the following format: |
| 37 | + "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". |
| 38 | + For example: "zones/us-west3-b/diskTypes/pd-ssd" |
| 39 | + disk_size_gb: size of the new disk in gigabytes |
| 40 | + kms_key_name: URL of the key from KMS. The key might be from another project, as |
| 41 | + long as you have access to it. The data will be encrypted with the same key |
| 42 | + in the new disk. This value uses following format: |
| 43 | + "projects/{kms_project_id}/locations/{region}/keyRings/{key_ring}/cryptoKeys/{key}" |
| 44 | + disk_link: a link to the disk you want to use as a source for the new disk. |
| 45 | + This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}" |
| 46 | + image_link: a link to the image you want to use as a source for the new disk. |
| 47 | + This value uses the following format: "projects/{project_name}/global/images/{image_name}" |
| 48 | +
|
| 49 | + Returns: |
| 50 | + An attachable disk. |
| 51 | + """ |
| 52 | + disk_client = compute_v1.DisksClient() |
| 53 | + disk = compute_v1.Disk() |
| 54 | + disk.zone = zone |
| 55 | + disk.size_gb = disk_size_gb |
| 56 | + if disk_link: |
| 57 | + disk.source_disk = disk_link |
| 58 | + if image_link: |
| 59 | + disk.source_image = image_link |
| 60 | + disk.type_ = disk_type |
| 61 | + disk.name = disk_name |
| 62 | + disk.disk_encryption_key = compute_v1.CustomerEncryptionKey() |
| 63 | + disk.disk_encryption_key.kms_key_name = kms_key_name |
| 64 | + operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk) |
| 65 | + |
| 66 | + wait_for_extended_operation(operation, "disk creation") |
| 67 | + |
| 68 | + return disk_client.get(project=project_id, zone=zone, disk=disk_name) |
| 69 | + |
| 70 | +# </INGREDIENT> |
0 commit comments