2019-01-30 13:48:51 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-17 11:06:00 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-01-30 13:48:51 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-08-17 11:06:00 -04:00
|
|
|
"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
|
2020-08-17 11:06:27 -04:00
|
|
|
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").(packer.Ui)
|
|
|
|
if s.ForceDeregister {
|
|
|
|
ui.Say("Force Deregister flag found, skipping prevalidating OMI Name")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:06:00 -04:00
|
|
|
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))
|
2020-08-17 11:06:00 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:06:00 -04:00
|
|
|
for _, omi := range resp.Images {
|
2019-01-31 11:17:39 -05:00
|
|
|
if omi.ImageName == s.DestOmiName {
|
|
|
|
images = append(images, omi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(images) > 0 {
|
2020-08-17 11:06:00 -04:00
|
|
|
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) {}
|