Skip to content

Commit accf778

Browse files
m-strzelczykdandhlee
authored andcommitted
docs(samples): Improving snapshot samples (#276)
Improving the code snippets for disk snapshots, so they can be used on this page: [Create and manage disk snapshots ](https://cloud.google.com/compute/docs/disks/create-snapshots)
1 parent 1542371 commit accf778

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+563
-167
lines changed

compute/compute/ingredients/operations/handle_extended_operation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ def wait_for_extended_operation(
5555
result = operation.result(timeout=timeout)
5656

5757
if operation.error_code:
58-
print(f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", file=sys.stderr)
59-
print(f"Operation ID: {operation.name}")
58+
print(f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
59+
file=sys.stderr, flush=True)
60+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6061
raise operation.exception() or RuntimeError(operation.error_message)
6162

6263
if operation.warnings:
63-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
64+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
6465
for warning in operation.warnings:
65-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
66+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
6667

6768
return result
6869
# </INGREDIENT>

compute/compute/ingredients/snapshots/create.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,60 @@
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-
19+
from typing import Optional
2020

2121
from google.cloud import compute_v1
2222

2323

2424
# <INGREDIENT create_snapshot>
25-
def create_snapshot(project_id: str, zone: str, disk_name: str, snapshot_name: str) -> compute_v1.Snapshot:
25+
def create_snapshot(project_id: str, disk_name: str, snapshot_name: str, *,
26+
zone: Optional[str] = None, region: Optional[str] = None,
27+
location: Optional[str] = None, disk_project_id: Optional[str] = None) -> compute_v1.Snapshot:
2628
"""
2729
Create a snapshot of a disk.
2830
31+
You need to pass `zone` or `region` parameter relevant to the disk you want to
32+
snapshot, but not both. Pass `zone` parameter for zonal disks and `region` for
33+
regional disks.
34+
2935
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 is the disk you want to snapshot.
36+
project_id: project ID or project number of the Cloud project you want
37+
to use to store the snapshot.
3238
disk_name: name of the disk you want to snapshot.
3339
snapshot_name: name of the snapshot to be created.
40+
zone: name of the zone in which is the disk you want to snapshot (for zonal disks).
41+
region: name of the region in which is the disk you want to snapshot (for regional disks).
42+
location: The Cloud Storage multi-region or the Cloud Storage region where you
43+
want to store your snapshot.
44+
You can specify only one storage location. Available locations:
45+
https://cloud.google.com/storage/docs/locations#available-locations
46+
disk_project_id: project ID or project number of the Cloud project that
47+
hosts the disk you want to snapshot. If not provided, will look for
48+
the disk in the `project_id` project.
3449
3550
Returns:
3651
The new snapshot instance.
3752
"""
38-
disk_client = compute_v1.DisksClient()
39-
disk = disk_client.get(project=project_id, zone=zone, disk=disk_name)
53+
if zone is None and region is None:
54+
raise RuntimeError("You need to specify `zone` or `region` for this function to work.")
55+
if zone is not None and region is not None:
56+
raise RuntimeError("You can't set both `zone` and `region` parameters.")
57+
58+
if disk_project_id is None:
59+
disk_project_id = project_id
60+
61+
if zone is not None:
62+
disk_client = compute_v1.DisksClient()
63+
disk = disk_client.get(project=disk_project_id, zone=zone, disk=disk_name)
64+
else:
65+
regio_disk_client = compute_v1.RegionDisksClient()
66+
disk = regio_disk_client.get(project=disk_project_id, region=region, disk=disk_name)
67+
4068
snapshot = compute_v1.Snapshot()
4169
snapshot.source_disk = disk.self_link
4270
snapshot.name = snapshot_name
71+
if location:
72+
snapshot.storage_locations = [location]
4373

4474
snapshot_client = compute_v1.SnapshotsClient()
4575
operation = snapshot_client.insert(project=project_id, snapshot_resource=snapshot)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT get_snapshot>
25+
def get_snapshot(project_id: str, snapshot_name: str) -> compute_v1.Snapshot:
26+
"""
27+
Get information about a Snapshot.
28+
29+
Args:
30+
project_id: project ID or project number of the Cloud project you want to use.
31+
snapshot_name: the name of the snapshot you want to look up.
32+
33+
Returns:
34+
A Snapshot object.
35+
"""
36+
37+
snapshot_client = compute_v1.SnapshotsClient()
38+
39+
return snapshot_client.get(project=project_id, snapshot=snapshot_name)
40+
# </INGREDIENT>
41+
42+

compute/compute/ingredients/snapshots/list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323

2424
# <INGREDIENT list_snapshots>
25-
def list_snapshots(project_id: str, filter_: str = "") -> Iterable[compute_v1.Snapshot]:
25+
def list_snapshots(project_id: str, filter: str = "") -> Iterable[compute_v1.Snapshot]:
2626
"""
2727
List snapshots from a project.
2828
2929
Args:
3030
project_id: project ID or project number of the Cloud project you want to use.
31-
filter_: filter to be applied when listing snapshots. Learn more about filters here:
31+
filter: filter to be applied when listing snapshots. Learn more about filters here:
3232
https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest
3333
3434
Returns:
@@ -38,7 +38,7 @@ def list_snapshots(project_id: str, filter_: str = "") -> Iterable[compute_v1.Sn
3838
snapshot_client = compute_v1.SnapshotsClient()
3939
request = compute_v1.ListSnapshotsRequest()
4040
request.project = project_id
41-
request.filter = filter_
41+
request.filter = filter
4242

4343
return snapshot_client.list(request)
4444
# </INGREDIENT>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# flake8: noqa
16+
17+
# <REGION compute_snapshot_delete_by_filter>
18+
# <IMPORTS/>
19+
20+
# <INGREDIENT wait_for_extended_operation />
21+
22+
# <INGREDIENT delete_snapshot />
23+
24+
# <INGREDIENT list_snapshots />
25+
26+
def delete_snapshots_by_filter(project_id: str, filter: str):
27+
"""
28+
Deletes all snapshots in project that meet the filter criteria.
29+
30+
Args:
31+
project_id: project ID or project number of the Cloud project you want to use.
32+
filter: filter to be applied when looking for snapshots for deletion.
33+
"""
34+
for snapshot in list_snapshots(project_id, filter):
35+
delete_snapshot(project_id, snapshot.name)
36+
37+
# </REGION compute_snapshot_delete_by_filter>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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_snapshot_get>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT get_snapshot />
20+
21+
# </REGION compute_snapshot_get>

compute/compute/snippets/disks/autodelete_change.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/disks/create_empty_disk.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/disks/create_from_image.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/disks/create_from_snapshot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/disks/delete.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/firewall/create.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/firewall/delete.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/firewall/patch.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def wait_for_extended_operation(
6161
print(
6262
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6363
file=sys.stderr,
64+
flush=True,
6465
)
65-
print(f"Operation ID: {operation.name}")
66+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6667
raise operation.exception() or RuntimeError(operation.error_message)
6768

6869
if operation.warnings:
69-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
70+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7071
for warning in operation.warnings:
71-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
72+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7273

7374
return result
7475

compute/compute/snippets/images/create.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ def wait_for_extended_operation(
6363
print(
6464
f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
6565
file=sys.stderr,
66+
flush=True,
6667
)
67-
print(f"Operation ID: {operation.name}")
68+
print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
6869
raise operation.exception() or RuntimeError(operation.error_message)
6970

7071
if operation.warnings:
71-
print(f"Warnings during {verbose_name}:\n", file=sys.stderr)
72+
print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
7273
for warning in operation.warnings:
73-
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
74+
print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)
7475

7576
return result
7677

0 commit comments

Comments
 (0)