github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_vpn_gateway_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "google.golang.org/api/compute/v1" 11 ) 12 13 func TestAccComputeVpnGateway_basic(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckComputeVpnGatewayDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccComputeVpnGateway_basic, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckComputeVpnGatewayExists( 23 "google_compute_vpn_gateway.foobar"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func testAccCheckComputeVpnGatewayDestroy(s *terraform.State) error { 31 config := testAccProvider.Meta().(*Config) 32 project := config.Project 33 34 vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "google_compute_network" { 38 continue 39 } 40 41 region := rs.Primary.Attributes["region"] 42 name := rs.Primary.Attributes["name"] 43 44 _, err := vpnGatewaysService.Get(project, region, name).Do() 45 46 if err == nil { 47 return fmt.Errorf("Error, VPN Gateway %s in region %s still exists", 48 name, region) 49 } 50 } 51 52 return nil 53 } 54 55 func testAccCheckComputeVpnGatewayExists(n string) resource.TestCheckFunc { 56 return func(s *terraform.State) error { 57 rs, ok := s.RootModule().Resources[n] 58 if !ok { 59 return fmt.Errorf("Not found: %s", n) 60 } 61 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("No ID is set") 64 } 65 66 config := testAccProvider.Meta().(*Config) 67 name := rs.Primary.Attributes["name"] 68 region := rs.Primary.Attributes["region"] 69 project := config.Project 70 71 vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) 72 _, err := vpnGatewaysService.Get(project, region, name).Do() 73 74 if err != nil { 75 return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err) 76 } 77 78 return nil 79 } 80 } 81 82 const testAccComputeVpnGateway_basic = ` 83 resource "google_compute_network" "foobar" { 84 name = "tf-test-network" 85 ipv4_range = "10.0.0.0/16" 86 } 87 resource "google_compute_vpn_gateway" "foobar" { 88 name = "tf-test-vpn-gateway" 89 network = "${google_compute_network.foobar.self_link}" 90 region = "us-central1" 91 } `