Commit be1e60aa authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazonebs: Validate region exists

parent 4fa27e6a
...@@ -7,6 +7,7 @@ package amazonebs ...@@ -7,6 +7,7 @@ package amazonebs
import ( import (
"errors" "errors"
"fmt"
"github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
...@@ -70,6 +71,8 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -70,6 +71,8 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
if b.config.Region == "" { if b.config.Region == "" {
errs = append(errs, errors.New("A region must be specified")) errs = append(errs, errors.New("A region must be specified"))
} else if _, ok := aws.Regions[b.config.Region]; !ok {
errs = append(errs, fmt.Errorf("Unknown region: %s", b.config.Region))
} }
if b.config.SSHUsername == "" { if b.config.SSHUsername == "" {
......
...@@ -11,7 +11,7 @@ func testConfig() map[string]interface{} { ...@@ -11,7 +11,7 @@ func testConfig() map[string]interface{} {
"secret_key": "bar", "secret_key": "bar",
"source_ami": "foo", "source_ami": "foo",
"instance_type": "foo", "instance_type": "foo",
"region": "foo", "region": "us-east-1",
"ssh_username": "root", "ssh_username": "root",
} }
} }
...@@ -89,13 +89,13 @@ func TestBuilderPrepare_Region(t *testing.T) { ...@@ -89,13 +89,13 @@ func TestBuilderPrepare_Region(t *testing.T) {
config := testConfig() config := testConfig()
// Test good // Test good
config["region"] = "foo" config["region"] = "us-east-1"
err := b.Prepare(config) err := b.Prepare(config)
if err != nil { if err != nil {
t.Fatalf("should not have error: %s", err) t.Fatalf("should not have error: %s", err)
} }
if b.config.Region != "foo" { if b.config.Region != "us-east-1" {
t.Errorf("invalid: %s", b.config.Region) t.Errorf("invalid: %s", b.config.Region)
} }
...@@ -106,6 +106,14 @@ func TestBuilderPrepare_Region(t *testing.T) { ...@@ -106,6 +106,14 @@ func TestBuilderPrepare_Region(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("should have error") t.Fatal("should have error")
} }
// Test invalid
config["region"] = "i-am-not-real"
b = Builder{}
err = b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
} }
func TestBuilderPrepare_SecretKey(t *testing.T) { func TestBuilderPrepare_SecretKey(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment