Skip to content

Commit

Permalink
docs(samples): Migrate samples to use new type of operations (#264)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
m-strzelczyk and parthea authored Apr 21, 2022
1 parent d736a77 commit 9e8c6e4
Show file tree
Hide file tree
Showing 114 changed files with 2,402 additions and 652 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,8 @@ def set_disk_autodelete(project_id: str, zone: str, instance_name: str, disk_nam

disk.auto_delete = autodelete

operation = instance_client.update_unary(project=project_id, zone=zone, instance=instance_name, instance_resource=instance)
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)

if operation.error:
print("Error during instance update:", operation.error, file=sys.stderr)
raise RuntimeError(operation.error)
if operation.warnings:
print("Warnings during instance update:\n", file=sys.stderr)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
operation = instance_client.update(project=project_id, zone=zone, instance=instance_name, instance_resource=instance)

wait_for_extended_operation(operation, "disk update")
return
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,9 @@ def create_empty_disk(
disk.type_ = disk_type

disk_client = compute_v1.DisksClient()
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)

if operation.error:
print("Error during disk creation:", operation.error, file=sys.stderr)
raise RuntimeError(operation.error)
if operation.warnings:
print("Warnings during disk creation:\n", file=sys.stderr)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk.name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,9 @@ def create_disk_from_image(
disk.source_image = source_image

disk_client = compute_v1.DisksClient()
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

if operation.error:
print("Error during disk creation:", operation.error, file=sys.stderr)
raise RuntimeError(operation.error)
if operation.warnings:
print("Warnings during disk creation:\n", file=sys.stderr)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk.name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,9 @@ def create_disk_from_snapshot(project_id: str, zone: str, disk_name: str, disk_t
disk.source_snapshot = snapshot_link
disk.type_ = disk_type
disk.name = disk_name
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)

if operation.error:
print("Error during disk creation:", operation.error, file=sys.stderr)
raise RuntimeError(operation.error)

if operation.warnings:
print("Warnings during disk creation:\n", file=sys.stderr)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
wait_for_extended_operation(operation, "disk creation")

return disk_client.get(project=project_id, zone=zone, disk=disk_name)
# </INGREDIENT>
13 changes: 2 additions & 11 deletions packages/google-cloud-compute/samples/ingredients/disks/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,7 @@ def delete_disk(project_id: str, zone: str, disk_name: str) -> NoReturn:
disk_name: name of the disk you want to delete.
"""
disk_client = compute_v1.DisksClient()
operation = disk_client.delete_unary(project=project_id, zone=zone, disk=disk_name)
operation_client = compute_v1.ZoneOperationsClient()
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)

if operation.error:
print("Error during disk delete operation:", operation.error, file=sys.stderr)
raise RuntimeError(operation.error)
if operation.warnings:
print("Warnings during disk delete operation:\n", file=sys.stderr)
for warning in operation.warnings:
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
operation = disk_client.delete(project=project_id, zone=zone, disk=disk_name)
wait_for_extended_operation(operation, "disk deletion")
return
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa

from google.cloud import compute_v1


Expand Down Expand Up @@ -61,12 +62,11 @@ def create_firewall_rule(
# firewall_rule.priority = 0

firewall_client = compute_v1.FirewallsClient()
op = firewall_client.insert_unary(
operation = firewall_client.insert(
project=project_id, firewall_resource=firewall_rule
)

op_client = compute_v1.GlobalOperationsClient()
op_client.wait(project=project_id, operation=op.name)
wait_for_extended_operation(operation, "firewall rule creation")

return firewall_client.get(project=project_id, firewall=firewall_rule_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa

from google.cloud import compute_v1


Expand All @@ -29,11 +30,10 @@ def delete_firewall_rule(project_id: str, firewall_rule_name: str) -> None:
firewall_rule_name: name of the firewall rule you want to delete.
"""
firewall_client = compute_v1.FirewallsClient()
operation = firewall_client.delete_unary(
operation = firewall_client.delete(
project=project_id, firewall=firewall_rule_name
)

operation_client = compute_v1.GlobalOperationsClient()
operation_client.wait(project=project_id, operation=operation.name)
wait_for_extended_operation(operation, "firewall rule deletion")
return
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa

from google.cloud import compute_v1


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

operation_client = compute_v1.GlobalOperationsClient()
operation_client.wait(project=project_id, operation=operation.name)
wait_for_extended_operation(operation, "firewall rule patching")
return
# </INGREDIENT>

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa
import sys

from google.cloud import compute_v1

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

template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert_unary(
operation = template_client.insert(
project=project_id, instance_template_resource=template
)
operation_client.wait(project=project_id, operation=op.name)

wait_for_extended_operation(operation, "instance template creation")

return template_client.get(project=project_id, instance_template=template_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def create_template_from_instance(
template.source_instance_params.disk_configs = [disk]

template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert_unary(
operation = template_client.insert(
project=project_id, instance_template_resource=template
)
operation_client.wait(project=project_id, operation=op.name)

wait_for_extended_operation(operation, "instance template creation")

return template_client.get(project=project_id, instance_template=template_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ def create_template_with_subnet(
template.properties.network_interfaces = [network_interface]

template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.insert_unary(
operation = template_client.insert(
project=project_id, instance_template_resource=template
)
operation_client.wait(project=project_id, operation=op.name)
wait_for_extended_operation(operation, "instance template creation")

return template_client.get(project=project_id, instance_template=template_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ def delete_instance_template(project_id: str, template_name: str):
template_name: name of the template to delete.
"""
template_client = compute_v1.InstanceTemplatesClient()
operation_client = compute_v1.GlobalOperationsClient()
op = template_client.delete_unary(
operation = template_client.delete(
project=project_id, instance_template=template_name
)
operation_client.wait(project=project_id, operation=op.name)
wait_for_extended_operation(operation, "instance template deletion")
return
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
# flake8: noqa

import re
import sys
from google.cloud import compute_v1
import time
from typing import List

from google.cloud import compute_v1


# <INGREDIENT create_instance>
def create_instance(
Expand Down Expand Up @@ -79,7 +78,6 @@ def create_instance(
Instance object.
"""
instance_client = compute_v1.InstancesClient()
operation_client = compute_v1.ZoneOperationsClient()

# Use the network interface provided in the network_link argument.
network_interface = compute_v1.NetworkInterface()
Expand Down Expand Up @@ -135,19 +133,10 @@ def create_instance(
# Wait for the create operation to complete.
print(f"Creating the {instance_name} instance in {zone}...")

operation = instance_client.insert_unary(request=request)
start = time.time()
while operation.status != compute_v1.Operation.Status.DONE:
operation = operation_client.wait(
operation=operation.name, zone=zone, project=project_id
)
if time.time() - start >= 300: # 5 minutes
raise TimeoutError()
if operation.error:
print("Error during creation:", operation.error, file=sys.stderr)
raise RuntimeError(operation.error)
if operation.warnings:
print("Warning during creation:", operation.warnings, file=sys.stderr)
operation = instance_client.insert(request=request)

wait_for_extended_operation(operation, "instance creation")

print(f"Instance {instance_name} created.")
return instance_client.get(project=project_id, zone=zone, instance=instance_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def create_instance_from_template(
Returns:
Instance object.
"""
operation_client = compute_v1.ZoneOperationsClient()
instance_client = compute_v1.InstancesClient()

instance_insert_request = compute_v1.InsertInstanceRequest()
Expand All @@ -50,8 +49,8 @@ def create_instance_from_template(
instance_insert_request.source_instance_template = instance_template_url
instance_insert_request.instance_resource.name = instance_name

op = instance_client.insert_unary(instance_insert_request)
operation_client.wait(project=project_id, zone=zone, operation=op.name)
operation = instance_client.insert(instance_insert_request)
wait_for_extended_operation(operation, "instance creation")

return instance_client.get(project=project_id, zone=zone, instance=instance_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def create_instance_from_template_with_overrides(
Returns:
Instance object.
"""
operation_client = compute_v1.ZoneOperationsClient()
instance_client = compute_v1.InstancesClient()
instance_template_client = compute_v1.InstanceTemplatesClient()

Expand Down Expand Up @@ -90,8 +89,8 @@ def create_instance_from_template_with_overrides(
instance_insert_request.instance_resource = instance
instance_insert_request.source_instance_template = instance_template.self_link

op = instance_client.insert_unary(instance_insert_request)
operation_client.wait(project=project_id, zone=zone, operation=op.name)
operation = instance_client.insert(instance_insert_request)
wait_for_extended_operation(operation, "instance creation")

return instance_client.get(project=project_id, zone=zone, instance=instance_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def add_extended_memory_to_instance(
Instance object.
"""
instance_client = compute_v1.InstancesClient()
operation_client = compute_v1.ZoneOperationsClient()
instance = instance_client.get(
project=project_id, zone=zone, instance=instance_name
)
Expand All @@ -51,10 +50,10 @@ def add_extended_memory_to_instance(
instance.Status.TERMINATED.name,
instance.Status.STOPPED.name,
):
op = instance_client.stop_unary(
operation = instance_client.stop(
project=project_id, zone=zone, instance=instance_name
)
operation_client.wait(project=project_id, zone=zone, operation=op.name)
wait_for_extended_operation(operation, "instance stopping")
start = time.time()
while instance.status not in (
instance.Status.TERMINATED.name,
Expand All @@ -77,13 +76,13 @@ def add_extended_memory_to_instance(
# cmt.memory_mb = new_memory
# cmt.extra_memory_used = True
# instance.machine_type = str(cmt)
op = instance_client.update_unary(
operation = instance_client.update(
project=project_id,
zone=zone,
instance=instance_name,
instance_resource=instance,
)
operation_client.wait(project=project_id, zone=zone, operation=op.name)
wait_for_extended_operation(operation, "instance update")

return instance_client.get(project=project_id, zone=zone, instance=instance_name)
# </INGREDIENT>
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,12 @@ def delete_instance(project_id: str, zone: str, machine_name: str) -> None:
machine_name: name of the machine you want to delete.
"""
instance_client = compute_v1.InstancesClient()
operation_client = compute_v1.ZoneOperationsClient()

print(f"Deleting {machine_name} from {zone}...")
operation = instance_client.delete_unary(
operation = instance_client.delete(
project=project_id, zone=zone, instance=machine_name
)
start = time.time()
while operation.status != compute_v1.Operation.Status.DONE:
operation = operation_client.wait(
operation=operation.name, zone=zone, project=project_id
)
if time.time() - start >= 300: # 5 minutes
raise TimeoutError()
if operation.error:
print("Error during deletion:", operation.error, file=sys.stderr)
return
if operation.warnings:
print("Warning during deletion:", operation.warnings, file=sys.stderr)
wait_for_extended_operation(operation, "instance deletion")
print(f"Instance {machine_name} deleted.")
return
# </INGREDIENT>
Loading

0 comments on commit 9e8c6e4

Please sign in to comment.