Skip to content

Commit 8f39ccc

Browse files
feat: Created 2 new Samples - Hyperdisk create pool, Create Hyperdisk from pool (#12748)
* Created 2 new Samples: Hyperdisk create pool, Create Hyperdisk From pool. Updated tests.
1 parent 3ca526d commit 8f39ccc

File tree

7 files changed

+464
-0
lines changed

7 files changed

+464
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2024 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+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT create_hyperdisk_from_pool>
24+
def create_hyperdisk_from_pool(
25+
project_id: str,
26+
zone: str,
27+
disk_name: str,
28+
storage_pool_name: str,
29+
disk_size_gb: int = 100,
30+
) -> compute_v1.Disk:
31+
"""Creates a Hyperdisk from a specified storage pool in Google Cloud.
32+
Args:
33+
project_id (str): The ID of the Google Cloud project.
34+
zone (str): The zone where the disk will be created.
35+
disk_name (str): The name of the disk you want to create.
36+
storage_pool_name (str): The name of the storage pool from which the disk will be created.
37+
disk_size_gb (int): The size of the disk in gigabytes.
38+
Returns:
39+
compute_v1.Disk: The created disk from the storage pool.
40+
"""
41+
disk = compute_v1.Disk()
42+
disk.zone = zone
43+
disk.size_gb = disk_size_gb
44+
disk.name = disk_name
45+
disk.type = f"projects/{project_id}/zones/{zone}/diskTypes/hyperdisk-balanced"
46+
disk.storage_pool = (
47+
f"projects/{project_id}/zones/{zone}/storagePools/{storage_pool_name}"
48+
)
49+
# Optional parameters
50+
# disk.provisioned_iops = 10000
51+
# disk.provisioned_throughput = 140
52+
53+
disk_client = compute_v1.DisksClient()
54+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
55+
wait_for_extended_operation(operation, "disk creation")
56+
57+
new_disk = disk_client.get(project=project_id, zone=zone, disk=disk.name)
58+
print(new_disk.status)
59+
print(new_disk.provisioned_iops)
60+
print(new_disk.provisioned_throughput)
61+
# Example response:
62+
# READY
63+
# 3600
64+
# 290
65+
66+
return new_disk
67+
68+
69+
# </INGREDIENT>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2024 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+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
# <INGREDIENT create_hyperdisk_storage_pool>
23+
24+
25+
def create_hyperdisk_storage_pool(
26+
project_id: str,
27+
zone: str,
28+
storage_pool_name: str,
29+
storage_pool_type: str = "hyperdisk-balanced",
30+
) -> compute_v1.StoragePool:
31+
"""Creates a hyperdisk storage pool in the specified project and zone.
32+
Args:
33+
project_id (str): The ID of the Google Cloud project.
34+
zone (str): The zone where the storage pool will be created.
35+
storage_pool_name (str): The name of the storage pool.
36+
storage_pool_type (str, optional): The type of the storage pool. Defaults to "hyperdisk-balanced".
37+
Returns:
38+
compute_v1.StoragePool: The created storage pool.
39+
"""
40+
41+
pool = compute_v1.StoragePool()
42+
pool.name = storage_pool_name
43+
pool.zone = zone
44+
pool.storage_pool_type = (
45+
f"projects/{project_id}/zones/{zone}/storagePoolTypes/{storage_pool_type}"
46+
)
47+
pool.capacity_provisioning_type = "ADVANCED"
48+
pool.pool_provisioned_capacity_gb = 10240
49+
pool.performance_provisioning_type = "STANDARD"
50+
51+
# Relevant if the storage pool type is hyperdisk-balanced.
52+
pool.pool_provisioned_iops = 10000
53+
pool.pool_provisioned_throughput = 1024
54+
55+
pool_client = compute_v1.StoragePoolsClient()
56+
operation = pool_client.insert(
57+
project=project_id, zone=zone, storage_pool_resource=pool
58+
)
59+
wait_for_extended_operation(operation, "disk creation")
60+
61+
new_pool = pool_client.get(project=project_id, zone=zone, storage_pool=pool.name)
62+
print(new_pool.pool_provisioned_iops)
63+
print(new_pool.pool_provisioned_throughput)
64+
print(new_pool.capacity_provisioning_type)
65+
# Example response:
66+
# 10000
67+
# 1024
68+
# ADVANCED
69+
70+
return new_pool
71+
72+
73+
# </INGREDIENT>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024 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+
# <REGION compute_hyperdisk_create_from_pool>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_hyperdisk_from_pool />
22+
23+
# </REGION compute_hyperdisk_create_from_pool>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024 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+
# <REGION compute_hyperdisk_pool_create>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_hyperdisk_storage_pool />
22+
23+
# </REGION compute_hyperdisk_pool_create>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright 2024 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_hyperdisk_create_from_pool]
23+
from __future__ import annotations
24+
25+
import sys
26+
from typing import Any
27+
28+
from google.api_core.extended_operation import ExtendedOperation
29+
from google.cloud import compute_v1
30+
31+
32+
def wait_for_extended_operation(
33+
operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
34+
) -> Any:
35+
"""
36+
Waits for the extended (long-running) operation to complete.
37+
38+
If the operation is successful, it will return its result.
39+
If the operation ends with an error, an exception will be raised.
40+
If there were any warnings during the execution of the operation
41+
they will be printed to sys.stderr.
42+
43+
Args:
44+
operation: a long-running operation you want to wait on.
45+
verbose_name: (optional) a more verbose name of the operation,
46+
used only during error and warning reporting.
47+
timeout: how long (in seconds) to wait for operation to finish.
48+
If None, wait indefinitely.
49+
50+
Returns:
51+
Whatever the operation.result() returns.
52+
53+
Raises:
54+
This method will raise the exception received from `operation.exception()`
55+
or RuntimeError if there is no exception set, but there is an `error_code`
56+
set for the `operation`.
57+
58+
In case of an operation taking longer than `timeout` seconds to complete,
59+
a `concurrent.futures.TimeoutError` will be raised.
60+
"""
61+
result = operation.result(timeout=timeout)
62+
63+
if operation.error_code:
64+
print(
65+
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
66+
file=sys.stderr,
67+
flush=True,
68+
)
69+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
70+
raise operation.exception() or RuntimeError(operation.error_message)
71+
72+
if operation.warnings:
73+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
74+
for warning in operation.warnings:
75+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
76+
77+
return result
78+
79+
80+
def create_hyperdisk_from_pool(
81+
project_id: str,
82+
zone: str,
83+
disk_name: str,
84+
storage_pool_name: str,
85+
disk_size_gb: int = 100,
86+
) -> compute_v1.Disk:
87+
"""Creates a Hyperdisk from a specified storage pool in Google Cloud.
88+
Args:
89+
project_id (str): The ID of the Google Cloud project.
90+
zone (str): The zone where the disk will be created.
91+
disk_name (str): The name of the disk you want to create.
92+
storage_pool_name (str): The name of the storage pool from which the disk will be created.
93+
disk_size_gb (int): The size of the disk in gigabytes.
94+
Returns:
95+
compute_v1.Disk: The created disk from the storage pool.
96+
"""
97+
disk = compute_v1.Disk()
98+
disk.zone = zone
99+
disk.size_gb = disk_size_gb
100+
disk.name = disk_name
101+
disk.type = f"projects/{project_id}/zones/{zone}/diskTypes/hyperdisk-balanced"
102+
disk.storage_pool = (
103+
f"projects/{project_id}/zones/{zone}/storagePools/{storage_pool_name}"
104+
)
105+
# Optional parameters
106+
# disk.provisioned_iops = 10000
107+
# disk.provisioned_throughput = 140
108+
109+
disk_client = compute_v1.DisksClient()
110+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
111+
wait_for_extended_operation(operation, "disk creation")
112+
113+
new_disk = disk_client.get(project=project_id, zone=zone, disk=disk.name)
114+
print(new_disk.status)
115+
print(new_disk.provisioned_iops)
116+
print(new_disk.provisioned_throughput)
117+
# Example response:
118+
# READY
119+
# 3600
120+
# 290
121+
122+
return new_disk
123+
124+
125+
# [END compute_hyperdisk_create_from_pool]

0 commit comments

Comments
 (0)