github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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  	AMIForceDeleteSnapshot  bool              `mapstructure:"force_delete_snapshot"`
    23  	AMIEncryptBootVolume    bool              `mapstructure:"encrypt_boot"`
    24  	AMIKmsKeyId             string            `mapstructure:"kms_key_id"`
    25  	SnapshotTags            map[string]string `mapstructure:"snapshot_tags"`
    26  	SnapshotUsers           []string          `mapstructure:"snapshot_users"`
    27  	SnapshotGroups          []string          `mapstructure:"snapshot_groups"`
    28  }
    29  
    30  func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error {
    31  	var errs []error
    32  	if c.AMIName == "" {
    33  		errs = append(errs, fmt.Errorf("ami_name must be specified"))
    34  	}
    35  
    36  	if len(c.AMIRegions) > 0 {
    37  		regionSet := make(map[string]struct{})
    38  		regions := make([]string, 0, len(c.AMIRegions))
    39  
    40  		for _, region := range c.AMIRegions {
    41  			// If we already saw the region, then don't look again
    42  			if _, ok := regionSet[region]; ok {
    43  				continue
    44  			}
    45  
    46  			// Mark that we saw the region
    47  			regionSet[region] = struct{}{}
    48  
    49  			if !c.AMISkipRegionValidation {
    50  				// Verify the region is real
    51  				if valid := ValidateRegion(region); valid == false {
    52  					errs = append(errs, fmt.Errorf("Unknown region: %s", region))
    53  					continue
    54  				}
    55  			}
    56  
    57  			regions = append(regions, region)
    58  		}
    59  
    60  		c.AMIRegions = regions
    61  	}
    62  
    63  	if len(c.AMIUsers) > 0 && c.AMIEncryptBootVolume {
    64  		errs = append(errs, fmt.Errorf("Cannot share AMI with encrypted boot volume"))
    65  	}
    66  
    67  	if len(c.SnapshotUsers) > 0 && len(c.AMIKmsKeyId) == 0 && c.AMIEncryptBootVolume {
    68  		errs = append(errs, fmt.Errorf("Cannot share snapshot encrypted with default KMS key"))
    69  	}
    70  
    71  	if len(errs) > 0 {
    72  		return errs
    73  	}
    74  
    75  	return nil
    76  }