Skip to content

Commit 664e81a

Browse files
m-strzelczykpartheasavija-tv
authored andcommitted
docs(samples): Various ways of creating a new disk (#308)
* docs(samples): Samples for various ways to create disks Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Savija Vijayaraghavan <[email protected]>
1 parent 9789403 commit 664e81a

23 files changed

+1356
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2022 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_disk_from_customer_encrypted_disk>
24+
def create_disk_from_customer_encrypted_disk(
25+
project_id: str, zone: str, disk_name: str, disk_type: str,
26+
disk_size_gb: int, disk_link: str,
27+
encryption_key: bytes) -> compute_v1.Disk:
28+
"""
29+
Creates a zonal non-boot persistent disk in a project with the copy of data from an existing disk.
30+
31+
The encryption key must be the same for the source disk and the new disk.
32+
33+
Args:
34+
project_id: project ID or project number of the Cloud project you want to use.
35+
zone: name of the zone in which you want to create the disk.
36+
disk_name: name of the disk you want to create.
37+
disk_type: the type of disk you want to create. This value uses the following format:
38+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
39+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
40+
disk_size_gb: size of the new disk in gigabytes
41+
disk_link: a link to the disk you want to use as a source for the new disk.
42+
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
43+
encryption_key: customer-supplied encryption key used for encrypting
44+
data in the source disk. The data will be encrypted with the same key
45+
in the new disk.
46+
47+
Returns:
48+
An attachable copy of an existing disk.
49+
"""
50+
disk_client = compute_v1.DisksClient()
51+
disk = compute_v1.Disk()
52+
disk.zone = zone
53+
disk.size_gb = disk_size_gb
54+
disk.source_disk = disk_link
55+
disk.type_ = disk_type
56+
disk.name = disk_name
57+
disk.disk_encryption_key = compute_v1.CustomerEncryptionKey()
58+
disk.disk_encryption_key.raw_key = encryption_key
59+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
60+
61+
wait_for_extended_operation(operation, "disk creation")
62+
63+
return disk_client.get(project=project_id, zone=zone, disk=disk_name)
64+
# </INGREDIENT>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2022 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_disk_from_kms_encrypted_disk>
24+
def create_disk_from_kms_encrypted_disk(
25+
project_id: str, zone: str, disk_name: str, disk_type: str,
26+
disk_size_gb: int, disk_link: str,
27+
kms_key_name: str) -> compute_v1.Disk:
28+
"""
29+
Creates a zonal non-boot disk in a project with the copy of data from an existing disk.
30+
31+
The encryption key must be the same for the source disk and the new disk.
32+
33+
Args:
34+
project_id: project ID or project number of the Cloud project you want to use.
35+
zone: name of the zone in which you want to create the disk.
36+
disk_name: name of the disk you want to create.
37+
disk_type: the type of disk you want to create. This value uses the following format:
38+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
39+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
40+
disk_size_gb: size of the new disk in gigabytes
41+
disk_link: a link to the disk you want to use as a source for the new disk.
42+
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
43+
kms_key_name: URL of the key from KMS. The key might be from another project, as
44+
long as you have access to it. The data will be encrypted with the same key
45+
in the new disk. This value uses following format:
46+
"projects/{kms_project_id}/locations/{region}/keyRings/{key_ring}/cryptoKeys/{key}"
47+
48+
Returns:
49+
An attachable copy of an existing disk.
50+
"""
51+
disk_client = compute_v1.DisksClient()
52+
disk = compute_v1.Disk()
53+
disk.zone = zone
54+
disk.size_gb = disk_size_gb
55+
disk.source_disk = disk_link
56+
disk.type_ = disk_type
57+
disk.name = disk_name
58+
disk.disk_encryption_key = compute_v1.CustomerEncryptionKey()
59+
disk.disk_encryption_key.kms_key_name = kms_key_name
60+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
61+
62+
wait_for_extended_operation(operation, "disk creation")
63+
64+
return disk_client.get(project=project_id, zone=zone, disk=disk_name)
65+
# </INGREDIENT>

compute/compute/ingredients/disks/create_from_snapshot.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# folder for complete code samples that are ready to be used.
1717
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1818
# flake8: noqa
19-
import sys
2019

2120

2221
from google.cloud import compute_v1
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2022 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_disk_from_disk>
24+
def create_disk_from_disk(project_id: str, zone: str, disk_name: str, disk_type: str,
25+
disk_size_gb: int, disk_link: str) -> compute_v1.Disk:
26+
"""
27+
Creates a disk in a project in a given zone.
28+
29+
Args:
30+
project_id: project ID or project number of the Cloud project you want to use.
31+
zone: name of the zone in which you want to create the disk.
32+
disk_name: name of the disk you want to create.
33+
disk_type: the type of disk you want to create. This value uses the following format:
34+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
35+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
36+
disk_size_gb: size of the new disk in gigabytes
37+
disk_link: a link to the disk you want to use as a source for the new disk.
38+
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
39+
40+
Returns:
41+
An attachable disk.
42+
"""
43+
disk_client = compute_v1.DisksClient()
44+
disk = compute_v1.Disk()
45+
disk.zone = zone
46+
disk.size_gb = disk_size_gb
47+
disk.source_disk = disk_link
48+
disk.type_ = disk_type
49+
disk.name = disk_name
50+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
51+
52+
wait_for_extended_operation(operation, "disk creation")
53+
54+
return disk_client.get(project=project_id, zone=zone, disk=disk_name)
55+
# </INGREDIENT>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2022 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+
from typing import Optional
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT create_kms_encrypted_disk>
25+
def create_kms_encrypted_disk(project_id: str, zone: str, disk_name: str, disk_type: str,
26+
disk_size_gb: int, kms_key_name: str,
27+
disk_link: Optional[str] = None, image_link: Optional[str] = None) -> compute_v1.Disk:
28+
"""
29+
Creates a zonal disk in a project. If you do not provide values for disk_link or image_link,
30+
an empty disk will be created.
31+
32+
Args:
33+
project_id: project ID or project number of the Cloud project you want to use.
34+
zone: name of the zone in which you want to create the disk.
35+
disk_name: name of the disk you want to create.
36+
disk_type: the type of disk you want to create. This value uses the following format:
37+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
38+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
39+
disk_size_gb: size of the new disk in gigabytes
40+
kms_key_name: URL of the key from KMS. The key might be from another project, as
41+
long as you have access to it. The data will be encrypted with the same key
42+
in the new disk. This value uses following format:
43+
"projects/{kms_project_id}/locations/{region}/keyRings/{key_ring}/cryptoKeys/{key}"
44+
disk_link: a link to the disk you want to use as a source for the new disk.
45+
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
46+
image_link: a link to the image you want to use as a source for the new disk.
47+
This value uses the following format: "projects/{project_name}/global/images/{image_name}"
48+
49+
Returns:
50+
An attachable disk.
51+
"""
52+
disk_client = compute_v1.DisksClient()
53+
disk = compute_v1.Disk()
54+
disk.zone = zone
55+
disk.size_gb = disk_size_gb
56+
if disk_link:
57+
disk.source_disk = disk_link
58+
if image_link:
59+
disk.source_image = image_link
60+
disk.type_ = disk_type
61+
disk.name = disk_name
62+
disk.disk_encryption_key = compute_v1.CustomerEncryptionKey()
63+
disk.disk_encryption_key.kms_key_name = kms_key_name
64+
operation = disk_client.insert(project=project_id, zone=zone, disk_resource=disk)
65+
66+
wait_for_extended_operation(operation, "disk creation")
67+
68+
return disk_client.get(project=project_id, zone=zone, disk=disk_name)
69+
70+
# </INGREDIENT>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2022 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+
from typing import Iterable, Optional
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT create_regional_disk_from_disk>
25+
def create_regional_disk(project_id: str, region: str, replica_zones: Iterable[str],
26+
disk_name: str, disk_type: str,
27+
disk_size_gb: int,
28+
disk_link: Optional[str] = None,
29+
snapshot_link: Optional[str] = None) -> compute_v1.Disk:
30+
"""
31+
Creates a regional disk from an existing zonal disk in a given project.
32+
33+
Args:
34+
project_id: project ID or project number of the Cloud project you want to use.
35+
region: name of the region in which you want to create the disk.
36+
replica_zones: an iterable collection of zone names in which you want to keep
37+
the new disks' replicas. One of the replica zones of the clone must match
38+
the zone of the source disk.
39+
disk_name: name of the disk you want to create.
40+
disk_type: the type of disk you want to create. This value uses the following format:
41+
"regions/{region}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
42+
For example: "regions/us-west3/diskTypes/pd-ssd"
43+
disk_size_gb: size of the new disk in gigabytes
44+
disk_link: a link to the disk you want to use as a source for the new disk.
45+
This value uses the following format: "projects/{project_name}/zones/{zone}/disks/{disk_name}"
46+
snapshot_link: a link to the snapshot you want to use as a source for the new disk.
47+
This value uses the following format: "projects/{project_name}/global/snapshots/{snapshot_name}"
48+
49+
Returns:
50+
An attachable regional disk.
51+
"""
52+
disk_client = compute_v1.RegionDisksClient()
53+
disk = compute_v1.Disk()
54+
disk.replica_zones = replica_zones
55+
disk.size_gb = disk_size_gb
56+
if disk_link:
57+
disk.source_disk = disk_link
58+
if snapshot_link:
59+
disk.source_snapshot = snapshot_link
60+
disk.type_ = disk_type
61+
disk.region = region
62+
disk.name = disk_name
63+
operation = disk_client.insert(project=project_id, region=region, disk_resource=disk)
64+
65+
wait_for_extended_operation(operation, "disk creation")
66+
67+
return disk_client.get(project=project_id, region=region, disk=disk_name)
68+
# </INGREDIENT>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2022 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+
import sys
20+
from typing import NoReturn
21+
22+
from google.cloud import compute_v1
23+
24+
25+
# <INGREDIENT delete_regional_disk>
26+
def delete_regional_disk(project_id: str, region: str, disk_name: str) -> NoReturn:
27+
"""
28+
Deletes a disk from a project.
29+
30+
Args:
31+
project_id: project ID or project number of the Cloud project you want to use.
32+
region:name of the region where the disk is located.
33+
disk_name: name of the disk that you want to delete.
34+
"""
35+
disk_client = compute_v1.RegionDisksClient()
36+
operation = disk_client.delete(project=project_id, region=region, disk=disk_name)
37+
wait_for_extended_operation(operation, "regional disk deletion")
38+
return
39+
# </INGREDIENT>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2022 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_disk_clone_encrypted_disk>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_disk_from_customer_encrypted_disk />
22+
23+
# </REGION compute_disk_clone_encrypted_disk>

0 commit comments

Comments
 (0)