packer-cn/builder/ucloud/uhost/step_pre_validate.go

69 lines
1.6 KiB
Go
Raw Normal View History

2019-06-13 03:16:49 -04:00
package uhost
import (
"context"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type stepPreValidate struct {
Region string
Zone string
ImageDestinations []ImageDestination
}
func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
if err := s.validateRegions(state); err != nil {
return halt(state, err, "")
}
if err := s.validateZones(state); err != nil {
return halt(state, err, "")
}
return multistep.ActionContinue
}
func (s *stepPreValidate) validateRegions(state multistep.StateBag) error {
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
ui.Say("Validating region and copied regions...")
var errs *packer.MultiError
if err := config.ValidateRegion(s.Region); err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
for _, imageDestination := range s.ImageDestinations {
if err := config.ValidateRegion(imageDestination.Region); err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
func (s *stepPreValidate) validateZones(state multistep.StateBag) error {
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
ui.Say("Validating availability_zone...")
var errs *packer.MultiError
if err := config.ValidateZone(s.Region, s.Zone); err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
func (s *stepPreValidate) Cleanup(multistep.StateBag) {}