packer-cn/builder/osc/common/step_pre_validate.go

68 lines
1.7 KiB
Go
Raw Normal View History

2019-01-30 13:48:51 -05:00
package common
import (
"context"
"fmt"
"github.com/antihax/optional"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/outscale/osc-sdk-go/osc"
2019-01-30 13:48:51 -05:00
)
// StepPreValidate provides an opportunity to pre-validate any configuration for
// the build before actually doing any time consuming work
//
type StepPreValidate struct {
DestOmiName string
ForceDeregister bool
API string
2019-01-30 13:48:51 -05:00
}
func (s *StepPreValidate) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
2019-01-30 13:48:51 -05:00
if s.ForceDeregister {
ui.Say("Force Deregister flag found, skipping prevalidating OMI Name")
return multistep.ActionContinue
}
var (
conn = state.Get("osc").(*osc.APIClient)
images []interface{}
)
2019-01-30 13:48:51 -05:00
ui.Say(fmt.Sprintf("Prevalidating OMI Name: %s", s.DestOmiName))
resp, _, err := conn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{
Filters: osc.FiltersImage{
ImageNames: []string{s.DestOmiName},
},
}),
2019-01-30 13:48:51 -05:00
})
if err != nil {
err := fmt.Errorf("Error querying OMI: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
for _, omi := range resp.Images {
if omi.ImageName == s.DestOmiName {
images = append(images, omi)
}
}
if len(images) > 0 {
err := fmt.Errorf("Error: name conflicts with an existing OMI: %s", s.DestOmiName)
2019-01-30 13:48:51 -05:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2020-08-18 14:52:49 -04:00
return multistep.ActionContinue
2019-01-30 13:48:51 -05:00
}
func (s *StepPreValidate) Cleanup(multistep.StateBag) {}