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