40
40
# [START compute_instances_list]
41
41
def list_instances (project_id : str , zone : str ) -> typing .Iterable [compute_v1 .Instance ]:
42
42
"""
43
- Gets a list of instances created in given project in given zone.
44
- Returns an iterable collection of Instance objects.
43
+ List all instances in the given zone in the specified project.
45
44
46
45
Args:
47
- project_id: ID or number of the project you want to use.
48
- zone: Name of the zone you want to check, for example: us-west3-b
49
-
46
+ project_id: project ID or project number of the Cloud project you want to use.
47
+ zone: name of the zone you want to use. For example: “us-west3-b”
50
48
Returns:
51
49
An iterable collection of Instance objects.
52
50
"""
@@ -58,20 +56,18 @@ def list_instances(project_id: str, zone: str) -> typing.Iterable[compute_v1.Ins
58
56
print (f" - { instance .name } ({ instance .machine_type } )" )
59
57
60
58
return instance_list
61
-
62
-
63
59
# [END compute_instances_list]
64
60
61
+
65
62
# [START compute_instances_list_all]
66
63
def list_all_instances (
67
64
project_id : str ,
68
65
) -> typing .Dict [str , typing .Iterable [compute_v1 .Instance ]]:
69
66
"""
70
- Returns a dictionary of all instances present in a project, grouped by their zone.
67
+ Return a dictionary of all instances present in a project, grouped by their zone.
71
68
72
69
Args:
73
- project_id: ID or number of the project you want to use.
74
-
70
+ project_id: project ID or project number of the Cloud project you want to use.
75
71
Returns:
76
72
A dictionary with zone names as keys (in form of "zones/{zone_name}") and
77
73
iterable collections of Instance objects as values.
@@ -87,8 +83,6 @@ def list_all_instances(
87
83
for instance in response .instances :
88
84
print (f" - { instance .name } ({ instance .machine_type } )" )
89
85
return all_instances
90
-
91
-
92
86
# [END compute_instances_list_all]
93
87
94
88
@@ -102,33 +96,28 @@ def create_instance(
102
96
network_name : str = "global/networks/default" ,
103
97
) -> compute_v1 .Instance :
104
98
"""
105
- Sends an instance creation request to GCP and waits for it to complete.
99
+ Send an instance creation request to the Compute Engine API and wait for it to complete.
106
100
107
101
Args:
108
- project_id: ID or number of the project you want to use.
109
- zone: Name of the zone you want to use, for example: us-west3-b
110
- instance_name: Name of the new machine.
111
- machine_type: Machine type you want to create in following format:
112
- "zones/{zone}/machineTypes/{type_name}". For example:
113
- "zones/europe-west3-c/machineTypes/f1-micro"
114
- You can find the list of available machine types using:
115
- https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
116
- source_image: Path the the disk image you want to use for your boot
102
+ project_id: project ID or project number of the Cloud project you want to use.
103
+ zone: name of the zone you want to use. For example: “us-west3-b”
104
+ instance_name: name of the new virtual machine.
105
+ machine_type: machine type of the VM being created. This value uses the
106
+ following format: "zones/{zone}/machineTypes/{type_name}".
107
+ For example: "zones/europe-west3-c/machineTypes/f1-micro"
108
+ source_image: path to the operating system image to mount on your boot
117
109
disk. This can be one of the public images
118
- (e.g. "projects/debian-cloud/global/images/family/debian-10")
110
+ (like "projects/debian-cloud/global/images/family/debian-10")
119
111
or a private image you have access to.
120
- You can check the list of available public images using:
121
- $ gcloud compute images list
122
- network_name: Name of the network you want the new instance to use.
123
- For example: global/networks/default - if you want to use the
124
- default network.
125
-
112
+ network_name: name of the network you want the new instance to use.
113
+ For example: "global/networks/default" represents the `default`
114
+ network interface, which is created automatically for each project.
126
115
Returns:
127
116
Instance object.
128
117
"""
129
118
instance_client = compute_v1 .InstancesClient ()
130
119
131
- # Every machine requires at least one persistent disk
120
+ # Describe the size and source image of the boot disk to attach to the instance.
132
121
disk = compute_v1 .AttachedDisk ()
133
122
initialize_params = compute_v1 .AttachedDiskInitializeParams ()
134
123
initialize_params .source_image = (
@@ -140,25 +129,25 @@ def create_instance(
140
129
disk .boot = True
141
130
disk .type_ = compute_v1 .AttachedDisk .Type .PERSISTENT
142
131
143
- # Every machine needs to be connected to a VPC network.
144
- # The 'default' network is created automatically in every project.
132
+ # Use the network interface provided in the network_name argument.
145
133
network_interface = compute_v1 .NetworkInterface ()
146
134
network_interface .name = network_name
147
135
148
- # Collecting all the information into the Instance object
136
+ # Collect information into the Instance object.
149
137
instance = compute_v1 .Instance ()
150
138
instance .name = instance_name
151
139
instance .disks = [disk ]
152
140
full_machine_type_name = f"zones/{ zone } /machineTypes/{ machine_type } "
153
141
instance .machine_type = full_machine_type_name
154
142
instance .network_interfaces = [network_interface ]
155
143
156
- # Preparing the InsertInstanceRequest
144
+ # Prepare the request to insert an instance.
157
145
request = compute_v1 .InsertInstanceRequest ()
158
146
request .zone = zone
159
147
request .project = project_id
160
148
request .instance_resource = instance
161
149
150
+ # Wait for the create operation to complete.
162
151
print (f"Creating the { instance_name } instance in { zone } ..." )
163
152
operation = instance_client .insert (request = request )
164
153
if operation .status == compute_v1 .Operation .Status .RUNNING :
@@ -172,20 +161,18 @@ def create_instance(
172
161
print ("Warning during creation:" , operation .warnings , file = sys .stderr )
173
162
print (f"Instance { instance_name } created." )
174
163
return instance
175
-
176
-
177
164
# [END compute_instances_create]
178
165
179
166
180
167
# [START compute_instances_delete]
181
168
def delete_instance (project_id : str , zone : str , machine_name : str ) -> None :
182
169
"""
183
- Sends a delete request to GCP and waits for it to complete.
170
+ Send an instance deletion request to the Compute Engine API and wait for it to complete.
184
171
185
172
Args:
186
- project_id: ID or number of the project you want to use.
187
- zone: Name of the zone you want to use, for example: us-west3-b
188
- machine_name: Name of the machine you want to delete.
173
+ project_id: project ID or project number of the Cloud project you want to use.
174
+ zone: name of the zone you want to use. For example: “ us-west3-b”
175
+ machine_name: name of the machine you want to delete.
189
176
"""
190
177
instance_client = compute_v1 .InstancesClient ()
191
178
@@ -204,8 +191,6 @@ def delete_instance(project_id: str, zone: str, machine_name: str) -> None:
204
191
print ("Warning during deletion:" , operation .warnings , file = sys .stderr )
205
192
print (f"Instance { machine_name } deleted." )
206
193
return
207
-
208
-
209
194
# [END compute_instances_delete]
210
195
211
196
@@ -220,7 +205,7 @@ def wait_for_operation(
220
205
Args:
221
206
operation: The Operation object representing the operation you want to
222
207
wait on.
223
- project_id: ID or number of the project owning the operation .
208
+ project_id: project ID or project number of the Cloud project you want to use .
224
209
225
210
Returns:
226
211
Finished Operation object.
@@ -232,13 +217,11 @@ def wait_for_operation(
232
217
kwargs ["zone" ] = operation .zone .rsplit ("/" , maxsplit = 1 )[1 ]
233
218
elif operation .region :
234
219
client = compute_v1 .RegionOperationsClient ()
235
- # Operation.region is a full URL address of a zone , so we need to extract just the name
220
+ # Operation.region is a full URL address of a region , so we need to extract just the name
236
221
kwargs ["region" ] = operation .region .rsplit ("/" , maxsplit = 1 )[1 ]
237
222
else :
238
223
client = compute_v1 .GlobalOperationsClient ()
239
224
return client .wait (** kwargs )
240
-
241
-
242
225
# [END compute_instances_operation_check]
243
226
244
227
0 commit comments