1
votes

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 [email protected] [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
Where are you seeing a nil map?Flimzy

1 Answers

4
votes

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()