2017-04-06 05:19:17 -04:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
"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 {
|
2017-04-11 06:19:28 -04:00
|
|
|
// 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
|
2017-04-11 06:19:28 -04:00
|
|
|
snapshotID string
|
2017-04-06 05:19:17 -04:00
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
// The name of the zone
|
|
|
|
zoneName string
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
// The client for making API calls
|
2020-09-09 04:27:48 -04:00
|
|
|
client *scw.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{}
|
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 {
|
2020-09-09 04:27:48 -04:00
|
|
|
return fmt.Sprintf("%s:%s", a.zoneName, a.imageID)
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) String() string {
|
2020-09-09 04:27:48 -04:00
|
|
|
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{} {
|
2020-01-30 05:27:58 -05:00
|
|
|
return a.StateData[name]
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) Destroy() error {
|
2017-04-11 06:19:28 -04:00
|
|
|
log.Printf("Destroying image: %s (%s)", a.imageID, a.imageName)
|
2020-09-09 04:27:48 -04:00
|
|
|
instanceAPI := instance.NewAPI(a.client)
|
|
|
|
|
|
|
|
err := instanceAPI.DeleteImage(&instance.DeleteImageRequest{
|
|
|
|
ImageID: a.imageID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-04-11 06:19:28 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("Destroying snapshot: %s (%s)", a.snapshotID, a.snapshotName)
|
2020-09-09 04:27:48 -04:00
|
|
|
err = instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{
|
|
|
|
SnapshotID: a.snapshotID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-04-11 06:19:28 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|