github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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  	AlicloudDiskDevices               `mapstructure:",squash"`
    40  }
    41  
    42  func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error {
    43  	var errs []error
    44  	if c.AlicloudImageName == "" {
    45  		errs = append(errs, fmt.Errorf("image_name must be specified"))
    46  	} else if len(c.AlicloudImageName) < 2 || len(c.AlicloudImageName) > 128 {
    47  		errs = append(errs, fmt.Errorf("image_name must less than 128 letters and more than 1 letters"))
    48  	} else if strings.HasPrefix(c.AlicloudImageName, "http://") ||
    49  		strings.HasPrefix(c.AlicloudImageName, "https://") {
    50  		errs = append(errs, fmt.Errorf("image_name can't start with 'http://' or 'https://'"))
    51  	}
    52  	reg := regexp.MustCompile("\\s+")
    53  	if reg.FindString(c.AlicloudImageName) != "" {
    54  		errs = append(errs, fmt.Errorf("image_name can't include spaces"))
    55  	}
    56  
    57  	if len(c.AlicloudImageDestinationRegions) > 0 {
    58  		regionSet := make(map[string]struct{})
    59  		regions := make([]string, 0, len(c.AlicloudImageDestinationRegions))
    60  
    61  		for _, region := range c.AlicloudImageDestinationRegions {
    62  			// If we already saw the region, then don't look again
    63  			if _, ok := regionSet[region]; ok {
    64  				continue
    65  			}
    66  
    67  			// Mark that we saw the region
    68  			regionSet[region] = struct{}{}
    69  
    70  			if !c.AlicloudImageSkipRegionValidation {
    71  				// Verify the region is real
    72  				if valid := validateRegion(region); valid != nil {
    73  					errs = append(errs, fmt.Errorf("Unknown region: %s", region))
    74  					continue
    75  				}
    76  			}
    77  
    78  			regions = append(regions, region)
    79  		}
    80  
    81  		c.AlicloudImageDestinationRegions = regions
    82  	}
    83  
    84  	if len(errs) > 0 {
    85  		return errs
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func validateRegion(region string) error {
    92  
    93  	for _, valid := range common.ValidRegions {
    94  		if region == string(valid) {
    95  			return nil
    96  		}
    97  	}
    98  
    99  	return fmt.Errorf("Not a valid alicloud region: %s", region)
   100  }