diff --git a/compute/client_library/ingredients/disks/create_hyperdisk_from_pool.py b/compute/client_library/ingredients/disks/create_hyperdisk_from_pool.py new file mode 100644 index 00000000000..67f3bc20de0 --- /dev/null +++ b/compute/client_library/ingredients/disks/create_hyperdisk_from_pool.py @@ -0,0 +1,69 @@ +# Copyright 2024 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 + + +# +def create_hyperdisk_from_pool( + project_id: str, + zone: str, + disk_name: str, + storage_pool_name: str, + disk_size_gb: int = 100, +) -> compute_v1.Disk: + """Creates a Hyperdisk from a specified storage pool in Google Cloud. + Args: + project_id (str): The ID of the Google Cloud project. + zone (str): The zone where the disk will be created. + disk_name (str): The name of the disk you want to create. + storage_pool_name (str): The name of the storage pool from which the disk will be created. + disk_size_gb (int): The size of the disk in gigabytes. + Returns: + compute_v1.Disk: The created disk from the storage pool. + """ + disk = compute_v1.Disk() + disk.zone = zone + disk.size_gb = disk_size_gb + disk.name = disk_name + disk.type = f"projects/{project_id}/zones/{zone}/diskTypes/hyperdisk-balanced" + disk.storage_pool = ( + f"projects/{project_id}/zones/{zone}/storagePools/{storage_pool_name}" + ) + # Optional parameters + # disk.provisioned_iops = 10000 + # disk.provisioned_throughput = 140 + + disk_client = compute_v1.DisksClient() + operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk) + wait_for_extended_operation(operation, "disk creation") + + new_disk = disk_client.get(project=project_id, zone=zone, disk=disk.name) + print(new_disk.status) + print(new_disk.provisioned_iops) + print(new_disk.provisioned_throughput) + # Example response: + # READY + # 3600 + # 290 + + return new_disk + + +# diff --git a/compute/client_library/ingredients/disks/create_hyperdisk_storage_pool.py b/compute/client_library/ingredients/disks/create_hyperdisk_storage_pool.py new file mode 100644 index 00000000000..f53010e5fec --- /dev/null +++ b/compute/client_library/ingredients/disks/create_hyperdisk_storage_pool.py @@ -0,0 +1,73 @@ +# Copyright 2024 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 + +# + + +def create_hyperdisk_storage_pool( + project_id: str, + zone: str, + storage_pool_name: str, + storage_pool_type: str = "hyperdisk-balanced", +) -> compute_v1.StoragePool: + """Creates a hyperdisk storage pool in the specified project and zone. + Args: + project_id (str): The ID of the Google Cloud project. + zone (str): The zone where the storage pool will be created. + storage_pool_name (str): The name of the storage pool. + storage_pool_type (str, optional): The type of the storage pool. Defaults to "hyperdisk-balanced". + Returns: + compute_v1.StoragePool: The created storage pool. + """ + + pool = compute_v1.StoragePool() + pool.name = storage_pool_name + pool.zone = zone + pool.storage_pool_type = ( + f"projects/{project_id}/zones/{zone}/storagePoolTypes/{storage_pool_type}" + ) + pool.capacity_provisioning_type = "ADVANCED" + pool.pool_provisioned_capacity_gb = 10240 + pool.performance_provisioning_type = "STANDARD" + + # Relevant if the storage pool type is hyperdisk-balanced. + pool.pool_provisioned_iops = 10000 + pool.pool_provisioned_throughput = 1024 + + pool_client = compute_v1.StoragePoolsClient() + operation = pool_client.insert( + project=project_id, zone=zone, storage_pool_resource=pool + ) + wait_for_extended_operation(operation, "disk creation") + + new_pool = pool_client.get(project=project_id, zone=zone, storage_pool=pool.name) + print(new_pool.pool_provisioned_iops) + print(new_pool.pool_provisioned_throughput) + print(new_pool.capacity_provisioning_type) + # Example response: + # 10000 + # 1024 + # ADVANCED + + return new_pool + + +# diff --git a/compute/client_library/recipes/disks/create_hyperdisk_from_pool.py b/compute/client_library/recipes/disks/create_hyperdisk_from_pool.py new file mode 100644 index 00000000000..64efa405e64 --- /dev/null +++ b/compute/client_library/recipes/disks/create_hyperdisk_from_pool.py @@ -0,0 +1,23 @@ +# Copyright 2024 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/recipes/disks/create_hyperdisk_storage_pool.py b/compute/client_library/recipes/disks/create_hyperdisk_storage_pool.py new file mode 100644 index 00000000000..56daf9913de --- /dev/null +++ b/compute/client_library/recipes/disks/create_hyperdisk_storage_pool.py @@ -0,0 +1,23 @@ +# Copyright 2024 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/disks/create_hyperdisk_from_pool.py b/compute/client_library/snippets/disks/create_hyperdisk_from_pool.py new file mode 100644 index 00000000000..38fe0156870 --- /dev/null +++ b/compute/client_library/snippets/disks/create_hyperdisk_from_pool.py @@ -0,0 +1,125 @@ +# Copyright 2024 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_hyperdisk_create_from_pool] +from __future__ import annotations + +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: + """ + Waits 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 create_hyperdisk_from_pool( + project_id: str, + zone: str, + disk_name: str, + storage_pool_name: str, + disk_size_gb: int = 100, +) -> compute_v1.Disk: + """Creates a Hyperdisk from a specified storage pool in Google Cloud. + Args: + project_id (str): The ID of the Google Cloud project. + zone (str): The zone where the disk will be created. + disk_name (str): The name of the disk you want to create. + storage_pool_name (str): The name of the storage pool from which the disk will be created. + disk_size_gb (int): The size of the disk in gigabytes. + Returns: + compute_v1.Disk: The created disk from the storage pool. + """ + disk = compute_v1.Disk() + disk.zone = zone + disk.size_gb = disk_size_gb + disk.name = disk_name + disk.type = f"projects/{project_id}/zones/{zone}/diskTypes/hyperdisk-balanced" + disk.storage_pool = ( + f"projects/{project_id}/zones/{zone}/storagePools/{storage_pool_name}" + ) + # Optional parameters + # disk.provisioned_iops = 10000 + # disk.provisioned_throughput = 140 + + disk_client = compute_v1.DisksClient() + operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk) + wait_for_extended_operation(operation, "disk creation") + + new_disk = disk_client.get(project=project_id, zone=zone, disk=disk.name) + print(new_disk.status) + print(new_disk.provisioned_iops) + print(new_disk.provisioned_throughput) + # Example response: + # READY + # 3600 + # 290 + + return new_disk + + +# [END compute_hyperdisk_create_from_pool] diff --git a/compute/client_library/snippets/disks/create_hyperdisk_storage_pool.py b/compute/client_library/snippets/disks/create_hyperdisk_storage_pool.py new file mode 100644 index 00000000000..62a51971807 --- /dev/null +++ b/compute/client_library/snippets/disks/create_hyperdisk_storage_pool.py @@ -0,0 +1,128 @@ +# Copyright 2024 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_hyperdisk_pool_create] +from __future__ import annotations + +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: + """ + Waits 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 create_hyperdisk_storage_pool( + project_id: str, + zone: str, + storage_pool_name: str, + storage_pool_type: str = "hyperdisk-balanced", +) -> compute_v1.StoragePool: + """Creates a hyperdisk storage pool in the specified project and zone. + Args: + project_id (str): The ID of the Google Cloud project. + zone (str): The zone where the storage pool will be created. + storage_pool_name (str): The name of the storage pool. + storage_pool_type (str, optional): The type of the storage pool. Defaults to "hyperdisk-balanced". + Returns: + compute_v1.StoragePool: The created storage pool. + """ + + pool = compute_v1.StoragePool() + pool.name = storage_pool_name + pool.zone = zone + pool.storage_pool_type = ( + f"projects/{project_id}/zones/{zone}/storagePoolTypes/{storage_pool_type}" + ) + pool.capacity_provisioning_type = "ADVANCED" + pool.pool_provisioned_capacity_gb = 10240 + pool.performance_provisioning_type = "STANDARD" + + # Relevant if the storage pool type is hyperdisk-balanced. + pool.pool_provisioned_iops = 10000 + pool.pool_provisioned_throughput = 1024 + + pool_client = compute_v1.StoragePoolsClient() + operation = pool_client.insert( + project=project_id, zone=zone, storage_pool_resource=pool + ) + wait_for_extended_operation(operation, "disk creation") + + new_pool = pool_client.get(project=project_id, zone=zone, storage_pool=pool.name) + print(new_pool.pool_provisioned_iops) + print(new_pool.pool_provisioned_throughput) + print(new_pool.capacity_provisioning_type) + # Example response: + # 10000 + # 1024 + # ADVANCED + + return new_pool + + +# [END compute_hyperdisk_pool_create] diff --git a/compute/client_library/snippets/tests/test_disks.py b/compute/client_library/snippets/tests/test_disks.py index 050fd910e58..3fbfa8ab65e 100644 --- a/compute/client_library/snippets/tests/test_disks.py +++ b/compute/client_library/snippets/tests/test_disks.py @@ -25,6 +25,8 @@ from ..disks.create_from_image import create_disk_from_image from ..disks.create_from_source import create_disk_from_disk from ..disks.create_hyperdisk import create_hyperdisk +from ..disks.create_hyperdisk_from_pool import create_hyperdisk_from_pool +from ..disks.create_hyperdisk_storage_pool import create_hyperdisk_storage_pool from ..disks.create_kms_encrypted_disk import create_kms_encrypted_disk from ..disks.delete import delete_disk from ..disks.list import list_disks @@ -196,6 +198,15 @@ def autodelete_compute_instance(): delete_instance(PROJECT, ZONE, instance_name) +@pytest.fixture(scope="session") +def autodelete_hyperdisk_pool(): + pool_name = "test-pool-" + uuid.uuid4().hex[:6] + pool = create_hyperdisk_storage_pool(PROJECT, ZONE, pool_name) + yield pool + pool_client = compute_v1.StoragePoolsClient() + pool_client.delete(project=PROJECT, zone=ZONE, storage_pool=pool_name) + + def test_disk_create_delete(autodelete_disk_name): disk_type = f"zones/{ZONE}/diskTypes/pd-standard" debian_image = get_image_from_family("debian-cloud", "debian-11") @@ -336,6 +347,18 @@ def test_disk_resize(autodelete_blank_disk, autodelete_regional_blank_disk): ) +def test_create_hyperdisk_pool(autodelete_hyperdisk_pool): + assert "hyperdisk" in autodelete_hyperdisk_pool.storage_pool_type + + +def test_create_hyperdisk_from_pool(autodelete_hyperdisk_pool, autodelete_disk_name): + disk = create_hyperdisk_from_pool( + PROJECT, ZONE, autodelete_disk_name, autodelete_hyperdisk_pool.name + ) + assert disk.storage_pool == autodelete_hyperdisk_pool.self_link + assert "hyperdisk" in disk.type + + def test_create_hyperdisk(autodelete_disk_name): disk = create_hyperdisk(PROJECT, ZONE, autodelete_disk_name, 100) assert "hyperdisk" in disk.type_.lower()