Skip to content

Commit c3b2bc6

Browse files
authored
Merge pull request #10 from UpCloudLtd/fix/template-prefix
fix: template prefix usage
2 parents 589f035 + 493db2c commit c3b2bc6

4 files changed

Lines changed: 24 additions & 20 deletions

File tree

builder/upcloud/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"os"
88
"time"
99

10+
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
1011
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
1112
"github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
12-
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
1313
"github.com/hashicorp/packer-plugin-sdk/common"
1414
"github.com/hashicorp/packer-plugin-sdk/communicator"
1515
"github.com/hashicorp/packer-plugin-sdk/packer"
@@ -146,6 +146,12 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
146146
}
147147
}
148148

149+
if len(c.TemplatePrefix) > 40 {
150+
errs = packer.MultiErrorAppend(
151+
errs, errors.New("'template_prefix' must be 0-40 characters"),
152+
)
153+
}
154+
149155
if errs != nil && len(errs.Errors) > 0 {
150156
return nil, errs
151157
}

builder/upcloud/step_create_server.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
3737
ui.Say(fmt.Sprintf("Creating server based on storage %q...", storage.Title))
3838

3939
response, err := driver.CreateServer(&internal.ServerOpts{
40-
StorageUuid: storage.UUID,
41-
StorageSize: s.Config.StorageSize,
42-
Zone: s.Config.Zone,
43-
TemplatePrefix: s.Config.TemplatePrefix,
44-
SshPublicKey: sshKeyPublic,
45-
Networking: s.Config.Networking,
40+
StorageUuid: storage.UUID,
41+
StorageSize: s.Config.StorageSize,
42+
Zone: s.Config.Zone,
43+
SshPublicKey: sshKeyPublic,
44+
Networking: s.Config.Networking,
4645
})
4746
if err != nil {
4847
return internal.StepHaltWithError(state, err)

builder/upcloud/step_create_template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
87
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
8+
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
99
"github.com/hashicorp/packer-plugin-sdk/multistep"
1010
"github.com/hashicorp/packer-plugin-sdk/packer"
1111
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
@@ -37,7 +37,7 @@ func (s *StepCreateTemplate) Run(_ context.Context, state multistep.StateBag) mu
3737

3838
for _, zone := range s.Config.CloneZones {
3939
ui.Say(fmt.Sprintf("Cloning storage %q to zone %q...", storage.UUID, zone))
40-
title := fmt.Sprintf("packer-%s-%s-cloned-disk1", s.Config.TemplatePrefix, internal.GetNowString())
40+
title := fmt.Sprintf("packer-%s-cloned-disk1", internal.GetNowString())
4141
clonedStorage, err := driver.CloneStorage(storage.UUID, zone, title)
4242
if err != nil {
4343
return internal.StepHaltWithError(state, err)

internal/driver.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
const (
15-
DefaultPlan = "1xCPU-2GB"
15+
DefaultPlan = "1xCPU-2GB"
16+
DefaultHostname = "custom"
1617
)
1718

1819
type (
@@ -40,12 +41,11 @@ type (
4041
}
4142

4243
ServerOpts struct {
43-
StorageUuid string
44-
StorageSize int
45-
Zone string
46-
TemplatePrefix string
47-
SshPublicKey string
48-
Networking []request.CreateServerInterface
44+
StorageUuid string
45+
StorageSize int
46+
Zone string
47+
SshPublicKey string
48+
Networking []request.CreateServerInterface
4949
}
5050
)
5151

@@ -269,13 +269,12 @@ func (d *driver) GetServerStorage(serverUuid string) (*upcloud.ServerStorageDevi
269269
}
270270

271271
func (d *driver) prepareCreateRequest(opts *ServerOpts) *request.CreateServerRequest {
272-
title := fmt.Sprintf("packer-%s-%s", opts.TemplatePrefix, GetNowString())
273-
hostname := opts.TemplatePrefix
274-
titleDisk := fmt.Sprintf("%s-disk1", title)
272+
title := fmt.Sprintf("packer-%s-%s", DefaultHostname, GetNowString())
273+
titleDisk := fmt.Sprintf("%s-disk1", DefaultHostname)
275274

276275
request := request.CreateServerRequest{
277276
Title: title,
278-
Hostname: hostname,
277+
Hostname: DefaultHostname,
279278
Zone: opts.Zone,
280279
PasswordDelivery: request.PasswordDeliveryNone,
281280
Plan: DefaultPlan,

0 commit comments

Comments
 (0)