Mengambil daftar resource Route yang tersedia di project yang ditentukan
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Contoh ini mencantumkan semua rute jaringan dalam project tertentu.
Rute menentukan jalur dari instance VM di jaringan VPC ke tujuan tertentu. Tujuan ini dapat berada di dalam atau di luar jaringan VPC.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]