github.phpd.cn/hashicorp/packer@v1.3.2/builder/alicloud/ecs/step_pre_validate.go (about)

     1  package ecs
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/denverdino/aliyungo/common"
     8  	"github.com/denverdino/aliyungo/ecs"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  type stepPreValidate struct {
    14  	AlicloudDestImageName string
    15  	ForceDelete           bool
    16  }
    17  
    18  func (s *stepPreValidate) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    19  	ui := state.Get("ui").(packer.Ui)
    20  	if s.ForceDelete {
    21  		ui.Say("Force delete flag found, skipping prevalidating image name.")
    22  		return multistep.ActionContinue
    23  	}
    24  
    25  	client := state.Get("client").(*ecs.Client)
    26  	config := state.Get("config").(*Config)
    27  	ui.Say("Prevalidating image name...")
    28  	images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
    29  		ImageName: s.AlicloudDestImageName,
    30  		RegionId:  common.Region(config.AlicloudRegion)})
    31  
    32  	if err != nil {
    33  		err := fmt.Errorf("Error querying alicloud image: %s", err)
    34  		state.Put("error", err)
    35  		ui.Error(err.Error())
    36  		return multistep.ActionHalt
    37  	}
    38  
    39  	if len(images) > 0 {
    40  		err := fmt.Errorf("Error: name conflicts with an existing alicloud image: %s", images[0].ImageId)
    41  		state.Put("error", err)
    42  		ui.Error(err.Error())
    43  		return multistep.ActionHalt
    44  	}
    45  
    46  	return multistep.ActionContinue
    47  }
    48  
    49  func (s *stepPreValidate) Cleanup(multistep.StateBag) {}