diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index 6fae6ac10..32b3af623 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -1,6 +1,7 @@ package aws import ( + "os" "strings" "unicode" @@ -36,6 +37,10 @@ func (c *Config) AWSRegion() (aws.Region, error) { return aws.Regions[c.Region], nil } + if v := os.Getenv("AWS_REGION"); v != "" { + return aws.Regions[v], nil + } + md, err := aws.GetMetaData("placement/availability-zone") if err != nil { return aws.Region{}, err diff --git a/builtin/providers/aws/resource_aws_vpc_test.go b/builtin/providers/aws/resource_aws_vpc_test.go new file mode 100644 index 000000000..30c3fe7ab --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_test.go @@ -0,0 +1,86 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/goamz/ec2" +) + +func TestAccVpc(t *testing.T) { + testAccPreCheck(t) + + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckVpcDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccVpcConfig, + Check: testAccCheckVpcExists("aws_vpc.foo"), + }, + }, + }) +} + +func testAccCheckVpcDestroy(s *terraform.State) error { + conn := testAccProvider.ec2conn + + for _, rs := range s.Resources { + if rs.Type != "aws_vpc" { + continue + } + + // Try to find the VPC + resp, err := conn.DescribeVpcs([]string{rs.ID}, ec2.NewFilter()) + if err == nil { + if len(resp.VPCs) > 0 { + return fmt.Errorf("VPCs still exist.") + } + + return nil + } + + // Verify the error is what we want + ec2err, ok := err.(*ec2.Error) + if !ok { + return err + } + if ec2err.Code != "InvalidVpcID.NotFound" { + return err + } + } + + return nil +} + +func testAccCheckVpcExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.ID == "" { + return fmt.Errorf("No VPC ID is set") + } + + conn := testAccProvider.ec2conn + resp, err := conn.DescribeVpcs([]string{rs.ID}, ec2.NewFilter()) + if err != nil { + return err + } + if len(resp.VPCs) == 0 { + return fmt.Errorf("VPC not found") + } + + return nil + } +} + +const testAccVpcConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} +` diff --git a/builtin/providers/aws/resource_provider.go b/builtin/providers/aws/resource_provider.go index f5ada7080..38ddbeba7 100644 --- a/builtin/providers/aws/resource_provider.go +++ b/builtin/providers/aws/resource_provider.go @@ -20,7 +20,15 @@ type ResourceProvider struct { } func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) { - return nil, nil + v := &config.Validator{ + Optional: []string{ + "access_key", + "secret_key", + "region", + }, + } + + return v.Validate(c) } func (p *ResourceProvider) ValidateResource( @@ -36,24 +44,24 @@ func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error { // Get the auth and region. This can fail if keys/regions were not // specified and we're attempting to use the environment. var errs []error - log.Println("Building AWS auth structure") + log.Println("[INFO] Building AWS auth structure") auth, err := p.Config.AWSAuth() if err != nil { errs = append(errs, err) } - log.Println("Building AWS region structure") + log.Println("[INFO] Building AWS region structure") region, err := p.Config.AWSRegion() if err != nil { errs = append(errs, err) } if len(errs) == 0 { - log.Println("Initializing EC2 connection") + log.Println("[INFO] Initializing EC2 connection") p.ec2conn = ec2.New(auth, region) - log.Println("Initializing ELB connection") + log.Println("[INFO] Initializing ELB connection") p.elbconn = elb.New(auth, region) - log.Println("Initializing AutoScaling connection") + log.Println("[INFO] Initializing AutoScaling connection") p.autoscalingconn = autoscaling.New(auth, region) } diff --git a/builtin/providers/aws/resource_provider_test.go b/builtin/providers/aws/resource_provider_test.go index 433e94b2e..fddcb3a39 100644 --- a/builtin/providers/aws/resource_provider_test.go +++ b/builtin/providers/aws/resource_provider_test.go @@ -1,6 +1,8 @@ package aws import ( + "os" + "log" "reflect" "testing" @@ -8,6 +10,16 @@ import ( "github.com/hashicorp/terraform/terraform" ) +var testAccProviders map[string]terraform.ResourceProvider +var testAccProvider *ResourceProvider + +func init() { + testAccProvider = new(ResourceProvider) + testAccProviders = map[string]terraform.ResourceProvider{ + "aws": testAccProvider, + } +} + func TestResourceProvider_impl(t *testing.T) { var _ terraform.ResourceProvider = new(ResourceProvider) } @@ -41,3 +53,16 @@ func TestResourceProvider_Configure(t *testing.T) { t.Fatalf("bad: %#v", rp.Config) } } + +func testAccPreCheck(t *testing.T) { + if v := os.Getenv("AWS_ACCESS_KEY"); v == "" { + t.Fatal("AWS_ACCESS_KEY must be set for acceptance tests") + } + if v := os.Getenv("AWS_SECRET_KEY"); v == "" { + t.Fatal("AWS_SECRET_KEY must be set for acceptance tests") + } + if v := os.Getenv("AWS_REGION"); v == "" { + log.Println("[INFO] Test: Using us-west-2 as test region") + os.Setenv("AWS_REGION", "us-west-2") + } +}