Go以Google API Client Libraries來取得GCP Interconnect Cloud Router。
事前要求
參考「GCP 設定本機應用程式存取憑證 Application Default Credentials」設定credential。
參考「Golang 建立GCP Interconnect Cloud Router」建立Cloud Router。
取得Cloud Router
呼叫compute.RouterService.Get
輸入project id、region、cloud router名稱來取得Cloud Router。
main.go
package main
import (
"context"
"fmt"
compute "google.golang.org/api/compute/v1"
)
func main() {
ctx := context.Background()
service, err := compute.NewService(ctx)
if err != nil {
panic(err)
}
routersService := compute.NewRoutersService(service)
projectId := "project-id-1"
region := "asia-east1"
routerName := "demo-cloudrouter-002"
call := routersService.Get(projectId, region, routerName)
router, err := call.Do()
if err != nil {
panic(err)
}
fmt.Println(router.Name) // demo-cloudrouter-002
fmt.Println(router.Kind) // compute#router
fmt.Println(router.Bgp.Asn) // 16550
fmt.Println(router.Bgp.AdvertiseMode) // DEFAULT
fmt.Println(router.Network) // https://www.googleapis.com/compute/v1/projects/project-id-1/global/networks/demo-vpc-001
fmt.Println(router.SelfLink) // https://www.googleapis.com/compute/v1/projects/project-id-1/regions/asia-east1/routers/demo-cloudrouter-002
}
測試
執行Go應用程式印出以下。
demo-cloudrouter-002
compute#router
16550
DEFAULT
https://www.googleapis.com/compute/v1/projects/project-id-1/global/networks/demo-vpc-001
https://www.googleapis.com/compute/v1/projects/project-id-1/regions/asia-east1/routers/demo-cloudrouter-002
在GCP console查看此instance。
沒有留言:
張貼留言