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

Commit 767d7c8

Browse files
docs(samples): Migrate samples to use new type of operations (#264)
Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 005f9eb commit 767d7c8

File tree

114 files changed

+2402
-652
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+2402
-652
lines changed

samples/ingredients/disks/autodelete_change.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,8 @@ def set_disk_autodelete(project_id: str, zone: str, instance_name: str, disk_nam
4646

4747
disk.auto_delete = autodelete
4848

49-
operation = instance_client.update_unary(project=project_id, zone=zone, instance=instance_name, instance_resource=instance)
50-
operation_client = compute_v1.ZoneOperationsClient()
51-
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
52-
53-
if operation.error:
54-
print("Error during instance update:", operation.error, file=sys.stderr)
55-
raise RuntimeError(operation.error)
56-
if operation.warnings:
57-
print("Warnings during instance update:\n", file=sys.stderr)
58-
for warning in operation.warnings:
59-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
49+
operation = instance_client.update(project=project_id, zone=zone, instance=instance_name, instance_resource=instance)
50+
51+
wait_for_extended_operation(operation, "disk update")
6052
return
6153
# </INGREDIENT>

samples/ingredients/disks/create_empty_disk.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,9 @@ def create_empty_disk(
4747
disk.type_ = disk_type
4848

4949
disk_client = compute_v1.DisksClient()
50-
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
51-
operation_client = compute_v1.ZoneOperationsClient()
52-
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
53-
54-
if operation.error:
55-
print("Error during disk creation:", operation.error, file=sys.stderr)
56-
raise RuntimeError(operation.error)
57-
if operation.warnings:
58-
print("Warnings during disk creation:\n", file=sys.stderr)
59-
for warning in operation.warnings:
60-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
50+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
51+
52+
wait_for_extended_operation(operation, "disk creation")
6153

6254
return disk_client.get(project=project_id, zone=zone, disk=disk.name)
6355
# </INGREDIENT>

samples/ingredients/disks/create_from_image.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,9 @@ def create_disk_from_image(
5151
disk.source_image = source_image
5252

5353
disk_client = compute_v1.DisksClient()
54-
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
55-
operation_client = compute_v1.ZoneOperationsClient()
56-
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
54+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
5755

58-
if operation.error:
59-
print("Error during disk creation:", operation.error, file=sys.stderr)
60-
raise RuntimeError(operation.error)
61-
if operation.warnings:
62-
print("Warnings during disk creation:\n", file=sys.stderr)
63-
for warning in operation.warnings:
64-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
56+
wait_for_extended_operation(operation, "disk creation")
6557

6658
return disk_client.get(project=project_id, zone=zone, disk=disk.name)
6759
# </INGREDIENT>

samples/ingredients/disks/create_from_snapshot.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,9 @@ def create_disk_from_snapshot(project_id: str, zone: str, disk_name: str, disk_t
4848
disk.source_snapshot = snapshot_link
4949
disk.type_ = disk_type
5050
disk.name = disk_name
51-
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
52-
operation_client = compute_v1.ZoneOperationsClient()
53-
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
51+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
5452

55-
if operation.error:
56-
print("Error during disk creation:", operation.error, file=sys.stderr)
57-
raise RuntimeError(operation.error)
58-
59-
if operation.warnings:
60-
print("Warnings during disk creation:\n", file=sys.stderr)
61-
for warning in operation.warnings:
62-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
53+
wait_for_extended_operation(operation, "disk creation")
6354

6455
return disk_client.get(project=project_id, zone=zone, disk=disk_name)
6556
# </INGREDIENT>

samples/ingredients/disks/delete.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,7 @@ def delete_disk(project_id: str, zone: str, disk_name: str) -> NoReturn:
3333
disk_name: name of the disk you want to delete.
3434
"""
3535
disk_client = compute_v1.DisksClient()
36-
operation = disk_client.delete_unary(project=project_id, zone=zone, disk=disk_name)
37-
operation_client = compute_v1.ZoneOperationsClient()
38-
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
39-
40-
if operation.error:
41-
print("Error during disk delete operation:", operation.error, file=sys.stderr)
42-
raise RuntimeError(operation.error)
43-
if operation.warnings:
44-
print("Warnings during disk delete operation:\n", file=sys.stderr)
45-
for warning in operation.warnings:
46-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
36+
operation = disk_client.delete(project=project_id, zone=zone, disk=disk_name)
37+
wait_for_extended_operation(operation, "disk deletion")
4738
return
4839
# </INGREDIENT>

samples/ingredients/firewall/create.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# folder for complete code samples that are ready to be used.
1717
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1818
# flake8: noqa
19+
1920
from google.cloud import compute_v1
2021

2122

@@ -61,12 +62,11 @@ def create_firewall_rule(
6162
# firewall_rule.priority = 0
6263

6364
firewall_client = compute_v1.FirewallsClient()
64-
op = firewall_client.insert_unary(
65+
operation = firewall_client.insert(
6566
project=project_id, firewall_resource=firewall_rule
6667
)
6768

68-
op_client = compute_v1.GlobalOperationsClient()
69-
op_client.wait(project=project_id, operation=op.name)
69+
wait_for_extended_operation(operation, "firewall rule creation")
7070

7171
return firewall_client.get(project=project_id, firewall=firewall_rule_name)
7272
# </INGREDIENT>

samples/ingredients/firewall/delete.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# folder for complete code samples that are ready to be used.
1717
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1818
# flake8: noqa
19+
1920
from google.cloud import compute_v1
2021

2122

@@ -29,11 +30,10 @@ def delete_firewall_rule(project_id: str, firewall_rule_name: str) -> None:
2930
firewall_rule_name: name of the firewall rule you want to delete.
3031
"""
3132
firewall_client = compute_v1.FirewallsClient()
32-
operation = firewall_client.delete_unary(
33+
operation = firewall_client.delete(
3334
project=project_id, firewall=firewall_rule_name
3435
)
3536

36-
operation_client = compute_v1.GlobalOperationsClient()
37-
operation_client.wait(project=project_id, operation=operation.name)
37+
wait_for_extended_operation(operation, "firewall rule deletion")
3838
return
3939
# </INGREDIENT>

samples/ingredients/firewall/patch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# folder for complete code samples that are ready to be used.
1717
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1818
# flake8: noqa
19+
1920
from google.cloud import compute_v1
2021

2122

@@ -35,12 +36,11 @@ def patch_firewall_priority(project_id: str, firewall_rule_name: str, priority:
3536
# The patch operation doesn't require the full definition of a Firewall object. It will only update
3637
# the values that were set in it, in this case it will only change the priority.
3738
firewall_client = compute_v1.FirewallsClient()
38-
operation = firewall_client.patch_unary(
39+
operation = firewall_client.patch(
3940
project=project_id, firewall=firewall_rule_name, firewall_resource=firewall_rule
4041
)
4142

42-
operation_client = compute_v1.GlobalOperationsClient()
43-
operation_client.wait(project=project_id, operation=operation.name)
43+
wait_for_extended_operation(operation, "firewall rule patching")
4444
return
4545
# </INGREDIENT>
4646

samples/ingredients/instance-templates/create.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# folder for complete code samples that are ready to be used.
1717
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1818
# flake8: noqa
19+
import sys
1920

2021
from google.cloud import compute_v1
2122

@@ -64,11 +65,11 @@ def create_template(project_id: str, template_name: str) -> compute_v1.InstanceT
6465
template.properties.network_interfaces = [network_interface]
6566

6667
template_client = compute_v1.InstanceTemplatesClient()
67-
operation_client = compute_v1.GlobalOperationsClient()
68-
op = template_client.insert_unary(
68+
operation = template_client.insert(
6969
project=project_id, instance_template_resource=template
7070
)
71-
operation_client.wait(project=project_id, operation=op.name)
71+
72+
wait_for_extended_operation(operation, "instance template creation")
7273

7374
return template_client.get(project=project_id, instance_template=template_name)
7475
# </INGREDIENT>

samples/ingredients/instance-templates/create_from_instance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def create_template_from_instance(
5454
template.source_instance_params.disk_configs = [disk]
5555

5656
template_client = compute_v1.InstanceTemplatesClient()
57-
operation_client = compute_v1.GlobalOperationsClient()
58-
op = template_client.insert_unary(
57+
operation = template_client.insert(
5958
project=project_id, instance_template_resource=template
6059
)
61-
operation_client.wait(project=project_id, operation=op.name)
60+
61+
wait_for_extended_operation(operation, "instance template creation")
6262

6363
return template_client.get(project=project_id, instance_template=template_name)
6464
# </INGREDIENT>

0 commit comments

Comments
 (0)