Merge pull request #3598 from owjjh/master

Adding ability to ignore region validation for AWS
This commit is contained in:
Chris Bednarski 2016-06-07 17:09:11 -07:00
commit 64fbb49416
7 changed files with 58 additions and 22 deletions

View File

@ -20,6 +20,7 @@ type AccessConfig struct {
AccessKey string `mapstructure:"access_key"` AccessKey string `mapstructure:"access_key"`
SecretKey string `mapstructure:"secret_key"` SecretKey string `mapstructure:"secret_key"`
RawRegion string `mapstructure:"region"` RawRegion string `mapstructure:"region"`
SkipValidation bool `mapstructure:"skip_region_validation"`
Token string `mapstructure:"token"` Token string `mapstructure:"token"`
ProfileName string `mapstructure:"profile"` ProfileName string `mapstructure:"profile"`
} }
@ -65,9 +66,11 @@ func (c *AccessConfig) Config() (*aws.Config, error) {
// the region from the instance metadata if possible. // the region from the instance metadata if possible.
func (c *AccessConfig) Region() (string, error) { func (c *AccessConfig) Region() (string, error) {
if c.RawRegion != "" { if c.RawRegion != "" {
if !c.SkipValidation {
if valid := ValidateRegion(c.RawRegion); valid == false { if valid := ValidateRegion(c.RawRegion); valid == false {
return "", fmt.Errorf("Not a valid region: %s", c.RawRegion) return "", fmt.Errorf("Not a valid region: %s", c.RawRegion)
} }
}
return c.RawRegion, nil return c.RawRegion, nil
} }
@ -82,7 +85,7 @@ func (c *AccessConfig) Region() (string, error) {
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error { func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error var errs []error
if c.RawRegion != "" { if c.RawRegion != "" && !c.SkipValidation {
if valid := ValidateRegion(c.RawRegion); valid == false { if valid := ValidateRegion(c.RawRegion); valid == false {
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion)) errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
} }

View File

@ -24,4 +24,17 @@ func TestAccessConfigPrepare_Region(t *testing.T) {
if err := c.Prepare(nil); err != nil { if err := c.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %s", err) t.Fatalf("shouldn't have err: %s", err)
} }
c.RawRegion = "custom"
if err := c.Prepare(nil); err == nil {
t.Fatalf("should have err")
}
c.RawRegion = "custom"
c.SkipValidation = true
if err := c.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %s", err)
}
c.SkipValidation = false
} }

View File

@ -15,6 +15,7 @@ type AMIConfig struct {
AMIGroups []string `mapstructure:"ami_groups"` AMIGroups []string `mapstructure:"ami_groups"`
AMIProductCodes []string `mapstructure:"ami_product_codes"` AMIProductCodes []string `mapstructure:"ami_product_codes"`
AMIRegions []string `mapstructure:"ami_regions"` AMIRegions []string `mapstructure:"ami_regions"`
AMISkipRegionValidation bool `mapstructure:"skip_region_validation"`
AMITags map[string]string `mapstructure:"tags"` AMITags map[string]string `mapstructure:"tags"`
AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"` AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"`
AMIForceDeregister bool `mapstructure:"force_deregister"` AMIForceDeregister bool `mapstructure:"force_deregister"`
@ -39,11 +40,13 @@ func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error {
// Mark that we saw the region // Mark that we saw the region
regionSet[region] = struct{}{} regionSet[region] = struct{}{}
if !c.AMISkipRegionValidation {
// Verify the region is real // Verify the region is real
if valid := ValidateRegion(region); valid == false { if valid := ValidateRegion(region); valid == false {
errs = append(errs, fmt.Errorf("Unknown region: %s", region)) errs = append(errs, fmt.Errorf("Unknown region: %s", region))
continue continue
} }
}
regions = append(regions, region) regions = append(regions, region)
} }

View File

@ -49,4 +49,12 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
if !reflect.DeepEqual(c.AMIRegions, expected) { if !reflect.DeepEqual(c.AMIRegions, expected) {
t.Fatalf("bad: %#v", c.AMIRegions) t.Fatalf("bad: %#v", c.AMIRegions)
} }
c.AMIRegions = []string{"custom"}
c.AMISkipRegionValidation = true
if err := c.Prepare(nil); err != nil {
t.Fatal("shouldn't have error")
}
c.AMISkipRegionValidation = false
} }

View File

@ -146,6 +146,9 @@ builder.
- `root_volume_size` (integer) - The size of the root volume for the chroot - `root_volume_size` (integer) - The size of the root volume for the chroot
environment, and the resulting AMI environment, and the resulting AMI
- `skip_region_validation` (boolean) - Set to true if you want to skip
validation of the ami_regions configuration option. Defaults to false.
- `tags` (object of key/value strings) - Tags applied to the AMI. - `tags` (object of key/value strings) - Tags applied to the AMI.
## Basic Example ## Basic Example

View File

@ -166,6 +166,9 @@ builder.
described above. Note that if this is specified, you must omit the described above. Note that if this is specified, you must omit the
`security_group_id`. `security_group_id`.
- `skip_region_validation` (boolean) - Set to true if you want to skip
validation of the region configuration option. Defaults to false.
- `spot_price` (string) - The maximum hourly price to pay for a spot instance - `spot_price` (string) - The maximum hourly price to pay for a spot instance
to create the AMI. Spot instances are a type of instance that EC2 starts to create the AMI. Spot instances are a type of instance that EC2 starts
when the current spot price is less than the maximum price you specify. Spot when the current spot price is less than the maximum price you specify. Spot

View File

@ -191,6 +191,9 @@ builder.
described above. Note that if this is specified, you must omit the described above. Note that if this is specified, you must omit the
`security_group_id`. `security_group_id`.
- `skip_region_validation` (boolean) - Set to true if you want to skip
validation of the region configuration option. Defaults to false.
- `spot_price` (string) - The maximum hourly price to launch a spot instance - `spot_price` (string) - The maximum hourly price to launch a spot instance
to create the AMI. It is a type of instances that EC2 starts when the to create the AMI. It is a type of instances that EC2 starts when the
maximum price that you specify exceeds the current spot price. Spot price maximum price that you specify exceeds the current spot price. Spot price