github.com/rothwerx/packer@v0.9.0/builder/amazon/common/ami_config.go (about) 1 package common 2 3 import ( 4 "fmt" 5 6 "github.com/mitchellh/packer/template/interpolate" 7 ) 8 9 // AMIConfig is for common configuration related to creating AMIs. 10 type AMIConfig struct { 11 AMIName string `mapstructure:"ami_name"` 12 AMIDescription string `mapstructure:"ami_description"` 13 AMIVirtType string `mapstructure:"ami_virtualization_type"` 14 AMIUsers []string `mapstructure:"ami_users"` 15 AMIGroups []string `mapstructure:"ami_groups"` 16 AMIProductCodes []string `mapstructure:"ami_product_codes"` 17 AMIRegions []string `mapstructure:"ami_regions"` 18 AMITags map[string]string `mapstructure:"tags"` 19 AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"` 20 AMIForceDeregister bool `mapstructure:"force_deregister"` 21 } 22 23 func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error { 24 var errs []error 25 if c.AMIName == "" { 26 errs = append(errs, fmt.Errorf("ami_name must be specified")) 27 } 28 29 if len(c.AMIRegions) > 0 { 30 regionSet := make(map[string]struct{}) 31 regions := make([]string, 0, len(c.AMIRegions)) 32 33 for _, region := range c.AMIRegions { 34 // If we already saw the region, then don't look again 35 if _, ok := regionSet[region]; ok { 36 continue 37 } 38 39 // Mark that we saw the region 40 regionSet[region] = struct{}{} 41 42 // Verify the region is real 43 if valid := ValidateRegion(region); valid == false { 44 errs = append(errs, fmt.Errorf("Unknown region: %s", region)) 45 continue 46 } 47 48 regions = append(regions, region) 49 } 50 51 c.AMIRegions = regions 52 } 53 54 if len(errs) > 0 { 55 return errs 56 } 57 58 return nil 59 }