github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go (about) 1 package vsphere 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/vmware/govmomi" 11 "github.com/vmware/govmomi/find" 12 "github.com/vmware/govmomi/object" 13 "github.com/vmware/govmomi/property" 14 "github.com/vmware/govmomi/vim25/mo" 15 "github.com/vmware/govmomi/vim25/types" 16 "golang.org/x/net/context" 17 ) 18 19 func TestAccVSphereVirtualMachine_basic(t *testing.T) { 20 var vm virtualMachine 21 var locationOpt string 22 var datastoreOpt string 23 24 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 25 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 26 } 27 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 28 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 29 } 30 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 31 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 32 } 33 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 34 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 35 } 36 template := os.Getenv("VSPHERE_TEMPLATE") 37 gateway := os.Getenv("VSPHERE_IPV4_GATEWAY") 38 label := os.Getenv("VSPHERE_NETWORK_LABEL") 39 ip_address := os.Getenv("VSPHERE_IPV4_ADDRESS") 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 45 Steps: []resource.TestStep{ 46 resource.TestStep{ 47 Config: fmt.Sprintf( 48 testAccCheckVSphereVirtualMachineConfig_basic, 49 locationOpt, 50 gateway, 51 label, 52 ip_address, 53 gateway, 54 datastoreOpt, 55 template, 56 ), 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm), 59 resource.TestCheckResourceAttr( 60 "vsphere_virtual_machine.foo", "name", "terraform-test"), 61 resource.TestCheckResourceAttr( 62 "vsphere_virtual_machine.foo", "vcpu", "2"), 63 resource.TestCheckResourceAttr( 64 "vsphere_virtual_machine.foo", "memory", "4096"), 65 resource.TestCheckResourceAttr( 66 "vsphere_virtual_machine.foo", "memory_reservation", "4096"), 67 resource.TestCheckResourceAttr( 68 "vsphere_virtual_machine.foo", "disk.#", "2"), 69 resource.TestCheckResourceAttr( 70 "vsphere_virtual_machine.foo", "disk.0.template", template), 71 resource.TestCheckResourceAttr( 72 "vsphere_virtual_machine.foo", "network_interface.#", "1"), 73 resource.TestCheckResourceAttr( 74 "vsphere_virtual_machine.foo", "network_interface.0.label", label), 75 ), 76 }, 77 }, 78 }) 79 } 80 81 func TestAccVSphereVirtualMachine_diskInitType(t *testing.T) { 82 var vm virtualMachine 83 var locationOpt string 84 var datastoreOpt string 85 86 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 87 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 88 } 89 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 90 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 91 } 92 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 93 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 94 } 95 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 96 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 97 } 98 template := os.Getenv("VSPHERE_TEMPLATE") 99 gateway := os.Getenv("VSPHERE_IPV4_GATEWAY") 100 label := os.Getenv("VSPHERE_NETWORK_LABEL") 101 ip_address := os.Getenv("VSPHERE_IPV4_ADDRESS") 102 103 resource.Test(t, resource.TestCase{ 104 PreCheck: func() { testAccPreCheck(t) }, 105 Providers: testAccProviders, 106 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 107 Steps: []resource.TestStep{ 108 resource.TestStep{ 109 Config: fmt.Sprintf( 110 testAccCheckVSphereVirtualMachineConfig_initType, 111 locationOpt, 112 gateway, 113 label, 114 ip_address, 115 gateway, 116 datastoreOpt, 117 template, 118 ), 119 Check: resource.ComposeTestCheckFunc( 120 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.thin", &vm), 121 resource.TestCheckResourceAttr( 122 "vsphere_virtual_machine.thin", "name", "terraform-test"), 123 resource.TestCheckResourceAttr( 124 "vsphere_virtual_machine.thin", "vcpu", "2"), 125 resource.TestCheckResourceAttr( 126 "vsphere_virtual_machine.thin", "memory", "4096"), 127 resource.TestCheckResourceAttr( 128 "vsphere_virtual_machine.thin", "disk.#", "2"), 129 resource.TestCheckResourceAttr( 130 "vsphere_virtual_machine.thin", "disk.0.template", template), 131 resource.TestCheckResourceAttr( 132 "vsphere_virtual_machine.thin", "disk.0.type", "thin"), 133 resource.TestCheckResourceAttr( 134 "vsphere_virtual_machine.thin", "disk.1.type", "eager_zeroed"), 135 resource.TestCheckResourceAttr( 136 "vsphere_virtual_machine.thin", "network_interface.#", "1"), 137 resource.TestCheckResourceAttr( 138 "vsphere_virtual_machine.thin", "network_interface.0.label", label), 139 ), 140 }, 141 }, 142 }) 143 } 144 145 func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { 146 var vm virtualMachine 147 var locationOpt string 148 var datastoreOpt string 149 150 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 151 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 152 } 153 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 154 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 155 } 156 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 157 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 158 } 159 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 160 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 161 } 162 template := os.Getenv("VSPHERE_TEMPLATE") 163 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 164 165 resource.Test(t, resource.TestCase{ 166 PreCheck: func() { testAccPreCheck(t) }, 167 Providers: testAccProviders, 168 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 169 Steps: []resource.TestStep{ 170 resource.TestStep{ 171 Config: fmt.Sprintf( 172 testAccCheckVSphereVirtualMachineConfig_dhcp, 173 locationOpt, 174 label, 175 datastoreOpt, 176 template, 177 ), 178 Check: resource.ComposeTestCheckFunc( 179 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), 180 resource.TestCheckResourceAttr( 181 "vsphere_virtual_machine.bar", "name", "terraform-test"), 182 resource.TestCheckResourceAttr( 183 "vsphere_virtual_machine.bar", "vcpu", "2"), 184 resource.TestCheckResourceAttr( 185 "vsphere_virtual_machine.bar", "memory", "4096"), 186 resource.TestCheckResourceAttr( 187 "vsphere_virtual_machine.bar", "disk.#", "1"), 188 resource.TestCheckResourceAttr( 189 "vsphere_virtual_machine.bar", "disk.0.template", template), 190 resource.TestCheckResourceAttr( 191 "vsphere_virtual_machine.bar", "network_interface.#", "1"), 192 resource.TestCheckResourceAttr( 193 "vsphere_virtual_machine.bar", "network_interface.0.label", label), 194 ), 195 }, 196 }, 197 }) 198 } 199 200 func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { 201 var vm virtualMachine 202 var locationOpt string 203 var datastoreOpt string 204 205 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 206 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 207 } 208 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 209 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 210 } 211 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 212 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 213 } 214 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 215 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 216 } 217 template := os.Getenv("VSPHERE_TEMPLATE") 218 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 219 220 resource.Test(t, resource.TestCase{ 221 PreCheck: func() { testAccPreCheck(t) }, 222 Providers: testAccProviders, 223 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 224 Steps: []resource.TestStep{ 225 resource.TestStep{ 226 Config: fmt.Sprintf( 227 testAccCheckVSphereVirtualMachineConfig_custom_configs, 228 locationOpt, 229 label, 230 datastoreOpt, 231 template, 232 ), 233 Check: resource.ComposeTestCheckFunc( 234 testAccCheckVSphereVirtualMachineExistsHasCustomConfig("vsphere_virtual_machine.car", &vm), 235 resource.TestCheckResourceAttr( 236 "vsphere_virtual_machine.car", "name", "terraform-test-custom"), 237 resource.TestCheckResourceAttr( 238 "vsphere_virtual_machine.car", "vcpu", "2"), 239 resource.TestCheckResourceAttr( 240 "vsphere_virtual_machine.car", "memory", "4096"), 241 resource.TestCheckResourceAttr( 242 "vsphere_virtual_machine.car", "disk.#", "1"), 243 resource.TestCheckResourceAttr( 244 "vsphere_virtual_machine.car", "disk.0.template", template), 245 resource.TestCheckResourceAttr( 246 "vsphere_virtual_machine.car", "network_interface.#", "1"), 247 resource.TestCheckResourceAttr( 248 "vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"), 249 resource.TestCheckResourceAttr( 250 "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrari"), 251 resource.TestCheckResourceAttr( 252 "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), 253 resource.TestCheckResourceAttr( 254 "vsphere_virtual_machine.car", "network_interface.0.label", label), 255 ), 256 }, 257 }, 258 }) 259 } 260 261 func TestAccVSphereVirtualMachine_createInExistingFolder(t *testing.T) { 262 var vm virtualMachine 263 var locationOpt string 264 var datastoreOpt string 265 var datacenter string 266 267 folder := "tf_test_createInExistingFolder" 268 269 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 270 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 271 datacenter = v 272 } 273 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 274 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 275 } 276 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 277 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 278 } 279 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 280 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 281 } 282 template := os.Getenv("VSPHERE_TEMPLATE") 283 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 284 285 resource.Test(t, resource.TestCase{ 286 PreCheck: func() { testAccPreCheck(t) }, 287 Providers: testAccProviders, 288 CheckDestroy: resource.ComposeTestCheckFunc( 289 testAccCheckVSphereVirtualMachineDestroy, 290 removeVSphereFolder(datacenter, folder, ""), 291 ), 292 Steps: []resource.TestStep{ 293 resource.TestStep{ 294 PreConfig: func() { createVSphereFolder(datacenter, folder) }, 295 Config: fmt.Sprintf( 296 testAccCheckVSphereVirtualMachineConfig_createInFolder, 297 folder, 298 locationOpt, 299 label, 300 datastoreOpt, 301 template, 302 ), 303 Check: resource.ComposeTestCheckFunc( 304 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.folder", &vm), 305 resource.TestCheckResourceAttr( 306 "vsphere_virtual_machine.folder", "name", "terraform-test-folder"), 307 resource.TestCheckResourceAttr( 308 "vsphere_virtual_machine.folder", "folder", folder), 309 resource.TestCheckResourceAttr( 310 "vsphere_virtual_machine.folder", "vcpu", "2"), 311 resource.TestCheckResourceAttr( 312 "vsphere_virtual_machine.folder", "memory", "4096"), 313 resource.TestCheckResourceAttr( 314 "vsphere_virtual_machine.folder", "disk.#", "1"), 315 resource.TestCheckResourceAttr( 316 "vsphere_virtual_machine.folder", "disk.0.template", template), 317 resource.TestCheckResourceAttr( 318 "vsphere_virtual_machine.folder", "network_interface.#", "1"), 319 resource.TestCheckResourceAttr( 320 "vsphere_virtual_machine.folder", "network_interface.0.label", label), 321 ), 322 }, 323 }, 324 }) 325 } 326 327 func TestAccVSphereVirtualMachine_createWithFolder(t *testing.T) { 328 var vm virtualMachine 329 var f folder 330 var locationOpt string 331 var folderLocationOpt string 332 var datastoreOpt string 333 334 folder := "tf_test_createWithFolder" 335 336 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 337 folderLocationOpt = fmt.Sprintf(" datacenter = \"%s\"\n", v) 338 locationOpt += folderLocationOpt 339 } 340 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 341 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 342 } 343 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 344 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 345 } 346 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 347 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 348 } 349 template := os.Getenv("VSPHERE_TEMPLATE") 350 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 351 352 resource.Test(t, resource.TestCase{ 353 PreCheck: func() { testAccPreCheck(t) }, 354 Providers: testAccProviders, 355 CheckDestroy: resource.ComposeTestCheckFunc( 356 testAccCheckVSphereVirtualMachineDestroy, 357 testAccCheckVSphereFolderDestroy, 358 ), 359 Steps: []resource.TestStep{ 360 resource.TestStep{ 361 Config: fmt.Sprintf( 362 testAccCheckVSphereVirtualMachineConfig_createWithFolder, 363 folder, 364 folderLocationOpt, 365 locationOpt, 366 label, 367 datastoreOpt, 368 template, 369 ), 370 Check: resource.ComposeTestCheckFunc( 371 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_folder", &vm), 372 testAccCheckVSphereFolderExists("vsphere_folder.with_folder", &f), 373 resource.TestCheckResourceAttr( 374 "vsphere_virtual_machine.with_folder", "name", "terraform-test-with-folder"), 375 // resource.TestCheckResourceAttr( 376 // "vsphere_virtual_machine.with_folder", "folder", folder), 377 resource.TestCheckResourceAttr( 378 "vsphere_virtual_machine.with_folder", "vcpu", "2"), 379 resource.TestCheckResourceAttr( 380 "vsphere_virtual_machine.with_folder", "memory", "4096"), 381 resource.TestCheckResourceAttr( 382 "vsphere_virtual_machine.with_folder", "disk.#", "1"), 383 resource.TestCheckResourceAttr( 384 "vsphere_virtual_machine.with_folder", "disk.0.template", template), 385 resource.TestCheckResourceAttr( 386 "vsphere_virtual_machine.with_folder", "network_interface.#", "1"), 387 resource.TestCheckResourceAttr( 388 "vsphere_virtual_machine.with_folder", "network_interface.0.label", label), 389 ), 390 }, 391 }, 392 }) 393 } 394 395 func TestAccVSphereVirtualMachine_createWithCdrom(t *testing.T) { 396 var vm virtualMachine 397 var locationOpt string 398 var datastoreOpt string 399 400 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 401 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 402 } 403 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 404 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 405 } 406 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 407 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 408 } 409 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 410 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 411 } 412 template := os.Getenv("VSPHERE_TEMPLATE") 413 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 414 cdromDatastore := os.Getenv("VSPHERE_CDROM_DATASTORE") 415 cdromPath := os.Getenv("VSPHERE_CDROM_PATH") 416 417 resource.Test(t, resource.TestCase{ 418 PreCheck: func() { testAccPreCheck(t) }, 419 Providers: testAccProviders, 420 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 421 Steps: []resource.TestStep{ 422 resource.TestStep{ 423 Config: fmt.Sprintf( 424 testAccCheckVsphereVirtualMachineConfig_cdrom, 425 locationOpt, 426 label, 427 datastoreOpt, 428 template, 429 cdromDatastore, 430 cdromPath, 431 ), 432 Check: resource.ComposeTestCheckFunc( 433 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_cdrom", &vm), 434 resource.TestCheckResourceAttr( 435 "vsphere_virtual_machine.with_cdrom", "name", "terraform-test-with-cdrom"), 436 resource.TestCheckResourceAttr( 437 "vsphere_virtual_machine.with_cdrom", "vcpu", "2"), 438 resource.TestCheckResourceAttr( 439 "vsphere_virtual_machine.with_cdrom", "memory", "4096"), 440 resource.TestCheckResourceAttr( 441 "vsphere_virtual_machine.with_cdrom", "disk.#", "1"), 442 resource.TestCheckResourceAttr( 443 "vsphere_virtual_machine.with_cdrom", "disk.0.template", template), 444 resource.TestCheckResourceAttr( 445 "vsphere_virtual_machine.with_cdrom", "cdrom.#", "1"), 446 resource.TestCheckResourceAttr( 447 "vsphere_virtual_machine.with_cdrom", "cdrom.0.datastore", cdromDatastore), 448 resource.TestCheckResourceAttr( 449 "vsphere_virtual_machine.with_cdrom", "cdrom.0.path", cdromPath), 450 resource.TestCheckResourceAttr( 451 "vsphere_virtual_machine.with_cdrom", "network_interface.#", "1"), 452 resource.TestCheckResourceAttr( 453 "vsphere_virtual_machine.with_cdrom", "network_interface.0.label", label), 454 ), 455 }, 456 }, 457 }) 458 } 459 460 func TestAccVSphereVirtualMachine_createWithExistingVmdk(t *testing.T) { 461 vmdk_path := os.Getenv("VSPHERE_VMDK_PATH") 462 label := os.Getenv("VSPHERE_NETWORK_LABEL") 463 464 var vm virtualMachine 465 var locationOpt string 466 var datastoreOpt string 467 468 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 469 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 470 } 471 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 472 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 473 } 474 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 475 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 476 } 477 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 478 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 479 } 480 481 resource.Test(t, resource.TestCase{ 482 PreCheck: func() { testAccPreCheck(t) }, 483 Providers: testAccProviders, 484 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 485 Steps: []resource.TestStep{ 486 resource.TestStep{ 487 Config: fmt.Sprintf( 488 testAccCheckVSphereVirtualMachineConfig_withExistingVmdk, 489 locationOpt, 490 label, 491 datastoreOpt, 492 vmdk_path, 493 ), 494 Check: resource.ComposeTestCheckFunc( 495 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_existing_vmdk", &vm), 496 resource.TestCheckResourceAttr( 497 "vsphere_virtual_machine.with_existing_vmdk", "name", "terraform-test-with-existing-vmdk"), 498 resource.TestCheckResourceAttr( 499 "vsphere_virtual_machine.with_existing_vmdk", "vcpu", "2"), 500 resource.TestCheckResourceAttr( 501 "vsphere_virtual_machine.with_existing_vmdk", "memory", "4096"), 502 resource.TestCheckResourceAttr( 503 "vsphere_virtual_machine.with_existing_vmdk", "disk.#", "1"), 504 resource.TestCheckResourceAttr( 505 "vsphere_virtual_machine.with_existing_vmdk", "disk.0.vmdk", vmdk_path), 506 resource.TestCheckResourceAttr( 507 "vsphere_virtual_machine.with_existing_vmdk", "disk.0.bootable", "true"), 508 resource.TestCheckResourceAttr( 509 "vsphere_virtual_machine.with_existing_vmdk", "network_interface.#", "1"), 510 resource.TestCheckResourceAttr( 511 "vsphere_virtual_machine.with_existing_vmdk", "network_interface.0.label", label), 512 ), 513 }, 514 }, 515 }) 516 } 517 518 func TestAccVSphereVirtualMachine_updateMemory(t *testing.T) { 519 var vm virtualMachine 520 var locationOpt string 521 var datastoreOpt string 522 523 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 524 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 525 } 526 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 527 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 528 } 529 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 530 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 531 } 532 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 533 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 534 } 535 template := os.Getenv("VSPHERE_TEMPLATE") 536 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 537 538 resource.Test(t, resource.TestCase{ 539 PreCheck: func() { testAccPreCheck(t) }, 540 Providers: testAccProviders, 541 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 542 Steps: []resource.TestStep{ 543 resource.TestStep{ 544 Config: fmt.Sprintf( 545 testAccCheckVSphereVirtualMachineConfig_updateMemoryInitial, 546 locationOpt, 547 label, 548 datastoreOpt, 549 template, 550 ), 551 Check: resource.ComposeTestCheckFunc( 552 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), 553 resource.TestCheckResourceAttr( 554 "vsphere_virtual_machine.bar", "name", "terraform-test"), 555 resource.TestCheckResourceAttr( 556 "vsphere_virtual_machine.bar", "vcpu", "2"), 557 resource.TestCheckResourceAttr( 558 "vsphere_virtual_machine.bar", "memory", "4096"), 559 resource.TestCheckResourceAttr( 560 "vsphere_virtual_machine.bar", "disk.#", "1"), 561 resource.TestCheckResourceAttr( 562 "vsphere_virtual_machine.bar", "disk.0.template", template), 563 resource.TestCheckResourceAttr( 564 "vsphere_virtual_machine.bar", "network_interface.#", "1"), 565 resource.TestCheckResourceAttr( 566 "vsphere_virtual_machine.bar", "network_interface.0.label", label), 567 ), 568 }, 569 resource.TestStep{ 570 Config: fmt.Sprintf( 571 testAccCheckVSphereVirtualMachineConfig_updateMemoryUpdate, 572 locationOpt, 573 label, 574 datastoreOpt, 575 template, 576 ), 577 Check: resource.ComposeTestCheckFunc( 578 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), 579 resource.TestCheckResourceAttr( 580 "vsphere_virtual_machine.bar", "name", "terraform-test"), 581 resource.TestCheckResourceAttr( 582 "vsphere_virtual_machine.bar", "vcpu", "2"), 583 resource.TestCheckResourceAttr( 584 "vsphere_virtual_machine.bar", "memory", "2048"), 585 resource.TestCheckResourceAttr( 586 "vsphere_virtual_machine.bar", "disk.#", "1"), 587 resource.TestCheckResourceAttr( 588 "vsphere_virtual_machine.bar", "disk.0.template", template), 589 resource.TestCheckResourceAttr( 590 "vsphere_virtual_machine.bar", "network_interface.#", "1"), 591 resource.TestCheckResourceAttr( 592 "vsphere_virtual_machine.bar", "network_interface.0.label", label), 593 ), 594 }, 595 }, 596 }) 597 } 598 599 func TestAccVSphereVirtualMachine_updateVcpu(t *testing.T) { 600 var vm virtualMachine 601 var locationOpt string 602 var datastoreOpt string 603 604 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 605 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 606 } 607 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 608 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 609 } 610 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 611 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 612 } 613 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 614 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 615 } 616 template := os.Getenv("VSPHERE_TEMPLATE") 617 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 618 619 resource.Test(t, resource.TestCase{ 620 PreCheck: func() { testAccPreCheck(t) }, 621 Providers: testAccProviders, 622 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 623 Steps: []resource.TestStep{ 624 resource.TestStep{ 625 Config: fmt.Sprintf( 626 testAccCheckVSphereVirtualMachineConfig_updateVcpuInitial, 627 locationOpt, 628 label, 629 datastoreOpt, 630 template, 631 ), 632 Check: resource.ComposeTestCheckFunc( 633 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), 634 resource.TestCheckResourceAttr( 635 "vsphere_virtual_machine.bar", "name", "terraform-test"), 636 resource.TestCheckResourceAttr( 637 "vsphere_virtual_machine.bar", "vcpu", "2"), 638 resource.TestCheckResourceAttr( 639 "vsphere_virtual_machine.bar", "memory", "4096"), 640 resource.TestCheckResourceAttr( 641 "vsphere_virtual_machine.bar", "disk.#", "1"), 642 resource.TestCheckResourceAttr( 643 "vsphere_virtual_machine.bar", "disk.0.template", template), 644 resource.TestCheckResourceAttr( 645 "vsphere_virtual_machine.bar", "network_interface.#", "1"), 646 resource.TestCheckResourceAttr( 647 "vsphere_virtual_machine.bar", "network_interface.0.label", label), 648 ), 649 }, 650 resource.TestStep{ 651 Config: fmt.Sprintf( 652 testAccCheckVSphereVirtualMachineConfig_updateVcpuUpdate, 653 locationOpt, 654 label, 655 datastoreOpt, 656 template, 657 ), 658 Check: resource.ComposeTestCheckFunc( 659 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), 660 resource.TestCheckResourceAttr( 661 "vsphere_virtual_machine.bar", "name", "terraform-test"), 662 resource.TestCheckResourceAttr( 663 "vsphere_virtual_machine.bar", "vcpu", "4"), 664 resource.TestCheckResourceAttr( 665 "vsphere_virtual_machine.bar", "memory", "4096"), 666 resource.TestCheckResourceAttr( 667 "vsphere_virtual_machine.bar", "disk.#", "1"), 668 resource.TestCheckResourceAttr( 669 "vsphere_virtual_machine.bar", "disk.0.template", template), 670 resource.TestCheckResourceAttr( 671 "vsphere_virtual_machine.bar", "network_interface.#", "1"), 672 resource.TestCheckResourceAttr( 673 "vsphere_virtual_machine.bar", "network_interface.0.label", label), 674 ), 675 }, 676 }, 677 }) 678 } 679 680 func TestAccVSphereVirtualMachine_ipv4Andipv6(t *testing.T) { 681 var vm virtualMachine 682 var locationOpt string 683 var datastoreOpt string 684 685 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 686 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 687 } 688 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 689 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 690 } 691 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 692 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 693 } 694 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 695 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 696 } 697 template := os.Getenv("VSPHERE_TEMPLATE") 698 label := os.Getenv("VSPHERE_NETWORK_LABEL") 699 ipv4Address := os.Getenv("VSPHERE_IPV4_ADDRESS") 700 ipv4Gateway := os.Getenv("VSPHERE_IPV4_GATEWAY") 701 ipv6Address := os.Getenv("VSPHERE_IPV6_ADDRESS") 702 ipv6Gateway := os.Getenv("VSPHERE_IPV6_GATEWAY") 703 704 resource.Test(t, resource.TestCase{ 705 PreCheck: func() { testAccPreCheck(t) }, 706 Providers: testAccProviders, 707 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 708 Steps: []resource.TestStep{ 709 resource.TestStep{ 710 Config: fmt.Sprintf( 711 testAccCheckVSphereVirtualMachineConfig_ipv4Andipv6, 712 locationOpt, 713 label, 714 ipv4Address, 715 ipv4Gateway, 716 ipv6Address, 717 ipv6Gateway, 718 datastoreOpt, 719 template, 720 ), 721 Check: resource.ComposeTestCheckFunc( 722 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.ipv4ipv6", &vm), 723 resource.TestCheckResourceAttr( 724 "vsphere_virtual_machine.ipv4ipv6", "name", "terraform-test-ipv4-ipv6"), 725 resource.TestCheckResourceAttr( 726 "vsphere_virtual_machine.ipv4ipv6", "vcpu", "2"), 727 resource.TestCheckResourceAttr( 728 "vsphere_virtual_machine.ipv4ipv6", "memory", "4096"), 729 resource.TestCheckResourceAttr( 730 "vsphere_virtual_machine.ipv4ipv6", "disk.#", "2"), 731 resource.TestCheckResourceAttr( 732 "vsphere_virtual_machine.ipv4ipv6", "disk.0.template", template), 733 resource.TestCheckResourceAttr( 734 "vsphere_virtual_machine.ipv4ipv6", "network_interface.#", "1"), 735 resource.TestCheckResourceAttr( 736 "vsphere_virtual_machine.ipv4ipv6", "network_interface.0.label", label), 737 resource.TestCheckResourceAttr( 738 "vsphere_virtual_machine.ipv4ipv6", "network_interface.0.ipv4_address", ipv4Address), 739 resource.TestCheckResourceAttr( 740 "vsphere_virtual_machine.ipv4ipv6", "network_interface.0.ipv4_gateway", ipv4Gateway), 741 resource.TestCheckResourceAttr( 742 "vsphere_virtual_machine.ipv4ipv6", "network_interface.0.ipv6_address", ipv6Address), 743 resource.TestCheckResourceAttr( 744 "vsphere_virtual_machine.ipv4ipv6", "network_interface.0.ipv6_gateway", ipv6Gateway), 745 ), 746 }, 747 }, 748 }) 749 } 750 751 func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { 752 client := testAccProvider.Meta().(*govmomi.Client) 753 finder := find.NewFinder(client.Client, true) 754 755 for _, rs := range s.RootModule().Resources { 756 if rs.Type != "vsphere_virtual_machine" { 757 continue 758 } 759 760 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 761 if err != nil { 762 return fmt.Errorf("error %s", err) 763 } 764 765 dcFolders, err := dc.Folders(context.TODO()) 766 if err != nil { 767 return fmt.Errorf("error %s", err) 768 } 769 770 folder := dcFolders.VmFolder 771 if len(rs.Primary.Attributes["folder"]) > 0 { 772 si := object.NewSearchIndex(client.Client) 773 folderRef, err := si.FindByInventoryPath( 774 context.TODO(), fmt.Sprintf("%v/vm/%v", rs.Primary.Attributes["datacenter"], rs.Primary.Attributes["folder"])) 775 if err != nil { 776 return err 777 } else if folderRef != nil { 778 folder = folderRef.(*object.Folder) 779 } 780 } 781 782 v, err := object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"]) 783 784 if v != nil { 785 return fmt.Errorf("Record still exists") 786 } 787 } 788 789 return nil 790 } 791 792 func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc { 793 return func(s *terraform.State) error { 794 795 rs, ok := s.RootModule().Resources[n] 796 if !ok { 797 return fmt.Errorf("Not found: %s", n) 798 } 799 800 if rs.Primary.ID == "" { 801 return fmt.Errorf("No ID is set") 802 } 803 804 client := testAccProvider.Meta().(*govmomi.Client) 805 finder := find.NewFinder(client.Client, true) 806 807 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 808 if err != nil { 809 return fmt.Errorf("error %s", err) 810 } 811 812 dcFolders, err := dc.Folders(context.TODO()) 813 if err != nil { 814 return fmt.Errorf("error %s", err) 815 } 816 817 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) 818 if err != nil { 819 return fmt.Errorf("error %s", err) 820 } 821 822 finder = finder.SetDatacenter(dc) 823 instance, err := finder.VirtualMachine(context.TODO(), rs.Primary.Attributes["name"]) 824 if err != nil { 825 return fmt.Errorf("error %s", err) 826 } 827 828 var mvm mo.VirtualMachine 829 830 collector := property.DefaultCollector(client.Client) 831 832 if err := collector.RetrieveOne(context.TODO(), instance.Reference(), []string{"config.extraConfig"}, &mvm); err != nil { 833 return fmt.Errorf("error %s", err) 834 } 835 836 var configMap = make(map[string]types.AnyType) 837 if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { 838 for _, v := range mvm.Config.ExtraConfig { 839 value := v.GetOptionValue() 840 configMap[value.Key] = value.Value 841 } 842 } else { 843 return fmt.Errorf("error no ExtraConfig") 844 } 845 846 if configMap["foo"] == nil { 847 return fmt.Errorf("error no ExtraConfig for 'foo'") 848 } 849 850 if configMap["foo"] != "bar" { 851 return fmt.Errorf("error ExtraConfig 'foo' != bar") 852 } 853 854 if configMap["car"] == nil { 855 return fmt.Errorf("error no ExtraConfig for 'car'") 856 } 857 858 if configMap["car"] != "ferrari" { 859 return fmt.Errorf("error ExtraConfig 'car' != ferrari") 860 } 861 862 if configMap["num"] == nil { 863 return fmt.Errorf("error no ExtraConfig for 'num'") 864 } 865 866 // todo this should be an int, getting back a string 867 if configMap["num"] != "42" { 868 return fmt.Errorf("error ExtraConfig 'num' != 42") 869 } 870 *vm = virtualMachine{ 871 name: rs.Primary.ID, 872 } 873 874 return nil 875 } 876 } 877 878 func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resource.TestCheckFunc { 879 return func(s *terraform.State) error { 880 rs, ok := s.RootModule().Resources[n] 881 if !ok { 882 return fmt.Errorf("Not found: %s", n) 883 } 884 885 if rs.Primary.ID == "" { 886 return fmt.Errorf("No ID is set") 887 } 888 889 client := testAccProvider.Meta().(*govmomi.Client) 890 finder := find.NewFinder(client.Client, true) 891 892 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 893 if err != nil { 894 return fmt.Errorf("error %s", err) 895 } 896 897 dcFolders, err := dc.Folders(context.TODO()) 898 if err != nil { 899 return fmt.Errorf("error %s", err) 900 } 901 902 folder := dcFolders.VmFolder 903 if len(rs.Primary.Attributes["folder"]) > 0 { 904 si := object.NewSearchIndex(client.Client) 905 folderRef, err := si.FindByInventoryPath( 906 context.TODO(), fmt.Sprintf("%v/vm/%v", rs.Primary.Attributes["datacenter"], rs.Primary.Attributes["folder"])) 907 if err != nil { 908 return err 909 } else if folderRef != nil { 910 folder = folderRef.(*object.Folder) 911 } 912 } 913 914 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"]) 915 916 *vm = virtualMachine{ 917 name: rs.Primary.ID, 918 } 919 920 return nil 921 } 922 } 923 924 const testAccCheckVSphereVirtualMachineConfig_basic = ` 925 resource "vsphere_virtual_machine" "foo" { 926 name = "terraform-test" 927 %s 928 vcpu = 2 929 memory = 4096 930 memory_reservation = 4096 931 gateway = "%s" 932 network_interface { 933 label = "%s" 934 ipv4_address = "%s" 935 ipv4_prefix_length = 24 936 ipv4_gateway = "%s" 937 } 938 disk { 939 %s 940 template = "%s" 941 iops = 500 942 } 943 disk { 944 size = 1 945 iops = 500 946 } 947 } 948 ` 949 const testAccCheckVSphereVirtualMachineConfig_initType = ` 950 resource "vsphere_virtual_machine" "thin" { 951 name = "terraform-test" 952 %s 953 vcpu = 2 954 memory = 4096 955 gateway = "%s" 956 network_interface { 957 label = "%s" 958 ipv4_address = "%s" 959 ipv4_prefix_length = 24 960 ipv4_gateway = "%s" 961 } 962 disk { 963 %s 964 template = "%s" 965 iops = 500 966 type = "thin" 967 } 968 disk { 969 size = 1 970 iops = 500 971 } 972 } 973 ` 974 const testAccCheckVSphereVirtualMachineConfig_dhcp = ` 975 resource "vsphere_virtual_machine" "bar" { 976 name = "terraform-test" 977 %s 978 vcpu = 2 979 memory = 4096 980 network_interface { 981 label = "%s" 982 } 983 disk { 984 %s 985 template = "%s" 986 } 987 } 988 ` 989 990 const testAccCheckVSphereVirtualMachineConfig_custom_configs = ` 991 resource "vsphere_virtual_machine" "car" { 992 name = "terraform-test-custom" 993 %s 994 vcpu = 2 995 memory = 4096 996 network_interface { 997 label = "%s" 998 } 999 custom_configuration_parameters { 1000 "foo" = "bar" 1001 "car" = "ferrari" 1002 "num" = 42 1003 } 1004 disk { 1005 %s 1006 template = "%s" 1007 } 1008 } 1009 ` 1010 1011 const testAccCheckVSphereVirtualMachineConfig_createInFolder = ` 1012 resource "vsphere_virtual_machine" "folder" { 1013 name = "terraform-test-folder" 1014 folder = "%s" 1015 %s 1016 vcpu = 2 1017 memory = 4096 1018 network_interface { 1019 label = "%s" 1020 } 1021 disk { 1022 %s 1023 template = "%s" 1024 } 1025 } 1026 ` 1027 1028 const testAccCheckVSphereVirtualMachineConfig_createWithFolder = ` 1029 resource "vsphere_folder" "with_folder" { 1030 path = "%s" 1031 %s 1032 } 1033 resource "vsphere_virtual_machine" "with_folder" { 1034 name = "terraform-test-with-folder" 1035 folder = "${vsphere_folder.with_folder.path}" 1036 %s 1037 vcpu = 2 1038 memory = 4096 1039 network_interface { 1040 label = "%s" 1041 } 1042 disk { 1043 %s 1044 template = "%s" 1045 } 1046 } 1047 ` 1048 1049 const testAccCheckVsphereVirtualMachineConfig_cdrom = ` 1050 resource "vsphere_virtual_machine" "with_cdrom" { 1051 name = "terraform-test-with-cdrom" 1052 %s 1053 vcpu = 2 1054 memory = 4096 1055 network_interface { 1056 label = "%s" 1057 } 1058 disk { 1059 %s 1060 template = "%s" 1061 } 1062 1063 cdrom { 1064 datastore = "%s" 1065 path = "%s" 1066 } 1067 } 1068 ` 1069 1070 const testAccCheckVSphereVirtualMachineConfig_withExistingVmdk = ` 1071 resource "vsphere_virtual_machine" "with_existing_vmdk" { 1072 name = "terraform-test-with-existing-vmdk" 1073 %s 1074 vcpu = 2 1075 memory = 4096 1076 network_interface { 1077 label = "%s" 1078 } 1079 disk { 1080 %s 1081 vmdk = "%s" 1082 bootable = true 1083 } 1084 } 1085 ` 1086 1087 const testAccCheckVSphereVirtualMachineConfig_updateMemoryInitial = ` 1088 resource "vsphere_virtual_machine" "bar" { 1089 name = "terraform-test" 1090 %s 1091 vcpu = 2 1092 memory = 4096 1093 network_interface { 1094 label = "%s" 1095 } 1096 disk { 1097 %s 1098 template = "%s" 1099 } 1100 } 1101 ` 1102 1103 const testAccCheckVSphereVirtualMachineConfig_updateMemoryUpdate = ` 1104 resource "vsphere_virtual_machine" "bar" { 1105 name = "terraform-test" 1106 %s 1107 vcpu = 2 1108 memory = 2048 1109 network_interface { 1110 label = "%s" 1111 } 1112 disk { 1113 %s 1114 template = "%s" 1115 } 1116 } 1117 ` 1118 1119 const testAccCheckVSphereVirtualMachineConfig_updateVcpuInitial = ` 1120 resource "vsphere_virtual_machine" "bar" { 1121 name = "terraform-test" 1122 %s 1123 vcpu = 2 1124 memory = 4096 1125 network_interface { 1126 label = "%s" 1127 } 1128 disk { 1129 %s 1130 template = "%s" 1131 } 1132 } 1133 ` 1134 1135 const testAccCheckVSphereVirtualMachineConfig_updateVcpuUpdate = ` 1136 resource "vsphere_virtual_machine" "bar" { 1137 name = "terraform-test" 1138 %s 1139 vcpu = 4 1140 memory = 4096 1141 network_interface { 1142 label = "%s" 1143 } 1144 disk { 1145 %s 1146 template = "%s" 1147 } 1148 } 1149 ` 1150 1151 const testAccCheckVSphereVirtualMachineConfig_ipv4Andipv6 = ` 1152 resource "vsphere_virtual_machine" "ipv4ipv6" { 1153 name = "terraform-test-ipv4-ipv6" 1154 %s 1155 vcpu = 2 1156 memory = 4096 1157 network_interface { 1158 label = "%s" 1159 ipv4_address = "%s" 1160 ipv4_prefix_length = 24 1161 ipv4_gateway = "%s" 1162 ipv6_address = "%s" 1163 ipv6_prefix_length = 64 1164 ipv6_gateway = "%s" 1165 } 1166 disk { 1167 %s 1168 template = "%s" 1169 iops = 500 1170 } 1171 disk { 1172 size = 1 1173 iops = 500 1174 } 1175 } 1176 `