Content-Length: 1163743 | pFad | https://github.com/googleapis/python-compute/commit/9d22f0d7fa14eea52d17c1df4d55358e747f0831

D7 docs(samples): Add samples for moving VM to different regions/zones (… · googleapis/python-compute@9d22f0d · GitHub
Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 9d22f0d

Browse files
docs(samples): Add samples for moving VM to different regions/zones (#244)
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 05ddcf7 commit 9d22f0d

31 files changed

+745
-142
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT create_empty_disk>
25+
def create_empty_disk(
26+
project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int
27+
) -> compute_v1.Disk:
28+
"""
29+
Creates a new empty disk in a project in given zone.
30+
31+
Args:
32+
project_id: project ID or project number of the Cloud project you want to use.
33+
zone: name of the zone in which you want to create the disk.
34+
disk_name: name of the disk you want to create.
35+
disk_type: the type of disk you want to create. This value uses the following format:
36+
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)".
37+
For example: "zones/us-west3-b/diskTypes/pd-ssd"
38+
disk_size_gb: size of the new disk in gigabytes
39+
40+
Returns:
41+
An unattached Disk instance.
42+
"""
43+
disk = compute_v1.Disk()
44+
disk.size_gb = disk_size_gb
45+
disk.name = disk_name
46+
disk.zone = zone
47+
disk.type_ = disk_type
48+
49+
disk_client = compute_v1.DisksClient()
50+
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk)
51+
operation_client = compute_v1.ZoneOperationsClient()
52+
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name)
53+
54+
if operation.error:
55+
print("Error during disk creation:", operation.error, file=sys.stderr)
56+
raise RuntimeError(operation.error)
57+
if operation.warnings:
58+
print("Warnings during disk creation:\n", file=sys.stderr)
59+
for warning in operation.warnings:
60+
print(f" - {warning.code}: {warning.message}", file=sys.stderr)
61+
62+
return disk_client.get(project=project_id, zone=zone, disk=disk.name)
63+
# </INGREDIENT>

samples/ingredients/disks/disk_from_snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# <INGREDIENT disk_from_snapshot>
2323
def disk_from_snapshot(
24-
disk_type: str, disk_size_gb: int, boot: bool, source_snapshot: str, auto_delete: bool = False
24+
disk_type: str, disk_size_gb: int, boot: bool, source_snapshot: str, auto_delete: bool = True
2525
) -> compute_v1.AttachedDisk():
2626
"""
2727
Create an AttachedDisk object to be used in VM instance creation. Uses a disk snapshot as the

samples/ingredients/disks/empty_disk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
# <INGREDIENT empty_disk>
23-
def empty_disk(disk_type: str, disk_size_gb: int, boot: bool = False, auto_delete: bool = False) -> compute_v1.AttachedDisk():
23+
def empty_disk(disk_type: str, disk_size_gb: int, boot: bool = False, auto_delete: bool = True) -> compute_v1.AttachedDisk():
2424
"""
2525
Create an AttachedDisk object to be used in VM instance creation. The created disk contains
2626
no data and requires formatting before it can be used.
@@ -43,7 +43,7 @@ def empty_disk(disk_type: str, disk_size_gb: int, boot: bool = False, auto_delet
4343
disk.initialize_params = initialize_params
4444
# Remember to set auto_delete to True if you want the disk to be deleted when you delete
4545
# your VM instance.
46-
disk.auto_delete = True
47-
disk.boot = False
46+
disk.auto_delete = auto_delete
47+
disk.boot = boot
4848
return disk
4949
# </INGREDIENT>

samples/ingredients/disks/from_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# <INGREDIENT disk_from_image>
2323
def disk_from_image(
24-
disk_type: str, disk_size_gb: int, boot: bool, source_image: str, auto_delete: bool = False
24+
disk_type: str, disk_size_gb: int, boot: bool, source_image: str, auto_delete: bool = True
2525
) -> compute_v1.AttachedDisk:
2626
"""
2727
Create an AttachedDisk object to be used in VM instance creation. Uses an image as the

samples/ingredients/disks/get.py

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+
# 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, Iterable
21+
22+
from google.cloud import compute_v1
23+
24+
25+
# <INGREDIENT get_disk>
26+
def get_disk(project_id: str, zone: str, disk_name: str) -> compute_v1.Disk:
27+
"""
28+
Gets 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+
zone: name of the zone where the disk exists.
33+
disk_name: name of the disk you want to retrieve.
34+
"""
35+
disk_client = compute_v1.DisksClient()
36+
return disk_client.get(project=project_id, zone=zone, disk=disk_name)
37+
# </INGREDIENT>

samples/ingredients/instances/create_instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,5 @@ def create_instance(
149149
if operation.warnings:
150150
print("Warning during creation:", operation.warnings, file=sys.stderr)
151151
print(f"Instance {instance_name} created.")
152-
return instance
152+
return instance_client.get(project=project_id, zone=zone, instance=instance_name)
153153
# </INGREDIENT>

samples/ingredients/instances/create_start_instance/create_from_custom_image.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
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-
#
201
# Licensed under the Apache License, Version 2.0 (the "License");
212
# you may not use this file except in compliance with the License.
223
# You may obtain a copy of the License at
@@ -55,7 +36,7 @@ def create_from_custom_image(
5536
Instance object.
5637
"""
5738
disk_type = f"zones/{zone}/diskTypes/pd-standard"
58-
disks = [disk_from_image(disk_type, 10, True, custom_image_link)]
39+
disks = [disk_from_image(disk_type, 10, True, custom_image_link, True)]
5940
instance = create_instance(project_id, zone, instance_name, disks)
6041
return instance
6142
# </INGREDIENT>

samples/ingredients/instances/create_start_instance/create_from_public_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def create_from_public_image(project_id: str, zone: str, instance_name: str) ->
3636
project="debian-cloud", family="debian-10"
3737
)
3838
disk_type = f"zones/{zone}/diskTypes/pd-standard"
39-
disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link)]
39+
disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link, True)]
4040
instance = create_instance(project_id, zone, instance_name, disks)
4141
return instance
4242
# </INGREDIENT>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 List
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT create_with_existing_disks>
25+
def create_with_existing_disks(project_id: str, zone: str, instance_name: str, disk_names: List[str]) -> compute_v1.Instance:
26+
"""
27+
Create a new VM instance using selected disks. The first disk in disk_names will
28+
be used as boot disk.
29+
30+
Args:
31+
project_id: project ID or project number of the Cloud project you want to use.
32+
zone: name of the zone to create the instance in. For example: "us-west3-b"
33+
instance_name: name of the new virtual machine (VM) instance.
34+
disk_names: list of disk names to be attached to the new virtual machine.
35+
First disk in this list will be used as the boot device.
36+
37+
Returns:
38+
Instance object.
39+
"""
40+
assert(len(disk_names) >= 1)
41+
disks = [get_disk(project_id, zone, disk_name) for disk_name in disk_names]
42+
attached_disks = []
43+
for disk in disks:
44+
adisk = compute_v1.AttachedDisk()
45+
adisk.source = disk.self_link
46+
attached_disks.append(adisk)
47+
attached_disks[0].boot = True
48+
instance = create_instance(project_id, zone, instance_name, attached_disks)
49+
return instance
50+
# </INGREDIENT>

samples/ingredients/instances/get.py

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+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT get_instance>
23+
def get_instance(project_id: str, zone: str, instance_name: str) -> compute_v1.Instance:
24+
"""
25+
Get information about a VM instance in the given zone in the specified project.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
zone: name of the zone you want to use. For example: “us-west3-b”
30+
instance_name: name of the VM instance you want to query.
31+
Returns:
32+
An Instance object.
33+
"""
34+
instance_client = compute_v1.InstancesClient()
35+
instance = instance_client.get(project=project_id, zone=zone, instance=instance_name)
36+
37+
return instance
38+
# </INGREDIENT>
39+
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_disk_create_empty_disk>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT create_empty_disk />
20+
21+
# </REGION compute_disk_create_empty_disk>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
17+
# <REGION compute_instances_create_with_existing_disks>
18+
# <IMPORTS/>
19+
20+
# <INGREDIENT get_disk />
21+
22+
23+
# <INGREDIENT create_instance />
24+
25+
26+
# <INGREDIENT create_with_existing_disks />
27+
# </REGION compute_instances_create_with_existing_disks>

samples/recipes/instances/get.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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_instances_get>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT get_instance />
20+
# </REGION compute_instances_get>

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/googleapis/python-compute/commit/9d22f0d7fa14eea52d17c1df4d55358e747f0831

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy