mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-10-01 01:36:09 -04:00
Add method for building resource URIs
This commit is contained in:
parent
a02a46e454
commit
14ef07aca9
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user