packer-cn/builder/oneandone/step_take_snapshot.go

53 lines
1.1 KiB
Go
Raw Normal View History

2016-11-13 17:34:36 -05:00
package oneandone
import (
"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{}
func (s *stepTakeSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
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) {
}