Go: 比较 “google.golang.org/genproto/googleapis/api/serviceusage/v1” 的 “State” 和字符串。

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

Go: Compare "google.golang.org/genproto/googleapis/api/serviceusage/v1".State With String

问题

我正在学习Go语言,目前的一个项目是获取GCP项目列表,并确定哪些项目启用了K8s API,然后获取K8s集群的版本。

我已经成功获取了项目列表,并可以进行筛选,但是在比较"google.golang.org/genproto/googleapis/api/serviceusage/v1"的State来检查K8s API是否启用时遇到了问题。

目前我有以下代码:

package main

import (
	"fmt"
	"log"
	"context"
	"reflect"

	resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
	resourcemanagerpb "google.golang.org/genproto/googleapis/cloud/resourcemanager/v3"
	serviceusage "cloud.google.com/go/serviceusage/apiv1"
	serviceusagepb "google.golang.org/genproto/googleapis/api/serviceusage/v1"
	"google.golang.org/api/iterator"
)

func main() {
	
	ProjectMap := getGCPProjects()
	if len(ProjectMap) > 0 {
		fmt.Println(ProjectMap)
		for key, value := range ProjectMap {
			fmt.Println("Checking K8s API for " + key)

			ctx := context.Background()
			c, err := serviceusage.NewClient(ctx)
			if err != nil{
				log.Fatal(err)
			}
			defer c.Close()

			req := &serviceusagepb.GetServiceRequest{
				 Name: value + "/services/container.googleapis.com",
			}

			resp, err := c.GetService(ctx, req)
			if err != nil{
				log.Fatal(err)
			}

			fmt.Println(reflect.TypeOf(resp.State))
			fmt.Println(resp.State)
			if resp.State == "ENABLED"{
				fmt.Println(resp.State)
			}

		}
	} else {
		log.Fatal("ProjectMap is null.")
	}


}

func getGCPProjects() map[string]string{
	
	ProjectMap := make(map[string]string)

	ctx := context.Background()
	c, err := resourcemanager.NewProjectsClient(ctx)
	if err != nil{
		log.Fatal(err)
	}
	defer c.Close()

	rqst := &resourcemanagerpb.SearchProjectsRequest{
		Query: "state:ACTIVE",
    }
    it := c.SearchProjects(ctx, rqst)
    for {
        resp, err := it.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            log.Fatal(err)
        }
		fmt.Println(resp)
		ProjectMap[resp.DisplayName] = resp.Name
    } 
	return ProjectMap
}

我想判断resp.State是否等于"ENABLED",以便我可以继续检查每个集群中部署的K8s版本。
然而,我目前遇到了以下错误:

invalid operation: resp.State == "ENABLED" (mismatched types "google.golang.org/genproto/googleapis/api/serviceusage/v1".State and untyped string)

我是否可以将API返回的内容转换为字符串,然后进行比较?否则,我还有其他的检查方法吗?

非常感谢您对此的帮助!

英文:

I'm learning Go at the moment, and one of my first projects is to get a list of GCP projects and determine which have the K8s API enabled, and then to acquire the version of the K8s clusters.

I've managed to get a list of projects, which I can filter through, although I'm running into an issue with comparing the "google.golang.org/genproto/googleapis/api/serviceusage/v1".State to check if the K8s API is enabled.

So far I have:


package main
import (
"fmt"
"log"
"context"
"reflect"
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
resourcemanagerpb "google.golang.org/genproto/googleapis/cloud/resourcemanager/v3"
serviceusage "cloud.google.com/go/serviceusage/apiv1"
serviceusagepb "google.golang.org/genproto/googleapis/api/serviceusage/v1"
"google.golang.org/api/iterator"
)
func main() {
ProjectMap := getGCPProjects()
if len(ProjectMap) > 0 {
fmt.Println(ProjectMap)
for key, value := range ProjectMap {
fmt.Println("Checking K8s API for " + key)
ctx := context.Background()
c, err := serviceusage.NewClient(ctx)
if err != nil{
log.Fatal(err)
}
defer c.Close()
req := &serviceusagepb.GetServiceRequest{
Name: value + "/services/container.googleapis.com",
}
resp, err := c.GetService(ctx, req)
if err != nil{
log.Fatal(err)
}
fmt.Println(reflect.TypeOf(resp.State))
fmt.Println(resp.State)
if resp.State == "ENABLED"{
fmt.Println(resp.State)
}
}
} else {
log.Fatal("ProjectMap is null.")
}
}
func getGCPProjects() map[string]string{
ProjectMap := make(map[string]string)
ctx := context.Background()
c, err := resourcemanager.NewProjectsClient(ctx)
if err != nil{
log.Fatal(err)
}
defer c.Close()
rqst := &resourcemanagerpb.SearchProjectsRequest{
Query: "state:ACTIVE",
}
it := c.SearchProjects(ctx, rqst)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(resp)
ProjectMap[resp.DisplayName] = resp.Name
} 
return ProjectMap
}

I want to see if resp.State equals "ENABLED" so that I can then go on to check the version of K8s deployed in each cluster.
However, I'm currently running into:

>invalid operation: resp.State == "ENABLED" (mismatched types "google.golang.org/genproto/googleapis/api/serviceusage/v1".State and untyped string)

Can I somehow convert what's returned from the API into a string to then compare with? Otherwise, how else could I check?

Any help with this would be greatly appreciated!

答案1

得分: 1

State 不是一个字符串,而是一个 int32 类型。

你可以将其与 int32 进行比较(例如 2),但最好的做法是使用定义的常量:

if resp.State == serviceusagepb.state_ENABLED {
  ...
}

我鼓励你考虑使用类似 Visual Studio Code 的工具。Go 团队提供了一个适用于 Go 的 扩展,它应该能够显著提高你开发代码的体验。

在这种情况下,如果你使用的是 Visual Studio Code 和 Go 扩展,编辑器会突出显示代码:

resp.State == "ENABLED"

并提示你:

invalid operation: cannot compare resp.State == "ENABLED"
mismatched types "google.golang.org/genproto/googleapis/api/serviceusage/v1".State and untyped string

如果你输入 if resp.State == serviceusagepb.,编辑器会提示可能的值列表。

Go 的在线文档(pkg.go.dev)也非常出色。

对于你使用的包,你可以在任何一个包前加上 https://pkg.go.dev/{package},例如 https://pkg.go.dev/google.golang.org/genproto/googleapis/api/serviceusage/v1,以访问从 API 生成的文档。

英文:

State is not a string but an int32

You could compare it with an int32 (e.g. 2) but it is better practice to use the defined constants:

if resp.State == serviceusagepb.state_ENABLED {
  ...
}

I encourage you to consider using a tool like Visual Studio Code. The (Google) Go team provides an extension for Go and it should significantly improve your experience developing code.

In this case, if you were using Visual Studio Code and the Go extension, the editor would have highlighted the code:

resp.State == "ENABLED"

And informed you:

invalid operation: cannot compare resp.State == "ENABLED"
mismatched types "google.golang.org/genproto/googleapis/api/serviceusage/v1".State and untyped string

Had you been typing if resp.State == serviceusagepb., the editor would have prompted you with a list of possible values.

Go's online documentation (pkg.go.dev) is also excellent.

For the packages you're using, you can prefix any of them with https://pkg.go.dev/{package} e.g. https://pkg.go.dev/google.golang.org/genproto/googleapis/api/serviceusage/v1 to be taken to the documentation generated from the API.

huangapple
  • 本文由 发表于 2022年10月6日 20:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/73973445.html
匿名

发表评论

匿名网友

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

确定