|
| 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 | + |
| 16 | +# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets |
| 17 | +# folder for complete code samples that are ready to be used. |
| 18 | +# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. |
| 19 | +# flake8: noqa |
| 20 | +from typing import Optional, Iterable |
| 21 | + |
| 22 | +from google.cloud import compute_v1 |
| 23 | + |
| 24 | + |
| 25 | +# <INGREDIENT create_image_from_snapshot> |
| 26 | +def create_image_from_snapshot(project_id: str, source_snapshot_name: str, image_name: str, |
| 27 | + source_project_id: Optional[str] = None, |
| 28 | + guest_os_features: Optional[Iterable[str]] = None, |
| 29 | + storage_location: Optional[str] = None) -> compute_v1.Image: |
| 30 | + """ |
| 31 | + Creates an image based on a snapshot. |
| 32 | +
|
| 33 | + Args: |
| 34 | + project_id: project ID or project number of the Cloud project you want to place your new image in. |
| 35 | + source_snapshot_name: name of the snapshot you want to use as a base of your image. |
| 36 | + image_name: name of the image you want to create. |
| 37 | + source_project_id: name of the project that hosts the source snapshot. If left unset, it's assumed to equal |
| 38 | + the `project_id`. |
| 39 | + guest_os_features: an iterable collection of guest features you want to enable for the bootable image. |
| 40 | + Learn more about Guest OS features here: |
| 41 | + https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#guest-os-features |
| 42 | + storage_location: the storage location of your image. For example, specify "us" to store the image in the |
| 43 | + `us` multi-region, or "us-central1" to store it in the `us-central1` region. If you do not make a selection, |
| 44 | + Compute Engine stores the image in the multi-region closest to your image's source location. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + An Image object. |
| 48 | + """ |
| 49 | + if source_project_id is None: |
| 50 | + source_project_id = project_id |
| 51 | + |
| 52 | + snapshot_client = compute_v1.SnapshotsClient() |
| 53 | + image_client = compute_v1.ImagesClient() |
| 54 | + src_snapshot = snapshot_client.get(project=source_project_id, snapshot=source_snapshot_name) |
| 55 | + |
| 56 | + image = compute_v1.Image() |
| 57 | + image.name = image_name |
| 58 | + image.source_snapshot = src_snapshot.self_link |
| 59 | + |
| 60 | + if storage_location: |
| 61 | + image.storage_locations = [storage_location] |
| 62 | + |
| 63 | + if guest_os_features: |
| 64 | + image.guest_os_features = [compute_v1.GuestOsFeature(type_=feature) for feature in guest_os_features] |
| 65 | + |
| 66 | + operation = image_client.insert(project=project_id, image_resource=image) |
| 67 | + |
| 68 | + wait_for_extended_operation(operation, "image creation from snapshot") |
| 69 | + |
| 70 | + return image_client.get(project=project_id, image=image_name) |
| 71 | +# </INGREDIENT> |
0 commit comments