지정된 프로젝트에서 사용할 수 있는 경로 리소스 목록 검색
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
이 샘플은 지정된 프로젝트의 모든 네트워크 경로를 나열합니다.
경로는 VPC 네트워크의 VM 인스턴스에서 특정 대상까지의 경로를 정의합니다. 이 대상은 VPC 네트워크 내부 또는 외부에 있을 수 있습니다.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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,["This 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\nGo\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\nJava\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\nPython\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\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]