1 回答
TA贡献2041条经验 获得超4个赞
将APIs Explorer用于 Cloud Resource Manager API v3projects.search
ORGANIZATION=[[YOUR-ORG]]
PROJECT=[[YOUR-PROJECT]] # Service Accounts are owned by Projects
ACCOUNT="tester"
# Enable Cloud Resource Manager API in a Project
# This Project will own the Service Account too
gcloud services enable cloudresourcemanager.googleapis.com \
--project=${PROJECT}
# Create the Service Account
gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}
EMAIL=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com
# Create a Service Account Key locally
# For testing purposes only
gcloud iam service-accounts keys create ${PWD}/${ACCOUNT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}
# Ensure the Service Account can browse the Organization's resources
gcloud organizations add-iam-policy-binding ${ORGANIZATION} \
--role=roles/browser \
--member=serviceAccount:${EMAIL}
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/${ACCOUNT}.json
export ORGANIZATION
go run .
并且: main.go:
package main
import (
"context"
"fmt"
"log"
"os"
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
resourcemanagerpb "google.golang.org/genproto/googleapis/cloud/resourcemanager/v3"
"google.golang.org/api/iterator"
)
func main() {
organization := os.Getenv("ORGANIZATION")
if organization == "" {
log.Fatalf("unable to obtain ORGANIZATION from the environment")
}
ctx := context.Background()
c, err := resourcemanager.NewProjectsClient(ctx)
if err != nil {
log.Fatal(err)
}
defer c.Close()
rqst := &resourcemanagerpb.SearchProjectsRequest{
Query: fmt.Sprintf("parent:organizations/%s", organization),
}
it := c.SearchProjects(ctx, rqst)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
log.Println(resp.DisplayName)
}
}
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报