获取项目的NAT网关

huangapple go评论112阅读模式
英文:

GCP - get project NAT GW's

问题

我们在GCP上有一个包含有效云NAT的帐户,现在我们想通过GCP SDK获取这些值。我尝试了以下代码,但是得到了空的响应(可能是因为我使用了错误的API,而不是ListExternalVpnGatewaysRequest)。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. compute "cloud.google.com/go/compute/apiv1"
  6. "google.golang.org/api/iterator"
  7. computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
  8. )
  9. func main() {
  10. ctx := context.Background()
  11. c, err := compute.NewExternalVpnGatewaysRESTClient(ctx)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. defer c.Close()
  16. proj := "dev-proj"
  17. req := &computepb.ListExternalVpnGatewaysRequest{
  18. Project: proj,
  19. }
  20. it := c.List(ctx, req)
  21. for {
  22. resp, err := it.Next()
  23. if err == iterator.Done {
  24. break
  25. }
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. // TODO: Use resp.
  30. _ = resp
  31. fmt.Println(resp)
  32. }
  33. }

我需要使用GCP GO SDK获取以下值:

获取项目的NAT网关

更新

我尝试了原样的代码,但是出现了错误:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/api/compute/v1"
  6. "log"
  7. )
  8. func main() {
  9. project := "my-proj"
  10. region := "my-region"
  11. ctx := context.Background()
  12. computeService, err := compute.New(ctx)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. req := computeService.Routers.List(project, region)
  17. if err := req.Pages(ctx, func(page *compute.RouterList) error {
  18. for _, router := range page.Items {
  19. // 处理每个`router`资源:
  20. fmt.Printf("%#v\n", router)
  21. // NAT Gateways可以在router.nats中找到
  22. }
  23. return nil
  24. }); err != nil {
  25. log.Fatal(err)
  26. }
  27. }
  28. 错误信息为./main.go:16:36: cannot use ctx (type context.Context) as type *http.Client in argument to compute.New
英文:

We have account on GCP which contain valid cloud Nat, now we want to get those values via
GCP sdk, I've tried the following and get empty response (maybe I use the wrong API and it not ListExternalVpnGatewaysRequest)

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. compute "cloud.google.com/go/compute/apiv1"
  6. "google.golang.org/api/iterator"
  7. computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
  8. )
  9. func main() {
  10. ctx := context.Background()
  11. c, err := compute.NewExternalVpnGatewaysRESTClient(ctx)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. defer c.Close()
  16. proj := "dev-proj"
  17. req := &computepb.ListExternalVpnGatewaysRequest{
  18. //Filter: new(string),
  19. //MaxResults: new(uint32),
  20. //OrderBy: new(string),
  21. //PageToken: new(string),
  22. Project: proj,
  23. //ReturnPartialSuccess: new(bool),
  24. }
  25. it := c.List(ctx, req)
  26. for {
  27. resp, err := it.Next()
  28. if err == iterator.Done {
  29. break
  30. }
  31. if err != nil {
  32. fmt.Println(err)
  33. }
  34. // TODO: Use resp.
  35. _ = resp
  36. fmt.Println(resp)
  37. }
  38. }

I need to get the following values using GCP GO SDK

获取项目的NAT网关

update

I tried the following as-is and I got error

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/api/compute/v1"
  6. "log"
  7. )
  8. func main() {
  9. project := "my-proj"
  10. region := "my-region"
  11. ctx := context.Background()
  12. computeService, err := compute.New(ctx)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. req := computeService.Routers.List(project, region)
  17. if err := req.Pages(ctx, func(page *compute.RouterList) error {
  18. for _, router := range page.Items {
  19. // process each `router` resource:
  20. fmt.Printf("%#v\n", router)
  21. // NAT Gateways are found in router.nats
  22. }
  23. return nil
  24. }); err != nil {
  25. log.Fatal(err)
  26. }
  27. }
  28. Error is: ./main.go:16:36: cannot use ctx (type context.Context) as type *http.Client in argument to compute.New

答案1

得分: 2

一个VPN网关与NAT网关不同。

使用以下代码列出路由器。在路由器列表中,可以找到NAT网关。

  1. import "google.golang.org/api/compute/v1"
  2. // 替换为您的项目的有效值
  3. project := "my-project"
  4. region := "my-region"
  5. ctx := context.Background()
  6. c, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
  7. if err != nil {
  8. log.Fatal(err)
  9. }
  10. computeService, err := compute.New(c)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. req := computeService.Routers.List(project, region)
  15. if err := req.Pages(ctx, func(page *compute.RouterList) error {
  16. for _, router := range page.Items {
  17. // 处理每个“router”资源:
  18. fmt.Printf("%#v\n", router)
  19. // NAT网关可以在router.nats中找到
  20. }
  21. return nil
  22. }); err != nil {
  23. log.Fatal(err)
  24. }

SDK文档

英文:

A VPN Gateway is not the same as a NAT Gateway.

Use this code to list routers. Within the list of routers, is the NAT Gateways

  1. import "google.golang.org/api/compute/v1"
  2. // Replace with valid values for your project
  3. project := "my-project"
  4. region := "my-region"
  5. ctx := context.Background()
  6. c, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
  7. if err != nil {
  8. log.Fatal(err)
  9. }
  10. computeService, err := compute.New(c)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. req := computeService.Routers.List(project, region)
  15. if err := req.Pages(ctx, func(page *compute.RouterList) error {
  16. for _, router := range page.Items {
  17. // process each `router` resource:
  18. fmt.Printf("%#v\n", router)
  19. // NAT Gateways are found in router.nats
  20. }
  21. return nil
  22. }); err != nil {
  23. log.Fatal(err)
  24. }

SDK Documentation

huangapple
  • 本文由 发表于 2022年3月19日 14:45:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/71535963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定