英文:
Error while trying to list folders inside a GCP organisation
问题
我正在尝试用GO编写一个简单的代码,使用resourcemanager API列出GCP组织中的所有文件夹。以下是我的代码:
package main
import (
"context"
"log"
resourcemanager "cloud.google.com/go/resourcemanager/apiv2"
"google.golang.org/api/iterator"
resourcemanagerpb "google.golang.org/genproto/googleapis/cloud/resourcemanager/v2"
)
func main() {
ctx := context.Background()
c, err := resourcemanager.NewFoldersClient(ctx)
if err != nil {
// TODO: 处理错误。
log.Println("错误:无法启动客户端。")
}
defer c.Close()
req := &resourcemanagerpb.ListFoldersRequest{
Parent: "organizations/<MY-ORG-NAME>",
}
it := c.ListFolders(ctx, req)
tries := 0
for {
resp, err := it.Next()
if err == iterator.Done || tries == 3 {
break
}
if err != nil {
log.Println(err)
}
// TODO: 使用 resp。
log.Println(resp)
tries++
}
}
这段代码直接从API文档中复制而来,我只是添加了我的组织名称,增加了一些日志功能,并在for循环中限制了尝试次数,因为它一直打印错误。
每当我运行代码时,我会收到以下错误消息:
2021/11/04 17:06:41 rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found); transport: received unexpected content-type "text/html; charset=UTF-8"
我不确定这是否是解决方案,但我认为我需要在我的目录中添加一个.proto
文件才能使其工作,但我不知道如何做,也不知道应该在其中放什么。我对GO和API都很陌生,所以这一切对我来说都很困惑。
非常感谢您的帮助!
英文:
I'm trying to write a simple code in GO that lists all the folders under an organisation in GCP using the resourcemanager API. Below is my code:
package main
import (
"context"
"log"
resourcemanager "cloud.google.com/go/resourcemanager/apiv2"
"google.golang.org/api/iterator"
resourcemanagerpb "google.golang.org/genproto/googleapis/cloud/resourcemanager/v2"
)
func main() {
ctx := context.Background()
c, err := resourcemanager.NewFoldersClient(ctx)
if err != nil {
// TODO: Handle error.
log.Println("Error: Failed to start client.")
}
defer c.Close()
req := &resourcemanagerpb.ListFoldersRequest{
Parent: "organizations/<MY-ORG-NAME>",
}
it := c.ListFolders(ctx, req)
tries := 0
for {
resp, err := it.Next()
if err == iterator.Done || tries == 3 {
break
}
if err != nil {
log.Println(err)
}
// TODO: Use resp.
log.Println(resp)
tries++
}
}
The code is directly copied from the API documentation, I just added my organisation name, added some log features and limited the tries in the for loop, since it was endlessly printing errors.
I'm getting the following error messages whenever I run the code:
2021/11/04 17:06:41 rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found); transport: received unexpected content-type "text/html; charset=UTF-8"
I'm not sure if this is the solution but; I think I need to add a .proto
file in my directory for it to work, but I didn't understand how to do that or what exactly to put in there. I'm new to GO and it's my first time working with API's so this all seems very confusing to me.
Any help is very much appreciated!
答案1
得分: 0
迁移到 API v3 解决了问题。这个代码完美运行!
感谢所有评论的人和找到解决方案的同事!
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
foldersListCall := foldersService.List()
foldersListCall.Parent("organizations/<MY-ORG-ID>")
resp, err := foldersListCall.Do()
if err != nil {
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.DisplayName)
}
}
英文:
Migrating to the API v3 solved the problem. This works perfectly!
Thank you to everyone who commented and to my coworker who found the solution!
package main
import (
"context"
"fmt"
"log"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v3"
)
func main() {
ctx := context.Background()
svc, err := cloudresourcemanager.NewService(ctx)
if err != nil {
log.Fatal(err)
}
foldersService := cloudresourcemanager.NewFoldersService(svc)
foldersListCall := foldersService.List()
foldersListCall.Parent("organizations/<MY-ORG-ID>")
resp, err := foldersListCall.Do()
if err != nil {
log.Fatal(err)
}
for _, fld := range resp.Folders {
fmt.Println(fld.DisplayName)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论