Skip to content

Commit fbaadd3

Browse files
committed
add new template name param
1 parent 8f8b014 commit fbaadd3

4 files changed

Lines changed: 30 additions & 15 deletions

File tree

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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ type Config struct {
4949
StorageName string `mapstructure:"storage_name"`
5050

5151
// Optional configuration values
52-
TemplatePrefix string `mapstructure:"template_prefix"`
53-
IsTemplateNameFixed bool `mapstructure:"is_template_name_fixed"`
54-
StorageSize int `mapstructure:"storage_size"`
55-
Timeout time.Duration `mapstructure:"state_timeout_duration"`
56-
CloneZones []string `mapstructure:"clone_zones"`
52+
TemplatePrefix string `mapstructure:"template_prefix"`
53+
TemplateName string `mapstructure:"template_name"`
54+
StorageSize int `mapstructure:"storage_size"`
55+
Timeout time.Duration `mapstructure:"state_timeout_duration"`
56+
CloneZones []string `mapstructure:"clone_zones"`
5757

5858
RawNetworking []internal.NetworkInterface `mapstructure:"network_interfaces"`
5959
Networking []request.CreateServerInterface
@@ -79,7 +79,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
7979
c.setEnv()
8080

8181
// defaults
82-
if c.TemplatePrefix == "" {
82+
if c.TemplatePrefix == "" && len(c.TemplateName) == 0{
8383
c.TemplatePrefix = DefaultTemplatePrefix
8484
}
8585

@@ -153,6 +153,18 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
153153
)
154154
}
155155

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+
156168
if errs != nil && len(errs.Errors) > 0 {
157169
return nil, errs
158170
}

builder/upcloud/config.hcl2spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +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-
IsTemplateNameFixed *bool `mapstructure:"is_template_name_fixed" cty:"is_template_name_fixed"`
74+
TemplateName *string `mapstructure:"template_name" cty:"template_name"`
7575
StorageSize *int `mapstructure:"storage_size" cty:"storage_size"`
7676
Timeout *string `mapstructure:"state_timeout_duration" cty:"state_timeout_duration"`
7777
CloneZones []string `mapstructure:"clone_zones" cty:"clone_zones"`
@@ -154,7 +154,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
154154
"storage_uuid": &hcldec.AttrSpec{Name: "storage_uuid", Type: cty.String, Required: false},
155155
"storage_name": &hcldec.AttrSpec{Name: "storage_name", Type: cty.String, Required: false},
156156
"template_prefix": &hcldec.AttrSpec{Name: "template_prefix", Type: cty.String, Required: false},
157-
"is_template_name_fixed": &hcldec.AttrSpec{Name: "is_template_name_fixed", Type: cty.Bool, Required: false},
157+
"template_name": &hcldec.AttrSpec{Name: "template_name", Type: cty.String, Required: false},
158158
"storage_size": &hcldec.AttrSpec{Name: "storage_size", Type: cty.Number, Required: false},
159159
"state_timeout_duration": &hcldec.AttrSpec{Name: "state_timeout_duration", Type: cty.String, Required: false},
160160
"clone_zones": &hcldec.AttrSpec{Name: "clone_zones", Type: cty.List(cty.String), Required: false},

builder/upcloud/step_create_template.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +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-
templateTitle := s.Config.TemplatePrefix
57-
if !s.Config.IsTemplateNameFixed {
58-
templateTitle = fmt.Sprintf("%s-%s", templateTitle, internal.GetNowString())
59-
}
60-
6163
t, err := driver.CreateTemplate(uuid, templateTitle)
6264
if err != nil {
6365
return internal.StepHaltWithError(state, err)
6466
}
65-
67+
6668
templates = append(templates, t)
6769
ui.Say(fmt.Sprintf("Template for storage %q created...", uuid))
6870
}

0 commit comments

Comments
 (0)