github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/aws/resource_aws_vpn_gateway_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSVpnGateway_basic(t *testing.T) { 15 var v, v2 ec2.VpnGateway 16 17 testNotEqual := func(*terraform.State) error { 18 if len(v.VpcAttachments) == 0 { 19 return fmt.Errorf("VPN gateway A is not attached") 20 } 21 if len(v2.VpcAttachments) == 0 { 22 return fmt.Errorf("VPN gateway B is not attached") 23 } 24 25 id1 := v.VpcAttachments[0].VpcId 26 id2 := v2.VpcAttachments[0].VpcId 27 if id1 == id2 { 28 return fmt.Errorf("Both attachment IDs are the same") 29 } 30 31 return nil 32 } 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckVpnGatewayDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccVpnGatewayConfig, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckVpnGatewayExists( 43 "aws_vpn_gateway.foo", &v), 44 ), 45 }, 46 47 resource.TestStep{ 48 Config: testAccVpnGatewayConfigChangeVPC, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckVpnGatewayExists( 51 "aws_vpn_gateway.foo", &v2), 52 testNotEqual, 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccAWSVpnGateway_delete(t *testing.T) { 60 var vpnGateway ec2.VpnGateway 61 62 testDeleted := func(r string) resource.TestCheckFunc { 63 return func(s *terraform.State) error { 64 _, ok := s.RootModule().Resources[r] 65 if ok { 66 return fmt.Errorf("VPN Gateway %q should have been deleted", r) 67 } 68 return nil 69 } 70 } 71 72 resource.Test(t, resource.TestCase{ 73 PreCheck: func() { testAccPreCheck(t) }, 74 Providers: testAccProviders, 75 CheckDestroy: testAccCheckVpnGatewayDestroy, 76 Steps: []resource.TestStep{ 77 resource.TestStep{ 78 Config: testAccVpnGatewayConfig, 79 Check: resource.ComposeTestCheckFunc( 80 testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &vpnGateway)), 81 }, 82 resource.TestStep{ 83 Config: testAccNoVpnGatewayConfig, 84 Check: resource.ComposeTestCheckFunc(testDeleted("aws_vpn_gateway.foo")), 85 }, 86 }, 87 }) 88 } 89 90 func TestAccAWSVpnGateway_tags(t *testing.T) { 91 var v ec2.VpnGateway 92 93 resource.Test(t, resource.TestCase{ 94 PreCheck: func() { testAccPreCheck(t) }, 95 Providers: testAccProviders, 96 CheckDestroy: testAccCheckVpnGatewayDestroy, 97 Steps: []resource.TestStep{ 98 resource.TestStep{ 99 Config: testAccCheckVpnGatewayConfigTags, 100 Check: resource.ComposeTestCheckFunc( 101 testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v), 102 testAccCheckTags(&v.Tags, "foo", "bar"), 103 ), 104 }, 105 106 resource.TestStep{ 107 Config: testAccCheckVpnGatewayConfigTagsUpdate, 108 Check: resource.ComposeTestCheckFunc( 109 testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v), 110 testAccCheckTags(&v.Tags, "foo", ""), 111 testAccCheckTags(&v.Tags, "bar", "baz"), 112 ), 113 }, 114 }, 115 }) 116 } 117 118 func testAccCheckVpnGatewayDestroy(s *terraform.State) error { 119 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 120 121 for _, rs := range s.RootModule().Resources { 122 if rs.Type != "aws_vpn_gateway" { 123 continue 124 } 125 126 // Try to find the resource 127 resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ 128 VpnGatewayIds: []*string{aws.String(rs.Primary.ID)}, 129 }) 130 if err == nil { 131 var v *ec2.VpnGateway 132 for _, g := range resp.VpnGateways { 133 if *g.VpnGatewayId == rs.Primary.ID { 134 v = g 135 } 136 } 137 138 if v == nil { 139 // wasn't found 140 return nil 141 } 142 143 if *v.State != "deleted" { 144 return fmt.Errorf("Expected VpnGateway to be in deleted state, but was not: %s", v) 145 } 146 return nil 147 } 148 149 // Verify the error is what we want 150 ec2err, ok := err.(awserr.Error) 151 if !ok { 152 return err 153 } 154 if ec2err.Code() != "InvalidVpnGatewayID.NotFound" { 155 return err 156 } 157 } 158 159 return nil 160 } 161 162 func testAccCheckVpnGatewayExists(n string, ig *ec2.VpnGateway) resource.TestCheckFunc { 163 return func(s *terraform.State) error { 164 rs, ok := s.RootModule().Resources[n] 165 if !ok { 166 return fmt.Errorf("Not found: %s", n) 167 } 168 169 if rs.Primary.ID == "" { 170 return fmt.Errorf("No ID is set") 171 } 172 173 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 174 resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ 175 VpnGatewayIds: []*string{aws.String(rs.Primary.ID)}, 176 }) 177 if err != nil { 178 return err 179 } 180 if len(resp.VpnGateways) == 0 { 181 return fmt.Errorf("VPNGateway not found") 182 } 183 184 *ig = *resp.VpnGateways[0] 185 186 return nil 187 } 188 } 189 190 const testAccNoVpnGatewayConfig = ` 191 resource "aws_vpc" "foo" { 192 cidr_block = "10.1.0.0/16" 193 } 194 ` 195 196 const testAccVpnGatewayConfig = ` 197 resource "aws_vpc" "foo" { 198 cidr_block = "10.1.0.0/16" 199 } 200 201 resource "aws_vpn_gateway" "foo" { 202 vpc_id = "${aws_vpc.foo.id}" 203 } 204 ` 205 206 const testAccVpnGatewayConfigChangeVPC = ` 207 resource "aws_vpc" "bar" { 208 cidr_block = "10.2.0.0/16" 209 } 210 211 resource "aws_vpn_gateway" "foo" { 212 vpc_id = "${aws_vpc.bar.id}" 213 } 214 ` 215 216 const testAccCheckVpnGatewayConfigTags = ` 217 resource "aws_vpc" "foo" { 218 cidr_block = "10.1.0.0/16" 219 } 220 221 resource "aws_vpn_gateway" "foo" { 222 vpc_id = "${aws_vpc.foo.id}" 223 tags { 224 foo = "bar" 225 } 226 } 227 ` 228 229 const testAccCheckVpnGatewayConfigTagsUpdate = ` 230 resource "aws_vpc" "foo" { 231 cidr_block = "10.1.0.0/16" 232 } 233 234 resource "aws_vpn_gateway" "foo" { 235 vpc_id = "${aws_vpc.foo.id}" 236 tags { 237 bar = "baz" 238 } 239 } 240 `