GCP Golang的动态发现客户端

huangapple go评论89阅读模式
英文:

Dynamic Discovery Client For GCP Golang

问题

我最近从Python转到了Go语言。我之前一直在使用Python与GCP一起工作。

我过去会传递作用域并指定我想要创建的发现客户端,就像这样:

def get_client(scopes, api, version="v1"):
    service_account_json = os.environ.get("SERVICE_ACCOUNT_KEY_JSON", None)
    if service_account_json is None:
        sys.exit("Exiting !!! No SSH_KEY_SERVICE_ACCOUNT env var found.")
    credentials = service_account.Credentials.from_service_account_info(
        json.loads(b64decode(service_account_json)), scopes=scopes
    )
    return discovery.build(api, version, credentials=credentials, cache_discovery=False)

这样就可以创建我想要的发现客户端,无论是compute engine service还是sqladmin

然而,在Go语言中我似乎找不到这个功能。
我找到了这个:https://pkg.go.dev/google.golang.org/api/discovery/v1

对于我想要创建的任何客户端,我都需要导入它,然后创建它,就像这样:

https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#examples

package main

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

func main() {
        ctx := context.Background()

        c, err := google.DefaultClient(ctx, cloudresourcemanager.CloudPlatformScope)
        if err != nil {
                log.Fatal(err)
        }

        cloudresourcemanagerService, err := cloudresourcemanager.New(c)
        if err != nil {
                log.Fatal(err)
        }

        req := cloudresourcemanagerService.Projects.List()
        if err := req.Pages(ctx, func(page *cloudresourcemanager.ListProjectsResponse) error {
                for _, project := range page.Projects {
                        // TODO: Change code below to process each `project` resource:
                        fmt.Printf("%#v\n", project)
                }
                return nil
        }); err != nil {
                log.Fatal(err)
        }
}

所以我必须导入每个客户端库才能获取相应的客户端。
"google.golang.org/api/cloudresourcemanager/v1"

没有动态创建的功能。

这是否可能,因为Go语言是严格类型检查的呢?谢谢。

英文:

I've recently shifted from python to golang. I had been using python to work with GCP.

I used to pass in the scopes and mention the discovery client I wanted to create like this :

def get_client(scopes, api, version="v1"):
    service_account_json = os.environ.get("SERVICE_ACCOUNT_KEY_JSON", None)
    if service_account_json is None:
        sys.exit("Exiting !!! No SSH_KEY_SERVICE_ACCOUNT env var found.")
    credentials = service_account.Credentials.from_service_account_info(
        json.loads(b64decode(service_account_json)), scopes=scopes
    )
    return discovery.build(api, version, credentials=credentials, cache_discovery=False)

And this would create my desired discovery client, whether it be compute engine service or sqladmin

However in go I don't seem to find this.
I found this : https://pkg.go.dev/google.golang.org/api/discovery/v1

For any client that I want to create I would've to import that and then create that, like this :

https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#examples

package main


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

func main() {
        ctx := context.Background()

        c, err := google.DefaultClient(ctx, cloudresourcemanager.CloudPlatformScope)
        if err != nil {
                log.Fatal(err)
        }

        cloudresourcemanagerService, err := cloudresourcemanager.New(c)
        if err != nil {
                log.Fatal(err)
        }

        req := cloudresourcemanagerService.Projects.List()
        if err := req.Pages(ctx, func(page *cloudresourcemanager.ListProjectsResponse) error {
                for _, project := range page.Projects {
                        // TODO: Change code below to process each `project` resource:
                        fmt.Printf("%#v\n", project)
                }
                return nil
        }); err != nil {
                log.Fatal(err)
        }
}

So I've to import each client library to get the client for that.
"google.golang.org/api/cloudresourcemanager/v1"

There's no dynamic creation of it.

Is it even possible, cause go is strict type checking 🤔

Thanks.

答案1

得分: 1

不,使用Golang的Google Cloud库是不可能的。

你对严格类型检查的观点抓住了要点,因为这肯定会削弱编译时类型检查的好处。在Golang中,返回具有不同签名的不同对象也是不好的做法,因为我们不使用鸭子类型,而是依赖于接口契约。

Golang是一种乏味而冗长的语言,这是有意设计的 GCP Golang的动态发现客户端

英文:

No, this is not possible with the Golang Google Cloud library.

You've nailed the point on the strict type checking, as it would definitely defeat the benefits of compile time type checking. It would also be a bad Golang practice to return different objects with different signatures, as we don't do duck typing and instead we rely on interface contracts.

Golang is boring and verbose, and it's like that by design GCP Golang的动态发现客户端

huangapple
  • 本文由 发表于 2021年7月14日 14:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/68372824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定