2014-07-10 20:31:16 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-10-09 01:43:13 +02:00
|
|
|
func TestAccVpc_basic(t *testing.T) {
|
2014-07-10 23:00:20 +02:00
|
|
|
var vpc ec2.VPC
|
|
|
|
|
2014-07-10 20:31:16 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2014-07-10 22:12:47 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
2014-07-10 20:31:16 +02:00
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpcDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcConfig,
|
2014-07-10 22:49:09 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2014-07-10 23:00:20 +02:00
|
|
|
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
|
|
|
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
|
2014-07-10 22:49:09 +02:00
|
|
|
),
|
2014-07-10 20:31:16 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:39:14 +01:00
|
|
|
func TestAccVpc_dedicatedTenancy(t *testing.T) {
|
|
|
|
var vpc ec2.VPC
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpcDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcDedicatedConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcExists("aws_vpc.bar", &vpc),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_vpc.bar", "instance_tenancy", "dedicated"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-09 02:54:00 +02:00
|
|
|
func TestAccVpc_tags(t *testing.T) {
|
|
|
|
var vpc ec2.VPC
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpcDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
|
|
|
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
|
|
|
|
testAccCheckTags(&vpc.Tags, "foo", "bar"),
|
|
|
|
),
|
|
|
|
},
|
2014-10-09 03:21:21 +02:00
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
|
|
|
testAccCheckTags(&vpc.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&vpc.Tags, "bar", "baz"),
|
|
|
|
),
|
|
|
|
},
|
2014-10-09 02:54:00 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:26:26 +02:00
|
|
|
func TestAccVpcUpdate(t *testing.T) {
|
|
|
|
var vpc ec2.VPC
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpcDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
|
|
|
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcConfigUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcExists("aws_vpc.foo", &vpc),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_vpc.foo", "enable_dns_hostnames", "true"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:31:16 +02:00
|
|
|
func testAccCheckVpcDestroy(s *terraform.State) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-10 20:31:16 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-10 20:31:16 +02:00
|
|
|
if rs.Type != "aws_vpc" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the VPC
|
2014-09-17 02:44:42 +02:00
|
|
|
resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter())
|
2014-07-10 20:31:16 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:00:20 +02:00
|
|
|
func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc {
|
2014-07-10 22:49:09 +02:00
|
|
|
return func(s *terraform.State) error {
|
2014-07-10 23:00:20 +02:00
|
|
|
if vpc.CidrBlock != expected {
|
|
|
|
return fmt.Errorf("Bad cidr: %s", vpc.CidrBlock)
|
2014-07-10 22:49:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:00:20 +02:00
|
|
|
func testAccCheckVpcExists(n string, vpc *ec2.VPC) resource.TestCheckFunc {
|
2014-07-10 20:31:16 +02:00
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-10 20:31:16 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-10 20:31:16 +02:00
|
|
|
return fmt.Errorf("No VPC ID is set")
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-09-17 02:44:42 +02:00
|
|
|
resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter())
|
2014-07-10 20:31:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.VPCs) == 0 {
|
|
|
|
return fmt.Errorf("VPC not found")
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:00:20 +02:00
|
|
|
*vpc = resp.VPCs[0]
|
|
|
|
|
2014-07-10 20:31:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccVpcConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
`
|
2014-08-20 20:26:26 +02:00
|
|
|
|
|
|
|
const testAccVpcConfigUpdate = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
enable_dns_hostnames = true
|
|
|
|
}
|
|
|
|
`
|
2014-10-09 02:54:00 +02:00
|
|
|
|
|
|
|
const testAccVpcConfigTags = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2014-10-09 03:21:21 +02:00
|
|
|
|
|
|
|
const testAccVpcConfigTagsUpdate = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2014-12-10 11:39:14 +01:00
|
|
|
const testAccVpcDedicatedConfig = `
|
|
|
|
resource "aws_vpc" "bar" {
|
|
|
|
instance_tenancy = "dedicated"
|
|
|
|
|
|
|
|
cidr_block = "10.2.0.0/16"
|
|
|
|
}
|
|
|
|
`
|