github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/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/rackspace/gophercloud/openstack/blockstorage/v1/volumes" 12 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip" 13 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" 14 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach" 15 "github.com/rackspace/gophercloud/openstack/compute/v2/servers" 16 "github.com/rackspace/gophercloud/pagination" 17 ) 18 19 func TestAccComputeV2Instance_basic(t *testing.T) { 20 var instance servers.Server 21 var testAccComputeV2Instance_basic = fmt.Sprintf(` 22 resource "openstack_compute_instance_v2" "foo" { 23 name = "terraform-test" 24 security_groups = ["default"] 25 network { 26 uuid = "%s" 27 } 28 metadata { 29 foo = "bar" 30 } 31 }`, 32 os.Getenv("OS_NETWORK_ID")) 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccComputeV2Instance_basic, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 43 testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"), 44 ), 45 }, 46 }, 47 }) 48 } 49 50 func TestAccComputeV2Instance_volumeAttach(t *testing.T) { 51 var instance servers.Server 52 var volume volumes.Volume 53 54 var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(` 55 resource "openstack_blockstorage_volume_v1" "myvol" { 56 name = "myvol" 57 size = 1 58 } 59 60 resource "openstack_compute_instance_v2" "foo" { 61 name = "terraform-test" 62 security_groups = ["default"] 63 volume { 64 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 65 } 66 }`) 67 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 72 Steps: []resource.TestStep{ 73 resource.TestStep{ 74 Config: testAccComputeV2Instance_volumeAttach, 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 77 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 78 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 79 ), 80 }, 81 }, 82 }) 83 } 84 85 func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) { 86 var instance servers.Server 87 var volume volumes.Volume 88 89 var testAccComputeV2Instance_volumeAttachPostCreationInstance = fmt.Sprintf(` 90 resource "openstack_compute_instance_v2" "foo" { 91 name = "terraform-test" 92 security_groups = ["default"] 93 }`) 94 95 var testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume = fmt.Sprintf(` 96 resource "openstack_blockstorage_volume_v1" "myvol" { 97 name = "myvol" 98 size = 1 99 } 100 101 resource "openstack_compute_instance_v2" "foo" { 102 name = "terraform-test" 103 security_groups = ["default"] 104 volume { 105 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 106 } 107 }`) 108 109 resource.Test(t, resource.TestCase{ 110 PreCheck: func() { testAccPreCheck(t) }, 111 Providers: testAccProviders, 112 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 113 Steps: []resource.TestStep{ 114 resource.TestStep{ 115 Config: testAccComputeV2Instance_volumeAttachPostCreationInstance, 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 118 ), 119 }, 120 resource.TestStep{ 121 Config: testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume, 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 124 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 125 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 126 ), 127 }, 128 }, 129 }) 130 } 131 132 func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) { 133 var instance servers.Server 134 var volume volumes.Volume 135 136 var testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume = fmt.Sprintf(` 137 resource "openstack_blockstorage_volume_v1" "myvol" { 138 name = "myvol" 139 size = 1 140 } 141 142 resource "openstack_compute_instance_v2" "foo" { 143 name = "terraform-test" 144 security_groups = ["default"] 145 volume { 146 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 147 } 148 }`) 149 150 var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(` 151 resource "openstack_compute_instance_v2" "foo" { 152 name = "terraform-test" 153 security_groups = ["default"] 154 }`) 155 156 resource.Test(t, resource.TestCase{ 157 PreCheck: func() { testAccPreCheck(t) }, 158 Providers: testAccProviders, 159 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 160 Steps: []resource.TestStep{ 161 resource.TestStep{ 162 Config: testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume, 163 Check: resource.ComposeTestCheckFunc( 164 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 165 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 166 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 167 ), 168 }, 169 resource.TestStep{ 170 Config: testAccComputeV2Instance_volumeDetachPostCreationInstance, 171 Check: resource.ComposeTestCheckFunc( 172 testAccCheckBlockStorageV1VolumeDoesNotExist(t, "openstack_blockstorage_volume_v1.myvol", &volume), 173 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 174 testAccCheckComputeV2InstanceVolumesDetached(&instance), 175 ), 176 }, 177 }, 178 }) 179 } 180 181 func TestAccComputeV2Instance_floatingIPAttachGlobally(t *testing.T) { 182 var instance servers.Server 183 var fip floatingip.FloatingIP 184 var testAccComputeV2Instance_floatingIPAttachGlobally = fmt.Sprintf(` 185 resource "openstack_compute_floatingip_v2" "myip" { 186 } 187 188 resource "openstack_compute_instance_v2" "foo" { 189 name = "terraform-test" 190 security_groups = ["default"] 191 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 192 193 network { 194 uuid = "%s" 195 } 196 }`, 197 os.Getenv("OS_NETWORK_ID")) 198 199 resource.Test(t, resource.TestCase{ 200 PreCheck: func() { testAccPreCheck(t) }, 201 Providers: testAccProviders, 202 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 203 Steps: []resource.TestStep{ 204 resource.TestStep{ 205 Config: testAccComputeV2Instance_floatingIPAttachGlobally, 206 Check: resource.ComposeTestCheckFunc( 207 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), 208 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 209 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 210 ), 211 }, 212 }, 213 }) 214 } 215 216 func TestAccComputeV2Instance_floatingIPAttachToNetwork(t *testing.T) { 217 var instance servers.Server 218 var fip floatingip.FloatingIP 219 var testAccComputeV2Instance_floatingIPAttachToNetwork = fmt.Sprintf(` 220 resource "openstack_compute_floatingip_v2" "myip" { 221 } 222 223 resource "openstack_compute_instance_v2" "foo" { 224 name = "terraform-test" 225 security_groups = ["default"] 226 227 network { 228 uuid = "%s" 229 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 230 access_network = true 231 } 232 }`, 233 os.Getenv("OS_NETWORK_ID")) 234 235 resource.Test(t, resource.TestCase{ 236 PreCheck: func() { testAccPreCheck(t) }, 237 Providers: testAccProviders, 238 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 239 Steps: []resource.TestStep{ 240 resource.TestStep{ 241 Config: testAccComputeV2Instance_floatingIPAttachToNetwork, 242 Check: resource.ComposeTestCheckFunc( 243 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), 244 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 245 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 246 ), 247 }, 248 }, 249 }) 250 } 251 252 func TestAccComputeV2Instance_floatingIPAttachAndChange(t *testing.T) { 253 var instance servers.Server 254 var fip floatingip.FloatingIP 255 var testAccComputeV2Instance_floatingIPAttachToNetwork_1 = fmt.Sprintf(` 256 resource "openstack_compute_floatingip_v2" "myip_1" { 257 } 258 259 resource "openstack_compute_floatingip_v2" "myip_2" { 260 } 261 262 resource "openstack_compute_instance_v2" "foo" { 263 name = "terraform-test" 264 security_groups = ["default"] 265 266 network { 267 uuid = "%s" 268 floating_ip = "${openstack_compute_floatingip_v2.myip_1.address}" 269 access_network = true 270 } 271 }`, 272 os.Getenv("OS_NETWORK_ID")) 273 274 var testAccComputeV2Instance_floatingIPAttachToNetwork_2 = fmt.Sprintf(` 275 resource "openstack_compute_floatingip_v2" "myip_1" { 276 } 277 278 resource "openstack_compute_floatingip_v2" "myip_2" { 279 } 280 281 resource "openstack_compute_instance_v2" "foo" { 282 name = "terraform-test" 283 security_groups = ["default"] 284 285 network { 286 uuid = "%s" 287 floating_ip = "${openstack_compute_floatingip_v2.myip_2.address}" 288 access_network = true 289 } 290 }`, 291 os.Getenv("OS_NETWORK_ID")) 292 293 resource.Test(t, resource.TestCase{ 294 PreCheck: func() { testAccPreCheck(t) }, 295 Providers: testAccProviders, 296 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 297 Steps: []resource.TestStep{ 298 resource.TestStep{ 299 Config: testAccComputeV2Instance_floatingIPAttachToNetwork_1, 300 Check: resource.ComposeTestCheckFunc( 301 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_1", &fip), 302 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 303 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 304 ), 305 }, 306 resource.TestStep{ 307 Config: testAccComputeV2Instance_floatingIPAttachToNetwork_2, 308 Check: resource.ComposeTestCheckFunc( 309 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_2", &fip), 310 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 311 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 312 ), 313 }, 314 }, 315 }) 316 } 317 318 func TestAccComputeV2Instance_multi_secgroups(t *testing.T) { 319 var instance servers.Server 320 var secgroup secgroups.SecurityGroup 321 var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(` 322 resource "openstack_compute_secgroup_v2" "foo" { 323 name = "terraform-test" 324 description = "a security group" 325 rule { 326 from_port = 22 327 to_port = 22 328 ip_protocol = "tcp" 329 cidr = "0.0.0.0/0" 330 } 331 } 332 333 resource "openstack_compute_instance_v2" "foo" { 334 name = "terraform-test" 335 security_groups = ["default", "${openstack_compute_secgroup_v2.foo.name}"] 336 network { 337 uuid = "%s" 338 } 339 }`, 340 os.Getenv("OS_NETWORK_ID")) 341 342 resource.Test(t, resource.TestCase{ 343 PreCheck: func() { testAccPreCheck(t) }, 344 Providers: testAccProviders, 345 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 346 Steps: []resource.TestStep{ 347 resource.TestStep{ 348 Config: testAccComputeV2Instance_multi_secgroups, 349 Check: resource.ComposeTestCheckFunc( 350 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.foo", &secgroup), 351 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 352 ), 353 }, 354 }, 355 }) 356 } 357 358 func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) { 359 var instance servers.Server 360 var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(` 361 resource "openstack_compute_instance_v2" "foo" { 362 name = "terraform-test" 363 security_groups = ["default"] 364 block_device { 365 uuid = "%s" 366 source_type = "image" 367 volume_size = 5 368 boot_index = 0 369 destination_type = "volume" 370 delete_on_termination = true 371 } 372 }`, 373 os.Getenv("OS_IMAGE_ID")) 374 375 resource.Test(t, resource.TestCase{ 376 PreCheck: func() { testAccPreCheck(t) }, 377 Providers: testAccProviders, 378 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 379 Steps: []resource.TestStep{ 380 resource.TestStep{ 381 Config: testAccComputeV2Instance_bootFromVolumeImage, 382 Check: resource.ComposeTestCheckFunc( 383 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 384 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 385 ), 386 }, 387 }, 388 }) 389 } 390 391 func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) { 392 var instance servers.Server 393 var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(` 394 resource "openstack_blockstorage_volume_v1" "foo" { 395 name = "terraform-test" 396 size = 5 397 image_id = "%s" 398 } 399 400 resource "openstack_compute_instance_v2" "foo" { 401 name = "terraform-test" 402 security_groups = ["default"] 403 block_device { 404 uuid = "${openstack_blockstorage_volume_v1.foo.id}" 405 source_type = "volume" 406 boot_index = 0 407 destination_type = "volume" 408 delete_on_termination = true 409 } 410 }`, 411 os.Getenv("OS_IMAGE_ID")) 412 413 resource.Test(t, resource.TestCase{ 414 PreCheck: func() { testAccPreCheck(t) }, 415 Providers: testAccProviders, 416 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 417 Steps: []resource.TestStep{ 418 resource.TestStep{ 419 Config: testAccComputeV2Instance_bootFromVolumeVolume, 420 Check: resource.ComposeTestCheckFunc( 421 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 422 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 423 ), 424 }, 425 }, 426 }) 427 } 428 429 func TestAccComputeV2Instance_bootFromVolumeForceNew(t *testing.T) { 430 var instance1_1 servers.Server 431 var instance1_2 servers.Server 432 var testAccComputeV2Instance_bootFromVolumeForceNew_1 = fmt.Sprintf(` 433 resource "openstack_compute_instance_v2" "instance_1" { 434 name = "instance_1" 435 security_groups = ["default"] 436 block_device { 437 uuid = "%s" 438 source_type = "image" 439 volume_size = 5 440 boot_index = 0 441 destination_type = "volume" 442 delete_on_termination = true 443 } 444 }`, 445 os.Getenv("OS_IMAGE_ID")) 446 447 var testAccComputeV2Instance_bootFromVolumeForceNew_2 = fmt.Sprintf(` 448 resource "openstack_compute_instance_v2" "instance_1" { 449 name = "instance_1" 450 security_groups = ["default"] 451 block_device { 452 uuid = "%s" 453 source_type = "image" 454 volume_size = 4 455 boot_index = 0 456 destination_type = "volume" 457 delete_on_termination = true 458 } 459 }`, 460 os.Getenv("OS_IMAGE_ID")) 461 462 resource.Test(t, resource.TestCase{ 463 PreCheck: func() { testAccPreCheck(t) }, 464 Providers: testAccProviders, 465 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 466 Steps: []resource.TestStep{ 467 resource.TestStep{ 468 Config: testAccComputeV2Instance_bootFromVolumeForceNew_1, 469 Check: resource.ComposeTestCheckFunc( 470 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1), 471 ), 472 }, 473 resource.TestStep{ 474 Config: testAccComputeV2Instance_bootFromVolumeForceNew_2, 475 Check: resource.ComposeTestCheckFunc( 476 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2), 477 testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2), 478 ), 479 }, 480 }, 481 }) 482 } 483 484 // TODO: verify the personality really exists on the instance. 485 func TestAccComputeV2Instance_personality(t *testing.T) { 486 var instance servers.Server 487 var testAccComputeV2Instance_personality = fmt.Sprintf(` 488 resource "openstack_compute_instance_v2" "foo" { 489 name = "terraform-test" 490 security_groups = ["default"] 491 personality { 492 file = "/tmp/foobar.txt" 493 content = "happy" 494 } 495 personality { 496 file = "/tmp/barfoo.txt" 497 content = "angry" 498 } 499 }`) 500 501 resource.Test(t, resource.TestCase{ 502 PreCheck: func() { testAccPreCheck(t) }, 503 Providers: testAccProviders, 504 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 505 Steps: []resource.TestStep{ 506 resource.TestStep{ 507 Config: testAccComputeV2Instance_personality, 508 Check: resource.ComposeTestCheckFunc( 509 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 510 ), 511 }, 512 }, 513 }) 514 } 515 516 func TestAccComputeV2Instance_multiEphemeral(t *testing.T) { 517 var instance servers.Server 518 var testAccComputeV2Instance_multiEphemeral = fmt.Sprintf(` 519 resource "openstack_compute_instance_v2" "foo" { 520 name = "terraform-test" 521 security_groups = ["default"] 522 block_device { 523 boot_index = 0 524 delete_on_termination = true 525 destination_type = "local" 526 source_type = "image" 527 uuid = "%s" 528 } 529 block_device { 530 boot_index = -1 531 delete_on_termination = true 532 destination_type = "local" 533 source_type = "blank" 534 volume_size = 1 535 } 536 block_device { 537 boot_index = -1 538 delete_on_termination = true 539 destination_type = "local" 540 source_type = "blank" 541 volume_size = 1 542 } 543 }`, 544 os.Getenv("OS_IMAGE_ID")) 545 546 resource.Test(t, resource.TestCase{ 547 PreCheck: func() { testAccPreCheck(t) }, 548 Providers: testAccProviders, 549 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 550 Steps: []resource.TestStep{ 551 resource.TestStep{ 552 Config: testAccComputeV2Instance_multiEphemeral, 553 Check: resource.ComposeTestCheckFunc( 554 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 555 ), 556 }, 557 }, 558 }) 559 } 560 561 func TestAccComputeV2Instance_accessIPv4(t *testing.T) { 562 var instance servers.Server 563 var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(` 564 resource "openstack_compute_floatingip_v2" "myip" { 565 } 566 567 resource "openstack_networking_network_v2" "network_1" { 568 name = "network_1" 569 } 570 571 resource "openstack_networking_subnet_v2" "subnet_1" { 572 name = "subnet_1" 573 network_id = "${openstack_networking_network_v2.network_1.id}" 574 cidr = "192.168.1.0/24" 575 ip_version = 4 576 enable_dhcp = true 577 no_gateway = true 578 } 579 580 resource "openstack_compute_instance_v2" "instance_1" { 581 depends_on = ["openstack_networking_subnet_v2.subnet_1"] 582 583 name = "instance_1" 584 security_groups = ["default"] 585 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 586 587 network { 588 uuid = "%s" 589 } 590 591 network { 592 uuid = "${openstack_networking_network_v2.network_1.id}" 593 fixed_ip_v4 = "192.168.1.100" 594 access_network = true 595 } 596 }`, os.Getenv("OS_NETWORK_ID")) 597 598 resource.Test(t, resource.TestCase{ 599 PreCheck: func() { testAccPreCheck(t) }, 600 Providers: testAccProviders, 601 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 602 Steps: []resource.TestStep{ 603 resource.TestStep{ 604 Config: testAccComputeV2Instance_accessIPv4, 605 Check: resource.ComposeTestCheckFunc( 606 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance), 607 resource.TestCheckResourceAttr( 608 "openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"), 609 ), 610 }, 611 }, 612 }) 613 } 614 615 func TestAccComputeV2Instance_ChangeFixedIP(t *testing.T) { 616 var instance1_1 servers.Server 617 var instance1_2 servers.Server 618 var testAccComputeV2Instance_ChangeFixedIP_1 = fmt.Sprintf(` 619 resource "openstack_compute_instance_v2" "instance_1" { 620 name = "instance_1" 621 security_groups = ["default"] 622 network { 623 uuid = "%s" 624 fixed_ip_v4 = "10.0.0.24" 625 } 626 }`, 627 os.Getenv("OS_NETWORK_ID")) 628 629 var testAccComputeV2Instance_ChangeFixedIP_2 = fmt.Sprintf(` 630 resource "openstack_compute_instance_v2" "instance_1" { 631 name = "instance_1" 632 security_groups = ["default"] 633 network { 634 uuid = "%s" 635 fixed_ip_v4 = "10.0.0.25" 636 } 637 }`, 638 os.Getenv("OS_NETWORK_ID")) 639 640 resource.Test(t, resource.TestCase{ 641 PreCheck: func() { testAccPreCheck(t) }, 642 Providers: testAccProviders, 643 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 644 Steps: []resource.TestStep{ 645 resource.TestStep{ 646 Config: testAccComputeV2Instance_ChangeFixedIP_1, 647 Check: resource.ComposeTestCheckFunc( 648 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1), 649 ), 650 }, 651 resource.TestStep{ 652 Config: testAccComputeV2Instance_ChangeFixedIP_2, 653 Check: resource.ComposeTestCheckFunc( 654 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2), 655 testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2), 656 ), 657 }, 658 }, 659 }) 660 } 661 662 func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error { 663 config := testAccProvider.Meta().(*Config) 664 computeClient, err := config.computeV2Client(OS_REGION_NAME) 665 if err != nil { 666 return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err) 667 } 668 669 for _, rs := range s.RootModule().Resources { 670 if rs.Type != "openstack_compute_instance_v2" { 671 continue 672 } 673 674 _, err := servers.Get(computeClient, rs.Primary.ID).Extract() 675 if err == nil { 676 return fmt.Errorf("Instance still exists") 677 } 678 } 679 680 return nil 681 } 682 683 func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc { 684 return func(s *terraform.State) error { 685 rs, ok := s.RootModule().Resources[n] 686 if !ok { 687 return fmt.Errorf("Not found: %s", n) 688 } 689 690 if rs.Primary.ID == "" { 691 return fmt.Errorf("No ID is set") 692 } 693 694 config := testAccProvider.Meta().(*Config) 695 computeClient, err := config.computeV2Client(OS_REGION_NAME) 696 if err != nil { 697 return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err) 698 } 699 700 found, err := servers.Get(computeClient, rs.Primary.ID).Extract() 701 if err != nil { 702 return err 703 } 704 705 if found.ID != rs.Primary.ID { 706 return fmt.Errorf("Instance not found") 707 } 708 709 *instance = *found 710 711 return nil 712 } 713 } 714 715 func testAccCheckComputeV2InstanceMetadata( 716 instance *servers.Server, k string, v string) resource.TestCheckFunc { 717 return func(s *terraform.State) error { 718 if instance.Metadata == nil { 719 return fmt.Errorf("No metadata") 720 } 721 722 for key, value := range instance.Metadata { 723 if k != key { 724 continue 725 } 726 727 if v == value.(string) { 728 return nil 729 } 730 731 return fmt.Errorf("Bad value for %s: %s", k, value) 732 } 733 734 return fmt.Errorf("Metadata not found: %s", k) 735 } 736 } 737 738 func testAccCheckComputeV2InstanceVolumeAttachment( 739 instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc { 740 return func(s *terraform.State) error { 741 var attachments []volumeattach.VolumeAttachment 742 743 config := testAccProvider.Meta().(*Config) 744 computeClient, err := config.computeV2Client(OS_REGION_NAME) 745 if err != nil { 746 return err 747 } 748 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 749 actual, err := volumeattach.ExtractVolumeAttachments(page) 750 if err != nil { 751 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 752 } 753 754 attachments = actual 755 return true, nil 756 }) 757 758 for _, attachment := range attachments { 759 if attachment.VolumeID == volume.ID { 760 return nil 761 } 762 } 763 764 return fmt.Errorf("Volume not found: %s", volume.ID) 765 } 766 } 767 768 func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc { 769 return func(s *terraform.State) error { 770 var attachments []volumeattach.VolumeAttachment 771 772 config := testAccProvider.Meta().(*Config) 773 computeClient, err := config.computeV2Client(OS_REGION_NAME) 774 if err != nil { 775 return err 776 } 777 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 778 actual, err := volumeattach.ExtractVolumeAttachments(page) 779 if err != nil { 780 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 781 } 782 783 attachments = actual 784 return true, nil 785 }) 786 787 if len(attachments) > 0 { 788 return fmt.Errorf("Volumes are still attached.") 789 } 790 791 return nil 792 } 793 } 794 795 func testAccCheckComputeV2InstanceBootVolumeAttachment( 796 instance *servers.Server) resource.TestCheckFunc { 797 return func(s *terraform.State) error { 798 var attachments []volumeattach.VolumeAttachment 799 800 config := testAccProvider.Meta().(*Config) 801 computeClient, err := config.computeV2Client(OS_REGION_NAME) 802 if err != nil { 803 return err 804 } 805 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 806 actual, err := volumeattach.ExtractVolumeAttachments(page) 807 if err != nil { 808 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 809 } 810 811 attachments = actual 812 return true, nil 813 }) 814 815 if len(attachments) == 1 { 816 return nil 817 } 818 819 return fmt.Errorf("No attached volume found.") 820 } 821 } 822 823 func testAccCheckComputeV2InstanceFloatingIPAttach( 824 instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc { 825 return func(s *terraform.State) error { 826 if fip.InstanceID == instance.ID { 827 return nil 828 } 829 830 return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID) 831 } 832 } 833 func testAccCheckComputeV2InstanceInstanceIDsDoNotMatch( 834 instance1, instance2 *servers.Server) resource.TestCheckFunc { 835 return func(s *terraform.State) error { 836 if instance1.ID == instance2.ID { 837 return fmt.Errorf("Instance was not recreated.") 838 } 839 840 return nil 841 } 842 }