Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 2d53eb1

Browse files
authored
docs: samples docstring and comments update (#69)
* chore: Updating docstrings to match samples from other languages. * feat: Fixing comments and docstrings to be in sync with other samples. * docs: Updating sample docstrings and comments Applying some changes after consulting with tech writer.
1 parent 7e8bc73 commit 2d53eb1

File tree

1 file changed

+29
-46
lines changed

1 file changed

+29
-46
lines changed

samples/snippets/quickstart.py

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@
4040
# [START compute_instances_list]
4141
def list_instances(project_id: str, zone: str) -> typing.Iterable[compute_v1.Instance]:
4242
"""
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.
4544
4645
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”
5048
Returns:
5149
An iterable collection of Instance objects.
5250
"""
@@ -58,20 +56,18 @@ def list_instances(project_id: str, zone: str) -> typing.Iterable[compute_v1.Ins
5856
print(f" - {instance.name} ({instance.machine_type})")
5957

6058
return instance_list
61-
62-
6359
# [END compute_instances_list]
6460

61+
6562
# [START compute_instances_list_all]
6663
def list_all_instances(
6764
project_id: str,
6865
) -> typing.Dict[str, typing.Iterable[compute_v1.Instance]]:
6966
"""
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.
7168
7269
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.
7571
Returns:
7672
A dictionary with zone names as keys (in form of "zones/{zone_name}") and
7773
iterable collections of Instance objects as values.
@@ -87,8 +83,6 @@ def list_all_instances(
8783
for instance in response.instances:
8884
print(f" - {instance.name} ({instance.machine_type})")
8985
return all_instances
90-
91-
9286
# [END compute_instances_list_all]
9387

9488

@@ -102,33 +96,28 @@ def create_instance(
10296
network_name: str = "global/networks/default",
10397
) -> compute_v1.Instance:
10498
"""
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.
106100
107101
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
117109
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")
119111
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.
126115
Returns:
127116
Instance object.
128117
"""
129118
instance_client = compute_v1.InstancesClient()
130119

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.
132121
disk = compute_v1.AttachedDisk()
133122
initialize_params = compute_v1.AttachedDiskInitializeParams()
134123
initialize_params.source_image = (
@@ -140,25 +129,25 @@ def create_instance(
140129
disk.boot = True
141130
disk.type_ = compute_v1.AttachedDisk.Type.PERSISTENT
142131

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.
145133
network_interface = compute_v1.NetworkInterface()
146134
network_interface.name = network_name
147135

148-
# Collecting all the information into the Instance object
136+
# Collect information into the Instance object.
149137
instance = compute_v1.Instance()
150138
instance.name = instance_name
151139
instance.disks = [disk]
152140
full_machine_type_name = f"zones/{zone}/machineTypes/{machine_type}"
153141
instance.machine_type = full_machine_type_name
154142
instance.network_interfaces = [network_interface]
155143

156-
# Preparing the InsertInstanceRequest
144+
# Prepare the request to insert an instance.
157145
request = compute_v1.InsertInstanceRequest()
158146
request.zone = zone
159147
request.project = project_id
160148
request.instance_resource = instance
161149

150+
# Wait for the create operation to complete.
162151
print(f"Creating the {instance_name} instance in {zone}...")
163152
operation = instance_client.insert(request=request)
164153
if operation.status == compute_v1.Operation.Status.RUNNING:
@@ -172,20 +161,18 @@ def create_instance(
172161
print("Warning during creation:", operation.warnings, file=sys.stderr)
173162
print(f"Instance {instance_name} created.")
174163
return instance
175-
176-
177164
# [END compute_instances_create]
178165

179166

180167
# [START compute_instances_delete]
181168
def delete_instance(project_id: str, zone: str, machine_name: str) -> None:
182169
"""
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.
184171
185172
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.
189176
"""
190177
instance_client = compute_v1.InstancesClient()
191178

@@ -204,8 +191,6 @@ def delete_instance(project_id: str, zone: str, machine_name: str) -> None:
204191
print("Warning during deletion:", operation.warnings, file=sys.stderr)
205192
print(f"Instance {machine_name} deleted.")
206193
return
207-
208-
209194
# [END compute_instances_delete]
210195

211196

@@ -220,7 +205,7 @@ def wait_for_operation(
220205
Args:
221206
operation: The Operation object representing the operation you want to
222207
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.
224209
225210
Returns:
226211
Finished Operation object.
@@ -232,13 +217,11 @@ def wait_for_operation(
232217
kwargs["zone"] = operation.zone.rsplit("/", maxsplit=1)[1]
233218
elif operation.region:
234219
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
236221
kwargs["region"] = operation.region.rsplit("/", maxsplit=1)[1]
237222
else:
238223
client = compute_v1.GlobalOperationsClient()
239224
return client.wait(**kwargs)
240-
241-
242225
# [END compute_instances_operation_check]
243226

244227

0 commit comments

Comments
 (0)