packer-cn/builder/digitalocean/artifact.go

57 lines
1.2 KiB
Go
Raw Normal View History

package digitalocean
import (
2017-04-08 15:52:57 -04:00
"context"
"fmt"
"log"
"strconv"
"strings"
"github.com/digitalocean/godo"
)
type Artifact struct {
// The name of the snapshot
2018-11-22 21:33:34 -05:00
SnapshotName string
// The ID of the image
2018-11-22 21:33:34 -05:00
SnapshotId int
// The name of the region
2018-11-22 21:33:34 -05:00
RegionNames []string
// The client for making API calls
2018-11-22 21:33:34 -05:00
Client *godo.Client
// StateData should store data such as GeneratedData
// to be shared with post-processors
StateData map[string]interface{}
}
func (*Artifact) BuilderId() string {
return BuilderId
}
func (*Artifact) Files() []string {
// No files with DigitalOcean
return nil
}
func (a *Artifact) Id() string {
2018-11-22 21:33:34 -05:00
return fmt.Sprintf("%s:%s", strings.Join(a.RegionNames[:], ","), strconv.FormatUint(uint64(a.SnapshotId), 10))
}
func (a *Artifact) String() string {
2018-11-22 21:33:34 -05:00
return fmt.Sprintf("A snapshot was created: '%v' (ID: %v) in regions '%v'", a.SnapshotName, a.SnapshotId, strings.Join(a.RegionNames[:], ","))
}
func (a *Artifact) State(name string) interface{} {
return a.StateData[name]
}
func (a *Artifact) Destroy() error {
2018-11-22 21:33:34 -05:00
log.Printf("Destroying image: %d (%s)", a.SnapshotId, a.SnapshotName)
_, err := a.Client.Images.Delete(context.TODO(), a.SnapshotId)
return err
}