Reputation: 3
How can I get a list of projects that the golang cloud client has access to using the serviceaccount key that I have provided? Seems like there is an API available: https://cloud.google.com/bigquery/docs/reference/rest/v2/projects/list
However, the Google Cloud client library does not expose it.
My code looks like this
client, err := bigquery.NewClient(ctx, conn.ProjectID, option.WithServiceAccountFile(fname))
I want to enumerate the list of projects that this client can access.
Upvotes: 0
Views: 534
Reputation: 172993
below example can give you an idea - it is sandboxed go that is part of our BQ UI
func mainReturn() interface{} {
bigqueryService, err := getBigqueryService() ; if(err != nil) {return "ERROR"}
err2 := getProjects(bigqueryService) ; if(err2 != nil) {return "ERROR"}
return "OK"
}
func getProjects(bigqueryService *bigquery.Service)(error) {
var buf bytes.Buffer
var pagetoken string = ""
ind := 0
for {
call := bigqueryService.Projects.List()
if len(pagetoken) > 0 {call = call.PageToken(pagetoken)}
list, err := call.Do() ; if err != nil {return err}
if len(pagetoken) == 0 {
xFprintf(&buf, "TotalItems: %v\n", list.TotalItems)
}
for _, project := range list.Projects {
xFprintf(&buf, "========= Projects %v ==============\n", ind)
xFprintf(&buf, "Id: %v\n", project.Id)
xFprintf(&buf, "Kind: %v\n", project.Kind)
project := project.Id
del := strings.LastIndex(project, ".")
if del != -1 {project = project[del+1:]}
ind++;
}
pagetoken = list.NextPageToken ; if len(pagetoken) == 0 {break}
}
xPrintln(buf.String())
return nil
}
Please ignore some funny looking functions like xFprintf - this is just way we sandboxed go
Upvotes: 3