지정된 프로젝트에서 사용할 수 있는 경로 리소스 목록 검색

이 샘플은 지정된 프로젝트의 모든 네트워크 경로를 나열합니다. 경로는 VPC 네트워크의 VM 인스턴스에서 특정 대상까지의 경로를 정의합니다. 이 대상은 VPC 네트워크 내부 또는 외부에 있을 수 있습니다.

코드 샘플

Go

이 샘플을 사용해 보기 전에 Compute Engine 빠른 시작: 클라이언트 라이브러리 사용Go 설정 안내를 따르세요. 자세한 내용은 Compute Engine Go API 참고 문서를 확인하세요.

Compute Engine에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
	"google.golang.org/api/iterator"
)

// listRoutes prints a list of routes created in given project.
func listRoutes(w io.Writer, projectID string) error {
	// projectID := "your_project_id"

	ctx := context.Background()
	client, err := compute.NewRoutesRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewRoutesRESTClient: %w", err)
	}
	defer client.Close()

	req := &computepb.ListRoutesRequest{
		Project: projectID,
	}
	routes := client.List(ctx, req)

	for {
		instance, err := routes.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return err
		}
		fmt.Fprintf(w, "- %s\n", *instance.Name)
	}

	return nil
}

Java

이 샘플을 사용해 보기 전에 Compute Engine 빠른 시작: 클라이언트 라이브러리 사용Java 설정 안내를 따르세요. 자세한 내용은 Compute Engine Java API 참고 문서를 확인하세요.

Compute Engine에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.


import com.google.cloud.compute.v1.ListRoutesRequest;
import com.google.cloud.compute.v1.Route;
import com.google.cloud.compute.v1.RoutesClient;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;

public class ListRoute {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "your-project-id";

    listRoutes(projectId);
  }

  // Lists routes from a project.
  public static List<Route> listRoutes(String projectId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (RoutesClient routesClient = RoutesClient.create()) {
      ListRoutesRequest request = ListRoutesRequest.newBuilder()
              .setProject(projectId)
              .build();

      return Lists.newArrayList(routesClient.list(request).iterateAll());
    }
  }
}

Python

이 샘플을 사용해 보기 전에 Compute Engine 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Compute Engine Python API 참고 문서를 확인하세요.

Compute Engine에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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)

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참고하세요.