擷取指定專案中可用的路線資源清單
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
這個範例會列出指定專案中的所有網路路徑。路徑會定義從虛擬私有雲網路中的 VM 執行個體到特定目的地的路徑。目的地可位於虛擬私有雲網路內或外部。
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis document provides code samples in Go, Java, and Python for listing all network routes within a specified Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eA network route defines a path from Virtual Machine (VM) instances in a Virtual Private Cloud (VPC) network to a destination, which can be internal or external to the VPC.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample requires setting up Application Default Credentials (ADC) for Compute Engine authentication and includes links to the respective API reference documentation and quickstart guides for each language.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code demonstrates how to utilize the Compute Engine API clients in each language to retrieve and display a list of routes associated with the given project ID.\u003c/p\u003e\n"]]],[],null,["# Retrieves the list of Route resources available in the specified project\n\nThis sample lists all network routes in a given project.\nA route defines a path from VM instances in the VPC network to a specific destination. This destination can be inside or outside the VPC network.\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \tcomputepb \"cloud.google.com/go/compute/apiv1/computepb\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // listRoutes prints a list of routes created in given project.\n func listRoutes(w io.Writer, projectID string) error {\n \t// projectID := \"your_project_id\"\n\n \tctx := context.Background()\n \tclient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_RoutesClient_NewRoutesRESTClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewRoutesRESTClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \treq := &computepb.ListRoutesRequest{\n \t\tProject: projectID,\n \t}\n \troutes := client.List(ctx, req)\n\n \tfor {\n \t\tinstance, err := routes.Next()\n \t\tif err == iterator.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_Operation_Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"- %s\\n\", *instance.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_Operation_Name)\n \t}\n\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListRoutesRequest.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Route.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.RoutesClient.html;\n import com.google.common.collect.Lists;\n import java.io.IOException;\n import java.util.List;\n\n public class ListRoute {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // Project ID or project number of the Cloud project you want to use.\n String projectId = \"your-project-id\";\n\n listRoutes(projectId);\n }\n\n // Lists routes from a project.\n public static List\u003cRoute\u003e listRoutes(String projectId) throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.RoutesClient.html routesClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.RoutesClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListRoutesRequest.html request = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListRoutesRequest.html.newBuilder()\n .setProject(projectId)\n .build();\n\n return Lists.newArrayList(routesClient.list(request).iterateAll());\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from __future__ import annotations\n\n from collections.abc import Iterable\n\n from google.cloud import compute_v1\n\n\n def list_routes(\n project_id: str,\n ) -\u003e Iterable[compute_v1.Route]:\n \"\"\"\n Lists routes in project.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n\n Returns:\n An iterable collection of routes found in given project.\n \"\"\"\n\n route_client = compute_v1.RoutesClient()\n return route_client.list(project=project_id)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]