2014-07-11 00:10:05 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
2014-07-11 00:10:05 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSSubnet(t *testing.T) {
|
|
|
|
var v ec2.Subnet
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
2015-03-02 23:32:48 +01:00
|
|
|
if *v.CIDRBlock != "10.1.1.0/24" {
|
2015-03-05 21:43:52 +01:00
|
|
|
return fmt.Errorf("bad cidr: %s", *v.CIDRBlock)
|
2014-07-11 00:10:05 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
if *v.MapPublicIPOnLaunch != true {
|
2015-03-05 21:43:52 +01:00
|
|
|
return fmt.Errorf("bad MapPublicIpOnLaunch: %t", *v.MapPublicIPOnLaunch)
|
2014-09-10 07:13:16 +02:00
|
|
|
}
|
2014-09-16 16:37:22 +02:00
|
|
|
|
2014-07-11 00:10:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckSubnetDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccSubnetConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckSubnetExists(
|
|
|
|
"aws_subnet.foo", &v),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckSubnetDestroy(s *terraform.State) error {
|
2015-03-02 23:32:48 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
|
2014-07-11 00:10:05 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-11 00:10:05 +02:00
|
|
|
if rs.Type != "aws_subnet" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
2015-03-02 23:32:48 +01:00
|
|
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
|
|
|
SubnetIDs: []string{rs.Primary.ID},
|
|
|
|
})
|
2014-07-11 00:10:05 +02:00
|
|
|
if err == nil {
|
|
|
|
if len(resp.Subnets) > 0 {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-03-02 23:32:48 +01:00
|
|
|
ec2err, ok := err.(aws.APIError)
|
2014-07-11 00:10:05 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ec2err.Code != "InvalidSubnetID.NotFound" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-11 00:10:05 +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-11 00:10:05 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2015-03-02 23:32:48 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
|
|
|
|
resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
|
|
|
|
SubnetIDs: []string{rs.Primary.ID},
|
|
|
|
})
|
2014-07-11 00:10:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.Subnets) == 0 {
|
|
|
|
return fmt.Errorf("Subnet not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = resp.Subnets[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccSubnetConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-09-10 07:13:16 +02:00
|
|
|
map_public_ip_on_launch = true
|
2014-07-11 00:10:05 +02:00
|
|
|
}
|
|
|
|
`
|