github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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  	AMISkipRegionValidation bool              `mapstructure:"skip_region_validation"`
    19  	AMITags                 map[string]string `mapstructure:"tags"`
    20  	AMIEnhancedNetworking   bool              `mapstructure:"enhanced_networking"`
    21  	AMIForceDeregister      bool              `mapstructure:"force_deregister"`
    22  }
    23  
    24  func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error {
    25  	var errs []error
    26  	if c.AMIName == "" {
    27  		errs = append(errs, fmt.Errorf("ami_name must be specified"))
    28  	}
    29  
    30  	if len(c.AMIRegions) > 0 {
    31  		regionSet := make(map[string]struct{})
    32  		regions := make([]string, 0, len(c.AMIRegions))
    33  
    34  		for _, region := range c.AMIRegions {
    35  			// If we already saw the region, then don't look again
    36  			if _, ok := regionSet[region]; ok {
    37  				continue
    38  			}
    39  
    40  			// Mark that we saw the region
    41  			regionSet[region] = struct{}{}
    42  
    43  			if !c.AMISkipRegionValidation {
    44  				// Verify the region is real
    45  				if valid := ValidateRegion(region); valid == false {
    46  					errs = append(errs, fmt.Errorf("Unknown region: %s", region))
    47  					continue
    48  				}
    49  			}
    50  
    51  			regions = append(regions, region)
    52  		}
    53  
    54  		c.AMIRegions = regions
    55  	}
    56  
    57  	if len(errs) > 0 {
    58  		return errs
    59  	}
    60  
    61  	return nil
    62  }