Skip to content

Commit 3ca526d

Browse files
feat: Compute Hyperdisk Create Sample (#12747)
* Create hyperdisk Create Sample
1 parent 0cf659a commit 3ca526d

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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>
24+
def create_hyperdisk(
25+
project_id: str,
26+
zone: str,
27+
disk_name: str,
28+
disk_size_gb: int = 100,
29+
disk_type: str = "hyperdisk-balanced",
30+
) -> compute_v1.Disk:
31+
"""Creates a Hyperdisk in the specified project and zone with the given parameters.
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+
disk_size_gb (int): The size of the disk in gigabytes.
37+
disk_type (str): The type of the disk. Defaults to "hyperdisk-balanced".
38+
Returns:
39+
compute_v1.Disk: The created disk object.
40+
"""
41+
42+
disk = compute_v1.Disk()
43+
disk.zone = zone
44+
disk.size_gb = disk_size_gb
45+
disk.name = disk_name
46+
type_disk = disk_type
47+
disk.type = f"projects/{project_id}/zones/{zone}/diskTypes/{type_disk}"
48+
disk.provisioned_iops = 10000
49+
disk.provisioned_throughput = 140
50+
51+
disk_client = compute_v1.DisksClient()
52+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
53+
wait_for_extended_operation(operation, "disk creation")
54+
55+
new_disk = disk_client.get(project=project_id, zone=zone, disk=disk.name)
56+
print(new_disk.status)
57+
print(new_disk.provisioned_iops)
58+
print(new_disk.provisioned_throughput)
59+
# Example response:
60+
# READY
61+
# 10000
62+
# 140
63+
64+
return new_disk
65+
66+
67+
# </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>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_hyperdisk />
22+
23+
# </REGION compute_hyperdisk_create>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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]
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(
81+
project_id: str,
82+
zone: str,
83+
disk_name: str,
84+
disk_size_gb: int = 100,
85+
disk_type: str = "hyperdisk-balanced",
86+
) -> compute_v1.Disk:
87+
"""Creates a Hyperdisk in the specified project and zone with the given parameters.
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+
disk_size_gb (int): The size of the disk in gigabytes.
93+
disk_type (str): The type of the disk. Defaults to "hyperdisk-balanced".
94+
Returns:
95+
compute_v1.Disk: The created disk object.
96+
"""
97+
98+
disk = compute_v1.Disk()
99+
disk.zone = zone
100+
disk.size_gb = disk_size_gb
101+
disk.name = disk_name
102+
type_disk = disk_type
103+
disk.type = f"projects/{project_id}/zones/{zone}/diskTypes/{type_disk}"
104+
disk.provisioned_iops = 10000
105+
disk.provisioned_throughput = 140
106+
107+
disk_client = compute_v1.DisksClient()
108+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
109+
wait_for_extended_operation(operation, "disk creation")
110+
111+
new_disk = disk_client.get(project=project_id, zone=zone, disk=disk.name)
112+
print(new_disk.status)
113+
print(new_disk.provisioned_iops)
114+
print(new_disk.provisioned_throughput)
115+
# Example response:
116+
# READY
117+
# 10000
118+
# 140
119+
120+
return new_disk
121+
122+
123+
# [END compute_hyperdisk_create]

compute/client_library/snippets/tests/test_disks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from ..disks.create_empty_disk import create_empty_disk
2525
from ..disks.create_from_image import create_disk_from_image
2626
from ..disks.create_from_source import create_disk_from_disk
27+
from ..disks.create_hyperdisk import create_hyperdisk
2728
from ..disks.create_kms_encrypted_disk import create_kms_encrypted_disk
2829
from ..disks.delete import delete_disk
2930
from ..disks.list import list_disks
@@ -333,3 +334,8 @@ def test_disk_resize(autodelete_blank_disk, autodelete_regional_blank_disk):
333334
).size_gb
334335
== 23
335336
)
337+
338+
339+
def test_create_hyperdisk(autodelete_disk_name):
340+
disk = create_hyperdisk(PROJECT, ZONE, autodelete_disk_name, 100)
341+
assert "hyperdisk" in disk.type_.lower()

0 commit comments

Comments
 (0)