generated from hashicorp/packer-plugin-scaffolding
/
step_create_template.go
60 lines (52 loc) · 1.46 KB
/
step_create_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
package gridscale
import (
"context"
"errors"
"fmt"
"github.com/gridscale/gsclient-go/v3"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepCreateTemplate struct {
client gsclient.TemplateOperator
config *Config
ui packer.Ui
}
func (s *stepCreateTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := s.client
c := s.config
ui := s.ui
// Get snapshot UUID
snapshotUUID, ok := state.Get("snapshot_uuid").(string)
if !ok {
err := errors.New("cannot convert snapshot_uuid to string")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
if snapshotUUID == "" {
err := errors.New("snapshot_uuid is empty")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Creating template: %v", c.TemplateName))
template, err := client.CreateTemplate(
context.Background(),
gsclient.TemplateCreateRequest{
Name: c.TemplateName,
SnapshotUUID: snapshotUUID,
})
if err != nil {
err := fmt.Errorf("Error creating template: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("template_uuid", template.ObjectUUID)
ui.Say(fmt.Sprintf("Created template %v with uuid: %v", c.TemplateName, template.ObjectUUID))
return multistep.ActionContinue
}
func (s *stepCreateTemplate) Cleanup(state multistep.StateBag) {
// no cleanup
}