Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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 and data disk created from snapshot.

    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
Ejemplo n.º 4
0
def print_images_list_by_page(project: str, page_size: int = 10) -> None:
    """
    Prints a list of all non-deprecated image names available in a given project,
    divided into pages as returned by the Compute Engine API.

    Args:
        project: project ID or project number of the Cloud project you want to list images from.
        page_size: size of the pages you want the API to return on each call.

    Returns:
        None.
    """
    images_client = compute_v1.ImagesClient()
    # Listing only non-deprecated images to reduce the size of the reply.
    images_list_request = compute_v1.ListImagesRequest(
        project=project,
        max_results=page_size,
        filter="deprecated.state != DEPRECATED")

    # Use the `pages` attribute of returned iterable to have more granular control of
    # iteration over paginated results from the API. Each time you want to access the
    # next page, the library retrieves that page from the API.
    for page_num, page in enumerate(
            images_client.list(request=images_list_request).pages, start=1):
        print(f"Page {page_num}: ")
        for img in page.items:
            print(f" - {img.name}")
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
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_unary(project=PROJECT, image_resource=image)

    wait_for_operation(op, PROJECT)
    try:
        image = image_client.get(project=PROJECT, image=image.name)
        request.cls.image = image
        yield image
    finally:
        op = image_client.delete_unary(project=PROJECT, image=image.name)
        wait_for_operation(op, PROJECT)
def test_create_instance_from_template_override(instance_template,
                                                autodelete_instance_name):
    image_client = compute_v1.ImagesClient()

    image = image_client.get_from_family(project="ubuntu-os-cloud",
                                         family="ubuntu-2004-lts")
    instance = create_instance_from_template_with_overrides(
        PROJECT,
        INSTANCE_ZONE,
        autodelete_instance_name,
        instance_template.name,
        f"zones/{INSTANCE_ZONE}/machineTypes/n2-standard-2",
        image.self_link,
    )

    assert instance.name == autodelete_instance_name
    assert instance.machine_type.endswith("n2-standard-2")
    assert len(instance.disks) == 2
def print_images_list(project: str) -> None:
    """
    Prints a list of all non-deprecated image names available in given project.

    Args:
        project: project ID or project number of the Cloud project you want to list images from.

    Returns:
        None.
    """
    images_client = compute_v1.ImagesClient()
    # Listing only non-deprecated images to reduce the size of the reply.
    images_list_request = compute_v1.ListImagesRequest(project=project, max_results=100,
                                                       filter="deprecated.state != DEPRECATED")

    # Although the `max_results` parameter is specified in the request, the iterable returned
    # by the `list()` method hides the pagination mechanic. The library makes multiple
    # requests to the API for you, so you can simply iterate over all the images.
    for img in images_client.list(request=images_list_request):
        print(f" -  {img.name}")
def get_active_debian():
    image_client = compute_v1.ImagesClient()

    return image_client.get_from_family(project="debian-cloud",
                                        family="debian-11")