2016-11-13 17:34:36 -05:00
|
|
|
package oneandone
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
"github.com/1and1/oneandone-cloudserver-sdk-go"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2016-11-13 17:34:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepTakeSnapshot struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepTakeSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2016-11-13 17:34:36 -05:00
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
ui.Say("Creating Snapshot...")
|
|
|
|
|
|
|
|
token := oneandone.SetToken(c.Token)
|
|
|
|
api := oneandone.New(token, c.Url)
|
|
|
|
|
|
|
|
serverId := state.Get("server_id").(string)
|
|
|
|
|
|
|
|
req := oneandone.ImageConfig{
|
|
|
|
Name: c.SnapshotName,
|
|
|
|
Description: "Packer image",
|
|
|
|
ServerId: serverId,
|
|
|
|
Frequency: "WEEKLY",
|
|
|
|
NumImages: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
img_id, img, err := api.CreateImage(&req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2016-11-15 18:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = api.WaitForState(img, "ENABLED", 10, c.Retries)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("snapshot_id", img_id)
|
|
|
|
state.Put("snapshot_name", img.Name)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepTakeSnapshot) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|