2016-11-13 17:34:36 -05:00
|
|
|
package oneandone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1and1/oneandone-cloudserver-sdk-go"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepTakeSnapshot struct{}
|
|
|
|
|
|
|
|
func (s *stepTakeSnapshot) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
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) {
|
|
|
|
}
|