Add method for building resource names

This commit is contained in:
katexochen 2022-08-05 12:18:26 +02:00 committed by Paul Meyer
parent 14ef07aca9
commit df9db94079
5 changed files with 71 additions and 12 deletions

View File

@ -324,6 +324,23 @@ func (c *Client) generateUID() (string, error) {
return string(uid), nil
}
// buildInstanceName returns a formatted name string.
// The names are joined with a '-'.
// If names is empty, the returned value is c.name + "-" + c.uid.
func (c *Client) buildResourceName(names ...string) string {
builder := strings.Builder{}
builder.WriteString(c.name)
builder.WriteRune('-')
for _, name := range names {
builder.WriteString(name)
builder.WriteRune('-')
}
builder.WriteString(c.uid)
return builder.String()
}
func (c *Client) resourceURI(scope resourceScope, resourceType, resourceName string) string {
const baseURI = "https://www.googleapis.com/compute/v1/projects/"
@ -338,17 +355,17 @@ func (c *Client) resourceURI(scope resourceScope, resourceType, resourceName str
case scopeRegion:
builder.WriteString("/regions/")
builder.WriteString(c.region)
builder.WriteString("/")
builder.WriteRune('/')
case scopeZone:
builder.WriteString("/zones/")
builder.WriteString(c.zone)
builder.WriteString("/")
builder.WriteRune('/')
default:
panic("unknown scope")
}
builder.WriteString(resourceType)
builder.WriteString("/")
builder.WriteRune('/')
builder.WriteString(resourceName)
return builder.String()

View File

@ -123,6 +123,48 @@ func TestInit(t *testing.T) {
assert.Equal("name", client.name)
}
func TestBuildResourceName(t *testing.T) {
testCases := map[string]struct {
clientUID string
clientName string
names []string
wantName string
}{
"no names": {
clientUID: "uid",
clientName: "name",
wantName: "name-uid",
},
"one name": {
clientUID: "uid",
clientName: "name",
names: []string{"foo"},
wantName: "name-foo-uid",
},
"two names": {
clientUID: "uid",
clientName: "name",
names: []string{"foo", "bar"},
wantName: "name-foo-bar-uid",
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
client := Client{
name: tc.clientName,
uid: tc.clientUID,
}
name := client.buildResourceName(tc.names...)
assert.Equal(tc.wantName, name)
})
}
}
func TestResourceURI(t *testing.T) {
testCases := map[string]struct {
scope resourceScope

View File

@ -27,7 +27,7 @@ func (c *Client) CreateInstances(ctx context.Context, input CreateInstancesInput
ops := []Operation{}
workerTemplateInput := insertInstanceTemplateInput{
Name: c.name + "-worker-" + c.uid,
Name: c.buildResourceName("worker"),
Network: c.network,
SecondarySubnetworkRangeName: c.secondarySubnetworkRange,
Subnetwork: c.subnetwork,
@ -50,7 +50,7 @@ func (c *Client) CreateInstances(ctx context.Context, input CreateInstancesInput
c.workerTemplate = workerTemplateInput.Name
controlPlaneTemplateInput := insertInstanceTemplateInput{
Name: c.name + "-control-plane-" + c.uid,
Name: c.buildResourceName("control-plane"),
Network: c.network,
Subnetwork: c.subnetwork,
SecondarySubnetworkRangeName: c.secondarySubnetworkRange,

View File

@ -39,7 +39,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error {
//
c.loadbalancers = append(c.loadbalancers, &loadBalancer{
name: c.name + "-" + "kube" + "-" + c.uid,
name: c.buildResourceName("kube"),
ip: c.loadbalancerIP,
frontendPort: constants.KubernetesPort,
backendPortName: "kubernetes",
@ -48,7 +48,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error {
})
c.loadbalancers = append(c.loadbalancers, &loadBalancer{
name: c.name + "-" + "boot" + "-" + c.uid,
name: c.buildResourceName("boot"),
ip: c.loadbalancerIPname,
frontendPort: constants.BootstrapperPort,
backendPortName: "bootstrapper",
@ -56,7 +56,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error {
})
c.loadbalancers = append(c.loadbalancers, &loadBalancer{
name: c.name + "-" + "verify" + "-" + c.uid,
name: c.buildResourceName("verify"),
ip: c.loadbalancerIPname,
frontendPort: constants.VerifyServiceNodePortGRPC,
backendPortName: "verify",
@ -64,7 +64,7 @@ func (c *Client) CreateLoadBalancers(ctx context.Context) error {
})
c.loadbalancers = append(c.loadbalancers, &loadBalancer{
name: c.name + "-" + "debugd" + "-" + c.uid,
name: c.buildResourceName("debugd"),
ip: c.loadbalancerIPname,
frontendPort: constants.DebugdPort,
backendPortName: "debugd",
@ -350,7 +350,7 @@ func (c *Client) terminateHealthCheck(ctx context.Context, lb *loadBalancer) err
}
func (c *Client) createIPAddr(ctx context.Context) error {
ipName := c.name + "-" + c.uid
ipName := c.buildResourceName()
insertReq := &computepb.InsertAddressRequest{
Project: c.project,
Region: c.region,
@ -365,7 +365,7 @@ func (c *Client) createIPAddr(ctx context.Context) error {
if err := c.waitForOperations(ctx, []Operation{op}); err != nil {
return err
}
c.loadbalancerIPname = c.name + "-" + c.uid
c.loadbalancerIPname = ipName
getReq := &computepb.GetAddressRequest{
Project: c.project,

View File

@ -88,7 +88,7 @@ type FirewallInput struct {
// CreateVPCs creates all necessary VPC networks.
func (c *Client) CreateVPCs(ctx context.Context) error {
c.network = c.name + "-" + c.uid
c.network = c.buildResourceName()
op, err := c.createVPC(ctx, c.network)
if err != nil {