2019-01-23 08:04:05 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2019-03-22 09:53:28 -04:00
|
|
|
openapi "github.com/hyperonecom/h1-client-go"
|
2019-01-23 08:04:05 -05:00
|
|
|
)
|
|
|
|
|
2019-02-23 08:57:19 -05:00
|
|
|
type stepCreateImage struct {
|
|
|
|
imageID string
|
|
|
|
}
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*openapi.APIClient)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-01-23 08:04:05 -05:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
vmID := state.Get("vm_id").(string)
|
|
|
|
|
|
|
|
ui.Say("Creating image...")
|
|
|
|
|
|
|
|
image, _, err := client.ImageApi.ImageCreate(ctx, openapi.ImageCreate{
|
|
|
|
Name: config.ImageName,
|
|
|
|
Vm: vmID,
|
|
|
|
Service: config.ImageService,
|
|
|
|
Description: config.ImageDescription,
|
|
|
|
Tag: config.ImageTags,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error creating image: %s", formatOpenAPIError(err))
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:57:19 -05:00
|
|
|
s.imageID = image.Id
|
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
state.Put("image_id", image.Id)
|
|
|
|
state.Put("image_name", image.Name)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-02-23 08:57:19 -05:00
|
|
|
func (s *stepCreateImage) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.imageID == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*openapi.APIClient)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-02-23 08:57:19 -05:00
|
|
|
|
|
|
|
_, err := client.ImageApi.ImageDelete(context.TODO(), s.imageID)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error deleting image '%s' - consider deleting it manually: %s",
|
|
|
|
s.imageID, formatOpenAPIError(err)))
|
|
|
|
}
|
|
|
|
}
|