List routes in a project

This sample lists all routes in a given project.

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

from __future__ import annotations

from collections.abc import Iterable

from google.cloud import compute_v1


def list_routes(
    project_id: str,
) -> Iterable[compute_v1.Route]:
    """
    Lists routes in project.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.

    Returns:
        An iterable collection of routes found in given project.
    """

    route_client = compute_v1.RoutesClient()
    return route_client.list(project=project_id)

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.