github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/azurerm/resource_arm_virtual_machine_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/Azure/azure-sdk-for-go/arm/compute" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAzureRMVirtualMachine_basicLinuxMachine(t *testing.T) { 15 var vm compute.VirtualMachine 16 ri := acctest.RandInt() 17 config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: config, 25 Check: resource.ComposeTestCheckFunc( 26 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccAzureRMVirtualMachine_withDataDisk(t *testing.T) { 34 var vm compute.VirtualMachine 35 36 ri := acctest.RandInt() 37 config := fmt.Sprintf(testAccAzureRMVirtualMachine_withDataDisk, ri, ri, ri, ri, ri, ri, ri) 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 42 Steps: []resource.TestStep{ 43 { 44 Config: config, 45 Check: resource.ComposeTestCheckFunc( 46 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func TestAccAzureRMVirtualMachine_tags(t *testing.T) { 54 var vm compute.VirtualMachine 55 56 ri := acctest.RandInt() 57 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 58 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineUpdated, ri, ri, ri, ri, ri, ri, ri) 59 resource.Test(t, resource.TestCase{ 60 PreCheck: func() { testAccPreCheck(t) }, 61 Providers: testAccProviders, 62 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 63 Steps: []resource.TestStep{ 64 { 65 Config: preConfig, 66 Check: resource.ComposeTestCheckFunc( 67 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 68 resource.TestCheckResourceAttr( 69 "azurerm_virtual_machine.test", "tags.%", "2"), 70 resource.TestCheckResourceAttr( 71 "azurerm_virtual_machine.test", "tags.environment", "Production"), 72 resource.TestCheckResourceAttr( 73 "azurerm_virtual_machine.test", "tags.cost-center", "Ops"), 74 ), 75 }, 76 77 { 78 Config: postConfig, 79 Check: resource.ComposeTestCheckFunc( 80 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 81 resource.TestCheckResourceAttr( 82 "azurerm_virtual_machine.test", "tags.%", "1"), 83 resource.TestCheckResourceAttr( 84 "azurerm_virtual_machine.test", "tags.environment", "Production"), 85 ), 86 }, 87 }, 88 }) 89 } 90 91 //This is a regression test around https://github.com/hashicorp/terraform/issues/6517 92 //Because we use CreateOrUpdate, we were sending an empty password on update requests 93 func TestAccAzureRMVirtualMachine_updateMachineSize(t *testing.T) { 94 var vm compute.VirtualMachine 95 96 ri := acctest.RandInt() 97 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 98 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_updatedLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 99 resource.Test(t, resource.TestCase{ 100 PreCheck: func() { testAccPreCheck(t) }, 101 Providers: testAccProviders, 102 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 103 Steps: []resource.TestStep{ 104 { 105 Config: preConfig, 106 Check: resource.ComposeTestCheckFunc( 107 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 108 resource.TestCheckResourceAttr( 109 "azurerm_virtual_machine.test", "vm_size", "Standard_A0"), 110 ), 111 }, 112 { 113 Config: postConfig, 114 Check: resource.ComposeTestCheckFunc( 115 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 116 resource.TestCheckResourceAttr( 117 "azurerm_virtual_machine.test", "vm_size", "Standard_A1"), 118 ), 119 }, 120 }, 121 }) 122 } 123 124 func TestAccAzureRMVirtualMachine_basicWindowsMachine(t *testing.T) { 125 var vm compute.VirtualMachine 126 ri := acctest.RandInt() 127 config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicWindowsMachine, ri, ri, ri, ri, ri, ri) 128 resource.Test(t, resource.TestCase{ 129 PreCheck: func() { testAccPreCheck(t) }, 130 Providers: testAccProviders, 131 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 132 Steps: []resource.TestStep{ 133 { 134 Config: config, 135 Check: resource.ComposeTestCheckFunc( 136 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func TestAccAzureRMVirtualMachine_windowsUnattendedConfig(t *testing.T) { 144 var vm compute.VirtualMachine 145 ri := acctest.RandInt() 146 config := fmt.Sprintf(testAccAzureRMVirtualMachine_windowsUnattendedConfig, ri, ri, ri, ri, ri, ri) 147 resource.Test(t, resource.TestCase{ 148 PreCheck: func() { testAccPreCheck(t) }, 149 Providers: testAccProviders, 150 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 151 Steps: []resource.TestStep{ 152 { 153 Config: config, 154 Check: resource.ComposeTestCheckFunc( 155 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 156 ), 157 }, 158 }, 159 }) 160 } 161 162 func TestAccAzureRMVirtualMachine_winRMConfig(t *testing.T) { 163 var vm compute.VirtualMachine 164 ri := acctest.RandInt() 165 config := fmt.Sprintf(testAccAzureRMVirtualMachine_winRMConfig, ri, ri, ri, ri, ri, ri) 166 resource.Test(t, resource.TestCase{ 167 PreCheck: func() { testAccPreCheck(t) }, 168 Providers: testAccProviders, 169 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 170 Steps: []resource.TestStep{ 171 { 172 Config: config, 173 Check: resource.ComposeTestCheckFunc( 174 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 175 ), 176 }, 177 }, 178 }) 179 } 180 181 func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) { 182 var vm compute.VirtualMachine 183 ri := acctest.RandInt() 184 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 185 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri) 186 resource.Test(t, resource.TestCase{ 187 PreCheck: func() { testAccPreCheck(t) }, 188 Providers: testAccProviders, 189 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 190 Steps: []resource.TestStep{ 191 { 192 Config: preConfig, 193 Check: resource.ComposeTestCheckFunc( 194 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 195 ), 196 }, 197 { 198 Config: postConfig, 199 Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(true), 200 }, 201 }, 202 }) 203 } 204 205 func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) { 206 var vm compute.VirtualMachine 207 ri := acctest.RandInt() 208 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDestroyOSDisk, ri, ri, ri, ri, ri, ri, ri) 209 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri) 210 resource.Test(t, resource.TestCase{ 211 PreCheck: func() { testAccPreCheck(t) }, 212 Providers: testAccProviders, 213 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 214 Steps: []resource.TestStep{ 215 { 216 Config: preConfig, 217 Check: resource.ComposeTestCheckFunc( 218 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), 219 ), 220 }, 221 { 222 Config: postConfig, 223 Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(false), 224 }, 225 }, 226 }) 227 } 228 229 func TestAccAzureRMVirtualMachine_ChangeComputerName(t *testing.T) { 230 var afterCreate, afterUpdate compute.VirtualMachine 231 232 ri := acctest.RandInt() 233 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_machineNameBeforeUpdate, ri, ri, ri, ri, ri, ri, ri) 234 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_updateMachineName, ri, ri, ri, ri, ri, ri, ri) 235 resource.Test(t, resource.TestCase{ 236 PreCheck: func() { testAccPreCheck(t) }, 237 Providers: testAccProviders, 238 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 239 Steps: []resource.TestStep{ 240 resource.TestStep{ 241 Config: preConfig, 242 Check: resource.ComposeTestCheckFunc( 243 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &afterCreate), 244 ), 245 }, 246 247 resource.TestStep{ 248 Config: postConfig, 249 Check: resource.ComposeTestCheckFunc( 250 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &afterUpdate), 251 testAccCheckVirtualMachineRecreated( 252 t, &afterCreate, &afterUpdate), 253 ), 254 }, 255 }, 256 }) 257 } 258 259 func testCheckAzureRMVirtualMachineExists(name string, vm *compute.VirtualMachine) resource.TestCheckFunc { 260 return func(s *terraform.State) error { 261 // Ensure we have enough information in state to look up in API 262 rs, ok := s.RootModule().Resources[name] 263 if !ok { 264 return fmt.Errorf("Not found: %s", name) 265 } 266 267 vmName := rs.Primary.Attributes["name"] 268 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 269 if !hasResourceGroup { 270 return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName) 271 } 272 273 conn := testAccProvider.Meta().(*ArmClient).vmClient 274 275 resp, err := conn.Get(resourceGroup, vmName, "") 276 if err != nil { 277 return fmt.Errorf("Bad: Get on vmClient: %s", err) 278 } 279 280 if resp.StatusCode == http.StatusNotFound { 281 return fmt.Errorf("Bad: VirtualMachine %q (resource group: %q) does not exist", vmName, resourceGroup) 282 } 283 284 *vm = resp 285 286 return nil 287 } 288 } 289 290 func testAccCheckVirtualMachineRecreated(t *testing.T, 291 before, after *compute.VirtualMachine) resource.TestCheckFunc { 292 return func(s *terraform.State) error { 293 if before.ID == after.ID { 294 t.Fatalf("Expected change of Virtual Machine IDs, but both were %v", before.ID) 295 } 296 return nil 297 } 298 } 299 300 func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error { 301 conn := testAccProvider.Meta().(*ArmClient).vmClient 302 303 for _, rs := range s.RootModule().Resources { 304 if rs.Type != "azurerm_virtual_machine" { 305 continue 306 } 307 308 name := rs.Primary.Attributes["name"] 309 resourceGroup := rs.Primary.Attributes["resource_group_name"] 310 311 resp, err := conn.Get(resourceGroup, name, "") 312 313 if err != nil { 314 return nil 315 } 316 317 if resp.StatusCode != http.StatusNotFound { 318 return fmt.Errorf("Virtual Machine still exists:\n%#v", resp.Properties) 319 } 320 } 321 322 return nil 323 } 324 325 func testCheckAzureRMVirtualMachineOSDiskVHDExistance(shouldExist bool) resource.TestCheckFunc { 326 return func(s *terraform.State) error { 327 for _, rs := range s.RootModule().Resources { 328 if rs.Type != "azurerm_storage_container" { 329 continue 330 } 331 332 // fetch storage account and container name 333 resourceGroup := rs.Primary.Attributes["resource_group_name"] 334 storageAccountName := rs.Primary.Attributes["storage_account_name"] 335 containerName := rs.Primary.Attributes["name"] 336 storageClient, _, err := testAccProvider.Meta().(*ArmClient).getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 337 if err != nil { 338 return fmt.Errorf("Error creating Blob storage client: %s", err) 339 } 340 341 exists, err := storageClient.BlobExists(containerName, "myosdisk1.vhd") 342 if err != nil { 343 return fmt.Errorf("Error checking if OS Disk VHD Blob exists: %s", err) 344 } 345 346 if exists && !shouldExist { 347 return fmt.Errorf("OS Disk VHD Blob still exists") 348 } 349 } 350 351 return nil 352 } 353 } 354 355 var testAccAzureRMVirtualMachine_basicLinuxMachine = ` 356 resource "azurerm_resource_group" "test" { 357 name = "acctestrg-%d" 358 location = "West US" 359 } 360 361 resource "azurerm_virtual_network" "test" { 362 name = "acctvn-%d" 363 address_space = ["10.0.0.0/16"] 364 location = "West US" 365 resource_group_name = "${azurerm_resource_group.test.name}" 366 } 367 368 resource "azurerm_subnet" "test" { 369 name = "acctsub-%d" 370 resource_group_name = "${azurerm_resource_group.test.name}" 371 virtual_network_name = "${azurerm_virtual_network.test.name}" 372 address_prefix = "10.0.2.0/24" 373 } 374 375 resource "azurerm_network_interface" "test" { 376 name = "acctni-%d" 377 location = "West US" 378 resource_group_name = "${azurerm_resource_group.test.name}" 379 380 ip_configuration { 381 name = "testconfiguration1" 382 subnet_id = "${azurerm_subnet.test.id}" 383 private_ip_address_allocation = "dynamic" 384 } 385 } 386 387 resource "azurerm_storage_account" "test" { 388 name = "accsa%d" 389 resource_group_name = "${azurerm_resource_group.test.name}" 390 location = "westus" 391 account_type = "Standard_LRS" 392 393 tags { 394 environment = "staging" 395 } 396 } 397 398 resource "azurerm_storage_container" "test" { 399 name = "vhds" 400 resource_group_name = "${azurerm_resource_group.test.name}" 401 storage_account_name = "${azurerm_storage_account.test.name}" 402 container_access_type = "private" 403 } 404 405 resource "azurerm_virtual_machine" "test" { 406 name = "acctvm-%d" 407 location = "West US" 408 resource_group_name = "${azurerm_resource_group.test.name}" 409 network_interface_ids = ["${azurerm_network_interface.test.id}"] 410 vm_size = "Standard_A0" 411 412 storage_image_reference { 413 publisher = "Canonical" 414 offer = "UbuntuServer" 415 sku = "14.04.2-LTS" 416 version = "latest" 417 } 418 419 storage_os_disk { 420 name = "myosdisk1" 421 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 422 caching = "ReadWrite" 423 create_option = "FromImage" 424 } 425 426 os_profile { 427 computer_name = "hostname%d" 428 admin_username = "testadmin" 429 admin_password = "Password1234!" 430 } 431 432 os_profile_linux_config { 433 disable_password_authentication = false 434 } 435 436 tags { 437 environment = "Production" 438 cost-center = "Ops" 439 } 440 } 441 ` 442 443 var testAccAzureRMVirtualMachine_machineNameBeforeUpdate = ` 444 resource "azurerm_resource_group" "test" { 445 name = "acctestrg-%d" 446 location = "West US" 447 } 448 449 resource "azurerm_virtual_network" "test" { 450 name = "acctvn-%d" 451 address_space = ["10.0.0.0/16"] 452 location = "West US" 453 resource_group_name = "${azurerm_resource_group.test.name}" 454 } 455 456 resource "azurerm_subnet" "test" { 457 name = "acctsub-%d" 458 resource_group_name = "${azurerm_resource_group.test.name}" 459 virtual_network_name = "${azurerm_virtual_network.test.name}" 460 address_prefix = "10.0.2.0/24" 461 } 462 463 resource "azurerm_network_interface" "test" { 464 name = "acctni-%d" 465 location = "West US" 466 resource_group_name = "${azurerm_resource_group.test.name}" 467 468 ip_configuration { 469 name = "testconfiguration1" 470 subnet_id = "${azurerm_subnet.test.id}" 471 private_ip_address_allocation = "dynamic" 472 } 473 } 474 475 resource "azurerm_storage_account" "test" { 476 name = "accsa%d" 477 resource_group_name = "${azurerm_resource_group.test.name}" 478 location = "westus" 479 account_type = "Standard_LRS" 480 481 tags { 482 environment = "staging" 483 } 484 } 485 486 resource "azurerm_storage_container" "test" { 487 name = "vhds" 488 resource_group_name = "${azurerm_resource_group.test.name}" 489 storage_account_name = "${azurerm_storage_account.test.name}" 490 container_access_type = "private" 491 } 492 493 resource "azurerm_virtual_machine" "test" { 494 name = "acctvm-%d" 495 location = "West US" 496 resource_group_name = "${azurerm_resource_group.test.name}" 497 network_interface_ids = ["${azurerm_network_interface.test.id}"] 498 vm_size = "Standard_A0" 499 delete_os_disk_on_termination = true 500 501 storage_image_reference { 502 publisher = "Canonical" 503 offer = "UbuntuServer" 504 sku = "14.04.2-LTS" 505 version = "latest" 506 } 507 508 storage_os_disk { 509 name = "myosdisk1" 510 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 511 caching = "ReadWrite" 512 create_option = "FromImage" 513 } 514 515 os_profile { 516 computer_name = "hostname%d" 517 admin_username = "testadmin" 518 admin_password = "Password1234!" 519 } 520 521 os_profile_linux_config { 522 disable_password_authentication = false 523 } 524 525 tags { 526 environment = "Production" 527 cost-center = "Ops" 528 } 529 } 530 ` 531 532 var testAccAzureRMVirtualMachine_basicLinuxMachineDestroyOSDisk = ` 533 resource "azurerm_resource_group" "test" { 534 name = "acctestrg-%d" 535 location = "West US" 536 } 537 538 resource "azurerm_virtual_network" "test" { 539 name = "acctvn-%d" 540 address_space = ["10.0.0.0/16"] 541 location = "West US" 542 resource_group_name = "${azurerm_resource_group.test.name}" 543 } 544 545 resource "azurerm_subnet" "test" { 546 name = "acctsub-%d" 547 resource_group_name = "${azurerm_resource_group.test.name}" 548 virtual_network_name = "${azurerm_virtual_network.test.name}" 549 address_prefix = "10.0.2.0/24" 550 } 551 552 resource "azurerm_network_interface" "test" { 553 name = "acctni-%d" 554 location = "West US" 555 resource_group_name = "${azurerm_resource_group.test.name}" 556 557 ip_configuration { 558 name = "testconfiguration1" 559 subnet_id = "${azurerm_subnet.test.id}" 560 private_ip_address_allocation = "dynamic" 561 } 562 } 563 564 resource "azurerm_storage_account" "test" { 565 name = "accsa%d" 566 resource_group_name = "${azurerm_resource_group.test.name}" 567 location = "westus" 568 account_type = "Standard_LRS" 569 570 tags { 571 environment = "staging" 572 } 573 } 574 575 resource "azurerm_storage_container" "test" { 576 name = "vhds" 577 resource_group_name = "${azurerm_resource_group.test.name}" 578 storage_account_name = "${azurerm_storage_account.test.name}" 579 container_access_type = "private" 580 } 581 582 resource "azurerm_virtual_machine" "test" { 583 name = "acctvm-%d" 584 location = "West US" 585 resource_group_name = "${azurerm_resource_group.test.name}" 586 network_interface_ids = ["${azurerm_network_interface.test.id}"] 587 vm_size = "Standard_A0" 588 589 storage_image_reference { 590 publisher = "Canonical" 591 offer = "UbuntuServer" 592 sku = "14.04.2-LTS" 593 version = "latest" 594 } 595 596 storage_os_disk { 597 name = "myosdisk1" 598 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 599 caching = "ReadWrite" 600 create_option = "FromImage" 601 } 602 603 delete_os_disk_on_termination = true 604 605 os_profile { 606 computer_name = "hostname%d" 607 admin_username = "testadmin" 608 admin_password = "Password1234!" 609 } 610 611 os_profile_linux_config { 612 disable_password_authentication = false 613 } 614 615 tags { 616 environment = "Production" 617 cost-center = "Ops" 618 } 619 } 620 ` 621 622 var testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM = ` 623 resource "azurerm_resource_group" "test" { 624 name = "acctestrg-%d" 625 location = "West US" 626 } 627 628 resource "azurerm_virtual_network" "test" { 629 name = "acctvn-%d" 630 address_space = ["10.0.0.0/16"] 631 location = "West US" 632 resource_group_name = "${azurerm_resource_group.test.name}" 633 } 634 635 resource "azurerm_subnet" "test" { 636 name = "acctsub-%d" 637 resource_group_name = "${azurerm_resource_group.test.name}" 638 virtual_network_name = "${azurerm_virtual_network.test.name}" 639 address_prefix = "10.0.2.0/24" 640 } 641 642 resource "azurerm_network_interface" "test" { 643 name = "acctni-%d" 644 location = "West US" 645 resource_group_name = "${azurerm_resource_group.test.name}" 646 647 ip_configuration { 648 name = "testconfiguration1" 649 subnet_id = "${azurerm_subnet.test.id}" 650 private_ip_address_allocation = "dynamic" 651 } 652 } 653 654 resource "azurerm_storage_account" "test" { 655 name = "accsa%d" 656 resource_group_name = "${azurerm_resource_group.test.name}" 657 location = "westus" 658 account_type = "Standard_LRS" 659 660 tags { 661 environment = "staging" 662 } 663 } 664 665 resource "azurerm_storage_container" "test" { 666 name = "vhds" 667 resource_group_name = "${azurerm_resource_group.test.name}" 668 storage_account_name = "${azurerm_storage_account.test.name}" 669 container_access_type = "private" 670 } 671 ` 672 673 var testAccAzureRMVirtualMachine_withDataDisk = ` 674 resource "azurerm_resource_group" "test" { 675 name = "acctestrg-%d" 676 location = "West US" 677 } 678 679 resource "azurerm_virtual_network" "test" { 680 name = "acctvn-%d" 681 address_space = ["10.0.0.0/16"] 682 location = "West US" 683 resource_group_name = "${azurerm_resource_group.test.name}" 684 } 685 686 resource "azurerm_subnet" "test" { 687 name = "acctsub-%d" 688 resource_group_name = "${azurerm_resource_group.test.name}" 689 virtual_network_name = "${azurerm_virtual_network.test.name}" 690 address_prefix = "10.0.2.0/24" 691 } 692 693 resource "azurerm_network_interface" "test" { 694 name = "acctni-%d" 695 location = "West US" 696 resource_group_name = "${azurerm_resource_group.test.name}" 697 698 ip_configuration { 699 name = "testconfiguration1" 700 subnet_id = "${azurerm_subnet.test.id}" 701 private_ip_address_allocation = "dynamic" 702 } 703 } 704 705 resource "azurerm_storage_account" "test" { 706 name = "accsa%d" 707 resource_group_name = "${azurerm_resource_group.test.name}" 708 location = "westus" 709 account_type = "Standard_LRS" 710 711 tags { 712 environment = "staging" 713 } 714 } 715 716 resource "azurerm_storage_container" "test" { 717 name = "vhds" 718 resource_group_name = "${azurerm_resource_group.test.name}" 719 storage_account_name = "${azurerm_storage_account.test.name}" 720 container_access_type = "private" 721 } 722 723 resource "azurerm_virtual_machine" "test" { 724 name = "acctvm-%d" 725 location = "West US" 726 resource_group_name = "${azurerm_resource_group.test.name}" 727 network_interface_ids = ["${azurerm_network_interface.test.id}"] 728 vm_size = "Standard_A0" 729 730 storage_image_reference { 731 publisher = "Canonical" 732 offer = "UbuntuServer" 733 sku = "14.04.2-LTS" 734 version = "latest" 735 } 736 737 storage_os_disk { 738 name = "myosdisk1" 739 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 740 caching = "ReadWrite" 741 create_option = "FromImage" 742 } 743 744 storage_data_disk { 745 name = "mydatadisk1" 746 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/mydatadisk1.vhd" 747 disk_size_gb = "1023" 748 create_option = "Empty" 749 lun = 0 750 } 751 752 os_profile { 753 computer_name = "hostname%d" 754 admin_username = "testadmin" 755 admin_password = "Password1234!" 756 } 757 758 os_profile_linux_config { 759 disable_password_authentication = false 760 } 761 762 tags { 763 environment = "Production" 764 cost-center = "Ops" 765 } 766 } 767 ` 768 769 var testAccAzureRMVirtualMachine_basicLinuxMachineUpdated = ` 770 resource "azurerm_resource_group" "test" { 771 name = "acctestrg-%d" 772 location = "West US" 773 } 774 775 resource "azurerm_virtual_network" "test" { 776 name = "acctvn-%d" 777 address_space = ["10.0.0.0/16"] 778 location = "West US" 779 resource_group_name = "${azurerm_resource_group.test.name}" 780 } 781 782 resource "azurerm_subnet" "test" { 783 name = "acctsub-%d" 784 resource_group_name = "${azurerm_resource_group.test.name}" 785 virtual_network_name = "${azurerm_virtual_network.test.name}" 786 address_prefix = "10.0.2.0/24" 787 } 788 789 resource "azurerm_network_interface" "test" { 790 name = "acctni-%d" 791 location = "West US" 792 resource_group_name = "${azurerm_resource_group.test.name}" 793 794 ip_configuration { 795 name = "testconfiguration1" 796 subnet_id = "${azurerm_subnet.test.id}" 797 private_ip_address_allocation = "dynamic" 798 } 799 } 800 801 resource "azurerm_storage_account" "test" { 802 name = "accsa%d" 803 resource_group_name = "${azurerm_resource_group.test.name}" 804 location = "westus" 805 account_type = "Standard_LRS" 806 807 tags { 808 environment = "staging" 809 } 810 } 811 812 resource "azurerm_storage_container" "test" { 813 name = "vhds" 814 resource_group_name = "${azurerm_resource_group.test.name}" 815 storage_account_name = "${azurerm_storage_account.test.name}" 816 container_access_type = "private" 817 } 818 819 resource "azurerm_virtual_machine" "test" { 820 name = "acctvm-%d" 821 location = "West US" 822 resource_group_name = "${azurerm_resource_group.test.name}" 823 network_interface_ids = ["${azurerm_network_interface.test.id}"] 824 vm_size = "Standard_A0" 825 826 storage_image_reference { 827 publisher = "Canonical" 828 offer = "UbuntuServer" 829 sku = "14.04.2-LTS" 830 version = "latest" 831 } 832 833 storage_os_disk { 834 name = "myosdisk1" 835 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 836 caching = "ReadWrite" 837 create_option = "FromImage" 838 } 839 840 os_profile { 841 computer_name = "hostname%d" 842 admin_username = "testadmin" 843 admin_password = "Password1234!" 844 } 845 846 os_profile_linux_config { 847 disable_password_authentication = false 848 } 849 850 tags { 851 environment = "Production" 852 } 853 } 854 ` 855 856 var testAccAzureRMVirtualMachine_updatedLinuxMachine = ` 857 resource "azurerm_resource_group" "test" { 858 name = "acctestrg-%d" 859 location = "West US" 860 } 861 862 resource "azurerm_virtual_network" "test" { 863 name = "acctvn-%d" 864 address_space = ["10.0.0.0/16"] 865 location = "West US" 866 resource_group_name = "${azurerm_resource_group.test.name}" 867 } 868 869 resource "azurerm_subnet" "test" { 870 name = "acctsub-%d" 871 resource_group_name = "${azurerm_resource_group.test.name}" 872 virtual_network_name = "${azurerm_virtual_network.test.name}" 873 address_prefix = "10.0.2.0/24" 874 } 875 876 resource "azurerm_network_interface" "test" { 877 name = "acctni-%d" 878 location = "West US" 879 resource_group_name = "${azurerm_resource_group.test.name}" 880 881 ip_configuration { 882 name = "testconfiguration1" 883 subnet_id = "${azurerm_subnet.test.id}" 884 private_ip_address_allocation = "dynamic" 885 } 886 } 887 888 resource "azurerm_storage_account" "test" { 889 name = "accsa%d" 890 resource_group_name = "${azurerm_resource_group.test.name}" 891 location = "westus" 892 account_type = "Standard_LRS" 893 894 tags { 895 environment = "staging" 896 } 897 } 898 899 resource "azurerm_storage_container" "test" { 900 name = "vhds" 901 resource_group_name = "${azurerm_resource_group.test.name}" 902 storage_account_name = "${azurerm_storage_account.test.name}" 903 container_access_type = "private" 904 } 905 906 resource "azurerm_virtual_machine" "test" { 907 name = "acctvm-%d" 908 location = "West US" 909 resource_group_name = "${azurerm_resource_group.test.name}" 910 network_interface_ids = ["${azurerm_network_interface.test.id}"] 911 vm_size = "Standard_A1" 912 913 storage_image_reference { 914 publisher = "Canonical" 915 offer = "UbuntuServer" 916 sku = "14.04.2-LTS" 917 version = "latest" 918 } 919 920 storage_os_disk { 921 name = "myosdisk1" 922 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 923 caching = "ReadWrite" 924 create_option = "FromImage" 925 } 926 927 os_profile { 928 computer_name = "hostname%d" 929 admin_username = "testadmin" 930 admin_password = "Password1234!" 931 } 932 933 os_profile_linux_config { 934 disable_password_authentication = false 935 } 936 } 937 ` 938 939 var testAccAzureRMVirtualMachine_basicWindowsMachine = ` 940 resource "azurerm_resource_group" "test" { 941 name = "acctestrg-%d" 942 location = "West US" 943 } 944 945 resource "azurerm_virtual_network" "test" { 946 name = "acctvn-%d" 947 address_space = ["10.0.0.0/16"] 948 location = "West US" 949 resource_group_name = "${azurerm_resource_group.test.name}" 950 } 951 952 resource "azurerm_subnet" "test" { 953 name = "acctsub-%d" 954 resource_group_name = "${azurerm_resource_group.test.name}" 955 virtual_network_name = "${azurerm_virtual_network.test.name}" 956 address_prefix = "10.0.2.0/24" 957 } 958 959 resource "azurerm_network_interface" "test" { 960 name = "acctni-%d" 961 location = "West US" 962 resource_group_name = "${azurerm_resource_group.test.name}" 963 964 ip_configuration { 965 name = "testconfiguration1" 966 subnet_id = "${azurerm_subnet.test.id}" 967 private_ip_address_allocation = "dynamic" 968 } 969 } 970 971 resource "azurerm_storage_account" "test" { 972 name = "accsa%d" 973 resource_group_name = "${azurerm_resource_group.test.name}" 974 location = "westus" 975 account_type = "Standard_LRS" 976 977 tags { 978 environment = "staging" 979 } 980 } 981 982 resource "azurerm_storage_container" "test" { 983 name = "vhds" 984 resource_group_name = "${azurerm_resource_group.test.name}" 985 storage_account_name = "${azurerm_storage_account.test.name}" 986 container_access_type = "private" 987 } 988 989 resource "azurerm_virtual_machine" "test" { 990 name = "acctvm-%d" 991 location = "West US" 992 resource_group_name = "${azurerm_resource_group.test.name}" 993 network_interface_ids = ["${azurerm_network_interface.test.id}"] 994 vm_size = "Standard_A0" 995 996 storage_image_reference { 997 publisher = "MicrosoftWindowsServer" 998 offer = "WindowsServer" 999 sku = "2012-Datacenter" 1000 version = "latest" 1001 } 1002 1003 storage_os_disk { 1004 name = "myosdisk1" 1005 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 1006 caching = "ReadWrite" 1007 create_option = "FromImage" 1008 } 1009 1010 os_profile { 1011 computer_name = "winhost01" 1012 admin_username = "testadmin" 1013 admin_password = "Password1234!" 1014 } 1015 1016 os_profile_windows_config { 1017 enable_automatic_upgrades = false 1018 provision_vm_agent = true 1019 } 1020 } 1021 ` 1022 1023 var testAccAzureRMVirtualMachine_windowsUnattendedConfig = ` 1024 resource "azurerm_resource_group" "test" { 1025 name = "acctestrg-%d" 1026 location = "West US" 1027 } 1028 1029 resource "azurerm_virtual_network" "test" { 1030 name = "acctvn-%d" 1031 address_space = ["10.0.0.0/16"] 1032 location = "West US" 1033 resource_group_name = "${azurerm_resource_group.test.name}" 1034 } 1035 1036 resource "azurerm_subnet" "test" { 1037 name = "acctsub-%d" 1038 resource_group_name = "${azurerm_resource_group.test.name}" 1039 virtual_network_name = "${azurerm_virtual_network.test.name}" 1040 address_prefix = "10.0.2.0/24" 1041 } 1042 1043 resource "azurerm_network_interface" "test" { 1044 name = "acctni-%d" 1045 location = "West US" 1046 resource_group_name = "${azurerm_resource_group.test.name}" 1047 1048 ip_configuration { 1049 name = "testconfiguration1" 1050 subnet_id = "${azurerm_subnet.test.id}" 1051 private_ip_address_allocation = "dynamic" 1052 } 1053 } 1054 1055 resource "azurerm_storage_account" "test" { 1056 name = "accsa%d" 1057 resource_group_name = "${azurerm_resource_group.test.name}" 1058 location = "westus" 1059 account_type = "Standard_LRS" 1060 1061 tags { 1062 environment = "staging" 1063 } 1064 } 1065 1066 resource "azurerm_storage_container" "test" { 1067 name = "vhds" 1068 resource_group_name = "${azurerm_resource_group.test.name}" 1069 storage_account_name = "${azurerm_storage_account.test.name}" 1070 container_access_type = "private" 1071 } 1072 1073 resource "azurerm_virtual_machine" "test" { 1074 name = "acctvm-%d" 1075 location = "West US" 1076 resource_group_name = "${azurerm_resource_group.test.name}" 1077 network_interface_ids = ["${azurerm_network_interface.test.id}"] 1078 vm_size = "Standard_A0" 1079 1080 storage_image_reference { 1081 publisher = "MicrosoftWindowsServer" 1082 offer = "WindowsServer" 1083 sku = "2012-Datacenter" 1084 version = "latest" 1085 } 1086 1087 storage_os_disk { 1088 name = "myosdisk1" 1089 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 1090 caching = "ReadWrite" 1091 create_option = "FromImage" 1092 } 1093 1094 os_profile { 1095 computer_name = "winhost01" 1096 admin_username = "testadmin" 1097 admin_password = "Password1234!" 1098 } 1099 1100 os_profile_windows_config { 1101 provision_vm_agent = true 1102 additional_unattend_config { 1103 pass = "oobeSystem" 1104 component = "Microsoft-Windows-Shell-Setup" 1105 setting_name = "FirstLogonCommands" 1106 content = "<FirstLogonCommands><SynchronousCommand><CommandLine>shutdown /r /t 0 /c \"initial reboot\"</CommandLine><Description>reboot</Description><Order>1</Order></SynchronousCommand></FirstLogonCommands>" 1107 } 1108 } 1109 } 1110 ` 1111 1112 var testAccAzureRMVirtualMachine_winRMConfig = ` 1113 resource "azurerm_resource_group" "test" { 1114 name = "acctestrg-%d" 1115 location = "West US" 1116 } 1117 1118 resource "azurerm_virtual_network" "test" { 1119 name = "acctvn-%d" 1120 address_space = ["10.0.0.0/16"] 1121 location = "West US" 1122 resource_group_name = "${azurerm_resource_group.test.name}" 1123 } 1124 1125 resource "azurerm_subnet" "test" { 1126 name = "acctsub-%d" 1127 resource_group_name = "${azurerm_resource_group.test.name}" 1128 virtual_network_name = "${azurerm_virtual_network.test.name}" 1129 address_prefix = "10.0.2.0/24" 1130 } 1131 1132 resource "azurerm_network_interface" "test" { 1133 name = "acctni-%d" 1134 location = "West US" 1135 resource_group_name = "${azurerm_resource_group.test.name}" 1136 1137 ip_configuration { 1138 name = "testconfiguration1" 1139 subnet_id = "${azurerm_subnet.test.id}" 1140 private_ip_address_allocation = "dynamic" 1141 } 1142 } 1143 1144 resource "azurerm_storage_account" "test" { 1145 name = "accsa%d" 1146 resource_group_name = "${azurerm_resource_group.test.name}" 1147 location = "westus" 1148 account_type = "Standard_LRS" 1149 1150 tags { 1151 environment = "staging" 1152 } 1153 } 1154 1155 resource "azurerm_storage_container" "test" { 1156 name = "vhds" 1157 resource_group_name = "${azurerm_resource_group.test.name}" 1158 storage_account_name = "${azurerm_storage_account.test.name}" 1159 container_access_type = "private" 1160 } 1161 1162 resource "azurerm_virtual_machine" "test" { 1163 name = "acctvm-%d" 1164 location = "West US" 1165 resource_group_name = "${azurerm_resource_group.test.name}" 1166 network_interface_ids = ["${azurerm_network_interface.test.id}"] 1167 vm_size = "Standard_A0" 1168 1169 storage_image_reference { 1170 publisher = "MicrosoftWindowsServer" 1171 offer = "WindowsServer" 1172 sku = "2012-Datacenter" 1173 version = "latest" 1174 } 1175 1176 storage_os_disk { 1177 name = "myosdisk1" 1178 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 1179 caching = "ReadWrite" 1180 create_option = "FromImage" 1181 } 1182 1183 os_profile { 1184 computer_name = "winhost01" 1185 admin_username = "testadmin" 1186 admin_password = "Password1234!" 1187 } 1188 1189 os_profile_windows_config { 1190 winrm { 1191 protocol = "http" 1192 } 1193 } 1194 } 1195 ` 1196 1197 var testAccAzureRMVirtualMachine_updateMachineName = ` 1198 resource "azurerm_resource_group" "test" { 1199 name = "acctestrg-%d" 1200 location = "West US" 1201 } 1202 1203 resource "azurerm_virtual_network" "test" { 1204 name = "acctvn-%d" 1205 address_space = ["10.0.0.0/16"] 1206 location = "West US" 1207 resource_group_name = "${azurerm_resource_group.test.name}" 1208 } 1209 1210 resource "azurerm_subnet" "test" { 1211 name = "acctsub-%d" 1212 resource_group_name = "${azurerm_resource_group.test.name}" 1213 virtual_network_name = "${azurerm_virtual_network.test.name}" 1214 address_prefix = "10.0.2.0/24" 1215 } 1216 1217 resource "azurerm_network_interface" "test" { 1218 name = "acctni-%d" 1219 location = "West US" 1220 resource_group_name = "${azurerm_resource_group.test.name}" 1221 1222 ip_configuration { 1223 name = "testconfiguration1" 1224 subnet_id = "${azurerm_subnet.test.id}" 1225 private_ip_address_allocation = "dynamic" 1226 } 1227 } 1228 1229 resource "azurerm_storage_account" "test" { 1230 name = "accsa%d" 1231 resource_group_name = "${azurerm_resource_group.test.name}" 1232 location = "westus" 1233 account_type = "Standard_LRS" 1234 1235 tags { 1236 environment = "staging" 1237 } 1238 } 1239 1240 resource "azurerm_storage_container" "test" { 1241 name = "vhds" 1242 resource_group_name = "${azurerm_resource_group.test.name}" 1243 storage_account_name = "${azurerm_storage_account.test.name}" 1244 container_access_type = "private" 1245 } 1246 1247 resource "azurerm_virtual_machine" "test" { 1248 name = "acctvm-%d" 1249 location = "West US" 1250 resource_group_name = "${azurerm_resource_group.test.name}" 1251 network_interface_ids = ["${azurerm_network_interface.test.id}"] 1252 vm_size = "Standard_A0" 1253 delete_os_disk_on_termination = true 1254 1255 storage_image_reference { 1256 publisher = "Canonical" 1257 offer = "UbuntuServer" 1258 sku = "14.04.2-LTS" 1259 version = "latest" 1260 } 1261 1262 storage_os_disk { 1263 name = "myosdisk1" 1264 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 1265 caching = "ReadWrite" 1266 create_option = "FromImage" 1267 } 1268 1269 os_profile { 1270 computer_name = "newhostname%d" 1271 admin_username = "testadmin" 1272 admin_password = "Password1234!" 1273 } 1274 1275 os_profile_linux_config { 1276 disable_password_authentication = false 1277 } 1278 } 1279 `