generated from hashicorp/packer-plugin-scaffolding
/
artifact.go
72 lines (57 loc) · 1.61 KB
/
artifact.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
62
63
64
65
66
67
68
69
70
71
72
package exoscaleimport
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/packer-plugin-sdk/multistep"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
const BuilderId = "packer.post-processor.exoscale-import"
type Artifact struct {
StateData map[string]interface{}
postProcessor *PostProcessor
state *multistep.BasicStateBag
templates []*egoscale.Template
}
func (a *Artifact) BuilderId() string {
return BuilderId
}
func (a *Artifact) Id() string {
return *a.templates[0].ID
}
func (a *Artifact) Files() []string {
return nil
}
func (a *Artifact) String() string {
templateName := *a.templates[0].Name
templateID := *a.templates[0].ID
templateZones := []string{}
for i := 0; i < len(a.templates); i++ {
templateZones = append(templateZones, *a.templates[0].Zone)
}
return fmt.Sprintf(
"%s @ %s (%s)",
templateName,
strings.Join(templateZones, ","),
templateID,
)
}
func (a *Artifact) State(name string) interface{} {
return a.StateData[name]
}
func (a *Artifact) Destroy() error {
// Nota Bene: a single DeleteTemplate deletes a given template (ID) across ALL zones [sc-37437]
// (iow. templates created in additional zones by CopyTemplate are deleted too)
ctx := exoapi.WithEndpoint(
context.Background(),
exoapi.NewReqEndpoint(a.postProcessor.config.APIEnvironment, *a.templates[0].Zone),
)
return a.postProcessor.exo.DeleteTemplate(ctx, *a.templates[0].Zone, a.templates[0])
}
func (a *Artifact) Template() *egoscale.Template {
return a.templates[0]
}
func (a *Artifact) Templates() []*egoscale.Template {
return a.templates
}