检索指定项目中可用的路由资源列表

此示例会列出给定项目中的所有网络路由。 路由用于定义从 VPC 网络中的虚拟机实例到特定目的地的路径。此目标可以位于 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 示例浏览器