2013-08-09 01:50:23 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-04 17:58:11 -04:00
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-08-09 01:50:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AMIConfig is for common configuration related to creating AMIs.
|
|
|
|
type AMIConfig struct {
|
2014-06-04 17:58:11 -04:00
|
|
|
AMIName string `mapstructure:"ami_name"`
|
|
|
|
AMIDescription string `mapstructure:"ami_description"`
|
|
|
|
AMIVirtType string `mapstructure:"ami_virtualization_type"`
|
|
|
|
AMIUsers []string `mapstructure:"ami_users"`
|
|
|
|
AMIGroups []string `mapstructure:"ami_groups"`
|
|
|
|
AMIProductCodes []string `mapstructure:"ami_product_codes"`
|
|
|
|
AMIRegions []string `mapstructure:"ami_regions"`
|
|
|
|
AMITags map[string]string `mapstructure:"tags"`
|
|
|
|
AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"`
|
2013-08-09 01:50:23 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
2013-08-09 01:50:23 -04:00
|
|
|
if c.AMIName == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("ami_name must be specified"))
|
|
|
|
}
|
|
|
|
|
2013-08-21 21:44:14 -04:00
|
|
|
if len(c.AMIRegions) > 0 {
|
2013-08-22 17:58:21 -04:00
|
|
|
regionSet := make(map[string]struct{})
|
|
|
|
regions := make([]string, 0, len(c.AMIRegions))
|
|
|
|
|
2013-08-21 21:44:14 -04:00
|
|
|
for _, region := range c.AMIRegions {
|
2013-08-22 17:58:21 -04:00
|
|
|
// If we already saw the region, then don't look again
|
|
|
|
if _, ok := regionSet[region]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that we saw the region
|
|
|
|
regionSet[region] = struct{}{}
|
|
|
|
|
|
|
|
// Verify the region is real
|
2015-04-05 17:58:48 -04:00
|
|
|
if valid := ValidateRegion(region); valid == false {
|
2013-08-21 21:44:14 -04:00
|
|
|
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
|
2013-08-22 17:58:21 -04:00
|
|
|
continue
|
2013-08-21 21:44:14 -04:00
|
|
|
}
|
2013-08-22 17:58:21 -04:00
|
|
|
|
|
|
|
regions = append(regions, region)
|
2013-08-21 21:44:14 -04:00
|
|
|
}
|
2013-08-22 17:58:21 -04:00
|
|
|
|
|
|
|
c.AMIRegions = regions
|
2013-08-21 21:44:14 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 01:50:23 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|