2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-12 04:46:21 -04:00
|
|
|
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
|
2019-06-13 03:16:49 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepPreValidate struct {
|
2019-06-14 02:50:46 -04:00
|
|
|
ProjectId string
|
2019-06-13 03:16:49 -04:00
|
|
|
Region string
|
|
|
|
Zone string
|
2019-10-12 04:46:21 -04:00
|
|
|
ImageDestinations []ucloudcommon.ImageDestination
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-06-14 02:50:46 -04:00
|
|
|
if err := s.validateProjectIds(state); err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, "")
|
2019-06-14 02:50:46 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 03:16:49 -04:00
|
|
|
if err := s.validateRegions(state); err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, "")
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.validateZones(state); err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, "")
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-06-14 02:50:46 -04:00
|
|
|
func (s *stepPreValidate) validateProjectIds(state multistep.StateBag) error {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
ui.Say("Validating project_id and copied project_ids...")
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
|
|
|
if err := config.ValidateProjectId(s.ProjectId); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, imageDestination := range s.ImageDestinations {
|
|
|
|
if err := config.ValidateProjectId(imageDestination.ProjectId); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 03:16:49 -04:00
|
|
|
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) {}
|