|
| 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 | +# https://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_compute_shared_reservation> |
| 24 | +def create_compute_shared_reservation( |
| 25 | + project_id: str, |
| 26 | + zone: str = "us-central1-a", |
| 27 | + reservation_name="your-reservation-name", |
| 28 | + shared_project_id: str = "shared-project-id", |
| 29 | +) -> compute_v1.Reservation: |
| 30 | + """Creates a compute reservation in GCP. |
| 31 | + Args: |
| 32 | + project_id (str): The ID of the Google Cloud project. |
| 33 | + zone (str): The zone to create the reservation. |
| 34 | + reservation_name (str): The name of the reservation to create. |
| 35 | + shared_project_id (str): The ID of the project that the reservation is shared with. |
| 36 | + Returns: |
| 37 | + Reservation object that represents the new reservation. |
| 38 | + """ |
| 39 | + |
| 40 | + instance_properties = compute_v1.AllocationSpecificSKUAllocationReservedInstanceProperties( |
| 41 | + machine_type="n1-standard-1", |
| 42 | + # Optional. Specifies amount of local ssd to reserve with each instance. |
| 43 | + local_ssds=[ |
| 44 | + compute_v1.AllocationSpecificSKUAllocationAllocatedInstancePropertiesReservedDisk( |
| 45 | + disk_size_gb=375, interface="NVME" |
| 46 | + ), |
| 47 | + ], |
| 48 | + ) |
| 49 | + |
| 50 | + reservation = compute_v1.Reservation( |
| 51 | + name=reservation_name, |
| 52 | + specific_reservation=compute_v1.AllocationSpecificSKUReservation( |
| 53 | + count=3, # Number of resources that are allocated. |
| 54 | + # If you use source_instance_template, you must exclude the instance_properties field. |
| 55 | + # It can be a full or partial URL. |
| 56 | + # source_instance_template="projects/[PROJECT_ID]/global/instanceTemplates/my-instance-template", |
| 57 | + instance_properties=instance_properties, |
| 58 | + ), |
| 59 | + share_settings=compute_v1.ShareSettings( |
| 60 | + share_type="SPECIFIC_PROJECTS", |
| 61 | + project_map={ |
| 62 | + shared_project_id: compute_v1.ShareSettingsProjectConfig( |
| 63 | + project_id=shared_project_id |
| 64 | + ) |
| 65 | + }, |
| 66 | + ), |
| 67 | + ) |
| 68 | + |
| 69 | + # Create a client |
| 70 | + client = compute_v1.ReservationsClient() |
| 71 | + |
| 72 | + operation = client.insert( |
| 73 | + project=project_id, |
| 74 | + zone=zone, |
| 75 | + reservation_resource=reservation, |
| 76 | + ) |
| 77 | + wait_for_extended_operation(operation, "Reservation creation") |
| 78 | + |
| 79 | + reservation = client.get( |
| 80 | + project=project_id, zone=zone, reservation=reservation_name |
| 81 | + ) |
| 82 | + shared_project = next(iter(reservation.share_settings.project_map.values())) |
| 83 | + |
| 84 | + print("Name: ", reservation.name) |
| 85 | + print("STATUS: ", reservation.status) |
| 86 | + print("SHARED PROJECT: ", shared_project) |
| 87 | + # Example response: |
| 88 | + # Name: your-reservation-name |
| 89 | + # STATUS: READY |
| 90 | + # SHARED PROJECT: project_id: "123456789012" |
| 91 | + |
| 92 | + return reservation |
| 93 | + |
| 94 | + |
| 95 | +# </INGREDIENT> |
0 commit comments