-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(samples): Making samples required for moving VM instance docs (#…
…242) Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
82f7b7e
commit 5638466
Showing
74 changed files
with
2,227 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets | ||
# folder for complete code samples that are ready to be used. | ||
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. | ||
# flake8: noqa | ||
import sys | ||
from typing import NoReturn | ||
|
||
|
||
from google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT set_disk_autodelete> | ||
def set_disk_autodelete(project_id: str, zone: str, instance_name: str, disk_name: str, autodelete: bool) -> NoReturn: | ||
""" | ||
Set the autodelete flag of a disk to given value. | ||
Args: | ||
project_id: project ID or project number of the Cloud project you want to use. | ||
zone: name of the zone in which is the disk you want to modify. | ||
instance_name: name of the instance the disk is attached to. | ||
disk_name: the name of the disk which flag you want to modify. | ||
autodelete: the new value of the autodelete flag. | ||
""" | ||
instance_client = compute_v1.InstancesClient() | ||
instance = instance_client.get(project=project_id, zone=zone, instance=instance_name) | ||
|
||
for disk in instance.disks: | ||
if disk.device_name == disk_name: | ||
break | ||
else: | ||
raise RuntimeError(f"Instance {instance_name} doesn't have a disk named {disk_name} attached.") | ||
|
||
disk.auto_delete = autodelete | ||
|
||
operation = instance_client.update_unary(project=project_id, zone=zone, instance=instance_name, instance_resource=instance) | ||
operation_client = compute_v1.ZoneOperationsClient() | ||
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name) | ||
|
||
if operation.error: | ||
print("Error during instance update:", operation.error, file=sys.stderr) | ||
raise RuntimeError(operation.error) | ||
if operation.warnings: | ||
print("Warnings during instance update:\n", file=sys.stderr) | ||
for warning in operation.warnings: | ||
print(f" - {warning.code}: {warning.message}", file=sys.stderr) | ||
return | ||
# </INGREDIENT> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets | ||
# folder for complete code samples that are ready to be used. | ||
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. | ||
# flake8: noqa | ||
import sys | ||
|
||
from google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT create_disk_from_image> | ||
def create_disk_from_image( | ||
project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int, source_image: str | ||
) -> compute_v1.Disk: | ||
""" | ||
Creates a new disk in a project in given zone using an image as base. | ||
Args: | ||
project_id: project ID or project number of the Cloud project you want to use. | ||
zone: name of the zone in which you want to create the disk. | ||
disk_name: name of the disk you want to create. | ||
disk_type: the type of disk you want to create. This value uses the following format: | ||
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". | ||
For example: "zones/us-west3-b/diskTypes/pd-ssd" | ||
disk_size_gb: size of the new disk in gigabytes | ||
source_image: source image to use when creating this disk. You must have read access to this disk. This | ||
can be one of the publicly available images or an image from one of your projects. | ||
This value uses the following format: "projects/{project_name}/global/images/{image_name}" | ||
Returns: | ||
An unattached Disk instance. | ||
""" | ||
disk = compute_v1.Disk() | ||
disk.size_gb = disk_size_gb | ||
disk.name = disk_name | ||
disk.zone = zone | ||
disk.type_ = disk_type | ||
disk.source_image = source_image | ||
|
||
disk_client = compute_v1.DisksClient() | ||
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk) | ||
operation_client = compute_v1.ZoneOperationsClient() | ||
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name) | ||
|
||
if operation.error: | ||
print("Error during disk creation:", operation.error, file=sys.stderr) | ||
raise RuntimeError(operation.error) | ||
if operation.warnings: | ||
print("Warnings during disk creation:\n", file=sys.stderr) | ||
for warning in operation.warnings: | ||
print(f" - {warning.code}: {warning.message}", file=sys.stderr) | ||
|
||
return disk_client.get(project=project_id, zone=zone, disk=disk.name) | ||
# </INGREDIENT> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets | ||
# folder for complete code samples that are ready to be used. | ||
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. | ||
# flake8: noqa | ||
import sys | ||
|
||
|
||
from google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT create_disk_from_snapshot> | ||
def create_disk_from_snapshot(project_id: str, zone: str, disk_name: str, disk_type: str, disk_size_gb: int, snapshot_link: str) -> compute_v1.Disk: | ||
""" | ||
Creates a new disk in a project in given zone. | ||
Args: | ||
project_id: project ID or project number of the Cloud project you want to use. | ||
zone: name of the zone in which you want to create the disk. | ||
disk_name: name of the disk you want to create. | ||
disk_type: the type of disk you want to create. This value uses the following format: | ||
"zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". | ||
For example: "zones/us-west3-b/diskTypes/pd-ssd" | ||
disk_size_gb: size of the new disk in gigabytes | ||
snapshot_link: a link to the snapshot you want to use as a source for the new disk. | ||
This value uses the following format: "projects/{project_name}/global/snapshots/{snapshot_name}" | ||
Returns: | ||
An unattached Disk instance. | ||
""" | ||
disk_client = compute_v1.DisksClient() | ||
disk = compute_v1.Disk() | ||
disk.zone = zone | ||
disk.size_gb = disk_size_gb | ||
disk.source_snapshot = snapshot_link | ||
disk.type_ = disk_type | ||
disk.name = disk_name | ||
operation = disk_client.insert_unary(project=project_id, zone=zone, disk_resource=disk) | ||
operation_client = compute_v1.ZoneOperationsClient() | ||
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name) | ||
|
||
if operation.error: | ||
print("Error during disk creation:", operation.error, file=sys.stderr) | ||
raise RuntimeError(operation.error) | ||
|
||
if operation.warnings: | ||
print("Warnings during disk creation:\n", file=sys.stderr) | ||
for warning in operation.warnings: | ||
print(f" - {warning.code}: {warning.message}", file=sys.stderr) | ||
|
||
return disk_client.get(project=project_id, zone=zone, disk=disk_name) | ||
# </INGREDIENT> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets | ||
# folder for complete code samples that are ready to be used. | ||
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. | ||
# flake8: noqa | ||
import sys | ||
from typing import NoReturn | ||
|
||
from google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT delete_disk> | ||
def delete_disk(project_id: str, zone: str, disk_name: str) -> NoReturn: | ||
""" | ||
Deletes a disk from a project. | ||
Args: | ||
project_id: project ID or project number of the Cloud project you want to use. | ||
zone: name of the zone in which is the disk you want to delete. | ||
disk_name: name of the disk you want to delete. | ||
""" | ||
disk_client = compute_v1.DisksClient() | ||
operation = disk_client.delete_unary(project=project_id, zone=zone, disk=disk_name) | ||
operation_client = compute_v1.ZoneOperationsClient() | ||
operation = operation_client.wait(project=project_id, zone=zone, operation=operation.name) | ||
|
||
if operation.error: | ||
print("Error during disk delete operation:", operation.error, file=sys.stderr) | ||
raise RuntimeError(operation.error) | ||
if operation.warnings: | ||
print("Warnings during disk delete operation:\n", file=sys.stderr) | ||
for warning in operation.warnings: | ||
print(f" - {warning.code}: {warning.message}", file=sys.stderr) | ||
return | ||
# </INGREDIENT> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets | ||
# folder for complete code samples that are ready to be used. | ||
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. | ||
# flake8: noqa | ||
import sys | ||
from typing import NoReturn, Iterable | ||
|
||
from google.cloud import compute_v1 | ||
|
||
|
||
# <INGREDIENT list_disks> | ||
def list_disks(project_id: str, zone: str, filter_: str = "") -> Iterable[compute_v1.Disk]: | ||
""" | ||
Deletes a disk from a project. | ||
Args: | ||
project_id: project ID or project number of the Cloud project you want to use. | ||
zone: name of the zone in which is the disk you want to delete. | ||
filter_: filter to be applied when listing disks. Learn more about filters here: | ||
https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListDisksRequest | ||
""" | ||
disk_client = compute_v1.DisksClient() | ||
request = compute_v1.ListDisksRequest() | ||
request.project = project_id | ||
request.zone = zone | ||
request.filter = filter_ | ||
return disk_client.list(request) | ||
# </INGREDIENT> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.