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