2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-06-01 20:14:51 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepPreValidate struct {
|
|
|
|
AlicloudDestImageName string
|
|
|
|
ForceDelete bool
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
if err := s.validateRegions(state); err != nil {
|
|
|
|
return halt(state, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.validateDestImageName(state); err != nil {
|
|
|
|
return halt(state, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPreValidate) validateRegions(state multistep.StateBag) error {
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
if config.AlicloudSkipValidation {
|
|
|
|
ui.Say("Skip region validation flag found, skipping prevalidating source region and copied regions.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Prevalidating source region and copied regions...")
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
|
|
|
if err := config.ValidateRegion(config.AlicloudRegion); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
for _, region := range config.AlicloudImageDestinationRegions {
|
|
|
|
if err := config.ValidateRegion(region); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPreValidate) validateDestImageName(state multistep.StateBag) error {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if s.ForceDelete {
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Force delete flag found, skipping prevalidating image name.")
|
2019-04-25 22:37:49 -04:00
|
|
|
return nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Prevalidating image name...")
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
describeImagesRequest := ecs.CreateDescribeImagesRequest()
|
|
|
|
describeImagesRequest.RegionId = config.AlicloudRegion
|
|
|
|
describeImagesRequest.ImageName = s.AlicloudDestImageName
|
|
|
|
describeImagesRequest.Status = ImageStatusQueried
|
|
|
|
|
|
|
|
imagesResponse, err := client.DescribeImages(describeImagesRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return fmt.Errorf("Error querying alicloud image: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
images := imagesResponse.Images.Image
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(images) > 0 {
|
2019-04-25 22:37:49 -04:00
|
|
|
return fmt.Errorf("Error: Image Name: '%s' is used by an existing alicloud image: %s", images[0].ImageName, images[0].ImageId)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
return nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPreValidate) Cleanup(multistep.StateBag) {}
|