This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
/
step_register_template.go
61 lines (50 loc) · 1.71 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
60
61
package exoscale
import (
"context"
"fmt"
"github.com/exoscale/egoscale"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type stepRegisterTemplate struct{}
func (s *stepRegisterTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
var (
exo = state.Get("exo").(*egoscale.Client)
ui = state.Get("ui").(packer.Ui)
config = state.Get("config").(*Config)
imageURL = state.Get("image_url").(string)
imageChecksum = state.Get("image_checksum").(string)
passwordEnabled = !config.TemplateDisablePassword
sshkeyEnabled = !config.TemplateDisableSSHKey
)
ui.Say("Registering Compute instance template")
resp, err := exo.GetWithContext(ctx, &egoscale.ListZones{Name: config.TemplateZone})
if err != nil {
ui.Error(fmt.Sprintf("unable to list zones: %s", err))
return multistep.ActionHalt
}
zone := resp.(*egoscale.Zone)
resp, err = exo.RequestWithContext(ctx, &egoscale.RegisterCustomTemplate{
Name: config.TemplateName,
Displaytext: config.TemplateDescription,
URL: imageURL,
Checksum: imageChecksum,
PasswordEnabled: &passwordEnabled,
SSHKeyEnabled: &sshkeyEnabled,
Details: func() map[string]string {
if config.TemplateUsername != "" {
return map[string]string{"username": config.TemplateUsername}
}
return nil
}(),
ZoneID: zone.ID,
})
if err != nil {
ui.Error(fmt.Sprintf("unable to export Compute instance snapshot: %s", err))
return multistep.ActionHalt
}
templates := resp.(*[]egoscale.Template)
state.Put("template", (*templates)[0])
return multistep.ActionContinue
}
func (s *stepRegisterTemplate) Cleanup(state multistep.StateBag) {}