dongshushen4392 2018-05-11 11:42
浏览 85
已采纳

如何使用Go SDK for Google Cloud Platform获取项目元数据?

I am using https://cloud.google.com/go/google.golang.org API to get common instance metadata from Google Compute Engine.

I have found an API to get metadata of a project by specifying project-id in params. Following is the code I have written:

package main

import (
    "fmt"
    "golang.org/x/net/context"
    "google.golang.org/api/compute/v1"
    "golang.org/x/oauth2/google"
)

func main() {
    ctx := context.Background()
    client, err := google.DefaultClient(ctx,compute.ComputeScope)
    if err != nil {
        fmt.Println(err)
    }
    computeService, err := compute.New(client)
    projectGetCall := computeService.Projects.Get("<project-id>")
    project, err := projectGetCall.Do()
    fmt.Println(project)
}

I am expecting it should get default credentials from GCP service account I have in my system and should print the Metadata to console. Output to console is:

&{0xc04222a150 2016-09-13T05:31:43.862-07:00 85694249451-compute@developer.gserviceaccount.com [alpha-api] 8538061022982459200 compute#project project-id [0xc04222d310 0xc04222d360 0xc04222d3b0 0xc04222d400 0xc04222d450 0xc04222d4a0 0xc04222d540 0xc04222d590 0xc04222d5e0 0xc04222d630 0xc04222d680 0xc04222d6d0 0xc04222d720 0xc04222d770 0xc04222d7c0 0xc04222d810 0xc04222d860 0xc04222d8b0 0xc04222d900 0xc04222d950 0xc04222d9a0 0xc04222d9f0 0xc04222da40 0xc04222da90 0xc04222dae0 0xc04222db30 0xc04222db80] https://www.googleapis.com/compute/v1/projects/project-id nil UNSPECIFIED_XPN_PROJECT_STATUS {200 map[X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN] X-Xss-Protection:[1; mode=block] Server:[GSE] Date:[Mon, 14 May 2018 06:01:39 GMT] Content-Type:[application/json; charset=UTF-8] Etag:["AZ9pASbV2zte89rFYbsuVT3-scI=/tQ5dONakPodnJi1mjg-QYom343E="] Vary:[Origin X-Origin] Alt-Svc:[hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35"] Expires:[Mon, 14 May 2018 06:01:39 GMT] Cache-Control:[private, max-age=0, must-revalidate, no-transform]]} [] []}

I don't have any experience on Go and have no idea how to extract details from this response

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-05-11 12:13
    关注

    With Projects.Get you create a pointer to a ProjectsGetCall. The call has not been made yet. You have to execute Do on the returned ProjectsGetCall. See the docs: https://godoc.org/google.golang.org/api/compute/v1#ProjectsGetCall.Do

    call := computeService.Projects.Get("<project-id>")
    project, err := call.Do()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码