|
16 | 16 | # folder for complete code samples that are ready to be used.
|
17 | 17 | # Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
|
18 | 18 | # flake8: noqa
|
19 |
| - |
| 19 | +from typing import Optional |
20 | 20 |
|
21 | 21 | from google.cloud import compute_v1
|
22 | 22 |
|
23 | 23 |
|
24 | 24 | # <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: |
26 | 28 | """
|
27 | 29 | Create a snapshot of a disk.
|
28 | 30 |
|
| 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 | +
|
29 | 35 | 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. |
32 | 38 | disk_name: name of the disk you want to snapshot.
|
33 | 39 | 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. |
34 | 49 |
|
35 | 50 | Returns:
|
36 | 51 | The new snapshot instance.
|
37 | 52 | """
|
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 | + |
40 | 68 | snapshot = compute_v1.Snapshot()
|
41 | 69 | snapshot.source_disk = disk.self_link
|
42 | 70 | snapshot.name = snapshot_name
|
| 71 | + if location: |
| 72 | + snapshot.storage_locations = [location] |
43 | 73 |
|
44 | 74 | snapshot_client = compute_v1.SnapshotsClient()
|
45 | 75 | operation = snapshot_client.insert(project=project_id, snapshot_resource=snapshot)
|
|
0 commit comments