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

Time:10-08

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!

CodePudding user response:

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.

  • Related