|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# flake8: noqa |
| 15 | + |
| 16 | + |
| 17 | +# This file is automatically generated. Please do not modify it directly. |
| 18 | +# Find the relevant recipe file in the samples/recipes or samples/ingredients |
| 19 | +# directory and apply your changes there. |
| 20 | + |
| 21 | + |
| 22 | +# [START compute_change_machine_type] |
| 23 | +import sys |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +from google.api_core.extended_operation import ExtendedOperation |
| 27 | +from google.cloud import compute_v1 |
| 28 | + |
| 29 | + |
| 30 | +def wait_for_extended_operation( |
| 31 | + operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300 |
| 32 | +) -> Any: |
| 33 | + """ |
| 34 | + This method will wait for the extended (long-running) operation to |
| 35 | + complete. If the operation is successful, it will return its result. |
| 36 | + If the operation ends with an error, an exception will be raised. |
| 37 | + If there were any warnings during the execution of the operation |
| 38 | + they will be printed to sys.stderr. |
| 39 | +
|
| 40 | + Args: |
| 41 | + operation: a long-running operation you want to wait on. |
| 42 | + verbose_name: (optional) a more verbose name of the operation, |
| 43 | + used only during error and warning reporting. |
| 44 | + timeout: how long (in seconds) to wait for operation to finish. |
| 45 | + If None, wait indefinitely. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + Whatever the operation.result() returns. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + This method will raise the exception received from `operation.exception()` |
| 52 | + or RuntimeError if there is no exception set, but there is an `error_code` |
| 53 | + set for the `operation`. |
| 54 | +
|
| 55 | + In case of an operation taking longer than `timeout` seconds to complete, |
| 56 | + a `concurrent.futures.TimeoutError` will be raised. |
| 57 | + """ |
| 58 | + result = operation.result(timeout=timeout) |
| 59 | + |
| 60 | + if operation.error_code: |
| 61 | + print( |
| 62 | + f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", |
| 63 | + file=sys.stderr, |
| 64 | + flush=True, |
| 65 | + ) |
| 66 | + print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True) |
| 67 | + raise operation.exception() or RuntimeError(operation.error_message) |
| 68 | + |
| 69 | + if operation.warnings: |
| 70 | + print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True) |
| 71 | + for warning in operation.warnings: |
| 72 | + print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True) |
| 73 | + |
| 74 | + return result |
| 75 | + |
| 76 | + |
| 77 | +def change_machine_type( |
| 78 | + project_id: str, zone: str, instance_name: str, new_machine_type: str |
| 79 | +) -> None: |
| 80 | + """ |
| 81 | + Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful. |
| 82 | +
|
| 83 | + Args: |
| 84 | + project_id: project ID or project number of the Cloud project you want to use. |
| 85 | + zone: name of the zone your instance belongs to. |
| 86 | + instance_name: name of the VM you want to modify. |
| 87 | + new_machine_type: the new machine type you want to use for the VM. |
| 88 | + For example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40` |
| 89 | + More about machine types: https://cloud.google.com/compute/docs/machine-resource |
| 90 | + """ |
| 91 | + client = compute_v1.InstancesClient() |
| 92 | + instance = client.get(project=project_id, zone=zone, instance=instance_name) |
| 93 | + |
| 94 | + if instance.status != compute_v1.Instance.Status.TERMINATED.name: |
| 95 | + raise RuntimeError( |
| 96 | + f"Only machines in TERMINATED state can have their machine type changed. " |
| 97 | + f"{instance.name} is in {instance.status}({instance.status_message}) state." |
| 98 | + ) |
| 99 | + |
| 100 | + machine_type = compute_v1.InstancesSetMachineTypeRequest() |
| 101 | + machine_type.machine_type = ( |
| 102 | + f"projects/{project_id}/zones/{zone}/machineTypes/{new_machine_type}" |
| 103 | + ) |
| 104 | + operation = client.set_machine_type( |
| 105 | + project=project_id, |
| 106 | + zone=zone, |
| 107 | + instance=instance_name, |
| 108 | + instances_set_machine_type_request_resource=machine_type, |
| 109 | + ) |
| 110 | + |
| 111 | + wait_for_extended_operation(operation, "changing machine type") |
| 112 | + |
| 113 | + |
| 114 | +# [END compute_change_machine_type] |
0 commit comments