-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Changing machine type sample (#9042)
* docs(samples): Changing machine type sample * Applying suggestions * Wording fix * Update compute/client_library/ingredients/instances/change_machine_type.py Co-authored-by: Dan Lee <[email protected]> * Applying suggestions --------- Co-authored-by: Karl Weinmeister <[email protected]> Co-authored-by: Dan Lee <[email protected]>
- Loading branch information
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
compute/client_library/ingredients/instances/change_machine_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# 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 google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT change_machine_type> | ||
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. | ||
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 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() | ||
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") | ||
# </INGREDIENT> |
22 changes: 22 additions & 0 deletions
22
compute/client_library/recipes/instances/change_machine_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
# <REGION compute_change_machine_type> | ||
# <IMPORTS/> | ||
|
||
# <INGREDIENT wait_for_extended_operation /> | ||
|
||
# <INGREDIENT change_machine_type /> | ||
# </REGION compute_change_machine_type> |
114 changes: 114 additions & 0 deletions
114
compute/client_library/snippets/instances/change_machine_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# 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 | ||
|
||
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 | ||
) -> None: | ||
""" | ||
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. | ||
zone: name of the zone your instance belongs to. | ||
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() | ||
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") | ||
|
||
|
||
# [END compute_change_machine_type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters