github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/common/ami_config.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/packer"
     6  )
     7  
     8  // AMIConfig is for common configuration related to creating AMIs.
     9  type AMIConfig struct {
    10  	AMIName         string   `mapstructure:"ami_name"`
    11  	AMIDescription  string   `mapstructure:"ami_description"`
    12  	AMIUsers        []string `mapstructure:"ami_users"`
    13  	AMIGroups       []string `mapstructure:"ami_groups"`
    14  	AMIProductCodes []string `mapstructure:"ami_product_codes"`
    15  }
    16  
    17  func (c *AMIConfig) Prepare(t *packer.ConfigTemplate) []error {
    18  	if t == nil {
    19  		var err error
    20  		t, err = packer.NewConfigTemplate()
    21  		if err != nil {
    22  			return []error{err}
    23  		}
    24  	}
    25  
    26  	templates := map[string]*string{
    27  		"ami_name":        &c.AMIName,
    28  		"ami_description": &c.AMIDescription,
    29  	}
    30  
    31  	errs := make([]error, 0)
    32  	for n, ptr := range templates {
    33  		var err error
    34  		*ptr, err = t.Process(*ptr, nil)
    35  		if err != nil {
    36  			errs = append(
    37  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    38  		}
    39  	}
    40  
    41  	sliceTemplates := map[string][]string{
    42  		"ami_users":         c.AMIUsers,
    43  		"ami_groups":        c.AMIGroups,
    44  		"ami_product_codes": c.AMIProductCodes,
    45  	}
    46  
    47  	for n, slice := range sliceTemplates {
    48  		for i, elem := range slice {
    49  			var err error
    50  			slice[i], err = t.Process(elem, nil)
    51  			if err != nil {
    52  				errs = append(
    53  					errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
    54  			}
    55  		}
    56  	}
    57  
    58  	if c.AMIName == "" {
    59  		errs = append(errs, fmt.Errorf("ami_name must be specified"))
    60  	}
    61  
    62  	if len(errs) > 0 {
    63  		return errs
    64  	}
    65  
    66  	return nil
    67  }