From 14ef07aca9b18ee0a5a8b95a2c32e89ab4d619dc Mon Sep 17 00:00:00 2001 From: katexochen <49727155+katexochen@users.noreply.github.com> Date: Fri, 5 Aug 2022 12:09:39 +0200 Subject: [PATCH] Add method for building resource URIs --- cli/internal/gcp/client/client.go | 39 ++++++++++++++++++++++ cli/internal/gcp/client/client_test.go | 44 +++++++++++++++++++++++++ cli/internal/gcp/client/loadbalancer.go | 8 ++--- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/cli/internal/gcp/client/client.go b/cli/internal/gcp/client/client.go index ad9691ac6..5ad43e0ec 100644 --- a/cli/internal/gcp/client/client.go +++ b/cli/internal/gcp/client/client.go @@ -8,6 +8,7 @@ import ( "fmt" "math/big" "net/http" + "strings" compute "cloud.google.com/go/compute/apiv1" admin "cloud.google.com/go/iam/admin/apiv1" @@ -323,6 +324,44 @@ func (c *Client) generateUID() (string, error) { return string(uid), nil } +func (c *Client) resourceURI(scope resourceScope, resourceType, resourceName string) string { + const baseURI = "https://www.googleapis.com/compute/v1/projects/" + + builder := strings.Builder{} + + builder.WriteString(baseURI) + builder.WriteString(c.project) + + switch scope { + case scopeGlobal: + builder.WriteString("/global/") + case scopeRegion: + builder.WriteString("/regions/") + builder.WriteString(c.region) + builder.WriteString("/") + case scopeZone: + builder.WriteString("/zones/") + builder.WriteString(c.zone) + builder.WriteString("/") + default: + panic("unknown scope") + } + + builder.WriteString(resourceType) + builder.WriteString("/") + builder.WriteString(resourceName) + + return builder.String() +} + +type resourceScope string + +const ( + scopeGlobal resourceScope = "global" + scopeRegion resourceScope = "region" + scopeZone resourceScope = "zone" +) + type closer interface { Close() error } diff --git a/cli/internal/gcp/client/client_test.go b/cli/internal/gcp/client/client_test.go index a0f557260..d0d2f479f 100644 --- a/cli/internal/gcp/client/client_test.go +++ b/cli/internal/gcp/client/client_test.go @@ -123,6 +123,50 @@ func TestInit(t *testing.T) { assert.Equal("name", client.name) } +func TestResourceURI(t *testing.T) { + testCases := map[string]struct { + scope resourceScope + resourceType string + resourceName string + wantURI string + }{ + "global resource": { + scope: scopeGlobal, + resourceType: "healthChecks", + resourceName: "name", + wantURI: "https://www.googleapis.com/compute/v1/projects/project/global/healthChecks/name", + }, + "regional resource": { + scope: scopeRegion, + resourceType: "healthChecks", + resourceName: "name", + wantURI: "https://www.googleapis.com/compute/v1/projects/project/regions/region/healthChecks/name", + }, + "zonal resource": { + scope: scopeZone, + resourceType: "instanceGroups", + resourceName: "name", + wantURI: "https://www.googleapis.com/compute/v1/projects/project/zones/zone/instanceGroups/name", + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + assert := assert.New(t) + + client := Client{ + project: "project", + zone: "zone", + region: "region", + } + + uri := client.resourceURI(tc.scope, tc.resourceType, tc.resourceName) + + assert.Equal(tc.wantURI, uri) + }) + } +} + func TestCloseAll(t *testing.T) { assert := assert.New(t) diff --git a/cli/internal/gcp/client/loadbalancer.go b/cli/internal/gcp/client/loadbalancer.go index 8077fdfe7..d07fa04ae 100644 --- a/cli/internal/gcp/client/loadbalancer.go +++ b/cli/internal/gcp/client/loadbalancer.go @@ -142,12 +142,12 @@ func (c *Client) createBackendService(ctx context.Context, lb *loadBalancer) err Name: proto.String(lb.name), Protocol: proto.String(computepb.BackendService_Protocol_name[int32(computepb.BackendService_TCP)]), LoadBalancingScheme: proto.String(computepb.BackendService_LoadBalancingScheme_name[int32(computepb.BackendService_EXTERNAL)]), - HealthChecks: []string{"https://www.googleapis.com/compute/v1/projects/" + c.project + "/regions/" + c.region + "/healthChecks/" + lb.name}, + HealthChecks: []string{c.resourceURI(scopeRegion, "healthChecks", lb.name)}, PortName: proto.String(lb.backendPortName), Backends: []*computepb.Backend{ { BalancingMode: proto.String(computepb.Backend_BalancingMode_name[int32(computepb.Backend_CONNECTION)]), - Group: proto.String("https://www.googleapis.com/compute/v1/projects/" + c.project + "/zones/" + c.zone + "/instanceGroups/" + c.controlPlaneInstanceGroup), + Group: proto.String(c.resourceURI(scopeZone, "instanceGroups", c.controlPlaneInstanceGroup)), }, }, }, @@ -171,11 +171,11 @@ func (c *Client) createForwardingRules(ctx context.Context, lb *loadBalancer) er Region: c.region, ForwardingRuleResource: &computepb.ForwardingRule{ Name: proto.String(lb.name), - IPAddress: proto.String("https://www.googleapis.com/compute/v1/projects/" + c.project + "/regions/" + c.region + "/addresses/" + c.loadbalancerIPname), + IPAddress: proto.String(c.resourceURI(scopeRegion, "addresses", c.loadbalancerIPname)), IPProtocol: proto.String(computepb.ForwardingRule_IPProtocolEnum_name[int32(computepb.ForwardingRule_TCP)]), LoadBalancingScheme: proto.String(computepb.ForwardingRule_LoadBalancingScheme_name[int32(computepb.ForwardingRule_EXTERNAL)]), Ports: []string{strconv.Itoa(lb.frontendPort)}, - BackendService: proto.String("https://www.googleapis.com/compute/v1/projects/" + c.project + "/regions/" + c.region + "/backendServices/" + lb.name), + BackendService: proto.String(c.resourceURI(scopeRegion, "backendServices", lb.name)), }, } resp, err := c.forwardingRulesAPI.Insert(ctx, req)