github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_lb_pool_v1_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" 11 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/monitors" 13 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/pools" 14 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips" 15 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" 16 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" 17 ) 18 19 func TestAccLBV1Pool_basic(t *testing.T) { 20 var pool pools.Pool 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckLBV1PoolDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccLBV1Pool_basic, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 31 resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "lb_provider", "haproxy"), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccLBV1Pool_update, 36 Check: resource.ComposeTestCheckFunc( 37 resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "name", "pool_1"), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func TestAccLBV1Pool_fullstack(t *testing.T) { 45 var instance1, instance2 servers.Server 46 var monitor monitors.Monitor 47 var network networks.Network 48 var pool pools.Pool 49 var secgroup secgroups.SecurityGroup 50 var subnet subnets.Subnet 51 var vip vips.VirtualIP 52 53 resource.Test(t, resource.TestCase{ 54 PreCheck: func() { testAccPreCheck(t) }, 55 Providers: testAccProviders, 56 CheckDestroy: testAccCheckLBV1PoolDestroy, 57 Steps: []resource.TestStep{ 58 resource.TestStep{ 59 Config: testAccLBV1Pool_fullstack_1, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), 62 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 63 testAccCheckComputeV2SecGroupExists("openstack_compute_secgroup_v2.secgroup_1", &secgroup), 64 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance1), 65 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_2", &instance2), 66 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 67 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor), 68 testAccCheckLBV1VIPExists("openstack_lb_vip_v1.vip_1", &vip), 69 ), 70 }, 71 resource.TestStep{ 72 Config: testAccLBV1Pool_fullstack_2, 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), 75 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 76 testAccCheckComputeV2SecGroupExists("openstack_compute_secgroup_v2.secgroup_1", &secgroup), 77 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance1), 78 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_2", &instance2), 79 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 80 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor), 81 testAccCheckLBV1VIPExists("openstack_lb_vip_v1.vip_1", &vip), 82 ), 83 }, 84 }, 85 }) 86 } 87 88 func TestAccLBV1Pool_timeout(t *testing.T) { 89 var pool pools.Pool 90 91 resource.Test(t, resource.TestCase{ 92 PreCheck: func() { testAccPreCheck(t) }, 93 Providers: testAccProviders, 94 CheckDestroy: testAccCheckLBV1PoolDestroy, 95 Steps: []resource.TestStep{ 96 resource.TestStep{ 97 Config: testAccLBV1Pool_timeout, 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 100 resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "lb_provider", "haproxy"), 101 ), 102 }, 103 }, 104 }) 105 } 106 107 func TestAccLBV1Pool_updateMonitor(t *testing.T) { 108 var monitor_1 monitors.Monitor 109 var monitor_2 monitors.Monitor 110 var network networks.Network 111 var pool pools.Pool 112 var subnet subnets.Subnet 113 114 resource.Test(t, resource.TestCase{ 115 PreCheck: func() { testAccPreCheck(t) }, 116 Providers: testAccProviders, 117 CheckDestroy: testAccCheckLBV1PoolDestroy, 118 Steps: []resource.TestStep{ 119 resource.TestStep{ 120 Config: testAccLBV1Pool_updateMonitor_1, 121 Check: resource.ComposeTestCheckFunc( 122 testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), 123 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 124 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 125 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor_1), 126 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_2", &monitor_2), 127 ), 128 }, 129 resource.TestStep{ 130 Config: testAccLBV1Pool_updateMonitor_2, 131 Check: resource.ComposeTestCheckFunc( 132 testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), 133 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 134 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 135 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor_1), 136 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_2", &monitor_2), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func testAccCheckLBV1PoolDestroy(s *terraform.State) error { 144 config := testAccProvider.Meta().(*Config) 145 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 146 if err != nil { 147 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 148 } 149 150 for _, rs := range s.RootModule().Resources { 151 if rs.Type != "openstack_lb_pool_v1" { 152 continue 153 } 154 155 _, err := pools.Get(networkingClient, rs.Primary.ID).Extract() 156 if err == nil { 157 return fmt.Errorf("LB Pool still exists") 158 } 159 } 160 161 return nil 162 } 163 164 func testAccCheckLBV1PoolExists(n string, pool *pools.Pool) resource.TestCheckFunc { 165 return func(s *terraform.State) error { 166 rs, ok := s.RootModule().Resources[n] 167 if !ok { 168 return fmt.Errorf("Not found: %s", n) 169 } 170 171 if rs.Primary.ID == "" { 172 return fmt.Errorf("No ID is set") 173 } 174 175 config := testAccProvider.Meta().(*Config) 176 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 177 if err != nil { 178 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 179 } 180 181 found, err := pools.Get(networkingClient, rs.Primary.ID).Extract() 182 if err != nil { 183 return err 184 } 185 186 if found.ID != rs.Primary.ID { 187 return fmt.Errorf("Pool not found") 188 } 189 190 *pool = *found 191 192 return nil 193 } 194 } 195 196 const testAccLBV1Pool_basic = ` 197 resource "openstack_networking_network_v2" "network_1" { 198 name = "network_1" 199 admin_state_up = "true" 200 } 201 202 resource "openstack_networking_subnet_v2" "subnet_1" { 203 cidr = "192.168.199.0/24" 204 ip_version = 4 205 network_id = "${openstack_networking_network_v2.network_1.id}" 206 } 207 208 resource "openstack_lb_pool_v1" "pool_1" { 209 name = "pool_1" 210 protocol = "HTTP" 211 lb_method = "ROUND_ROBIN" 212 lb_provider = "haproxy" 213 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 214 } 215 ` 216 217 const testAccLBV1Pool_update = ` 218 resource "openstack_networking_network_v2" "network_1" { 219 name = "network_1" 220 admin_state_up = "true" 221 } 222 223 resource "openstack_networking_subnet_v2" "subnet_1" { 224 cidr = "192.168.199.0/24" 225 ip_version = 4 226 network_id = "${openstack_networking_network_v2.network_1.id}" 227 } 228 229 resource "openstack_lb_pool_v1" "pool_1" { 230 name = "pool_1" 231 protocol = "HTTP" 232 lb_method = "ROUND_ROBIN" 233 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 234 } 235 ` 236 237 const testAccLBV1Pool_fullstack_1 = ` 238 resource "openstack_networking_network_v2" "network_1" { 239 name = "network_1" 240 admin_state_up = "true" 241 } 242 243 resource "openstack_networking_subnet_v2" "subnet_1" { 244 cidr = "192.168.199.0/24" 245 ip_version = 4 246 network_id = "${openstack_networking_network_v2.network_1.id}" 247 } 248 249 resource "openstack_compute_secgroup_v2" "secgroup_1" { 250 name = "secgroup_1" 251 description = "Rules for secgroup_1" 252 253 rule { 254 from_port = -1 255 to_port = -1 256 ip_protocol = "icmp" 257 cidr = "0.0.0.0/0" 258 } 259 260 rule { 261 from_port = 80 262 to_port = 80 263 ip_protocol = "tcp" 264 cidr = "0.0.0.0/0" 265 } 266 } 267 268 resource "openstack_compute_instance_v2" "instance_1" { 269 name = "instance_1" 270 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 271 272 network { 273 uuid = "${openstack_networking_network_v2.network_1.id}" 274 } 275 } 276 277 resource "openstack_compute_instance_v2" "instance_2" { 278 name = "instance_2" 279 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 280 281 network { 282 uuid = "${openstack_networking_network_v2.network_1.id}" 283 } 284 } 285 286 resource "openstack_lb_monitor_v1" "monitor_1" { 287 type = "TCP" 288 delay = 30 289 timeout = 5 290 max_retries = 3 291 admin_state_up = "true" 292 } 293 294 resource "openstack_lb_pool_v1" "pool_1" { 295 name = "pool_1" 296 protocol = "TCP" 297 lb_method = "ROUND_ROBIN" 298 monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"] 299 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 300 } 301 302 resource "openstack_lb_member_v1" "member_1" { 303 port = 80 304 admin_state_up = true 305 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 306 address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}" 307 } 308 309 resource "openstack_lb_member_v1" "member_2" { 310 port = 80 311 admin_state_up = true 312 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 313 address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}" 314 } 315 316 resource "openstack_lb_vip_v1" "vip_1" { 317 name = "vip_1" 318 protocol = "TCP" 319 port = 80 320 admin_state_up = true 321 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 322 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 323 } 324 ` 325 326 const testAccLBV1Pool_fullstack_2 = ` 327 resource "openstack_networking_network_v2" "network_1" { 328 name = "network_1" 329 admin_state_up = "true" 330 } 331 332 resource "openstack_networking_subnet_v2" "subnet_1" { 333 cidr = "192.168.199.0/24" 334 ip_version = 4 335 network_id = "${openstack_networking_network_v2.network_1.id}" 336 } 337 338 resource "openstack_compute_secgroup_v2" "secgroup_1" { 339 name = "secgroup_1" 340 description = "Rules for secgroup_1" 341 342 rule { 343 from_port = -1 344 to_port = -1 345 ip_protocol = "icmp" 346 cidr = "0.0.0.0/0" 347 } 348 349 rule { 350 from_port = 80 351 to_port = 80 352 ip_protocol = "tcp" 353 cidr = "0.0.0.0/0" 354 } 355 } 356 357 resource "openstack_compute_instance_v2" "instance_1" { 358 name = "instance_1" 359 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 360 361 network { 362 uuid = "${openstack_networking_network_v2.network_1.id}" 363 } 364 } 365 366 resource "openstack_compute_instance_v2" "instance_2" { 367 name = "instance_2" 368 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 369 user_data = "#cloud-config\ndisable_root: false" 370 371 network { 372 uuid = "${openstack_networking_network_v2.network_1.id}" 373 } 374 } 375 376 resource "openstack_lb_monitor_v1" "monitor_1" { 377 type = "TCP" 378 delay = 30 379 timeout = 5 380 max_retries = 3 381 admin_state_up = "true" 382 } 383 384 resource "openstack_lb_pool_v1" "pool_1" { 385 name = "pool_1" 386 protocol = "TCP" 387 lb_method = "ROUND_ROBIN" 388 monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"] 389 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 390 } 391 392 resource "openstack_lb_member_v1" "member_1" { 393 port = 80 394 admin_state_up = true 395 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 396 address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}" 397 } 398 399 resource "openstack_lb_member_v1" "member_2" { 400 port = 80 401 admin_state_up = true 402 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 403 address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}" 404 } 405 406 resource "openstack_lb_vip_v1" "vip_1" { 407 name = "vip_1" 408 protocol = "TCP" 409 port = 80 410 admin_state_up = true 411 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 412 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 413 } 414 ` 415 416 const testAccLBV1Pool_timeout = ` 417 resource "openstack_networking_network_v2" "network_1" { 418 name = "network_1" 419 admin_state_up = "true" 420 } 421 422 resource "openstack_networking_subnet_v2" "subnet_1" { 423 cidr = "192.168.199.0/24" 424 ip_version = 4 425 network_id = "${openstack_networking_network_v2.network_1.id}" 426 } 427 428 resource "openstack_lb_pool_v1" "pool_1" { 429 name = "pool_1" 430 protocol = "HTTP" 431 lb_method = "ROUND_ROBIN" 432 lb_provider = "haproxy" 433 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 434 435 timeouts { 436 create = "5m" 437 delete = "5m" 438 } 439 } 440 ` 441 442 const testAccLBV1Pool_updateMonitor_1 = ` 443 resource "openstack_networking_network_v2" "network_1" { 444 name = "network_1" 445 admin_state_up = "true" 446 } 447 448 resource "openstack_networking_subnet_v2" "subnet_1" { 449 cidr = "192.168.199.0/24" 450 ip_version = 4 451 network_id = "${openstack_networking_network_v2.network_1.id}" 452 } 453 454 resource "openstack_lb_monitor_v1" "monitor_1" { 455 type = "TCP" 456 delay = 30 457 timeout = 5 458 max_retries = 3 459 admin_state_up = "true" 460 } 461 462 resource "openstack_lb_monitor_v1" "monitor_2" { 463 type = "TCP" 464 delay = 30 465 timeout = 5 466 max_retries = 3 467 admin_state_up = "true" 468 } 469 470 resource "openstack_lb_pool_v1" "pool_1" { 471 name = "pool_1" 472 protocol = "TCP" 473 lb_method = "ROUND_ROBIN" 474 monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"] 475 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 476 } 477 ` 478 479 const testAccLBV1Pool_updateMonitor_2 = ` 480 resource "openstack_networking_network_v2" "network_1" { 481 name = "network_1" 482 admin_state_up = "true" 483 } 484 485 resource "openstack_networking_subnet_v2" "subnet_1" { 486 cidr = "192.168.199.0/24" 487 ip_version = 4 488 network_id = "${openstack_networking_network_v2.network_1.id}" 489 } 490 491 resource "openstack_lb_monitor_v1" "monitor_1" { 492 type = "TCP" 493 delay = 30 494 timeout = 5 495 max_retries = 3 496 admin_state_up = "true" 497 } 498 499 resource "openstack_lb_monitor_v1" "monitor_2" { 500 type = "TCP" 501 delay = 30 502 timeout = 5 503 max_retries = 3 504 admin_state_up = "true" 505 } 506 507 resource "openstack_lb_pool_v1" "pool_1" { 508 name = "pool_1" 509 protocol = "TCP" 510 lb_method = "ROUND_ROBIN" 511 monitor_ids = ["${openstack_lb_monitor_v1.monitor_2.id}"] 512 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 513 } 514 `