generated from hashicorp/packer-plugin-scaffolding
/
artifact.go
62 lines (51 loc) · 1.42 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
package lightsail
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lightsail"
)
// packersdk.Artifact implementation
type Artifact struct {
Name string
RegionNames []string
creds *credentials.Credentials
StateData map[string]interface{}
}
func (*Artifact) BuilderId() string {
return BuilderId
}
func (a *Artifact) Files() []string {
return []string{}
}
func (a *Artifact) Id() string {
return fmt.Sprintf("%s:%s", strings.Join(a.RegionNames[:], ","), a.Name)
}
func (a *Artifact) String() string {
return fmt.Sprintf("A snapshot was created: '%v' in regions '%v'", a.Name, strings.Join(a.RegionNames[:], ","))
}
func (a *Artifact) State(name string) interface{} {
return a.StateData[name]
}
func (a *Artifact) Destroy() error {
log.Printf("Deleting snapshot \"%s\"", a.Name)
awsCfg := &aws.Config{
Credentials: a.creds,
Region: &a.RegionNames[0],
}
newSession, err := session.NewSession(awsCfg)
if err != nil {
return fmt.Errorf("Failed setting up aws session: %v", newSession)
}
lsClient := lightsail.New(newSession)
_, err = lsClient.DeleteInstanceSnapshot(&lightsail.DeleteInstanceSnapshotInput{
InstanceSnapshotName: aws.String(a.Name),
})
if err != nil {
return fmt.Errorf("Failed to delete snapshot \"%s\": %s", a.Name, err)
}
return nil
}