-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathcreate_from_image.py
More file actions
59 lines (50 loc) · 2.45 KB
/
Copy pathcreate_from_image.py
File metadata and controls
59 lines (50 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa
import sys
from google.cloud import compute_v1
# <INGREDIENT create_disk_from_image>
def create_disk_from_image(
project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int, source_image: str
) -> compute_v1.Disk:
"""
Creates a new disk in a project in given zone using an image as base.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which you want to create the disk.
disk_name: name of the disk you want to create.
disk_type: the type of disk you want to create. This value uses the following format:
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
For example: "zones/us-west3-b/diskTypes/pd-ssd"
disk_size_gb: size of the new disk in gigabytes
source_image: source image to use when creating this disk. You must have read access to this disk. This
can be one of the publicly available images or an image from one of your projects.
This value uses the following format: "projects/{project_name}/global/images/{image_name}"
Returns:
An unattached Disk instance.
"""
disk = compute_v1.Disk()
disk.size_gb = disk_size_gb
disk.name = disk_name
disk.zone = zone
disk.type_ = disk_type
disk.source_image = source_image
disk_client = compute_v1.DisksClient()
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
wait_for_extended_operation(operation, "disk creation")
return disk_client.get(project=project_id, zone=zone, disk=disk.name)
# </INGREDIENT>