2019-01-29 12:27:42 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2019-01-29 12:27:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// OMIConfig is for common configuration related to creating OMIs.
|
|
|
|
type OMIConfig struct {
|
2019-02-05 14:18:44 -05:00
|
|
|
OMIName string `mapstructure:"omi_name"`
|
|
|
|
OMIDescription string `mapstructure:"omi_description"`
|
|
|
|
OMIVirtType string `mapstructure:"omi_virtualization_type"`
|
|
|
|
OMIAccountIDs []string `mapstructure:"omi_account_ids"`
|
|
|
|
OMIGroups []string `mapstructure:"omi_groups"`
|
|
|
|
OMIProductCodes []string `mapstructure:"omi_product_codes"`
|
|
|
|
OMIRegions []string `mapstructure:"omi_regions"`
|
|
|
|
OMISkipRegionValidation bool `mapstructure:"skip_region_validation"`
|
|
|
|
OMITags TagMap `mapstructure:"tags"`
|
|
|
|
OMIForceDeregister bool `mapstructure:"force_deregister"`
|
|
|
|
OMIForceDeleteSnapshot bool `mapstructure:"force_delete_snapshot"`
|
|
|
|
SnapshotTags TagMap `mapstructure:"snapshot_tags"`
|
|
|
|
SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids"`
|
|
|
|
SnapshotGroups []string `mapstructure:"snapshot_groups"`
|
2019-01-29 12:27:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if c.OMIName == "" {
|
2019-01-30 13:48:51 -05:00
|
|
|
errs = append(errs, fmt.Errorf("omi_name must be specified"))
|
2019-01-29 12:27:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
errs = append(errs, c.prepareRegions(accessConfig)...)
|
|
|
|
|
|
|
|
if len(c.OMIName) < 3 || len(c.OMIName) > 128 {
|
2019-01-30 13:48:51 -05:00
|
|
|
errs = append(errs, fmt.Errorf("omi_name must be between 3 and 128 characters long"))
|
2019-01-29 12:27:42 -05:00
|
|
|
}
|
|
|
|
|
2019-04-05 16:39:00 -04:00
|
|
|
if c.OMIName != templateCleanResourceName(c.OMIName) {
|
2019-01-29 12:27:42 -05:00
|
|
|
errs = append(errs, fmt.Errorf("OMIName should only contain "+
|
|
|
|
"alphanumeric characters, parentheses (()), square brackets ([]), spaces "+
|
|
|
|
"( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs "+
|
2019-01-30 13:48:51 -05:00
|
|
|
"(@), or underscores(_). You can use the `clean_omi_name` template "+
|
|
|
|
"filter to automatically clean your omi name."))
|
2019-01-29 12:27:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OMIConfig) prepareRegions(accessConfig *AccessConfig) (errs []error) {
|
|
|
|
if len(c.OMIRegions) > 0 {
|
|
|
|
regionSet := make(map[string]struct{})
|
|
|
|
regions := make([]string, 0, len(c.OMIRegions))
|
|
|
|
|
|
|
|
for _, region := range c.OMIRegions {
|
|
|
|
// 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{}{}
|
|
|
|
|
|
|
|
if (accessConfig != nil) && (region == accessConfig.RawRegion) {
|
|
|
|
// make sure we don't try to copy to the region we originally
|
|
|
|
// create the OMI in.
|
2019-01-30 14:04:06 -05:00
|
|
|
log.Printf("Cannot copy OMI to OUTSCALE session region '%s', deleting it from `omi_regions`.", region)
|
2019-01-29 12:27:42 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
regions = append(regions, region)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.OMIRegions = regions
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|