Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 35c548e

Browse files
authored
docs(samples): Adding samples for image related operations (#277)
Samples for the [Create, delete, and deprecate custom images](https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images) page.
1 parent ea286db commit 35c548e

12 files changed

+699
-12
lines changed

samples/ingredients/images/create.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
# folder for complete code samples that are ready to be used.
1818
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1919
# flake8: noqa
20-
import time
2120

22-
from google.cloud import compute_v1
2321
import warnings
22+
from typing import Optional
23+
24+
from google.cloud import compute_v1
2425

2526
# <INGREDIENT create_image>
2627
STOPPED_MACHINE_STATUS = (
@@ -29,8 +30,8 @@
2930
)
3031

3132

32-
def create_image(project_id: str, zone: str, source_disk_name: str, image_name: str,
33-
storage_location: str = None, force_create: bool = False) -> compute_v1.Image:
33+
def create_image_from_disk(project_id: str, zone: str, source_disk_name: str, image_name: str,
34+
storage_location: Optional[str] = None, force_create: bool = False) -> compute_v1.Image:
3435
"""
3536
Creates a new disk image.
3637
@@ -44,6 +45,9 @@ def create_image(project_id: str, zone: str, source_disk_name: str, image_name:
4445
source location.
4546
force_create: create the image even if the source disk is attached to a
4647
running instance.
48+
49+
Returns:
50+
An Image object.
4751
"""
4852
image_client = compute_v1.ImagesClient()
4953
disk_client = compute_v1.DisksClient()
@@ -77,7 +81,7 @@ def create_image(project_id: str, zone: str, source_disk_name: str, image_name:
7781

7882
operation = image_client.insert(project=project_id, image_resource=image)
7983

80-
wait_for_extended_operation(operation, "image creation")
84+
wait_for_extended_operation(operation, "image creation from disk")
8185

8286
return image_client.get(project=project_id, image=image_name)
8387
# </INGREDIENT>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
16+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
17+
# folder for complete code samples that are ready to be used.
18+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
19+
# flake8: noqa
20+
from typing import Optional, Iterable
21+
22+
from google.cloud import compute_v1
23+
24+
25+
# <INGREDIENT create_image_from_image>
26+
def create_image_from_image(project_id: str, source_image_name: str, image_name: str,
27+
source_project_id: Optional[str] = None,
28+
guest_os_features: Optional[Iterable[str]] = None,
29+
storage_location: Optional[str] = None) -> compute_v1.Image:
30+
"""
31+
Creates a copy of another image.
32+
33+
Args:
34+
project_id: project ID or project number of the Cloud project you want to place your new image in.
35+
source_image_name: name of the image you want to copy.
36+
image_name: name of the image you want to create.
37+
source_project_id: name of the project that hosts the source image. If left unset, it's assumed to equal
38+
the `project_id`.
39+
guest_os_features: an iterable collection of guest features you want to enable for the bootable image.
40+
Learn more about Guest OS features here:
41+
https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#guest-os-features
42+
storage_location: the storage location of your image. For example, specify "us" to store the image in the
43+
`us` multi-region, or "us-central1" to store it in the `us-central1` region. If you do not make a selection,
44+
Compute Engine stores the image in the multi-region closest to your image's source location.
45+
46+
Returns:
47+
An Image object.
48+
"""
49+
if source_project_id is None:
50+
source_project_id = project_id
51+
52+
image_client = compute_v1.ImagesClient()
53+
src_image = image_client.get(project=source_project_id, image=source_image_name)
54+
55+
image = compute_v1.Image()
56+
image.name = image_name
57+
image.source_image = src_image.self_link
58+
if storage_location:
59+
image.storage_locations = [storage_location]
60+
61+
if guest_os_features:
62+
image.guest_os_features = [compute_v1.GuestOsFeature(type_=feature) for feature in guest_os_features]
63+
64+
operation = image_client.insert(project=project_id, image_resource=image)
65+
66+
wait_for_extended_operation(operation, "image creation from image")
67+
68+
return image_client.get(project=project_id, image=image_name)
69+
# </INGREDIENT>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
16+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
17+
# folder for complete code samples that are ready to be used.
18+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
19+
# flake8: noqa
20+
from typing import Optional, Iterable
21+
22+
from google.cloud import compute_v1
23+
24+
25+
# <INGREDIENT create_image_from_snapshot>
26+
def create_image_from_snapshot(project_id: str, source_snapshot_name: str, image_name: str,
27+
source_project_id: Optional[str] = None,
28+
guest_os_features: Optional[Iterable[str]] = None,
29+
storage_location: Optional[str] = None) -> compute_v1.Image:
30+
"""
31+
Creates an image based on a snapshot.
32+
33+
Args:
34+
project_id: project ID or project number of the Cloud project you want to place your new image in.
35+
source_snapshot_name: name of the snapshot you want to use as a base of your image.
36+
image_name: name of the image you want to create.
37+
source_project_id: name of the project that hosts the source snapshot. If left unset, it's assumed to equal
38+
the `project_id`.
39+
guest_os_features: an iterable collection of guest features you want to enable for the bootable image.
40+
Learn more about Guest OS features here:
41+
https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#guest-os-features
42+
storage_location: the storage location of your image. For example, specify "us" to store the image in the
43+
`us` multi-region, or "us-central1" to store it in the `us-central1` region. If you do not make a selection,
44+
Compute Engine stores the image in the multi-region closest to your image's source location.
45+
46+
Returns:
47+
An Image object.
48+
"""
49+
if source_project_id is None:
50+
source_project_id = project_id
51+
52+
snapshot_client = compute_v1.SnapshotsClient()
53+
image_client = compute_v1.ImagesClient()
54+
src_snapshot = snapshot_client.get(project=source_project_id, snapshot=source_snapshot_name)
55+
56+
image = compute_v1.Image()
57+
image.name = image_name
58+
image.source_snapshot = src_snapshot.self_link
59+
60+
if storage_location:
61+
image.storage_locations = [storage_location]
62+
63+
if guest_os_features:
64+
image.guest_os_features = [compute_v1.GuestOsFeature(type_=feature) for feature in guest_os_features]
65+
66+
operation = image_client.insert(project=project_id, image_resource=image)
67+
68+
wait_for_extended_operation(operation, "image creation from snapshot")
69+
70+
return image_client.get(project=project_id, image=image_name)
71+
# </INGREDIENT>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
16+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
17+
# folder for complete code samples that are ready to be used.
18+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
19+
# flake8: noqa
20+
from typing import NoReturn
21+
22+
from google.cloud import compute_v1
23+
24+
25+
# <INGREDIENT set_deprecation_status>
26+
def set_deprecation_status(project_id: str, image_name: str, status: compute_v1.DeprecationStatus.State) -> NoReturn:
27+
"""
28+
Modify the deprecation status of an image.
29+
30+
Note: Image objects by default don't have the `deprecated` attribute at all unless it's set.
31+
32+
Args:
33+
project_id: project ID or project number of the Cloud project that hosts the image.
34+
image_name: name of the image you want to modify
35+
status: the status you want to set for the image. Available values are available in
36+
`compute_v1.DeprecationStatus.State` enum. Learn more about image deprecation statuses:
37+
https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#deprecation-states
38+
"""
39+
image_client = compute_v1.ImagesClient()
40+
deprecation_status = compute_v1.DeprecationStatus()
41+
deprecation_status.state = status.name
42+
operation = image_client.deprecate(project=project_id, image=image_name,
43+
deprecation_status_resource=deprecation_status)
44+
45+
wait_for_extended_operation(operation, "changing deprecation state of an image")
46+
# </INGREDIENT>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_images_create_from_image>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_image_from_image />
22+
# </REGION compute_images_create_from_image>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_images_create_from_snapshot>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_image_from_snapshot />
22+
# </REGION compute_images_create_from_snapshot>
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_images_set_deprecation_status>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT set_deprecation_status />
22+
# </REGION compute_images_set_deprecation_status>
23+

samples/snippets/images/create.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
# [START compute_windows_image_create]
2323
# [START compute_images_create]
2424
import sys
25-
import time
26-
from typing import Any
25+
from typing import Any, Optional
2726
import warnings
2827

2928
from google.api_core.extended_operation import ExtendedOperation
@@ -82,12 +81,12 @@ def wait_for_extended_operation(
8281
)
8382

8483

85-
def create_image(
84+
def create_image_from_disk(
8685
project_id: str,
8786
zone: str,
8887
source_disk_name: str,
8988
image_name: str,
90-
storage_location: str = None,
89+
storage_location: Optional[str] = None,
9190
force_create: bool = False,
9291
) -> compute_v1.Image:
9392
"""
@@ -103,6 +102,9 @@ def create_image(
103102
source location.
104103
force_create: create the image even if the source disk is attached to a
105104
running instance.
105+
106+
Returns:
107+
An Image object.
106108
"""
107109
image_client = compute_v1.ImagesClient()
108110
disk_client = compute_v1.DisksClient()
@@ -142,7 +144,7 @@ def create_image(
142144

143145
operation = image_client.insert(project=project_id, image_resource=image)
144146

145-
wait_for_extended_operation(operation, "image creation")
147+
wait_for_extended_operation(operation, "image creation from disk")
146148

147149
return image_client.get(project=project_id, image=image_name)
148150

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy