From b4267ad046b6113bef87d27e0224a27d777ac692 Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Tue, 24 Jan 2023 17:13:24 +0100 Subject: [PATCH 1/5] docs(samples): Changing machine type sample --- .../instances/change_machine_type.py | 54 ++++++++ .../recipes/instances/change_machine_type.py | 22 ++++ .../snippets/instances/change_machine_type.py | 117 ++++++++++++++++++ .../tests/test_instance_start_stop.py | 13 ++ 4 files changed, 206 insertions(+) create mode 100644 compute/client_library/ingredients/instances/change_machine_type.py create mode 100644 compute/client_library/recipes/instances/change_machine_type.py create mode 100644 compute/client_library/snippets/instances/change_machine_type.py diff --git a/compute/client_library/ingredients/instances/change_machine_type.py b/compute/client_library/ingredients/instances/change_machine_type.py new file mode 100644 index 00000000000..31f4734abe8 --- /dev/null +++ b/compute/client_library/ingredients/instances/change_machine_type.py @@ -0,0 +1,54 @@ +# Copyright 2023 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. + +# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets +# 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 typing import NoReturn + +from google.cloud import compute_v1 + + +# +def change_machine_type(project_id: str, zone: str, instance_name: str, new_machine_type: str) -> NoReturn: + """ + Changes the machine type of compute instance. The instance needs to be in `TERMINATED` state for this + to be successful. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone your instance belongs to. + instance_name: name of the instance your want to modify. + new_machine_type: the new machine type you want to use for the compute instance. + In example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` + More about machine types: https://cloud.google.com/compute/docs/machine-resource + """ + client = compute_v1.InstancesClient() + instance = client.get(project=project_id, zone=zone, instance=instance_name) + + if instance.status != compute_v1.Instance.Status.TERMINATED.name: + raise RuntimeError(f"Only machines in TERMINATED state can have their machine type changed. " + f"{instance.name} is in {instance.status}({instance.status_message}) state.") + + machine_type = compute_v1.InstancesSetMachineTypeRequest() + machine_type.machine_type = f"projects/{project_id}/zones/{zone}/machineTypes/{new_machine_type}" + operation = client.set_machine_type(project=project_id, zone=zone, instance=instance_name, + instances_set_machine_type_request_resource=machine_type) + + wait_for_extended_operation(operation, "changing machine type") + + return +# diff --git a/compute/client_library/recipes/instances/change_machine_type.py b/compute/client_library/recipes/instances/change_machine_type.py new file mode 100644 index 00000000000..20907fbbb2d --- /dev/null +++ b/compute/client_library/recipes/instances/change_machine_type.py @@ -0,0 +1,22 @@ +# Copyright 2023 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. +# flake8: noqa + +# +# + +# + +# +# diff --git a/compute/client_library/snippets/instances/change_machine_type.py b/compute/client_library/snippets/instances/change_machine_type.py new file mode 100644 index 00000000000..7cf41dc8893 --- /dev/null +++ b/compute/client_library/snippets/instances/change_machine_type.py @@ -0,0 +1,117 @@ +# Copyright 2023 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. +# flake8: noqa + + +# This file is automatically generated. Please do not modify it directly. +# Find the relevant recipe file in the samples/recipes or samples/ingredients +# directory and apply your changes there. + + +# [START compute_change_machine_type] +import sys +from typing import Any, NoReturn + +from google.api_core.extended_operation import ExtendedOperation +from google.cloud import compute_v1 + + +def wait_for_extended_operation( + operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300 +) -> Any: + """ + This method will wait for the extended (long-running) operation to + complete. If the operation is successful, it will return its result. + If the operation ends with an error, an exception will be raised. + If there were any warnings during the execution of the operation + they will be printed to sys.stderr. + + Args: + operation: a long-running operation you want to wait on. + verbose_name: (optional) a more verbose name of the operation, + used only during error and warning reporting. + timeout: how long (in seconds) to wait for operation to finish. + If None, wait indefinitely. + + Returns: + Whatever the operation.result() returns. + + Raises: + This method will raise the exception received from `operation.exception()` + or RuntimeError if there is no exception set, but there is an `error_code` + set for the `operation`. + + In case of an operation taking longer than `timeout` seconds to complete, + a `concurrent.futures.TimeoutError` will be raised. + """ + result = operation.result(timeout=timeout) + + if operation.error_code: + print( + f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", + file=sys.stderr, + flush=True, + ) + print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True) + raise operation.exception() or RuntimeError(operation.error_message) + + if operation.warnings: + print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True) + for warning in operation.warnings: + print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True) + + return result + + +def change_machine_type( + project_id: str, zone: str, instance_name: str, new_machine_type: str +) -> NoReturn: + """ + Changes the machine type of compute instance. The instance needs to be in `TERMINATED` state for this + to be successful. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone your instance belongs to. + instance_name: name of the instance your want to modify. + new_machine_type: the new machine type you want to use for the compute instance. + In example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` + More about machine types: https://cloud.google.com/compute/docs/machine-resource + """ + client = compute_v1.InstancesClient() + instance = client.get(project=project_id, zone=zone, instance=instance_name) + + if instance.status != compute_v1.Instance.Status.TERMINATED.name: + raise RuntimeError( + f"Only machines in TERMINATED state can have their machine type changed. " + f"{instance.name} is in {instance.status}({instance.status_message}) state." + ) + + machine_type = compute_v1.InstancesSetMachineTypeRequest() + machine_type.machine_type = ( + f"projects/{project_id}/zones/{zone}/machineTypes/{new_machine_type}" + ) + operation = client.set_machine_type( + project=project_id, + zone=zone, + instance=instance_name, + instances_set_machine_type_request_resource=machine_type, + ) + + wait_for_extended_operation(operation, "changing machine type") + + return + + +# [END compute_change_machine_type] diff --git a/compute/client_library/snippets/tests/test_instance_start_stop.py b/compute/client_library/snippets/tests/test_instance_start_stop.py index 8c6efc802de..a93fac9d90e 100644 --- a/compute/client_library/snippets/tests/test_instance_start_stop.py +++ b/compute/client_library/snippets/tests/test_instance_start_stop.py @@ -23,6 +23,8 @@ from ..disks.clone_encrypted_disk import create_disk_from_customer_encrypted_disk from ..disks.delete import delete_disk +from ..instances.change_machine_type import change_machine_type +from ..instances.get import get_instance from ..instances.start import start_instance from ..instances.start_encrypted import start_instance_with_encryption_key from ..instances.stop import stop_instance @@ -187,3 +189,14 @@ def test_clone_encrypted_disk(autodelete_disk_name, compute_encrypted_instance): encryption_key=KEY_B64) assert new_disk.name == autodelete_disk_name + + +def test_change_machine_type(compute_instance): + assert _get_status(compute_instance) == "RUNNING" + + stop_instance(PROJECT, INSTANCE_ZONE, compute_instance.name) + + assert not get_instance(PROJECT, INSTANCE_ZONE, compute_instance.name).machine_type.endswith("e2-standard-2") + change_machine_type(PROJECT, INSTANCE_ZONE, compute_instance.name, "e2-standard-2") + + assert get_instance(PROJECT, INSTANCE_ZONE, compute_instance.name).machine_type.endswith("e2-standard-2") From 49c020c93842b674c65d505eaa26fefacf422719 Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Fri, 27 Jan 2023 15:19:18 +0100 Subject: [PATCH 2/5] Applying suggestions --- .../ingredients/instances/change_machine_type.py | 9 ++++----- .../snippets/instances/change_machine_type.py | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/compute/client_library/ingredients/instances/change_machine_type.py b/compute/client_library/ingredients/instances/change_machine_type.py index 31f4734abe8..4a73502e686 100644 --- a/compute/client_library/ingredients/instances/change_machine_type.py +++ b/compute/client_library/ingredients/instances/change_machine_type.py @@ -25,15 +25,14 @@ # def change_machine_type(project_id: str, zone: str, instance_name: str, new_machine_type: str) -> NoReturn: """ - Changes the machine type of compute instance. The instance needs to be in `TERMINATED` state for this - to be successful. + Changes the machine type of a VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone your instance belongs to. - instance_name: name of the instance your want to modify. - new_machine_type: the new machine type you want to use for the compute instance. - In example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` + instance_name: name of the VM you want to modify. + new_machine_type: the new machine type you want to use for the VM. + For example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` More about machine types: https://cloud.google.com/compute/docs/machine-resource """ client = compute_v1.InstancesClient() diff --git a/compute/client_library/snippets/instances/change_machine_type.py b/compute/client_library/snippets/instances/change_machine_type.py index 7cf41dc8893..65e59f5c10e 100644 --- a/compute/client_library/snippets/instances/change_machine_type.py +++ b/compute/client_library/snippets/instances/change_machine_type.py @@ -78,15 +78,14 @@ def change_machine_type( project_id: str, zone: str, instance_name: str, new_machine_type: str ) -> NoReturn: """ - Changes the machine type of compute instance. The instance needs to be in `TERMINATED` state for this - to be successful. + Changes the machine type of a VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. Args: project_id: project ID or project number of the Cloud project you want to use. zone: name of the zone your instance belongs to. - instance_name: name of the instance your want to modify. - new_machine_type: the new machine type you want to use for the compute instance. - In example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` + instance_name: name of the VM you want to modify. + new_machine_type: the new machine type you want to use for the VM. + For example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` More about machine types: https://cloud.google.com/compute/docs/machine-resource """ client = compute_v1.InstancesClient() From 18664a0f154297c194a359f87e88c1293e03c140 Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Fri, 27 Jan 2023 15:24:49 +0100 Subject: [PATCH 3/5] Wording fix --- .../client_library/ingredients/instances/change_machine_type.py | 2 +- .../client_library/snippets/instances/change_machine_type.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compute/client_library/ingredients/instances/change_machine_type.py b/compute/client_library/ingredients/instances/change_machine_type.py index 4a73502e686..da0ab5a6fa7 100644 --- a/compute/client_library/ingredients/instances/change_machine_type.py +++ b/compute/client_library/ingredients/instances/change_machine_type.py @@ -25,7 +25,7 @@ # def change_machine_type(project_id: str, zone: str, instance_name: str, new_machine_type: str) -> NoReturn: """ - Changes the machine type of a VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. + Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. Args: project_id: project ID or project number of the Cloud project you want to use. diff --git a/compute/client_library/snippets/instances/change_machine_type.py b/compute/client_library/snippets/instances/change_machine_type.py index 65e59f5c10e..cf836981096 100644 --- a/compute/client_library/snippets/instances/change_machine_type.py +++ b/compute/client_library/snippets/instances/change_machine_type.py @@ -78,7 +78,7 @@ def change_machine_type( project_id: str, zone: str, instance_name: str, new_machine_type: str ) -> NoReturn: """ - Changes the machine type of a VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. + Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. Args: project_id: project ID or project number of the Cloud project you want to use. From f35ab2114dcd060db4dda96b10d7595b38537caa Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Fri, 3 Feb 2023 16:06:35 +0100 Subject: [PATCH 4/5] Update compute/client_library/ingredients/instances/change_machine_type.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- .../ingredients/instances/change_machine_type.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compute/client_library/ingredients/instances/change_machine_type.py b/compute/client_library/ingredients/instances/change_machine_type.py index da0ab5a6fa7..a6ef12aa249 100644 --- a/compute/client_library/ingredients/instances/change_machine_type.py +++ b/compute/client_library/ingredients/instances/change_machine_type.py @@ -44,8 +44,12 @@ def change_machine_type(project_id: str, zone: str, instance_name: str, new_mach machine_type = compute_v1.InstancesSetMachineTypeRequest() machine_type.machine_type = f"projects/{project_id}/zones/{zone}/machineTypes/{new_machine_type}" - operation = client.set_machine_type(project=project_id, zone=zone, instance=instance_name, - instances_set_machine_type_request_resource=machine_type) + operation = client.set_machine_type( + project=project_id, + zone=zone, + instance=instance_name, + instances_set_machine_type_request_resource=machine_type, + ) wait_for_extended_operation(operation, "changing machine type") From c64a24381dfa6eefe3d8a281aae3c8ec60eb811f Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Fri, 3 Feb 2023 16:15:00 +0100 Subject: [PATCH 5/5] Applying suggestions --- .../ingredients/instances/change_machine_type.py | 11 ++++++----- .../snippets/instances/change_machine_type.py | 6 ++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/compute/client_library/ingredients/instances/change_machine_type.py b/compute/client_library/ingredients/instances/change_machine_type.py index a6ef12aa249..2e4f78e10d0 100644 --- a/compute/client_library/ingredients/instances/change_machine_type.py +++ b/compute/client_library/ingredients/instances/change_machine_type.py @@ -17,13 +17,16 @@ # Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. # flake8: noqa -from typing import NoReturn - from google.cloud import compute_v1 # -def change_machine_type(project_id: str, zone: str, instance_name: str, new_machine_type: str) -> NoReturn: +def change_machine_type( + project_id: str, + zone: str, + instance_name: str, + new_machine_type: str +) -> None: """ Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. @@ -52,6 +55,4 @@ def change_machine_type(project_id: str, zone: str, instance_name: str, new_mach ) wait_for_extended_operation(operation, "changing machine type") - - return # diff --git a/compute/client_library/snippets/instances/change_machine_type.py b/compute/client_library/snippets/instances/change_machine_type.py index cf836981096..2b2268d6812 100644 --- a/compute/client_library/snippets/instances/change_machine_type.py +++ b/compute/client_library/snippets/instances/change_machine_type.py @@ -21,7 +21,7 @@ # [START compute_change_machine_type] import sys -from typing import Any, NoReturn +from typing import Any from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -76,7 +76,7 @@ def wait_for_extended_operation( def change_machine_type( project_id: str, zone: str, instance_name: str, new_machine_type: str -) -> NoReturn: +) -> None: """ Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. @@ -110,7 +110,5 @@ def change_machine_type( wait_for_extended_operation(operation, "changing machine type") - return - # [END compute_change_machine_type]