This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
sample_templates.py
201 lines (165 loc) · 8.11 KB
/
sample_templates.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright 2021 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.
from typing import Iterable
from google.cloud import compute_v1
def get_instance_template(project_id: str, template_name: str) -> compute_v1.InstanceTemplate:
"""
Retrieve an instance template, which you can use to create virtual machine
(VM) instances and managed instance groups (MIGs).
Args:
project_id: project ID or project number of the Cloud project you use.
template_name: name of the template to retrieve.
Returns:
InstanceTemplate object that represents the retrieved template.
"""
template_client = compute_v1.InstanceTemplatesClient()
return template_client.get(project=project_id, instance_template=template_name)
def list_instance_templates(project_id: str) -> Iterable[compute_v1.InstanceTemplate]:
"""
Get a list of InstanceTemplate objects available in a project.
Args:
project_id: project ID or project number of the Cloud project you use.
Returns:
Iterable list of InstanceTemplate objects.
"""
template_client = compute_v1.InstanceTemplatesClient()
return template_client.list(project=project_id)
def create_template(project_id: str, template_name: str) -> compute_v1.InstanceTemplate:
"""
Create a new instance template with the provided name and a specific
instance configuration.
Args:
project_id: project ID or project number of the Cloud project you use.
template_name: name of the new template to create.
Returns:
InstanceTemplate object that represents the new instance template.
"""
# The template describes the size and source image of the boot disk
# to attach to the instance.
disk = compute_v1.AttachedDisk()
initialize_params = compute_v1.AttachedDiskInitializeParams()
initialize_params.source_image = (
"projects/debian-cloud/global/images/family/debian-11"
)
initialize_params.disk_size_gb = 250
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
# The template connects the instance to the `default` network,
# without specifying a subnetwork.
network_interface = compute_v1.NetworkInterface()
network_interface.name = "global/networks/default"
# The template lets the instance use an external IP address.
network_interface.access_configs = [compute_v1.AccessConfig()]
network_interface.access_configs[0].name = "External NAT"
network_interface.access_configs[0].type_ = compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT
network_interface.access_configs[0].network_tier = (
compute_v1.AccessConfig.NetworkTier.PREMIUM
)
template = compute_v1.InstanceTemplate()
template.name = template_name
template.properties.disks = [disk]
template.properties.machine_type = "e2-standard-4"
template.properties.network_interfaces = [network_interface]
template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert(project=project_id, instance_template_resource=template)
operation_client.wait(project=project_id, operation=op.name)
return template_client.get(project=project_id, instance_template=template_name)
def create_template_from_instance(project_id: str, instance: str, template_name: str) -> compute_v1.InstanceTemplate:
"""
Create a new instance template based on an existing instance.
This new template specifies a different boot disk.
Args:
project_id: project ID or project number of the Cloud project you use.
instance: the instance to base the new template on. This value uses
the following format: "projects/{project}/zones/{zone}/instances/{instance_name}"
template_name: name of the new template to create.
Returns:
InstanceTemplate object that represents the new instance template.
"""
disk = compute_v1.DiskInstantiationConfig()
# Device name must match the name of a disk attached to the instance you are
# basing your template on.
disk.device_name = "disk-1"
# Replace the original boot disk image used in your instance with a Rocky Linux image.
disk.instantiate_from = (
compute_v1.DiskInstantiationConfig.InstantiateFrom.CUSTOM_IMAGE
)
disk.custom_image = "projects/rocky-linux-cloud/global/images/family/rocky-linux-8"
# Override the auto_delete setting.
disk.auto_delete = True
template = compute_v1.InstanceTemplate()
template.name = template_name
template.source_instance = instance
template.source_instance_params = compute_v1.SourceInstanceParams()
template.source_instance_params.disk_configs = [disk]
template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert(project=project_id, instance_template_resource=template)
operation_client.wait(project=project_id, operation=op.name)
return template_client.get(project=project_id, instance_template=template_name)
def create_template_with_subnet(
project_id: str, network: str, subnetwork: str, template_name: str
) -> compute_v1.InstanceTemplate:
"""
Create an instance template that uses a provided subnet.
Args:
project_id: project ID or project number of the Cloud project you use.
network: the network to be used in the new template. This value uses
the following format: "projects/{project}/global/networks/{network}"
subnetwork: the subnetwork to be used in the new template. This value
uses the following format: "projects/{project}/regions/{region}/subnetworks/{subnetwork}"
template_name: name of the new template to create.
Returns:
InstanceTemplate object that represents the new instance template.
"""
# The template describes the size and source image of the book disk to
# attach to the instance.
disk = compute_v1.AttachedDisk()
initialize_params = compute_v1.AttachedDiskInitializeParams()
initialize_params.source_image = (
"projects/debian-cloud/global/images/family/debian-11"
)
initialize_params.disk_size_gb = 250
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
template = compute_v1.InstanceTemplate()
template.name = template_name
template.properties = compute_v1.InstanceProperties()
template.properties.disks = [disk]
template.properties.machine_type = "e2-standard-4"
# The template connects the instance to the specified network and subnetwork.
network_interface = compute_v1.NetworkInterface()
network_interface.network = network
network_interface.subnetwork = subnetwork
template.properties.network_interfaces = [network_interface]
template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert(project=project_id, instance_template_resource=template)
operation_client.wait(project=project_id, operation=op.name)
return template_client.get(project=project_id, instance_template=template_name)
def delete_instance_template(project_id: str, template_name: str):
"""
Delete an instance template.
Args:
project_id: project ID or project number of the Cloud project you use.
template_name: name of the template to delete.
"""
template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.delete(project=project_id, instance_template=template_name)
operation_client.wait(project=project_id, operation=op.name)
return