github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_compute_router_interface_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccComputeRouterInterface_basic(t *testing.T) { 13 testId := acctest.RandString(10) 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckComputeRouterInterfaceDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccComputeRouterInterfaceBasic(testId), 21 Check: testAccCheckComputeRouterInterfaceExists( 22 "google_compute_router_interface.foobar"), 23 }, 24 resource.TestStep{ 25 Config: testAccComputeRouterInterfaceKeepRouter(testId), 26 Check: testAccCheckComputeRouterInterfaceDelete( 27 "google_compute_router_interface.foobar"), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckComputeRouterInterfaceDestroy(s *terraform.State) error { 34 config := testAccProvider.Meta().(*Config) 35 36 routersService := config.clientCompute.Routers 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "google_compute_router" { 40 continue 41 } 42 43 project, err := getTestProject(rs.Primary, config) 44 if err != nil { 45 return err 46 } 47 48 region, err := getTestRegion(rs.Primary, config) 49 if err != nil { 50 return err 51 } 52 53 routerName := rs.Primary.Attributes["router"] 54 55 _, err = routersService.Get(project, region, routerName).Do() 56 57 if err == nil { 58 return fmt.Errorf("Error, Router %s in region %s still exists", 59 routerName, region) 60 } 61 } 62 63 return nil 64 } 65 66 func testAccCheckComputeRouterInterfaceDelete(n string) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 config := testAccProvider.Meta().(*Config) 69 70 routersService := config.clientCompute.Routers 71 72 for _, rs := range s.RootModule().Resources { 73 if rs.Type != "google_compute_router_interface" { 74 continue 75 } 76 77 project, err := getTestProject(rs.Primary, config) 78 if err != nil { 79 return err 80 } 81 82 region, err := getTestRegion(rs.Primary, config) 83 if err != nil { 84 return err 85 } 86 87 name := rs.Primary.Attributes["name"] 88 routerName := rs.Primary.Attributes["router"] 89 90 router, err := routersService.Get(project, region, routerName).Do() 91 92 if err != nil { 93 return fmt.Errorf("Error Reading Router %s: %s", routerName, err) 94 } 95 96 ifaces := router.Interfaces 97 for _, iface := range ifaces { 98 99 if iface.Name == name { 100 return fmt.Errorf("Interface %s still exists on router %s/%s", name, region, router.Name) 101 } 102 } 103 } 104 105 return nil 106 } 107 } 108 109 func testAccCheckComputeRouterInterfaceExists(n string) resource.TestCheckFunc { 110 return func(s *terraform.State) error { 111 rs, ok := s.RootModule().Resources[n] 112 if !ok { 113 return fmt.Errorf("Not found: %s", n) 114 } 115 116 if rs.Primary.ID == "" { 117 return fmt.Errorf("No ID is set") 118 } 119 120 config := testAccProvider.Meta().(*Config) 121 122 project, err := getTestProject(rs.Primary, config) 123 if err != nil { 124 return err 125 } 126 127 region, err := getTestRegion(rs.Primary, config) 128 if err != nil { 129 return err 130 } 131 132 name := rs.Primary.Attributes["name"] 133 routerName := rs.Primary.Attributes["router"] 134 135 routersService := config.clientCompute.Routers 136 router, err := routersService.Get(project, region, routerName).Do() 137 138 if err != nil { 139 return fmt.Errorf("Error Reading Router %s: %s", routerName, err) 140 } 141 142 for _, iface := range router.Interfaces { 143 144 if iface.Name == name { 145 return nil 146 } 147 } 148 149 return fmt.Errorf("Interface %s not found for router %s", name, router.Name) 150 } 151 } 152 153 func testAccComputeRouterInterfaceBasic(testId string) string { 154 return fmt.Sprintf(` 155 resource "google_compute_network" "foobar" { 156 name = "router-interface-test-%s" 157 } 158 resource "google_compute_subnetwork" "foobar" { 159 name = "router-interface-test-%s" 160 network = "${google_compute_network.foobar.self_link}" 161 ip_cidr_range = "10.0.0.0/16" 162 region = "us-central1" 163 } 164 resource "google_compute_address" "foobar" { 165 name = "router-interface-test-%s" 166 region = "${google_compute_subnetwork.foobar.region}" 167 } 168 resource "google_compute_vpn_gateway" "foobar" { 169 name = "router-interface-test-%s" 170 network = "${google_compute_network.foobar.self_link}" 171 region = "${google_compute_subnetwork.foobar.region}" 172 } 173 resource "google_compute_forwarding_rule" "foobar_esp" { 174 name = "router-interface-test-%s-1" 175 region = "${google_compute_vpn_gateway.foobar.region}" 176 ip_protocol = "ESP" 177 ip_address = "${google_compute_address.foobar.address}" 178 target = "${google_compute_vpn_gateway.foobar.self_link}" 179 } 180 resource "google_compute_forwarding_rule" "foobar_udp500" { 181 name = "router-interface-test-%s-2" 182 region = "${google_compute_forwarding_rule.foobar_esp.region}" 183 ip_protocol = "UDP" 184 port_range = "500-500" 185 ip_address = "${google_compute_address.foobar.address}" 186 target = "${google_compute_vpn_gateway.foobar.self_link}" 187 } 188 resource "google_compute_forwarding_rule" "foobar_udp4500" { 189 name = "router-interface-test-%s-3" 190 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 191 ip_protocol = "UDP" 192 port_range = "4500-4500" 193 ip_address = "${google_compute_address.foobar.address}" 194 target = "${google_compute_vpn_gateway.foobar.self_link}" 195 } 196 resource "google_compute_router" "foobar"{ 197 name = "router-interface-test-%s" 198 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 199 network = "${google_compute_network.foobar.self_link}" 200 bgp { 201 asn = 64514 202 } 203 } 204 resource "google_compute_vpn_tunnel" "foobar" { 205 name = "router-interface-test-%s" 206 region = "${google_compute_forwarding_rule.foobar_udp4500.region}" 207 target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}" 208 shared_secret = "unguessable" 209 peer_ip = "8.8.8.8" 210 router = "${google_compute_router.foobar.name}" 211 } 212 resource "google_compute_router_interface" "foobar" { 213 name = "router-interface-test-%s" 214 router = "${google_compute_router.foobar.name}" 215 region = "${google_compute_router.foobar.region}" 216 ip_range = "169.254.3.1/30" 217 vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}" 218 } 219 `, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId) 220 } 221 222 func testAccComputeRouterInterfaceKeepRouter(testId string) string { 223 return fmt.Sprintf(` 224 resource "google_compute_network" "foobar" { 225 name = "router-interface-test-%s" 226 } 227 resource "google_compute_subnetwork" "foobar" { 228 name = "router-interface-test-%s" 229 network = "${google_compute_network.foobar.self_link}" 230 ip_cidr_range = "10.0.0.0/16" 231 region = "us-central1" 232 } 233 resource "google_compute_address" "foobar" { 234 name = "router-interface-test-%s" 235 region = "${google_compute_subnetwork.foobar.region}" 236 } 237 resource "google_compute_vpn_gateway" "foobar" { 238 name = "router-interface-test-%s" 239 network = "${google_compute_network.foobar.self_link}" 240 region = "${google_compute_subnetwork.foobar.region}" 241 } 242 resource "google_compute_forwarding_rule" "foobar_esp" { 243 name = "router-interface-test-%s-1" 244 region = "${google_compute_vpn_gateway.foobar.region}" 245 ip_protocol = "ESP" 246 ip_address = "${google_compute_address.foobar.address}" 247 target = "${google_compute_vpn_gateway.foobar.self_link}" 248 } 249 resource "google_compute_forwarding_rule" "foobar_udp500" { 250 name = "router-interface-test-%s-2" 251 region = "${google_compute_forwarding_rule.foobar_esp.region}" 252 ip_protocol = "UDP" 253 port_range = "500-500" 254 ip_address = "${google_compute_address.foobar.address}" 255 target = "${google_compute_vpn_gateway.foobar.self_link}" 256 } 257 resource "google_compute_forwarding_rule" "foobar_udp4500" { 258 name = "router-interface-test-%s-3" 259 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 260 ip_protocol = "UDP" 261 port_range = "4500-4500" 262 ip_address = "${google_compute_address.foobar.address}" 263 target = "${google_compute_vpn_gateway.foobar.self_link}" 264 } 265 resource "google_compute_router" "foobar"{ 266 name = "router-interface-test-%s" 267 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 268 network = "${google_compute_network.foobar.self_link}" 269 bgp { 270 asn = 64514 271 } 272 } 273 resource "google_compute_vpn_tunnel" "foobar" { 274 name = "router-interface-test-%s" 275 region = "${google_compute_forwarding_rule.foobar_udp4500.region}" 276 target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}" 277 shared_secret = "unguessable" 278 peer_ip = "8.8.8.8" 279 router = "${google_compute_router.foobar.name}" 280 } 281 `, testId, testId, testId, testId, testId, testId, testId, testId, testId) 282 }