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
/
quickstart.py
258 lines (220 loc) · 9.74 KB
/
quickstart.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
# 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.
"""
A sample script showing how to create, list and delete Google Compute Engine
instances using the google-cloud-compute library. It can be run from command
line to create, list and delete an instance in a given project in a given zone.
"""
# [START compute_instances_create]
# [START compute_instances_delete]
import sys
# [START compute_instances_list]
# [START compute_instances_list_all]
# [START compute_instances_operation_check]
import typing
import google.cloud.compute_v1 as compute_v1
# [END compute_instances_operation_check]
# [END compute_instances_list_all]
# [END compute_instances_list]
# [END compute_instances_delete]
# [END compute_instances_create]
# [START compute_instances_list]
def list_instances(project_id: str, zone: str) -> typing.Iterable[compute_v1.Instance]:
"""
List all instances in the given zone in the specified project.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use. For example: “us-west3-b”
Returns:
An iterable collection of Instance objects.
"""
instance_client = compute_v1.InstancesClient()
instance_list = instance_client.list(project=project_id, zone=zone)
print(f"Instances found in zone {zone}:")
for instance in instance_list:
print(f" - {instance.name} ({instance.machine_type})")
return instance_list
# [END compute_instances_list]
# [START compute_instances_list_all]
def list_all_instances(
project_id: str,
) -> typing.Dict[str, typing.Iterable[compute_v1.Instance]]:
"""
Return a dictionary of all instances present in a project, grouped by their zone.
Args:
project_id: project ID or project number of the Cloud project you want to use.
Returns:
A dictionary with zone names as keys (in form of "zones/{zone_name}") and
iterable collections of Instance objects as values.
"""
instance_client = compute_v1.InstancesClient()
agg_list = instance_client.aggregated_list(project=project_id)
all_instances = {}
print("Instances found:")
for zone, response in agg_list:
if response.instances:
all_instances[zone] = response.instances
print(f" {zone}:")
for instance in response.instances:
print(f" - {instance.name} ({instance.machine_type})")
return all_instances
# [END compute_instances_list_all]
# [START compute_instances_create]
def create_instance(
project_id: str,
zone: str,
instance_name: str,
machine_type: str = "n1-standard-1",
source_image: str = "projects/debian-cloud/global/images/family/debian-10",
network_name: str = "global/networks/default",
) -> compute_v1.Instance:
"""
Send an instance creation request to the Compute Engine API and wait for it to complete.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use. For example: “us-west3-b”
instance_name: name of the new virtual machine.
machine_type: machine type of the VM being created. This value uses the
following format: "zones/{zone}/machineTypes/{type_name}".
For example: "zones/europe-west3-c/machineTypes/f1-micro"
source_image: path to the operating system image to mount on your boot
disk. This can be one of the public images
(like "projects/debian-cloud/global/images/family/debian-10")
or a private image you have access to.
network_name: name of the network you want the new instance to use.
For example: "global/networks/default" represents the `default`
network interface, which is created automatically for each project.
Returns:
Instance object.
"""
instance_client = compute_v1.InstancesClient()
# Describe 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 = (
source_image # "projects/debian-cloud/global/images/family/debian-10"
)
initialize_params.disk_size_gb = "10"
disk.initialize_params = initialize_params
disk.auto_delete = True
disk.boot = True
disk.type_ = compute_v1.AttachedDisk.Type.PERSISTENT
# Use the network interface provided in the network_name argument.
network_interface = compute_v1.NetworkInterface()
network_interface.name = network_name
# Collect information into the Instance object.
instance = compute_v1.Instance()
instance.name = instance_name
instance.disks = [disk]
full_machine_type_name = f"zones/{zone}/machineTypes/{machine_type}"
instance.machine_type = full_machine_type_name
instance.network_interfaces = [network_interface]
# Prepare the request to insert an instance.
request = compute_v1.InsertInstanceRequest()
request.zone = zone
request.project = project_id
request.instance_resource = instance
# Wait for the create operation to complete.
print(f"Creating the {instance_name} instance in {zone}...")
operation = instance_client.insert(request=request)
if operation.status == compute_v1.Operation.Status.RUNNING:
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(
operation=operation.name, zone=zone, project=project_id
)
if operation.error:
print("Error during creation:", operation.error, file=sys.stderr)
if operation.warnings:
print("Warning during creation:", operation.warnings, file=sys.stderr)
print(f"Instance {instance_name} created.")
return instance
# [END compute_instances_create]
# [START compute_instances_delete]
def delete_instance(project_id: str, zone: str, machine_name: str) -> None:
"""
Send an instance deletion request to the Compute Engine API and wait for it to complete.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use. For example: “us-west3-b”
machine_name: name of the machine you want to delete.
"""
instance_client = compute_v1.InstancesClient()
print(f"Deleting {machine_name} from {zone}...")
operation = instance_client.delete(
project=project_id, zone=zone, instance=machine_name
)
if operation.status == compute_v1.Operation.Status.RUNNING:
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(
operation=operation.name, zone=zone, project=project_id
)
if operation.error:
print("Error during deletion:", operation.error, file=sys.stderr)
if operation.warnings:
print("Warning during deletion:", operation.warnings, file=sys.stderr)
print(f"Instance {machine_name} deleted.")
return
# [END compute_instances_delete]
# [START compute_instances_operation_check]
def wait_for_operation(
operation: compute_v1.Operation, project_id: str
) -> compute_v1.Operation:
"""
This method waits for an operation to be completed. Calling this function
will block until the operation is finished.
Args:
operation: The Operation object representing the operation you want to
wait on.
project_id: project ID or project number of the Cloud project you want to use.
Returns:
Finished Operation object.
"""
kwargs = {"project": project_id, "operation": operation.name}
if operation.zone:
client = compute_v1.ZoneOperationsClient()
# Operation.zone is a full URL address of a zone, so we need to extract just the name
kwargs["zone"] = operation.zone.rsplit("/", maxsplit=1)[1]
elif operation.region:
client = compute_v1.RegionOperationsClient()
# Operation.region is a full URL address of a region, so we need to extract just the name
kwargs["region"] = operation.region.rsplit("/", maxsplit=1)[1]
else:
client = compute_v1.GlobalOperationsClient()
return client.wait(**kwargs)
# [END compute_instances_operation_check]
def main(project_id: str, zone: str, instance_name: str) -> None:
create_instance(project_id, zone, instance_name)
zone_instances = list_instances(project_id, zone)
print(f"Instances found in {zone}:", ", ".join(i.name for i in zone_instances))
all_instances = list_all_instances(project_id)
print(f"Instances found in project {project_id}:")
for i_zone, instances in all_instances.items():
print(f"{i_zone}:", ", ".join(i.name for i in instances))
delete_instance(project_id, zone, instance_name)
if __name__ == "__main__":
import uuid
import google.auth
import google.auth.exceptions
try:
default_project_id = google.auth.default()[1]
except google.auth.exceptions.DefaultCredentialsError:
print(
"Please use `gcloud auth application-default login` "
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
)
else:
instance_name = "quickstart-" + uuid.uuid4().hex[:10]
instance_zone = "europe-central2-b"
main(default_project_id, instance_zone, instance_name)