Add method for building resource URIs

This commit is contained in:
katexochen 2022-08-05 12:09:39 +02:00 committed by Paul Meyer
parent a02a46e454
commit 14ef07aca9
3 changed files with 87 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"net/http" "net/http"
"strings"
compute "cloud.google.com/go/compute/apiv1" compute "cloud.google.com/go/compute/apiv1"
admin "cloud.google.com/go/iam/admin/apiv1" admin "cloud.google.com/go/iam/admin/apiv1"
@ -323,6 +324,44 @@ func (c *Client) generateUID() (string, error) {
return string(uid), nil 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 { type closer interface {
Close() error Close() error
} }

View File

@ -123,6 +123,50 @@ func TestInit(t *testing.T) {
assert.Equal("name", client.name) 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) { func TestCloseAll(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -142,12 +142,12 @@ func (c *Client) createBackendService(ctx context.Context, lb *loadBalancer) err
Name: proto.String(lb.name), Name: proto.String(lb.name),
Protocol: proto.String(computepb.BackendService_Protocol_name[int32(computepb.BackendService_TCP)]), Protocol: proto.String(computepb.BackendService_Protocol_name[int32(computepb.BackendService_TCP)]),
LoadBalancingScheme: proto.String(computepb.BackendService_LoadBalancingScheme_name[int32(computepb.BackendService_EXTERNAL)]), 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), PortName: proto.String(lb.backendPortName),
Backends: []*computepb.Backend{ Backends: []*computepb.Backend{
{ {
BalancingMode: proto.String(computepb.Backend_BalancingMode_name[int32(computepb.Backend_CONNECTION)]), 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, Region: c.region,
ForwardingRuleResource: &computepb.ForwardingRule{ ForwardingRuleResource: &computepb.ForwardingRule{
Name: proto.String(lb.name), 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)]), IPProtocol: proto.String(computepb.ForwardingRule_IPProtocolEnum_name[int32(computepb.ForwardingRule_TCP)]),
LoadBalancingScheme: proto.String(computepb.ForwardingRule_LoadBalancingScheme_name[int32(computepb.ForwardingRule_EXTERNAL)]), LoadBalancingScheme: proto.String(computepb.ForwardingRule_LoadBalancingScheme_name[int32(computepb.ForwardingRule_EXTERNAL)]),
Ports: []string{strconv.Itoa(lb.frontendPort)}, 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) resp, err := c.forwardingRulesAPI.Insert(ctx, req)