generated from hashicorp/packer-plugin-scaffolding
/
step_register_template.go
59 lines (48 loc) · 1.89 KB
/
step_register_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package exoscale
import (
"context"
"fmt"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
egoscale "github.com/exoscale/egoscale/v2"
)
type stepRegisterTemplate struct {
builder *Builder
}
func (s *stepRegisterTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
var (
snapshotChecksum = state.Get("snapshot_checksum").(string)
snapshotURL = state.Get("snapshot_url").(string)
ui = state.Get("ui").(packer.Ui)
templates = state.Get("templates").([]*egoscale.Template)
registerZone = s.builder.config.TemplateZones[0]
passwordEnabled = !s.builder.config.TemplateDisablePassword
sshkeyEnabled = !s.builder.config.TemplateDisableSSHKey
)
ui.Say(fmt.Sprintf("Registering compute instance template (in %s)", registerZone))
template, err := s.builder.exo.RegisterTemplate(
ctx,
registerZone,
&egoscale.Template{
BootMode: &s.builder.config.TemplateBootMode,
Build: nonEmptyStringPtr(s.builder.config.TemplateBuild),
Checksum: nonEmptyStringPtr(snapshotChecksum),
DefaultUser: nonEmptyStringPtr(s.builder.config.TemplateUsername),
Description: nonEmptyStringPtr(s.builder.config.TemplateDescription),
Maintainer: nonEmptyStringPtr(s.builder.config.TemplateMaintainer),
Name: &s.builder.config.TemplateName,
PasswordEnabled: &passwordEnabled,
SSHKeyEnabled: &sshkeyEnabled,
URL: nonEmptyStringPtr(snapshotURL),
Version: nonEmptyStringPtr(s.builder.config.TemplateVersion),
},
)
if err != nil {
ui.Error(fmt.Sprintf("Unable to register compute instance template: %v", err))
return multistep.ActionHalt
}
templates = append(templates, template)
state.Put("templates", templates)
return multistep.ActionContinue
}
func (s *stepRegisterTemplate) Cleanup(_ multistep.StateBag) {}