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

     1  package ecs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/denverdino/aliyungo/common"
    10  	"github.com/hashicorp/packer/template/interpolate"
    11  )
    12  
    13  type AlicloudDiskDevice struct {
    14  	DiskName           string `mapstructure:"disk_name"`
    15  	DiskCategory       string `mapstructure:"disk_category"`
    16  	DiskSize           int    `mapstructure:"disk_size"`
    17  	SnapshotId         string `mapstructure:"disk_snapshot_id"`
    18  	Description        string `mapstructure:"disk_description"`
    19  	DeleteWithInstance bool   `mapstructure:"disk_delete_with_instance"`
    20  	Device             string `mapstructure:"disk_device"`
    21  }
    22  
    23  type AlicloudDiskDevices struct {
    24  	ECSImagesDiskMappings []AlicloudDiskDevice `mapstructure:"image_disk_mappings"`
    25  }
    26  
    27  type AlicloudImageConfig struct {
    28  	AlicloudImageName                 string            `mapstructure:"image_name"`
    29  	AlicloudImageVersion              string            `mapstructure:"image_version"`
    30  	AlicloudImageDescription          string            `mapstructure:"image_description"`
    31  	AlicloudImageShareAccounts        []string          `mapstructure:"image_share_account"`
    32  	AlicloudImageUNShareAccounts      []string          `mapstructure:"image_unshare_account"`
    33  	AlicloudImageDestinationRegions   []string          `mapstructure:"image_copy_regions"`
    34  	AlicloudImageDestinationNames     []string          `mapstructure:"image_copy_names"`
    35  	AlicloudImageForceDelete          bool              `mapstructure:"image_force_delete"`
    36  	AlicloudImageForceDeleteSnapshots bool              `mapstructure:"image_force_delete_snapshots"`
    37  	AlicloudImageForceDeleteInstances bool              `mapstructure:"image_force_delete_instances"`
    38  	AlicloudImageSkipRegionValidation bool              `mapstructure:"skip_region_validation"`
    39  	AlicloudImageTags                 map[string]string `mapstructure:"tags"`
    40  	AlicloudDiskDevices               `mapstructure:",squash"`
    41  }
    42  
    43  func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error {
    44  	var errs []error
    45  	if c.AlicloudImageName == "" {
    46  		errs = append(errs, fmt.Errorf("image_name must be specified"))
    47  	} else if len(c.AlicloudImageName) < 2 || len(c.AlicloudImageName) > 128 {
    48  		errs = append(errs, fmt.Errorf("image_name must less than 128 letters and more than 1 letters"))
    49  	} else if strings.HasPrefix(c.AlicloudImageName, "http://") ||
    50  		strings.HasPrefix(c.AlicloudImageName, "https://") {
    51  		errs = append(errs, fmt.Errorf("image_name can't start with 'http://' or 'https://'"))
    52  	}
    53  	reg := regexp.MustCompile("\\s+")
    54  	if reg.FindString(c.AlicloudImageName) != "" {
    55  		errs = append(errs, fmt.Errorf("image_name can't include spaces"))
    56  	}
    57  
    58  	if len(c.AlicloudImageDestinationRegions) > 0 {
    59  		regionSet := make(map[string]struct{})
    60  		regions := make([]string, 0, len(c.AlicloudImageDestinationRegions))
    61  
    62  		for _, region := range c.AlicloudImageDestinationRegions {
    63  			// If we already saw the region, then don't look again
    64  			if _, ok := regionSet[region]; ok {
    65  				continue
    66  			}
    67  
    68  			// Mark that we saw the region
    69  			regionSet[region] = struct{}{}
    70  
    71  			if !c.AlicloudImageSkipRegionValidation {
    72  				// Verify the region is real
    73  				if valid := validateRegion(region); valid != nil {
    74  					errs = append(errs, fmt.Errorf("Unknown region: %s", region))
    75  					continue
    76  				}
    77  			}
    78  
    79  			regions = append(regions, region)
    80  		}
    81  
    82  		c.AlicloudImageDestinationRegions = regions
    83  	}
    84  
    85  	if len(errs) > 0 {
    86  		return errs
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func validateRegion(region string) error {
    93  
    94  	for _, valid := range common.ValidRegions {
    95  		if region == string(valid) {
    96  			return nil
    97  		}
    98  	}
    99  
   100  	return fmt.Errorf("Not a valid alicloud region: %s", region)
   101  }