github.com/gettyimages/terraform@v0.7.6-0.20161219132226-dc052c5707a3/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 "github.com/gophercloud/gophercloud" 12 "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes" 13 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips" 14 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" 15 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" 16 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" 17 "github.com/gophercloud/gophercloud/pagination" 18 ) 19 20 func TestAccComputeV2Instance_basic(t *testing.T) { 21 var instance servers.Server 22 var testAccComputeV2Instance_basic = fmt.Sprintf(` 23 resource "openstack_compute_instance_v2" "foo" { 24 name = "terraform-test" 25 security_groups = ["default"] 26 network { 27 uuid = "%s" 28 } 29 metadata { 30 foo = "bar" 31 } 32 }`, 33 os.Getenv("OS_NETWORK_ID")) 34 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccComputeV2Instance_basic, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 44 testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func TestAccComputeV2Instance_volumeAttach(t *testing.T) { 52 var instance servers.Server 53 var volume volumes.Volume 54 55 var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(` 56 resource "openstack_blockstorage_volume_v1" "myvol" { 57 name = "myvol" 58 size = 1 59 } 60 61 resource "openstack_compute_instance_v2" "foo" { 62 name = "terraform-test" 63 security_groups = ["default"] 64 volume { 65 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 66 } 67 }`) 68 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 73 Steps: []resource.TestStep{ 74 resource.TestStep{ 75 Config: testAccComputeV2Instance_volumeAttach, 76 Check: resource.ComposeTestCheckFunc( 77 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 78 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 79 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) { 87 var instance servers.Server 88 var volume volumes.Volume 89 90 var testAccComputeV2Instance_volumeAttachPostCreationInstance = fmt.Sprintf(` 91 resource "openstack_compute_instance_v2" "foo" { 92 name = "terraform-test" 93 security_groups = ["default"] 94 }`) 95 96 var testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume = fmt.Sprintf(` 97 resource "openstack_blockstorage_volume_v1" "myvol" { 98 name = "myvol" 99 size = 1 100 } 101 102 resource "openstack_compute_instance_v2" "foo" { 103 name = "terraform-test" 104 security_groups = ["default"] 105 volume { 106 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 107 } 108 }`) 109 110 resource.Test(t, resource.TestCase{ 111 PreCheck: func() { testAccPreCheck(t) }, 112 Providers: testAccProviders, 113 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 114 Steps: []resource.TestStep{ 115 resource.TestStep{ 116 Config: testAccComputeV2Instance_volumeAttachPostCreationInstance, 117 Check: resource.ComposeTestCheckFunc( 118 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 119 ), 120 }, 121 resource.TestStep{ 122 Config: testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume, 123 Check: resource.ComposeTestCheckFunc( 124 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 125 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 126 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 127 ), 128 }, 129 }, 130 }) 131 } 132 133 func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) { 134 var instance servers.Server 135 var volume volumes.Volume 136 137 var testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume = fmt.Sprintf(` 138 resource "openstack_blockstorage_volume_v1" "myvol" { 139 name = "myvol" 140 size = 1 141 } 142 143 resource "openstack_compute_instance_v2" "foo" { 144 name = "terraform-test" 145 security_groups = ["default"] 146 volume { 147 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 148 } 149 }`) 150 151 var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(` 152 resource "openstack_blockstorage_volume_v1" "myvol" { 153 name = "myvol" 154 size = 1 155 } 156 157 resource "openstack_compute_instance_v2" "foo" { 158 name = "terraform-test" 159 security_groups = ["default"] 160 }`) 161 162 resource.Test(t, resource.TestCase{ 163 PreCheck: func() { testAccPreCheck(t) }, 164 Providers: testAccProviders, 165 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 166 Steps: []resource.TestStep{ 167 resource.TestStep{ 168 Config: testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume, 169 Check: resource.ComposeTestCheckFunc( 170 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 171 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 172 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 173 ), 174 }, 175 resource.TestStep{ 176 Config: testAccComputeV2Instance_volumeDetachPostCreationInstance, 177 Check: resource.ComposeTestCheckFunc( 178 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 179 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 180 testAccCheckComputeV2InstanceVolumesDetached(&instance), 181 ), 182 }, 183 }, 184 }) 185 } 186 187 func TestAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation(t *testing.T) { 188 var instance servers.Server 189 var volume_1 volumes.Volume 190 var volume_2 volumes.Volume 191 192 var testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreationInstanceAndVolume = fmt.Sprintf(` 193 194 resource "openstack_blockstorage_volume_v1" "root_volume" { 195 name = "root_volume" 196 size = 1 197 image_id = "%s" 198 } 199 200 resource "openstack_blockstorage_volume_v1" "additional_volume" { 201 name = "additional_volume" 202 size = 1 203 } 204 205 resource "openstack_compute_instance_v2" "foo" { 206 name = "terraform-test" 207 security_groups = ["default"] 208 209 block_device { 210 uuid = "${openstack_blockstorage_volume_v1.root_volume.id}" 211 source_type = "volume" 212 boot_index = 0 213 destination_type = "volume" 214 delete_on_termination = false 215 } 216 217 volume { 218 volume_id = "${openstack_blockstorage_volume_v1.additional_volume.id}" 219 } 220 }`, 221 os.Getenv("OS_IMAGE_ID")) 222 223 var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(` 224 225 resource "openstack_blockstorage_volume_v1" "root_volume" { 226 name = "root_volume" 227 size = 1 228 image_id = "%s" 229 } 230 231 resource "openstack_blockstorage_volume_v1" "additional_volume" { 232 name = "additional_volume" 233 size = 1 234 } 235 236 resource "openstack_compute_instance_v2" "foo" { 237 name = "terraform-test" 238 security_groups = ["default"] 239 240 block_device { 241 uuid = "${openstack_blockstorage_volume_v1.root_volume.id}" 242 source_type = "volume" 243 boot_index = 0 244 destination_type = "volume" 245 delete_on_termination = false 246 } 247 }`, 248 os.Getenv("OS_IMAGE_ID")) 249 250 resource.Test(t, resource.TestCase{ 251 PreCheck: func() { testAccPreCheck(t) }, 252 Providers: testAccProviders, 253 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 254 Steps: []resource.TestStep{ 255 resource.TestStep{ 256 Config: testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreationInstanceAndVolume, 257 Check: resource.ComposeTestCheckFunc( 258 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1), 259 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2), 260 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 261 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1), 262 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_2), 263 ), 264 }, 265 resource.TestStep{ 266 Config: testAccComputeV2Instance_volumeDetachPostCreationInstance, 267 Check: resource.ComposeTestCheckFunc( 268 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1), 269 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2), 270 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 271 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1), 272 testAccCheckComputeV2InstanceVolumeDetached(&instance, "openstack_blockstorage_volume_v1.additional_volume"), 273 ), 274 }, 275 }, 276 }) 277 } 278 279 func TestAccComputeV2Instance_volumeAttachInstanceDelete(t *testing.T) { 280 var instance servers.Server 281 var volume_1 volumes.Volume 282 var volume_2 volumes.Volume 283 284 var testAccComputeV2Instance_volumeAttachInstanceDelete_1 = fmt.Sprintf(` 285 resource "openstack_blockstorage_volume_v1" "root_volume" { 286 name = "root_volume" 287 size = 1 288 image_id = "%s" 289 } 290 291 resource "openstack_blockstorage_volume_v1" "additional_volume" { 292 name = "additional_volume" 293 size = 1 294 } 295 296 resource "openstack_compute_instance_v2" "foo" { 297 name = "terraform-test" 298 security_groups = ["default"] 299 300 block_device { 301 uuid = "${openstack_blockstorage_volume_v1.root_volume.id}" 302 source_type = "volume" 303 boot_index = 0 304 destination_type = "volume" 305 delete_on_termination = false 306 } 307 308 volume { 309 volume_id = "${openstack_blockstorage_volume_v1.additional_volume.id}" 310 } 311 }`, 312 os.Getenv("OS_IMAGE_ID")) 313 314 var testAccComputeV2Instance_volumeAttachInstanceDelete_2 = fmt.Sprintf(` 315 resource "openstack_blockstorage_volume_v1" "root_volume" { 316 name = "root_volume" 317 size = 1 318 image_id = "%s" 319 } 320 321 resource "openstack_blockstorage_volume_v1" "additional_volume" { 322 name = "additional_volume" 323 size = 1 324 }`, 325 os.Getenv("OS_IMAGE_ID")) 326 327 resource.Test(t, resource.TestCase{ 328 PreCheck: func() { testAccPreCheck(t) }, 329 Providers: testAccProviders, 330 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 331 Steps: []resource.TestStep{ 332 resource.TestStep{ 333 Config: testAccComputeV2Instance_volumeAttachInstanceDelete_1, 334 Check: resource.ComposeTestCheckFunc( 335 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1), 336 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2), 337 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 338 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1), 339 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_2), 340 ), 341 }, 342 resource.TestStep{ 343 Config: testAccComputeV2Instance_volumeAttachInstanceDelete_2, 344 Check: resource.ComposeTestCheckFunc( 345 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1), 346 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2), 347 testAccCheckComputeV2InstanceDoesNotExist(t, "openstack_compute_instance_v2.foo", &instance), 348 testAccCheckComputeV2InstanceVolumeDetached(&instance, "openstack_blockstorage_volume_v1.root_volume"), 349 testAccCheckComputeV2InstanceVolumeDetached(&instance, "openstack_blockstorage_volume_v1.additional_volume"), 350 ), 351 }, 352 }, 353 }) 354 } 355 356 func TestAccComputeV2Instance_volumeAttachToNewInstance(t *testing.T) { 357 var instance_1 servers.Server 358 var instance_2 servers.Server 359 var volume_1 volumes.Volume 360 361 var testAccComputeV2Instance_volumeAttachToNewInstance_1 = fmt.Sprintf(` 362 resource "openstack_blockstorage_volume_v1" "volume_1" { 363 name = "volume_1" 364 size = 1 365 } 366 367 resource "openstack_compute_instance_v2" "instance_1" { 368 name = "instance_1" 369 security_groups = ["default"] 370 371 volume { 372 volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}" 373 } 374 } 375 376 resource "openstack_compute_instance_v2" "instance_2" { 377 depends_on = ["openstack_compute_instance_v2.instance_1"] 378 name = "instance_2" 379 security_groups = ["default"] 380 }`) 381 382 var testAccComputeV2Instance_volumeAttachToNewInstance_2 = fmt.Sprintf(` 383 resource "openstack_blockstorage_volume_v1" "volume_1" { 384 name = "volume_1" 385 size = 1 386 } 387 388 resource "openstack_compute_instance_v2" "instance_1" { 389 name = "instance_1" 390 security_groups = ["default"] 391 } 392 393 resource "openstack_compute_instance_v2" "instance_2" { 394 depends_on = ["openstack_compute_instance_v2.instance_1"] 395 name = "instance_2" 396 security_groups = ["default"] 397 398 volume { 399 volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}" 400 } 401 }`) 402 403 resource.Test(t, resource.TestCase{ 404 PreCheck: func() { testAccPreCheck(t) }, 405 Providers: testAccProviders, 406 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 407 Steps: []resource.TestStep{ 408 resource.TestStep{ 409 Config: testAccComputeV2Instance_volumeAttachToNewInstance_1, 410 Check: resource.ComposeTestCheckFunc( 411 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume_1), 412 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 413 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance_2), 414 testAccCheckComputeV2InstanceVolumeAttachment(&instance_1, &volume_1), 415 testAccCheckComputeV2InstanceVolumeDetached(&instance_2, "openstack_blockstorage_volume_v1.volume_1"), 416 ), 417 }, 418 resource.TestStep{ 419 Config: testAccComputeV2Instance_volumeAttachToNewInstance_2, 420 Check: resource.ComposeTestCheckFunc( 421 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume_1), 422 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 423 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance_2), 424 testAccCheckComputeV2InstanceVolumeDetached(&instance_1, "openstack_blockstorage_volume_v1.volume_1"), 425 testAccCheckComputeV2InstanceVolumeAttachment(&instance_2, &volume_1), 426 ), 427 }, 428 }, 429 }) 430 } 431 432 func TestAccComputeV2Instance_floatingIPAttachGlobally(t *testing.T) { 433 var instance servers.Server 434 var fip floatingips.FloatingIP 435 var testAccComputeV2Instance_floatingIPAttachGlobally = fmt.Sprintf(` 436 resource "openstack_compute_floatingip_v2" "myip" { 437 } 438 439 resource "openstack_compute_instance_v2" "foo" { 440 name = "terraform-test" 441 security_groups = ["default"] 442 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 443 444 network { 445 uuid = "%s" 446 } 447 }`, 448 os.Getenv("OS_NETWORK_ID")) 449 450 resource.Test(t, resource.TestCase{ 451 PreCheck: func() { testAccPreCheck(t) }, 452 Providers: testAccProviders, 453 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 454 Steps: []resource.TestStep{ 455 resource.TestStep{ 456 Config: testAccComputeV2Instance_floatingIPAttachGlobally, 457 Check: resource.ComposeTestCheckFunc( 458 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), 459 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 460 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 461 ), 462 }, 463 }, 464 }) 465 } 466 467 func TestAccComputeV2Instance_floatingIPAttachToNetwork(t *testing.T) { 468 var instance servers.Server 469 var fip floatingips.FloatingIP 470 var testAccComputeV2Instance_floatingIPAttachToNetwork = fmt.Sprintf(` 471 resource "openstack_compute_floatingip_v2" "myip" { 472 } 473 474 resource "openstack_compute_instance_v2" "foo" { 475 name = "terraform-test" 476 security_groups = ["default"] 477 478 network { 479 uuid = "%s" 480 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 481 access_network = true 482 } 483 }`, 484 os.Getenv("OS_NETWORK_ID")) 485 486 resource.Test(t, resource.TestCase{ 487 PreCheck: func() { testAccPreCheck(t) }, 488 Providers: testAccProviders, 489 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 490 Steps: []resource.TestStep{ 491 resource.TestStep{ 492 Config: testAccComputeV2Instance_floatingIPAttachToNetwork, 493 Check: resource.ComposeTestCheckFunc( 494 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), 495 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 496 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 497 ), 498 }, 499 }, 500 }) 501 } 502 503 func TestAccComputeV2Instance_floatingIPAttachAndChange(t *testing.T) { 504 var instance servers.Server 505 var fip floatingips.FloatingIP 506 var testAccComputeV2Instance_floatingIPAttachToNetwork_1 = fmt.Sprintf(` 507 resource "openstack_compute_floatingip_v2" "myip_1" { 508 } 509 510 resource "openstack_compute_floatingip_v2" "myip_2" { 511 } 512 513 resource "openstack_compute_instance_v2" "foo" { 514 name = "terraform-test" 515 security_groups = ["default"] 516 517 network { 518 uuid = "%s" 519 floating_ip = "${openstack_compute_floatingip_v2.myip_1.address}" 520 access_network = true 521 } 522 }`, 523 os.Getenv("OS_NETWORK_ID")) 524 525 var testAccComputeV2Instance_floatingIPAttachToNetwork_2 = fmt.Sprintf(` 526 resource "openstack_compute_floatingip_v2" "myip_1" { 527 } 528 529 resource "openstack_compute_floatingip_v2" "myip_2" { 530 } 531 532 resource "openstack_compute_instance_v2" "foo" { 533 name = "terraform-test" 534 security_groups = ["default"] 535 536 network { 537 uuid = "%s" 538 floating_ip = "${openstack_compute_floatingip_v2.myip_2.address}" 539 access_network = true 540 } 541 }`, 542 os.Getenv("OS_NETWORK_ID")) 543 544 resource.Test(t, resource.TestCase{ 545 PreCheck: func() { testAccPreCheck(t) }, 546 Providers: testAccProviders, 547 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 548 Steps: []resource.TestStep{ 549 resource.TestStep{ 550 Config: testAccComputeV2Instance_floatingIPAttachToNetwork_1, 551 Check: resource.ComposeTestCheckFunc( 552 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_1", &fip), 553 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 554 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 555 ), 556 }, 557 resource.TestStep{ 558 Config: testAccComputeV2Instance_floatingIPAttachToNetwork_2, 559 Check: resource.ComposeTestCheckFunc( 560 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_2", &fip), 561 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 562 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 563 ), 564 }, 565 }, 566 }) 567 } 568 569 func TestAccComputeV2Instance_multi_secgroups(t *testing.T) { 570 var instance_1 servers.Server 571 var secgroup_1 secgroups.SecurityGroup 572 var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(` 573 resource "openstack_compute_secgroup_v2" "secgroup_1" { 574 name = "secgroup_1" 575 description = "a security group" 576 rule { 577 from_port = 22 578 to_port = 22 579 ip_protocol = "tcp" 580 cidr = "0.0.0.0/0" 581 } 582 } 583 584 resource "openstack_compute_instance_v2" "instance_1" { 585 name = "instance_1" 586 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 587 network { 588 uuid = "%s" 589 } 590 }`, 591 os.Getenv("OS_NETWORK_ID")) 592 593 resource.Test(t, resource.TestCase{ 594 PreCheck: func() { testAccPreCheck(t) }, 595 Providers: testAccProviders, 596 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 597 Steps: []resource.TestStep{ 598 resource.TestStep{ 599 Config: testAccComputeV2Instance_multi_secgroups, 600 Check: resource.ComposeTestCheckFunc( 601 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1), 602 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 603 ), 604 }, 605 }, 606 }) 607 } 608 609 func TestAccComputeV2Instance_multi_secgroups_update(t *testing.T) { 610 var instance_1 servers.Server 611 var secgroup_1, secgroup_2 secgroups.SecurityGroup 612 var testAccComputeV2Instance_multi_secgroups_update_1 = fmt.Sprintf(` 613 resource "openstack_compute_secgroup_v2" "secgroup_1" { 614 name = "secgroup_1" 615 description = "a security group" 616 rule { 617 from_port = 22 618 to_port = 22 619 ip_protocol = "tcp" 620 cidr = "0.0.0.0/0" 621 } 622 } 623 624 resource "openstack_compute_secgroup_v2" "secgroup_2" { 625 name = "secgroup_2" 626 description = "another security group" 627 rule { 628 from_port = 80 629 to_port = 80 630 ip_protocol = "tcp" 631 cidr = "0.0.0.0/0" 632 } 633 } 634 635 resource "openstack_compute_instance_v2" "instance_1" { 636 name = "instance_1" 637 security_groups = ["default"] 638 }`) 639 640 var testAccComputeV2Instance_multi_secgroups_update_2 = fmt.Sprintf(` 641 resource "openstack_compute_secgroup_v2" "secgroup_1" { 642 name = "secgroup_1" 643 description = "a security group" 644 rule { 645 from_port = 22 646 to_port = 22 647 ip_protocol = "tcp" 648 cidr = "0.0.0.0/0" 649 } 650 } 651 652 resource "openstack_compute_secgroup_v2" "secgroup_2" { 653 name = "secgroup_2" 654 description = "another security group" 655 rule { 656 from_port = 80 657 to_port = 80 658 ip_protocol = "tcp" 659 cidr = "0.0.0.0/0" 660 } 661 } 662 663 resource "openstack_compute_instance_v2" "instance_1" { 664 name = "instance_1" 665 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}", "${openstack_compute_secgroup_v2.secgroup_2.name}"] 666 }`) 667 668 resource.Test(t, resource.TestCase{ 669 PreCheck: func() { testAccPreCheck(t) }, 670 Providers: testAccProviders, 671 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 672 Steps: []resource.TestStep{ 673 resource.TestStep{ 674 Config: testAccComputeV2Instance_multi_secgroups_update_1, 675 Check: resource.ComposeTestCheckFunc( 676 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1), 677 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2), 678 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 679 ), 680 }, 681 resource.TestStep{ 682 Config: testAccComputeV2Instance_multi_secgroups_update_2, 683 Check: resource.ComposeTestCheckFunc( 684 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1), 685 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2), 686 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 687 ), 688 }, 689 }, 690 }) 691 } 692 693 func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) { 694 var instance servers.Server 695 var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(` 696 resource "openstack_compute_instance_v2" "foo" { 697 name = "terraform-test" 698 security_groups = ["default"] 699 block_device { 700 uuid = "%s" 701 source_type = "image" 702 volume_size = 5 703 boot_index = 0 704 destination_type = "volume" 705 delete_on_termination = true 706 } 707 }`, 708 os.Getenv("OS_IMAGE_ID")) 709 710 resource.Test(t, resource.TestCase{ 711 PreCheck: func() { testAccPreCheck(t) }, 712 Providers: testAccProviders, 713 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 714 Steps: []resource.TestStep{ 715 resource.TestStep{ 716 Config: testAccComputeV2Instance_bootFromVolumeImage, 717 Check: resource.ComposeTestCheckFunc( 718 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 719 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 720 ), 721 }, 722 }, 723 }) 724 } 725 726 func TestAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume(t *testing.T) { 727 var instance servers.Server 728 var testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume = fmt.Sprintf(` 729 resource "openstack_blockstorage_volume_v1" "volume_1" { 730 name = "volume_1" 731 size = 1 732 } 733 734 resource "openstack_compute_instance_v2" "instance_1" { 735 name = "instance_1" 736 security_groups = ["default"] 737 block_device { 738 uuid = "%s" 739 source_type = "image" 740 volume_size = 2 741 boot_index = 0 742 destination_type = "volume" 743 delete_on_termination = true 744 } 745 746 volume { 747 volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}" 748 } 749 }`, 750 os.Getenv("OS_IMAGE_ID")) 751 752 resource.Test(t, resource.TestCase{ 753 PreCheck: func() { testAccPreCheck(t) }, 754 Providers: testAccProviders, 755 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 756 Steps: []resource.TestStep{ 757 resource.TestStep{ 758 Config: testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume, 759 Check: resource.ComposeTestCheckFunc( 760 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance), 761 ), 762 }, 763 }, 764 }) 765 } 766 767 func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) { 768 var instance servers.Server 769 var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(` 770 resource "openstack_blockstorage_volume_v1" "foo" { 771 name = "terraform-test" 772 size = 5 773 image_id = "%s" 774 } 775 776 resource "openstack_compute_instance_v2" "foo" { 777 name = "terraform-test" 778 security_groups = ["default"] 779 block_device { 780 uuid = "${openstack_blockstorage_volume_v1.foo.id}" 781 source_type = "volume" 782 boot_index = 0 783 destination_type = "volume" 784 delete_on_termination = true 785 } 786 }`, 787 os.Getenv("OS_IMAGE_ID")) 788 789 resource.Test(t, resource.TestCase{ 790 PreCheck: func() { testAccPreCheck(t) }, 791 Providers: testAccProviders, 792 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 793 Steps: []resource.TestStep{ 794 resource.TestStep{ 795 Config: testAccComputeV2Instance_bootFromVolumeVolume, 796 Check: resource.ComposeTestCheckFunc( 797 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 798 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 799 ), 800 }, 801 }, 802 }) 803 } 804 805 func TestAccComputeV2Instance_bootFromVolumeForceNew(t *testing.T) { 806 var instance1_1 servers.Server 807 var instance1_2 servers.Server 808 var testAccComputeV2Instance_bootFromVolumeForceNew_1 = fmt.Sprintf(` 809 resource "openstack_compute_instance_v2" "instance_1" { 810 name = "instance_1" 811 security_groups = ["default"] 812 block_device { 813 uuid = "%s" 814 source_type = "image" 815 volume_size = 5 816 boot_index = 0 817 destination_type = "volume" 818 delete_on_termination = true 819 } 820 }`, 821 os.Getenv("OS_IMAGE_ID")) 822 823 var testAccComputeV2Instance_bootFromVolumeForceNew_2 = fmt.Sprintf(` 824 resource "openstack_compute_instance_v2" "instance_1" { 825 name = "instance_1" 826 security_groups = ["default"] 827 block_device { 828 uuid = "%s" 829 source_type = "image" 830 volume_size = 4 831 boot_index = 0 832 destination_type = "volume" 833 delete_on_termination = true 834 } 835 }`, 836 os.Getenv("OS_IMAGE_ID")) 837 838 resource.Test(t, resource.TestCase{ 839 PreCheck: func() { testAccPreCheck(t) }, 840 Providers: testAccProviders, 841 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 842 Steps: []resource.TestStep{ 843 resource.TestStep{ 844 Config: testAccComputeV2Instance_bootFromVolumeForceNew_1, 845 Check: resource.ComposeTestCheckFunc( 846 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1), 847 ), 848 }, 849 resource.TestStep{ 850 Config: testAccComputeV2Instance_bootFromVolumeForceNew_2, 851 Check: resource.ComposeTestCheckFunc( 852 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2), 853 testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2), 854 ), 855 }, 856 }, 857 }) 858 } 859 860 func TestAccComputeV2Instance_blockDeviceNewVolume(t *testing.T) { 861 var instance_1 servers.Server 862 var testAccComputeV2Instance_blockDeviceNewVolume = fmt.Sprintf(` 863 resource "openstack_compute_instance_v2" "instance_1" { 864 name = "instance_1" 865 security_groups = ["default"] 866 block_device { 867 uuid = "%s" 868 source_type = "image" 869 destination_type = "local" 870 boot_index = 0 871 delete_on_termination = true 872 } 873 block_device { 874 source_type = "blank" 875 destination_type = "volume" 876 volume_size = 1 877 boot_index = 1 878 delete_on_termination = true 879 } 880 }`, 881 os.Getenv("OS_IMAGE_ID")) 882 883 resource.Test(t, resource.TestCase{ 884 PreCheck: func() { testAccPreCheck(t) }, 885 Providers: testAccProviders, 886 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 887 Steps: []resource.TestStep{ 888 resource.TestStep{ 889 Config: testAccComputeV2Instance_blockDeviceNewVolume, 890 Check: resource.ComposeTestCheckFunc( 891 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 892 ), 893 }, 894 }, 895 }) 896 } 897 898 func TestAccComputeV2Instance_blockDeviceExistingVolume(t *testing.T) { 899 var instance_1 servers.Server 900 var volume_1 volumes.Volume 901 var testAccComputeV2Instance_blockDeviceExistingVolume = fmt.Sprintf(` 902 resource "openstack_blockstorage_volume_v1" "volume_1" { 903 name = "volume_1" 904 size = 1 905 } 906 907 resource "openstack_compute_instance_v2" "instance_1" { 908 name = "instance_1" 909 security_groups = ["default"] 910 block_device { 911 uuid = "%s" 912 source_type = "image" 913 destination_type = "local" 914 boot_index = 0 915 delete_on_termination = true 916 } 917 block_device { 918 uuid = "${openstack_blockstorage_volume_v1.volume_1.id}" 919 source_type = "volume" 920 destination_type = "volume" 921 boot_index = 1 922 delete_on_termination = true 923 } 924 }`, 925 os.Getenv("OS_IMAGE_ID")) 926 927 resource.Test(t, resource.TestCase{ 928 PreCheck: func() { testAccPreCheck(t) }, 929 Providers: testAccProviders, 930 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 931 Steps: []resource.TestStep{ 932 resource.TestStep{ 933 Config: testAccComputeV2Instance_blockDeviceExistingVolume, 934 Check: resource.ComposeTestCheckFunc( 935 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 936 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume_1), 937 ), 938 }, 939 }, 940 }) 941 } 942 943 // TODO: verify the personality really exists on the instance. 944 func TestAccComputeV2Instance_personality(t *testing.T) { 945 var instance servers.Server 946 var testAccComputeV2Instance_personality = fmt.Sprintf(` 947 resource "openstack_compute_instance_v2" "foo" { 948 name = "terraform-test" 949 security_groups = ["default"] 950 personality { 951 file = "/tmp/foobar.txt" 952 content = "happy" 953 } 954 personality { 955 file = "/tmp/barfoo.txt" 956 content = "angry" 957 } 958 }`) 959 960 resource.Test(t, resource.TestCase{ 961 PreCheck: func() { testAccPreCheck(t) }, 962 Providers: testAccProviders, 963 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 964 Steps: []resource.TestStep{ 965 resource.TestStep{ 966 Config: testAccComputeV2Instance_personality, 967 Check: resource.ComposeTestCheckFunc( 968 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 969 ), 970 }, 971 }, 972 }) 973 } 974 975 func TestAccComputeV2Instance_multiEphemeral(t *testing.T) { 976 var instance servers.Server 977 var testAccComputeV2Instance_multiEphemeral = fmt.Sprintf(` 978 resource "openstack_compute_instance_v2" "foo" { 979 name = "terraform-test" 980 security_groups = ["default"] 981 block_device { 982 boot_index = 0 983 delete_on_termination = true 984 destination_type = "local" 985 source_type = "image" 986 uuid = "%s" 987 } 988 block_device { 989 boot_index = -1 990 delete_on_termination = true 991 destination_type = "local" 992 source_type = "blank" 993 volume_size = 1 994 } 995 block_device { 996 boot_index = -1 997 delete_on_termination = true 998 destination_type = "local" 999 source_type = "blank" 1000 volume_size = 1 1001 } 1002 }`, 1003 os.Getenv("OS_IMAGE_ID")) 1004 1005 resource.Test(t, resource.TestCase{ 1006 PreCheck: func() { testAccPreCheck(t) }, 1007 Providers: testAccProviders, 1008 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 1009 Steps: []resource.TestStep{ 1010 resource.TestStep{ 1011 Config: testAccComputeV2Instance_multiEphemeral, 1012 Check: resource.ComposeTestCheckFunc( 1013 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 1014 ), 1015 }, 1016 }, 1017 }) 1018 } 1019 1020 func TestAccComputeV2Instance_accessIPv4(t *testing.T) { 1021 var instance servers.Server 1022 var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(` 1023 resource "openstack_compute_floatingip_v2" "myip" { 1024 } 1025 1026 resource "openstack_networking_network_v2" "network_1" { 1027 name = "network_1" 1028 } 1029 1030 resource "openstack_networking_subnet_v2" "subnet_1" { 1031 name = "subnet_1" 1032 network_id = "${openstack_networking_network_v2.network_1.id}" 1033 cidr = "192.168.1.0/24" 1034 ip_version = 4 1035 enable_dhcp = true 1036 no_gateway = true 1037 } 1038 1039 resource "openstack_compute_instance_v2" "instance_1" { 1040 depends_on = ["openstack_networking_subnet_v2.subnet_1"] 1041 1042 name = "instance_1" 1043 security_groups = ["default"] 1044 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 1045 1046 network { 1047 uuid = "%s" 1048 } 1049 1050 network { 1051 uuid = "${openstack_networking_network_v2.network_1.id}" 1052 fixed_ip_v4 = "192.168.1.100" 1053 access_network = true 1054 } 1055 }`, os.Getenv("OS_NETWORK_ID")) 1056 1057 resource.Test(t, resource.TestCase{ 1058 PreCheck: func() { testAccPreCheck(t) }, 1059 Providers: testAccProviders, 1060 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 1061 Steps: []resource.TestStep{ 1062 resource.TestStep{ 1063 Config: testAccComputeV2Instance_accessIPv4, 1064 Check: resource.ComposeTestCheckFunc( 1065 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance), 1066 resource.TestCheckResourceAttr( 1067 "openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"), 1068 ), 1069 }, 1070 }, 1071 }) 1072 } 1073 1074 func TestAccComputeV2Instance_ChangeFixedIP(t *testing.T) { 1075 var instance1_1 servers.Server 1076 var instance1_2 servers.Server 1077 var testAccComputeV2Instance_ChangeFixedIP_1 = fmt.Sprintf(` 1078 resource "openstack_compute_instance_v2" "instance_1" { 1079 name = "instance_1" 1080 security_groups = ["default"] 1081 network { 1082 uuid = "%s" 1083 fixed_ip_v4 = "10.0.0.24" 1084 } 1085 }`, 1086 os.Getenv("OS_NETWORK_ID")) 1087 1088 var testAccComputeV2Instance_ChangeFixedIP_2 = fmt.Sprintf(` 1089 resource "openstack_compute_instance_v2" "instance_1" { 1090 name = "instance_1" 1091 security_groups = ["default"] 1092 network { 1093 uuid = "%s" 1094 fixed_ip_v4 = "10.0.0.25" 1095 } 1096 }`, 1097 os.Getenv("OS_NETWORK_ID")) 1098 1099 resource.Test(t, resource.TestCase{ 1100 PreCheck: func() { testAccPreCheck(t) }, 1101 Providers: testAccProviders, 1102 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 1103 Steps: []resource.TestStep{ 1104 resource.TestStep{ 1105 Config: testAccComputeV2Instance_ChangeFixedIP_1, 1106 Check: resource.ComposeTestCheckFunc( 1107 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1), 1108 ), 1109 }, 1110 resource.TestStep{ 1111 Config: testAccComputeV2Instance_ChangeFixedIP_2, 1112 Check: resource.ComposeTestCheckFunc( 1113 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2), 1114 testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2), 1115 ), 1116 }, 1117 }, 1118 }) 1119 } 1120 1121 func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error { 1122 config := testAccProvider.Meta().(*Config) 1123 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1124 if err != nil { 1125 return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err) 1126 } 1127 1128 for _, rs := range s.RootModule().Resources { 1129 if rs.Type != "openstack_compute_instance_v2" { 1130 continue 1131 } 1132 1133 _, err := servers.Get(computeClient, rs.Primary.ID).Extract() 1134 if err == nil { 1135 return fmt.Errorf("Instance still exists") 1136 } 1137 } 1138 1139 return nil 1140 } 1141 1142 func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc { 1143 return func(s *terraform.State) error { 1144 rs, ok := s.RootModule().Resources[n] 1145 if !ok { 1146 return fmt.Errorf("Not found: %s", n) 1147 } 1148 1149 if rs.Primary.ID == "" { 1150 return fmt.Errorf("No ID is set") 1151 } 1152 1153 config := testAccProvider.Meta().(*Config) 1154 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1155 if err != nil { 1156 return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err) 1157 } 1158 1159 found, err := servers.Get(computeClient, rs.Primary.ID).Extract() 1160 if err != nil { 1161 return err 1162 } 1163 1164 if found.ID != rs.Primary.ID { 1165 return fmt.Errorf("Instance not found") 1166 } 1167 1168 *instance = *found 1169 1170 return nil 1171 } 1172 } 1173 1174 func testAccCheckComputeV2InstanceDoesNotExist(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc { 1175 return func(s *terraform.State) error { 1176 config := testAccProvider.Meta().(*Config) 1177 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1178 if err != nil { 1179 return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err) 1180 } 1181 1182 _, err = servers.Get(computeClient, instance.ID).Extract() 1183 if err != nil { 1184 if _, ok := err.(gophercloud.ErrDefault404); ok { 1185 return nil 1186 } 1187 return err 1188 } 1189 1190 return fmt.Errorf("Instance still exists") 1191 } 1192 } 1193 1194 func testAccCheckComputeV2InstanceMetadata( 1195 instance *servers.Server, k string, v string) resource.TestCheckFunc { 1196 return func(s *terraform.State) error { 1197 if instance.Metadata == nil { 1198 return fmt.Errorf("No metadata") 1199 } 1200 1201 for key, value := range instance.Metadata { 1202 if k != key { 1203 continue 1204 } 1205 1206 if v == value { 1207 return nil 1208 } 1209 1210 return fmt.Errorf("Bad value for %s: %s", k, value) 1211 } 1212 1213 return fmt.Errorf("Metadata not found: %s", k) 1214 } 1215 } 1216 1217 func testAccCheckComputeV2InstanceVolumeAttachment( 1218 instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc { 1219 return func(s *terraform.State) error { 1220 var attachments []volumeattach.VolumeAttachment 1221 1222 config := testAccProvider.Meta().(*Config) 1223 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1224 if err != nil { 1225 return err 1226 } 1227 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 1228 actual, err := volumeattach.ExtractVolumeAttachments(page) 1229 if err != nil { 1230 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 1231 } 1232 1233 attachments = actual 1234 return true, nil 1235 }) 1236 1237 for _, attachment := range attachments { 1238 if attachment.VolumeID == volume.ID { 1239 return nil 1240 } 1241 } 1242 1243 return fmt.Errorf("Volume not found: %s", volume.ID) 1244 } 1245 } 1246 1247 func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc { 1248 return func(s *terraform.State) error { 1249 var attachments []volumeattach.VolumeAttachment 1250 1251 config := testAccProvider.Meta().(*Config) 1252 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1253 if err != nil { 1254 return err 1255 } 1256 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 1257 actual, err := volumeattach.ExtractVolumeAttachments(page) 1258 if err != nil { 1259 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 1260 } 1261 1262 attachments = actual 1263 return true, nil 1264 }) 1265 1266 if len(attachments) > 0 { 1267 return fmt.Errorf("Volumes are still attached.") 1268 } 1269 1270 return nil 1271 } 1272 } 1273 1274 func testAccCheckComputeV2InstanceBootVolumeAttachment( 1275 instance *servers.Server) resource.TestCheckFunc { 1276 return func(s *terraform.State) error { 1277 var attachments []volumeattach.VolumeAttachment 1278 1279 config := testAccProvider.Meta().(*Config) 1280 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1281 if err != nil { 1282 return err 1283 } 1284 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 1285 actual, err := volumeattach.ExtractVolumeAttachments(page) 1286 if err != nil { 1287 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 1288 } 1289 1290 attachments = actual 1291 return true, nil 1292 }) 1293 1294 if len(attachments) == 1 { 1295 return nil 1296 } 1297 1298 return fmt.Errorf("No attached volume found.") 1299 } 1300 } 1301 1302 func testAccCheckComputeV2InstanceFloatingIPAttach( 1303 instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc { 1304 return func(s *terraform.State) error { 1305 if fip.InstanceID == instance.ID { 1306 return nil 1307 } 1308 1309 return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID) 1310 } 1311 } 1312 func testAccCheckComputeV2InstanceInstanceIDsDoNotMatch( 1313 instance1, instance2 *servers.Server) resource.TestCheckFunc { 1314 return func(s *terraform.State) error { 1315 if instance1.ID == instance2.ID { 1316 return fmt.Errorf("Instance was not recreated.") 1317 } 1318 1319 return nil 1320 } 1321 } 1322 1323 func TestAccComputeV2Instance_stop_before_destroy(t *testing.T) { 1324 var instance servers.Server 1325 var testAccComputeV2Instance_stop_before_destroy = fmt.Sprintf(` 1326 resource "openstack_compute_instance_v2" "foo" { 1327 name = "terraform-test" 1328 security_groups = ["default"] 1329 network { 1330 uuid = "%s" 1331 } 1332 stop_before_destroy = true 1333 }`, 1334 os.Getenv("OS_NETWORK_ID")) 1335 1336 resource.Test(t, resource.TestCase{ 1337 PreCheck: func() { testAccPreCheck(t) }, 1338 Providers: testAccProviders, 1339 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 1340 Steps: []resource.TestStep{ 1341 resource.TestStep{ 1342 Config: testAccComputeV2Instance_stop_before_destroy, 1343 Check: resource.ComposeTestCheckFunc( 1344 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 1345 ), 1346 }, 1347 }, 1348 }) 1349 } 1350 1351 func testAccCheckComputeV2InstanceVolumeDetached(instance *servers.Server, volume_id string) resource.TestCheckFunc { 1352 return func(s *terraform.State) error { 1353 var attachments []volumeattach.VolumeAttachment 1354 1355 rs, ok := s.RootModule().Resources[volume_id] 1356 if !ok { 1357 return fmt.Errorf("Not found: %s", volume_id) 1358 } 1359 1360 if rs.Primary.ID == "" { 1361 return fmt.Errorf("No ID is set") 1362 } 1363 1364 config := testAccProvider.Meta().(*Config) 1365 computeClient, err := config.computeV2Client(OS_REGION_NAME) 1366 if err != nil { 1367 return err 1368 } 1369 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 1370 actual, err := volumeattach.ExtractVolumeAttachments(page) 1371 if err != nil { 1372 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 1373 } 1374 1375 attachments = actual 1376 return true, nil 1377 }) 1378 1379 for _, attachment := range attachments { 1380 if attachment.VolumeID == rs.Primary.ID { 1381 return fmt.Errorf("Volume is still attached.") 1382 } 1383 } 1384 1385 return nil 1386 } 1387 }