|
| 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 | + |
| 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 | + |
| 20 | +from google.cloud import compute_v1 |
| 21 | + |
| 22 | + |
| 23 | +# <INGREDIENT create_replicated_disk> |
| 24 | +def create_regional_replicated_disk( |
| 25 | + project_id, |
| 26 | + region, |
| 27 | + disk_name, |
| 28 | + size_gb, |
| 29 | + disk_type: str = "pd-ssd", |
| 30 | +) -> compute_v1.Disk: |
| 31 | + """Creates a synchronously replicated disk in a region across two zones. |
| 32 | + Args: |
| 33 | + project_id (str): The ID of the Google Cloud project. |
| 34 | + region (str): The region where the disk will be created. |
| 35 | + disk_name (str): The name of the disk. |
| 36 | + size_gb (int): The size of the disk in gigabytes. |
| 37 | + disk_type (str): The type of the disk. Default is 'pd-ssd'. |
| 38 | + Returns: |
| 39 | + compute_v1.Disk: The created disk object. |
| 40 | + """ |
| 41 | + disk = compute_v1.Disk() |
| 42 | + disk.name = disk_name |
| 43 | + |
| 44 | + # You can specify the zones where the disk will be replicated. |
| 45 | + disk.replica_zones = [ |
| 46 | + f"zones/{region}-a", |
| 47 | + f"zones/{region}-b", |
| 48 | + ] |
| 49 | + disk.size_gb = size_gb |
| 50 | + disk.type = f"regions/{region}/diskTypes/{disk_type}" |
| 51 | + |
| 52 | + client = compute_v1.RegionDisksClient() |
| 53 | + operation = client.insert(project=project_id, region=region, disk_resource=disk) |
| 54 | + |
| 55 | + wait_for_extended_operation(operation, "Replicated disk creation") |
| 56 | + |
| 57 | + return client.get(project=project_id, region=region, disk=disk_name) |
| 58 | + |
| 59 | + |
| 60 | +# </INGREDIENT> |
0 commit comments