2013-06-17 07:01:42 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-04-08 15:52:57 -04:00
|
|
|
"context"
|
2013-06-17 07:01:42 -04:00
|
|
|
"fmt"
|
2013-06-19 00:54:15 -04:00
|
|
|
"log"
|
2014-11-24 03:14:05 -05:00
|
|
|
"strconv"
|
2017-05-16 10:38:53 -04:00
|
|
|
"strings"
|
2015-06-10 17:02:06 -04:00
|
|
|
|
|
|
|
"github.com/digitalocean/godo"
|
2013-06-17 07:01:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Artifact struct {
|
|
|
|
// The name of the snapshot
|
2018-11-22 21:33:34 -05:00
|
|
|
SnapshotName string
|
2013-06-19 00:54:15 -04:00
|
|
|
|
|
|
|
// The ID of the image
|
2018-11-22 21:33:34 -05:00
|
|
|
SnapshotId int
|
2013-06-19 00:54:15 -04:00
|
|
|
|
2013-10-05 22:29:02 -04:00
|
|
|
// The name of the region
|
2018-11-22 21:33:34 -05:00
|
|
|
RegionNames []string
|
2013-10-05 22:29:02 -04:00
|
|
|
|
2013-06-19 00:54:15 -04:00
|
|
|
// The client for making API calls
|
2018-11-22 21:33:34 -05:00
|
|
|
Client *godo.Client
|
2020-01-30 05:27:58 -05:00
|
|
|
|
|
|
|
// StateData should store data such as GeneratedData
|
|
|
|
// to be shared with post-processors
|
|
|
|
StateData map[string]interface{}
|
2013-06-17 07:01:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2013-06-17 07:01:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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[:], ","))
|
2013-06-17 07:01:42 -04:00
|
|
|
}
|
2013-06-18 19:01:14 -04:00
|
|
|
|
2014-01-19 13:32:44 -05:00
|
|
|
func (a *Artifact) State(name string) interface{} {
|
2020-01-30 05:27:58 -05:00
|
|
|
return a.StateData[name]
|
2014-01-19 13:32:44 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 19:01:14 -04:00
|
|
|
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)
|
2015-06-10 17:02:06 -04:00
|
|
|
return err
|
2013-06-18 19:01:14 -04:00
|
|
|
}
|