github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_compute_router_peer_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 TestAccComputeRouterPeer_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: testAccCheckComputeRouterPeerDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccComputeRouterPeerBasic(testId), 21 Check: testAccCheckComputeRouterPeerExists( 22 "google_compute_router_peer.foobar"), 23 }, 24 resource.TestStep{ 25 Config: testAccComputeRouterPeerKeepRouter(testId), 26 Check: testAccCheckComputeRouterPeerDelete( 27 "google_compute_router_peer.foobar"), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckComputeRouterPeerDestroy(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 testAccCheckComputeRouterPeerDelete(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_peer" { 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 peers := router.BgpPeers 97 for _, peer := range peers { 98 99 if peer.Name == name { 100 return fmt.Errorf("Peer %s still exists on router %s/%s", name, region, router.Name) 101 } 102 } 103 } 104 105 return nil 106 } 107 } 108 109 func testAccCheckComputeRouterPeerExists(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 _, peer := range router.BgpPeers { 143 144 if peer.Name == name { 145 return nil 146 } 147 } 148 149 return fmt.Errorf("Peer %s not found for router %s", name, router.Name) 150 } 151 } 152 153 func testAccComputeRouterPeerBasic(testId string) string { 154 return fmt.Sprintf(` 155 resource "google_compute_network" "foobar" { 156 name = "router-peer-test-%s" 157 } 158 resource "google_compute_subnetwork" "foobar" { 159 name = "router-peer-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-peer-test-%s" 166 region = "${google_compute_subnetwork.foobar.region}" 167 } 168 resource "google_compute_vpn_gateway" "foobar" { 169 name = "router-peer-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-peer-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-peer-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-peer-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-peer-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-peer-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-peer-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 resource "google_compute_router_peer" "foobar" { 220 name = "router-peer-test-%s" 221 router = "${google_compute_router.foobar.name}" 222 region = "${google_compute_router.foobar.region}" 223 peer_ip_address = "169.254.3.2" 224 peer_asn = 65515 225 advertised_route_priority = 100 226 interface = "${google_compute_router_interface.foobar.name}" 227 } 228 `, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId) 229 } 230 231 func testAccComputeRouterPeerKeepRouter(testId string) string { 232 return fmt.Sprintf(` 233 resource "google_compute_network" "foobar" { 234 name = "router-peer-test-%s" 235 } 236 resource "google_compute_subnetwork" "foobar" { 237 name = "router-peer-test-%s" 238 network = "${google_compute_network.foobar.self_link}" 239 ip_cidr_range = "10.0.0.0/16" 240 region = "us-central1" 241 } 242 resource "google_compute_address" "foobar" { 243 name = "router-peer-test-%s" 244 region = "${google_compute_subnetwork.foobar.region}" 245 } 246 resource "google_compute_vpn_gateway" "foobar" { 247 name = "router-peer-test-%s" 248 network = "${google_compute_network.foobar.self_link}" 249 region = "${google_compute_subnetwork.foobar.region}" 250 } 251 resource "google_compute_forwarding_rule" "foobar_esp" { 252 name = "router-peer-test-%s-1" 253 region = "${google_compute_vpn_gateway.foobar.region}" 254 ip_protocol = "ESP" 255 ip_address = "${google_compute_address.foobar.address}" 256 target = "${google_compute_vpn_gateway.foobar.self_link}" 257 } 258 resource "google_compute_forwarding_rule" "foobar_udp500" { 259 name = "router-peer-test-%s-2" 260 region = "${google_compute_forwarding_rule.foobar_esp.region}" 261 ip_protocol = "UDP" 262 port_range = "500-500" 263 ip_address = "${google_compute_address.foobar.address}" 264 target = "${google_compute_vpn_gateway.foobar.self_link}" 265 } 266 resource "google_compute_forwarding_rule" "foobar_udp4500" { 267 name = "router-peer-test-%s-3" 268 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 269 ip_protocol = "UDP" 270 port_range = "4500-4500" 271 ip_address = "${google_compute_address.foobar.address}" 272 target = "${google_compute_vpn_gateway.foobar.self_link}" 273 } 274 resource "google_compute_router" "foobar"{ 275 name = "router-peer-test-%s" 276 region = "${google_compute_forwarding_rule.foobar_udp500.region}" 277 network = "${google_compute_network.foobar.self_link}" 278 bgp { 279 asn = 64514 280 } 281 } 282 resource "google_compute_vpn_tunnel" "foobar" { 283 name = "router-peer-test-%s" 284 region = "${google_compute_forwarding_rule.foobar_udp4500.region}" 285 target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}" 286 shared_secret = "unguessable" 287 peer_ip = "8.8.8.8" 288 router = "${google_compute_router.foobar.name}" 289 } 290 resource "google_compute_router_interface" "foobar" { 291 name = "router-peer-test-%s" 292 router = "${google_compute_router.foobar.name}" 293 region = "${google_compute_router.foobar.region}" 294 ip_range = "169.254.3.1/30" 295 vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}" 296 } 297 `, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId) 298 }