Skip to content

Commit 99335b6

Browse files
authored
Merge pull request #17 from perimeter-81/master
Added template_name configuration parameter
2 parents e38eda0 + d0e0274 commit 99335b6

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ This section describes the available configuration options for the builder. Plea
169169
* `storage_size` (int) The storage size in gigabytes. Defaults to `25`. Changing this value is useful if you aim to build a template for larger server configurations where the preconfigured server disk is larger than 25 GB. The operating system disk can also be later extended if needed. Note that Windows templates require large storage size, than default 25 Gb.
170170
* `state_timeout_duration` (string) The amount of time to wait for resource state changes. Defaults to `5m`.
171171
* `template_prefix` (string) The prefix to use for the generated template title. Defaults to an empty string, meaning the prefix will be the storage title. You can use this option to easily differentiate between different templates.
172+
* `template_name` (string) Similarly to `template_prefix`, but this will allow you to set the full template name and not just the prefix. Defaults to an empty string, meaning the name will be the storage title. You can use this option to easily differentiate between different templates. It cannot be used in conjunction with the prefix setting.
172173
* `clone_zones` ([]string) The array of extra zones (locations) where created templates should be cloned. Note that default `state_timeout_duration` is not enough for cloning, better to increase a value depending on storage size.
173174
* `ssh_private_key_path` (string) Path to SSH Private Key that will be used for provisioning and stored in the template.
174175
* `ssh_public_key_path` (string) Path to SSH Public Key that will be used for provisioning.

builder/upcloud/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
105105
StateData: map[string]interface{}{
106106
"generated_data": state.Get("generated_data"),
107107
"template_prefix": b.config.TemplatePrefix,
108+
"template_name": b.config.TemplateName,
108109
},
109110
}
110111

builder/upcloud/config.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Config struct {
5050

5151
// Optional configuration values
5252
TemplatePrefix string `mapstructure:"template_prefix"`
53+
TemplateName string `mapstructure:"template_name"`
5354
StorageSize int `mapstructure:"storage_size"`
5455
Timeout time.Duration `mapstructure:"state_timeout_duration"`
5556
CloneZones []string `mapstructure:"clone_zones"`
@@ -78,7 +79,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
7879
c.setEnv()
7980

8081
// defaults
81-
if c.TemplatePrefix == "" {
82+
if c.TemplatePrefix == "" && len(c.TemplateName) == 0{
8283
c.TemplatePrefix = DefaultTemplatePrefix
8384
}
8485

@@ -152,6 +153,18 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
152153
)
153154
}
154155

156+
if len(c.TemplateName) > 40 {
157+
errs = packer.MultiErrorAppend(
158+
errs, errors.New("'template_name' is limited to 40 characters"),
159+
)
160+
}
161+
162+
if len(c.TemplatePrefix) > 0 && len(c.TemplateName) > 0 {
163+
errs = packer.MultiErrorAppend(
164+
errs, errors.New("you can either use 'template_prefix' or 'template_name' in your configuration"),
165+
)
166+
}
167+
155168
if errs != nil && len(errs.Errors) > 0 {
156169
return nil, errs
157170
}

builder/upcloud/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type FlatConfig struct {
7171
StorageUUID *string `mapstructure:"storage_uuid" cty:"storage_uuid"`
7272
StorageName *string `mapstructure:"storage_name" cty:"storage_name"`
7373
TemplatePrefix *string `mapstructure:"template_prefix" cty:"template_prefix"`
74+
TemplateName *string `mapstructure:"template_name" cty:"template_name"`
7475
StorageSize *int `mapstructure:"storage_size" cty:"storage_size"`
7576
Timeout *string `mapstructure:"state_timeout_duration" cty:"state_timeout_duration"`
7677
CloneZones []string `mapstructure:"clone_zones" cty:"clone_zones"`
@@ -153,6 +154,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
153154
"storage_uuid": &hcldec.AttrSpec{Name: "storage_uuid", Type: cty.String, Required: false},
154155
"storage_name": &hcldec.AttrSpec{Name: "storage_name", Type: cty.String, Required: false},
155156
"template_prefix": &hcldec.AttrSpec{Name: "template_prefix", Type: cty.String, Required: false},
157+
"template_name": &hcldec.AttrSpec{Name: "template_name", Type: cty.String, Required: false},
156158
"storage_size": &hcldec.AttrSpec{Name: "storage_size", Type: cty.Number, Required: false},
157159
"state_timeout_duration": &hcldec.AttrSpec{Name: "state_timeout_duration", Type: cty.String, Required: false},
158160
"clone_zones": &hcldec.AttrSpec{Name: "clone_zones", Type: cty.List(cty.String), Required: false},

builder/upcloud/step_create_template.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,21 @@ func (s *StepCreateTemplate) Run(_ context.Context, state multistep.StateBag) mu
5050
// creating template
5151
templates := []*upcloud.Storage{}
5252

53+
// we either use template name or prefix.
54+
var templateTitle string
55+
if len(s.Config.TemplatePrefix) > 0 {
56+
templateTitle = fmt.Sprintf("%s-%s", s.Config.TemplatePrefix, internal.GetNowString())
57+
} else {
58+
templateTitle = s.Config.TemplateName
59+
}
60+
5361
for _, uuid := range storageUuids {
5462
ui.Say(fmt.Sprintf("Creating template for storage %q...", uuid))
55-
56-
t, err := driver.CreateTemplate(uuid, s.Config.TemplatePrefix)
63+
t, err := driver.CreateTemplate(uuid, templateTitle)
5764
if err != nil {
5865
return internal.StepHaltWithError(state, err)
5966
}
67+
6068
templates = append(templates, t)
6169
ui.Say(fmt.Sprintf("Template for storage %q created...", uuid))
6270
}

internal/driver.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ func (d *driver) StopServer(serverUuid string) error {
113113
return nil
114114
}
115115

116-
func (d *driver) CreateTemplate(serverStorageUuid, prefix string) (*upcloud.Storage, error) {
116+
func (d *driver) CreateTemplate(serverStorageUuid, templateTitle string) (*upcloud.Storage, error) {
117117
// create image
118-
templateTitle := fmt.Sprintf("%s-%s", prefix, GetNowString())
119118
response, err := d.svc.TemplatizeStorage(&request.TemplatizeStorageRequest{
120119
UUID: serverStorageUuid,
121120
Title: templateTitle,

0 commit comments

Comments
 (0)