Skip to content

Commit

Permalink
Merge pull request #10 from UpCloudLtd/fix/template-prefix
Browse files Browse the repository at this point in the history
fix: template prefix usage
  • Loading branch information
maxfrei committed Jun 17, 2021
2 parents 589f035 + 493db2c commit c3b2bc6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
8 changes: 7 additions & 1 deletion builder/upcloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
"time"

internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -146,6 +146,12 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}
}

if len(c.TemplatePrefix) > 40 {
errs = packer.MultiErrorAppend(
errs, errors.New("'template_prefix' must be 0-40 characters"),
)
}

if errs != nil && len(errs.Errors) > 0 {
return nil, errs
}
Expand Down
11 changes: 5 additions & 6 deletions builder/upcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
ui.Say(fmt.Sprintf("Creating server based on storage %q...", storage.Title))

response, err := driver.CreateServer(&internal.ServerOpts{
StorageUuid: storage.UUID,
StorageSize: s.Config.StorageSize,
Zone: s.Config.Zone,
TemplatePrefix: s.Config.TemplatePrefix,
SshPublicKey: sshKeyPublic,
Networking: s.Config.Networking,
StorageUuid: storage.UUID,
StorageSize: s.Config.StorageSize,
Zone: s.Config.Zone,
SshPublicKey: sshKeyPublic,
Networking: s.Config.Networking,
})
if err != nil {
return internal.StepHaltWithError(state, err)
Expand Down
4 changes: 2 additions & 2 deletions builder/upcloud/step_create_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/UpCloudLtd/upcloud-go-api/upcloud"
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
Expand Down Expand Up @@ -37,7 +37,7 @@ func (s *StepCreateTemplate) Run(_ context.Context, state multistep.StateBag) mu

for _, zone := range s.Config.CloneZones {
ui.Say(fmt.Sprintf("Cloning storage %q to zone %q...", storage.UUID, zone))
title := fmt.Sprintf("packer-%s-%s-cloned-disk1", s.Config.TemplatePrefix, internal.GetNowString())
title := fmt.Sprintf("packer-%s-cloned-disk1", internal.GetNowString())
clonedStorage, err := driver.CloneStorage(storage.UUID, zone, title)
if err != nil {
return internal.StepHaltWithError(state, err)
Expand Down
21 changes: 10 additions & 11 deletions internal/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

const (
DefaultPlan = "1xCPU-2GB"
DefaultPlan = "1xCPU-2GB"
DefaultHostname = "custom"
)

type (
Expand Down Expand Up @@ -40,12 +41,11 @@ type (
}

ServerOpts struct {
StorageUuid string
StorageSize int
Zone string
TemplatePrefix string
SshPublicKey string
Networking []request.CreateServerInterface
StorageUuid string
StorageSize int
Zone string
SshPublicKey string
Networking []request.CreateServerInterface
}
)

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

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

request := request.CreateServerRequest{
Title: title,
Hostname: hostname,
Hostname: DefaultHostname,
Zone: opts.Zone,
PasswordDelivery: request.PasswordDeliveryNone,
Plan: DefaultPlan,
Expand Down

0 comments on commit c3b2bc6

Please sign in to comment.