packer-cn/builder/scaleway/artifact.go

76 lines
1.5 KiB
Go
Raw Normal View History

2017-04-06 05:19:17 -04:00
package scaleway
import (
"fmt"
"log"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
2017-04-06 05:19:17 -04:00
)
type Artifact struct {
// The name of the image
imageName string
// The ID of the image
imageID string
2017-04-06 05:19:17 -04:00
// The name of the snapshot
snapshotName string
// The ID of the snapshot
snapshotID string
2017-04-06 05:19:17 -04:00
// The name of the zone
zoneName string
2017-04-06 05:19:17 -04:00
// The client for making API calls
client *scw.Client
// StateData should store data such as GeneratedData
// to be shared with post-processors
StateData map[string]interface{}
2017-04-06 05:19:17 -04:00
}
func (*Artifact) BuilderId() string {
return BuilderId
}
func (*Artifact) Files() []string {
// No files with Scaleway
return nil
}
func (a *Artifact) Id() string {
return fmt.Sprintf("%s:%s", a.zoneName, a.imageID)
2017-04-06 05:19:17 -04:00
}
func (a *Artifact) String() string {
return fmt.Sprintf("An image was created: '%v' (ID: %v) in zone '%v' based on snapshot '%v' (ID: %v)",
a.imageName, a.imageID, a.zoneName, a.snapshotName, a.snapshotID)
2017-04-06 05:19:17 -04:00
}
func (a *Artifact) State(name string) interface{} {
return a.StateData[name]
2017-04-06 05:19:17 -04:00
}
func (a *Artifact) Destroy() error {
log.Printf("Destroying image: %s (%s)", a.imageID, a.imageName)
instanceAPI := instance.NewAPI(a.client)
err := instanceAPI.DeleteImage(&instance.DeleteImageRequest{
ImageID: a.imageID,
})
if err != nil {
return err
}
log.Printf("Destroying snapshot: %s (%s)", a.snapshotID, a.snapshotName)
err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
SnapshotID: a.snapshotID,
})
if err != nil {
return err
}
return nil
2017-04-06 05:19:17 -04:00
}