检索指定项目中可用的路由资源列表
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
此示例会列出给定项目中的所有网络路由。路由定义了从 VPC 网络中的虚拟机实例到特定目的地的路径。此目的地可以在 VPC 网络内部或外部。
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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)."]]