diff --git a/compute/compute/snippets/sample_create_vm.py b/compute/compute/snippets/sample_create_vm.py new file mode 100644 index 000000000000..24331f9d76b4 --- /dev/null +++ b/compute/compute/snippets/sample_create_vm.py @@ -0,0 +1,421 @@ +# Copyright 2021 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. + +import sys +from typing import List + +# [START compute_instances_create_with_subnet] +# [START compute_instances_create_from_image_plus_snapshot_disk] +# [START compute_instances_create_from_snapshot] +# [START compute_instances_create_from_image_plus_empty_disk] +# [START compute_instances_create_from_custom_image] +# [START compute_instances_create_from_image ] +from google.cloud import compute_v1 + + +# [END compute_instances_create_from_image ] +# [END compute_instances_create_from_custom_image] +# [END compute_instances_create_from_image_plus_empty_disk] +# [END compute_instances_create_from_snapshot] +# [END compute_instances_create_from_image_plus_snapshot_disk] +# [END compute_instances_create_with_subnet] + + +# [START compute_instances_create_with_subnet] +# [START compute_instances_create_from_image_plus_snapshot_disk] +# [START compute_instances_create_from_image_plus_empty_disk] +# [START compute_instances_create_from_custom_image] +# [START compute_instances_create_from_image] +def disk_from_image( + disk_type: str, disk_size_gb: int, boot: bool, source_image: str +) -> compute_v1.AttachedDisk: + """ + Create an AttachedDisk object to be used in VM instance creation. Uses an image as the + source for the new disk. + + Args: + 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 + boot: boolean flag indicating whether this disk should be used as a boot disk of an instance + 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: + AttachedDisk object configured to be created using the specified image. + """ + boot_disk = compute_v1.AttachedDisk() + initialize_params = compute_v1.AttachedDiskInitializeParams() + initialize_params.source_image = source_image + initialize_params.disk_size_gb = disk_size_gb + initialize_params.disk_type = disk_type + boot_disk.initialize_params = initialize_params + # Remember to set auto_delete to True if you want the disk to be deleted when you delete + # your VM instance. + boot_disk.auto_delete = True + boot_disk.boot = boot + return boot_disk + + +# [END compute_instances_create_from_image] +# [END compute_instances_create_from_custom_image] +# [END compute_instances_create_from_image_plus_empty_disk] +# [END compute_instances_create_from_image_plus_snapshot_disk] +# [END compute_instances_create_with_subnet] + + +# [START compute_instances_create_from_image_plus_empty_disk] +def empty_disk(disk_type: str, disk_size_gb: int) -> compute_v1.AttachedDisk(): + """ + Create an AttachedDisk object to be used in VM instance creation. The created disk contains + no data and requires formatting before it can be used. + + Args: + 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 + + Returns: + AttachedDisk object configured to be created as an empty disk. + """ + disk = compute_v1.AttachedDisk() + initialize_params = compute_v1.AttachedDiskInitializeParams() + initialize_params.disk_type = disk_type + initialize_params.disk_size_gb = disk_size_gb + disk.initialize_params = initialize_params + # Remember to set auto_delete to True if you want the disk to be deleted when you delete + # your VM instance. + disk.auto_delete = True + disk.boot = False + return disk + + +# [END compute_instances_create_from_image_plus_empty_disk] + + +# [START compute_instances_create_from_image_plus_snapshot_disk] +# [START compute_instances_create_from_snapshot] +def disk_from_snapshot( + disk_type: str, disk_size_gb: int, boot: bool, disk_snapshot: str +) -> compute_v1.AttachedDisk(): + """ + Create an AttachedDisk object to be used in VM instance creation. Uses a disk snapshot as the + source for the new disk. + + Args: + 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 + boot: boolean flag indicating whether this disk should be used as a boot disk of an instance + disk_snapshot: disk snapshot to use when creating this disk. You must have read access to this disk. + This value uses the following format: "projects/{project_name}/global/snapshots/{snapshot_name}" + + Returns: + AttachedDisk object configured to be created using the specified snapshot. + """ + disk = compute_v1.AttachedDisk() + initialize_params = compute_v1.AttachedDiskInitializeParams() + initialize_params.source_snapshot = disk_snapshot + initialize_params.disk_type = disk_type + initialize_params.disk_size_gb = disk_size_gb + disk.initialize_params = initialize_params + # Remember to set auto_delete to True if you want the disk to be deleted when you delete + # your VM instance. + disk.auto_delete = True + disk.boot = boot + return disk + + +# [END compute_instances_create_from_snapshot] +# [END compute_instances_create_from_image_plus_snapshot_disk] + + +# [START compute_instances_create_with_subnet] +# [START compute_instances_create_from_image_plus_snapshot_disk] +# [START compute_instances_create_from_snapshot] +# [START compute_instances_create_from_image_plus_empty_disk] +# [START compute_instances_create_from_custom_image] +# [START compute_instances_create_from_image] +def create_with_disks( + project_id: str, + zone: str, + instance_name: str, + disks: List[compute_v1.AttachedDisk], + machine_type: str = "n1-standard-1", + network_link: str = "global/networks/default", + subnetwork_link: str = None, +) -> compute_v1.Instance: + """ + Send an instance creation request to the Compute Engine API and wait for it to complete. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + machine_type: machine type of the VM being created. This value uses the + following format: "zones/{zone}/machineTypes/{type_name}". + For example: "zones/europe-west3-c/machineTypes/f1-micro" + disks: a list of compute_v1.AttachedDisk objects describing the disks + you want to attach to your new instance. + network_link: name of the network you want the new instance to use. + For example: "global/networks/default" represents the network + named "default", which is created automatically for each project. + subnetwork_link: name of the subnetwork you want the new instance to use. + This value uses the following format: + "regions/{region}/subnetworks/{subnetwork_name}" + Returns: + Instance object. + """ + instance_client = compute_v1.InstancesClient() + operation_client = compute_v1.ZoneOperationsClient() + + # Use the network interface provided in the network_link argument. + network_interface = compute_v1.NetworkInterface() + network_interface.name = network_link + if subnetwork_link: + network_interface.subnetwork = subnetwork_link + + # Collect information into the Instance object. + instance = compute_v1.Instance() + instance.name = instance_name + instance.disks = disks + full_machine_type_name = f"zones/{zone}/machineTypes/{machine_type}" + instance.machine_type = full_machine_type_name + instance.network_interfaces = [network_interface] + + # Shielded Instance settings + # Values presented here are the defaults. + # instance.shielded_instance_config = compute_v1.ShieldedInstanceConfig() + # instance.shielded_instance_config.enable_secure_boot = False + # instance.shielded_instance_config.enable_vtpm = True + # instance.shielded_instance_config.enable_integrity_monitoring = True + + # Prepare the request to insert an instance. + request = compute_v1.InsertInstanceRequest() + request.zone = zone + request.project = project_id + request.instance_resource = instance + + # Wait for the create operation to complete. + print(f"Creating the {instance_name} instance in {zone}...") + + operation = instance_client.insert(request=request) + while operation.status != compute_v1.Operation.Status.DONE: + operation = operation_client.wait( + operation=operation.name, zone=zone, project=project_id + ) + if operation.error: + print("Error during creation:", operation.error, file=sys.stderr) + if operation.warnings: + print("Warning during creation:", operation.warnings, file=sys.stderr) + print(f"Instance {instance_name} created.") + return instance + + +# [END compute_instances_create_from_image] +# [END compute_instances_create_from_custom_image] +# [END compute_instances_create_from_image_plus_empty_disk] +# [END compute_instances_create_from_snapshot] +# [END compute_instances_create_from_image_plus_snapshot_disk] +# [END compute_instances_create_with_subnet] + + +# [START compute_instances_create_from_image] +def create_from_public_image(project_id: str, zone: str, instance_name: str): + """ + Create a new VM instance with Debian 10 operating system. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + + Returns: + Instance object. + """ + image_client = compute_v1.ImagesClient() + # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details + newest_debian = image_client.get_from_family( + project="debian-cloud", family="debian-10" + ) + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link)] + instance = create_with_disks(project_id, zone, instance_name, disks) + return instance + + +# [END compute_instances_create_from_image] + + +# [START compute_instances_create_from_custom_image] +def create_from_custom_image( + project_id: str, zone: str, instance_name: str, custom_image_link: str +): + """ + Create a new VM instance with custom image used as its boot disk. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + custom_image_link: link to the custom image you want to use in the form of: + "projects/{project_name}/global/images/{image_name}" + + Returns: + Instance object. + """ + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [disk_from_image(disk_type, 10, True, custom_image_link)] + instance = create_with_disks(project_id, zone, instance_name, disks) + return instance + + +# [END compute_instances_create_from_custom_image] + + +# [START compute_instances_create_from_image_plus_empty_disk] +def create_with_additional_disk(project_id: str, zone: str, instance_name: str): + """ + Create a new VM instance with Debian 10 operating system and a 11 GB additional + empty disk. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + + Returns: + Instance object. + """ + image_client = compute_v1.ImagesClient() + # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details + newest_debian = image_client.get_from_family( + project="debian-cloud", family="debian-10" + ) + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [ + disk_from_image(disk_type, 10, True, newest_debian.self_link), + empty_disk(disk_type, 11), + ] + instance = create_with_disks(project_id, zone, instance_name, disks) + return instance + + +# [END compute_instances_create_from_image_plus_empty_disk] + + +# [START compute_instances_create_from_snapshot] +def create_from_snapshot( + project_id: str, zone: str, instance_name: str, snapshot_link: str +): + """ + Create a new VM instance with Debian 10 operating system. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + snapshot_link: link to the snapshot you want to use as the source of your + boot disk in the form of: "projects/{project_name}/global/snapshots/{snapshot_name}" + + Returns: + Instance object. + """ + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [disk_from_snapshot(disk_type, 11, True, snapshot_link)] + instance = create_with_disks(project_id, zone, instance_name, disks) + return instance + + +# [END compute_instances_create_from_snapshot] + + +# [START compute_instances_create_from_image_plus_snapshot_disk] +def create_with_snapshotted_data_disk( + project_id: str, zone: str, instance_name: str, snapshot_link: str +): + """ + Create a new VM instance with Debian 10 operating system. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + snapshot_link: link to the snapshot you want to use as the source of your + data disk in the form of: "projects/{project_name}/global/snapshots/{snapshot_name}" + + Returns: + Instance object. + """ + image_client = compute_v1.ImagesClient() + # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details + newest_debian = image_client.get_from_family( + project="debian-cloud", family="debian-10" + ) + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [ + disk_from_image(disk_type, 10, True, newest_debian.self_link), + disk_from_snapshot(disk_type, 11, False, snapshot_link), + ] + instance = create_with_disks(project_id, zone, instance_name, disks) + return instance + + +# [END compute_instances_create_from_image_plus_snapshot_disk] + + +# [START compute_instances_create_with_subnet] +def create_with_subnet( + project_id: str, zone: str, instance_name: str, network_link: str, subnet_link: str +): + """ + Create a new VM instance with Debian 10 operating system in specified network and subnetwork. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + network_link: name of the network you want the new instance to use. + For example: "global/networks/default" represents the network + named "default", which is created automatically for each project. + subnetwork_link: name of the subnetwork you want the new instance to use. + This value uses the following format: + "regions/{region}/subnetworks/{subnetwork_name}" + + Returns: + Instance object. + """ + image_client = compute_v1.ImagesClient() + # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details + newest_debian = image_client.get_from_family( + project="debian-cloud", family="debian-10" + ) + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link)] + instance = create_with_disks( + project_id, + zone, + instance_name, + disks, + network_link=network_link, + subnetwork_link=subnet_link, + ) + return instance + + +# [END compute_instances_create_with_subnet] diff --git a/compute/compute/snippets/sample_images.py b/compute/compute/snippets/sample_images.py new file mode 100644 index 000000000000..9ea1ab459840 --- /dev/null +++ b/compute/compute/snippets/sample_images.py @@ -0,0 +1,59 @@ +# Copyright 2021 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. + +from typing import Iterable + +# [START compute_images_get] +# [START compute_images_list] +from google.cloud import compute_v1 + +# [END compute_images_list] +# [END compute_images_get] + + +# [START compute_images_list] +def list_images(project_id: str) -> Iterable[compute_v1.Image]: + """ + Retrieve a list of images available in given project. + + Args: + project_id: project ID or project number of the Cloud project you want to list images from. + + Returns: + An iterable collection of compute_v1.Image objects. + """ + image_client = compute_v1.ImagesClient() + return image_client.list(project=project_id) + + +# [END compute_images_list] + + +# [START compute_images_get] +def get_image(project_id: str, image_name: str) -> compute_v1.Image: + """ + Retrieve detailed information about a single image from a project. + + Args: + project_id: project ID or project number of the Cloud project you want to list images from. + image_name: name of the image you want to get details of. + + Returns: + An instance of compute_v1.Image object with information about specified image. + """ + image_client = compute_v1.ImagesClient() + return image_client.get(project=project_id, image=image_name) + + +# [END compute_images_get] diff --git a/compute/compute/snippets/test_sample_create_vm.py b/compute/compute/snippets/test_sample_create_vm.py new file mode 100644 index 000000000000..b69570f162a2 --- /dev/null +++ b/compute/compute/snippets/test_sample_create_vm.py @@ -0,0 +1,217 @@ +# Copyright 2021 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. +import uuid + +import google.auth +from google.cloud import compute_v1 +import pytest + + +from quickstart import delete_instance, wait_for_operation +from sample_create_vm import ( + create_from_custom_image, + create_from_public_image, + create_from_snapshot, + create_with_additional_disk, + create_with_snapshotted_data_disk, + create_with_subnet, +) + +PROJECT = google.auth.default()[1] +INSTANCE_ZONE = "europe-central2-b" + + +def get_active_debian(): + image_client = compute_v1.ImagesClient() + + return image_client.get_from_family(project="debian-cloud", family="debian-11") + + +@pytest.fixture(scope="class") +def src_disk(request): + disk_client = compute_v1.DisksClient() + + disk = compute_v1.Disk() + disk.source_image = get_active_debian().self_link + disk.name = "test-disk-" + uuid.uuid4().hex[:10] + op = disk_client.insert(project=PROJECT, zone=INSTANCE_ZONE, disk_resource=disk) + + wait_for_operation(op, PROJECT) + disk = disk_client.get(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name) + request.cls.disk = disk + + yield disk + + op = disk_client.delete(project=PROJECT, zone=INSTANCE_ZONE, disk=disk.name) + wait_for_operation(op, PROJECT) + + +@pytest.fixture(scope="class") +def snapshot(request, src_disk): + snapshot_client = compute_v1.SnapshotsClient() + snapshot = compute_v1.Snapshot() + snapshot.name = "test-snap-" + uuid.uuid4().hex[:10] + disk_client = compute_v1.DisksClient() + op = disk_client.create_snapshot( + project=PROJECT, + zone=INSTANCE_ZONE, + disk=src_disk.name, + snapshot_resource=snapshot, + ) + wait_for_operation(op, PROJECT) + + request.cls.snapshot = snapshot_client.get(project=PROJECT, snapshot=snapshot.name) + snapshot = request.cls.snapshot + + yield snapshot + + op = snapshot_client.delete(project=PROJECT, snapshot=snapshot.name) + wait_for_operation(op, PROJECT) + + +@pytest.fixture(scope="class") +def image(request, src_disk): + image_client = compute_v1.ImagesClient() + image = compute_v1.Image() + image.source_disk = src_disk.self_link + image.name = "test-image-" + uuid.uuid4().hex[:10] + op = image_client.insert(project=PROJECT, image_resource=image) + + wait_for_operation(op, PROJECT) + + image = image_client.get(project=PROJECT, image=image.name) + request.cls.image = image + yield image + + op = image_client.delete(project=PROJECT, image=image.name) + wait_for_operation(op, PROJECT) + + +@pytest.fixture() +def subnetwork(): + network_client = compute_v1.NetworksClient() + network = compute_v1.Network() + network.name = "test-network-" + uuid.uuid4().hex[:10] + network.auto_create_subnetworks = True + op = network_client.insert(project=PROJECT, network_resource=network) + wait_for_operation(op, PROJECT) + network = network_client.get(project=PROJECT, network=network.name) + + subnet = compute_v1.Subnetwork() + subnet.name = "test-subnet-" + uuid.uuid4().hex[:10] + subnet.network = network_client.get(project=PROJECT, network=network.name).self_link + subnet.region = "europe-central2" + subnet.ip_cidr_range = "10.0.0.0/20" + subnet_client = compute_v1.SubnetworksClient() + op = subnet_client.insert( + project=PROJECT, region="europe-central2", subnetwork_resource=subnet + ) + wait_for_operation(op, PROJECT) + subnet = subnet_client.get( + project=PROJECT, region="europe-central2", subnetwork=subnet.name + ) + + yield subnet + + op = subnet_client.delete(project=PROJECT, region='europe-central2', subnetwork=subnet.name) + wait_for_operation(op, PROJECT) + + op = network_client.delete(project=PROJECT, network=network.name) + wait_for_operation(op, PROJECT) + + +@pytest.mark.usefixtures("image", "snapshot") +class TestCreation: + def test_create_from_custom_image(self): + instance_name = "i" + uuid.uuid4().hex[:10] + instance = create_from_custom_image( + PROJECT, INSTANCE_ZONE, instance_name, self.image.self_link + ) + try: + assert ( + instance.disks[0].initialize_params.source_image == self.image.self_link + ) + finally: + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) + + def test_create_from_public_image(self): + instance_name = "i" + uuid.uuid4().hex[:10] + instance = create_from_public_image( + PROJECT, + INSTANCE_ZONE, + instance_name, + ) + try: + assert "debian-cloud" in instance.disks[0].initialize_params.source_image + assert "debian-10" in instance.disks[0].initialize_params.source_image + finally: + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) + + def test_create_from_snapshot(self): + instance_name = "i" + uuid.uuid4().hex[:10] + instance = create_from_snapshot( + PROJECT, INSTANCE_ZONE, instance_name, self.snapshot.self_link + ) + try: + assert ( + instance.disks[0].initialize_params.source_snapshot + == self.snapshot.self_link + ) + finally: + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) + + def test_create_with_additional_disk(self): + instance_name = "i" + uuid.uuid4().hex[:10] + instance = create_with_additional_disk(PROJECT, INSTANCE_ZONE, instance_name) + try: + assert any( + disk.initialize_params.disk_size_gb == 11 for disk in instance.disks + ) + assert any( + disk.initialize_params.disk_size_gb == 10 for disk in instance.disks + ) + assert len(instance.disks) == 2 + finally: + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) + + def test_create_with_snapshotted_data_disk(self): + instance_name = "i" + uuid.uuid4().hex[:10] + instance = create_with_snapshotted_data_disk( + PROJECT, INSTANCE_ZONE, instance_name, self.snapshot.self_link + ) + try: + assert any( + disk.initialize_params.disk_size_gb == 11 for disk in instance.disks + ) + assert any( + disk.initialize_params.disk_size_gb == 10 for disk in instance.disks + ) + assert len(instance.disks) == 2 + finally: + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) + + def test_create_with_subnet(self, subnetwork): + instance_name = "i" + uuid.uuid4().hex[:10] + instance = create_with_subnet( + PROJECT, + INSTANCE_ZONE, + instance_name, + subnetwork.network, + subnetwork.self_link, + ) + try: + assert instance.network_interfaces[0].name == subnetwork.network + assert instance.network_interfaces[0].subnetwork == subnetwork.self_link + finally: + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) diff --git a/compute/compute/snippets/test_sample_images.py b/compute/compute/snippets/test_sample_images.py new file mode 100644 index 000000000000..23346c1f8859 --- /dev/null +++ b/compute/compute/snippets/test_sample_images.py @@ -0,0 +1,33 @@ +# Copyright 2021 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. + +from sample_images import get_image, list_images + + +def test_list_images(): + images = list_images("debian-cloud") + for img in images: + assert img.kind == "compute#image" + assert img.self_link.startswith( + "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/" + ) + + +def test_get_image(): + images = list_images("debian-cloud") + image = next(iter(images)) + + image2 = get_image("debian-cloud", image.name) + + assert image == image2