github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_virtual_machine_extension_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "regexp" 9 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAzureRMVirtualMachineExtension_basic(t *testing.T) { 16 ri := acctest.RandInt() 17 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachineExtension_basic, ri, ri, ri, ri, ri, ri, ri, ri) 18 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachineExtension_basicUpdate, ri, ri, ri, ri, ri, ri, ri, ri) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testCheckAzureRMVirtualMachineExtensionDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: preConfig, 27 Check: resource.ComposeTestCheckFunc( 28 testCheckAzureRMVirtualMachineExtensionExists("azurerm_virtual_machine_extension.test"), 29 resource.TestMatchResourceAttr("azurerm_virtual_machine_extension.test", "settings", regexp.MustCompile("hostname")), 30 ), 31 }, 32 resource.TestStep{ 33 Config: postConfig, 34 Check: resource.ComposeTestCheckFunc( 35 testCheckAzureRMVirtualMachineExtensionExists("azurerm_virtual_machine_extension.test"), 36 resource.TestMatchResourceAttr("azurerm_virtual_machine_extension.test", "settings", regexp.MustCompile("whoami")), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccAzureRMVirtualMachineExtension_concurrent(t *testing.T) { 44 ri := acctest.RandInt() 45 config := fmt.Sprintf(testAccAzureRMVirtualMachineExtension_concurrent, ri, ri, ri, ri, ri, ri, ri, ri, ri) 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testCheckAzureRMVirtualMachineExtensionDestroy, 51 Steps: []resource.TestStep{ 52 resource.TestStep{ 53 Config: config, 54 Check: resource.ComposeTestCheckFunc( 55 testCheckAzureRMVirtualMachineExtensionExists("azurerm_virtual_machine_extension.test"), 56 testCheckAzureRMVirtualMachineExtensionExists("azurerm_virtual_machine_extension.test2"), 57 resource.TestMatchResourceAttr("azurerm_virtual_machine_extension.test", "settings", regexp.MustCompile("hostname")), 58 resource.TestMatchResourceAttr("azurerm_virtual_machine_extension.test2", "settings", regexp.MustCompile("whoami")), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func TestAccAzureRMVirtualMachineExtension_linuxDiagnostics(t *testing.T) { 66 ri := acctest.RandInt() 67 config := fmt.Sprintf(testAccAzureRMVirtualMachineExtension_linuxDiagnostics, ri, ri, ri, ri, ri, ri, ri, ri) 68 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testCheckAzureRMVirtualMachineExtensionDestroy, 73 Steps: []resource.TestStep{ 74 resource.TestStep{ 75 Config: config, 76 Check: resource.ComposeTestCheckFunc( 77 testCheckAzureRMVirtualMachineExtensionExists("azurerm_virtual_machine_extension.test"), 78 ), 79 }, 80 }, 81 }) 82 } 83 84 func testCheckAzureRMVirtualMachineExtensionExists(name string) resource.TestCheckFunc { 85 return func(s *terraform.State) error { 86 // Ensure we have enough information in state to look up in API 87 rs, ok := s.RootModule().Resources[name] 88 if !ok { 89 return fmt.Errorf("Not found: %s", name) 90 } 91 92 name := rs.Primary.Attributes["name"] 93 vmName := rs.Primary.Attributes["virtual_machine_name"] 94 resourceGroup := rs.Primary.Attributes["resource_group_name"] 95 96 conn := testAccProvider.Meta().(*ArmClient).vmExtensionClient 97 98 resp, err := conn.Get(resourceGroup, vmName, name, "") 99 if err != nil { 100 return fmt.Errorf("Bad: Get on vmExtensionClient: %s", err) 101 } 102 103 if resp.StatusCode == http.StatusNotFound { 104 return fmt.Errorf("Bad: VirtualMachine Extension %q (resource group: %q) does not exist", name, resourceGroup) 105 } 106 107 return nil 108 } 109 } 110 111 func testCheckAzureRMVirtualMachineExtensionDestroy(s *terraform.State) error { 112 conn := testAccProvider.Meta().(*ArmClient).vmExtensionClient 113 114 for _, rs := range s.RootModule().Resources { 115 if rs.Type != "azurerm_virtual_machine_extension" { 116 continue 117 } 118 119 name := rs.Primary.Attributes["name"] 120 vmName := rs.Primary.Attributes["virtual_machine_name"] 121 resourceGroup := rs.Primary.Attributes["resource_group_name"] 122 123 resp, err := conn.Get(resourceGroup, vmName, name, "") 124 125 if err != nil { 126 return nil 127 } 128 129 if resp.StatusCode != http.StatusNotFound { 130 return fmt.Errorf("Virtual Machine Extension still exists:\n%#v", resp.VirtualMachineExtensionProperties) 131 } 132 } 133 134 return nil 135 } 136 137 var testAccAzureRMVirtualMachineExtension_basic = ` 138 resource "azurerm_resource_group" "test" { 139 name = "acctestrg-%d" 140 location = "West US" 141 } 142 143 resource "azurerm_virtual_network" "test" { 144 name = "acctvn-%d" 145 address_space = ["10.0.0.0/16"] 146 location = "West US" 147 resource_group_name = "${azurerm_resource_group.test.name}" 148 } 149 150 resource "azurerm_subnet" "test" { 151 name = "acctsub-%d" 152 resource_group_name = "${azurerm_resource_group.test.name}" 153 virtual_network_name = "${azurerm_virtual_network.test.name}" 154 address_prefix = "10.0.2.0/24" 155 } 156 157 resource "azurerm_network_interface" "test" { 158 name = "acctni-%d" 159 location = "West US" 160 resource_group_name = "${azurerm_resource_group.test.name}" 161 162 ip_configuration { 163 name = "testconfiguration1" 164 subnet_id = "${azurerm_subnet.test.id}" 165 private_ip_address_allocation = "dynamic" 166 } 167 } 168 169 resource "azurerm_storage_account" "test" { 170 name = "accsa%d" 171 resource_group_name = "${azurerm_resource_group.test.name}" 172 location = "westus" 173 account_type = "Standard_LRS" 174 175 tags { 176 environment = "staging" 177 } 178 } 179 180 resource "azurerm_storage_container" "test" { 181 name = "vhds" 182 resource_group_name = "${azurerm_resource_group.test.name}" 183 storage_account_name = "${azurerm_storage_account.test.name}" 184 container_access_type = "private" 185 } 186 187 resource "azurerm_virtual_machine" "test" { 188 name = "acctvm-%d" 189 location = "West US" 190 resource_group_name = "${azurerm_resource_group.test.name}" 191 network_interface_ids = ["${azurerm_network_interface.test.id}"] 192 vm_size = "Standard_A0" 193 194 storage_image_reference { 195 publisher = "Canonical" 196 offer = "UbuntuServer" 197 sku = "14.04.2-LTS" 198 version = "latest" 199 } 200 201 storage_os_disk { 202 name = "myosdisk1" 203 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 204 caching = "ReadWrite" 205 create_option = "FromImage" 206 } 207 208 os_profile { 209 computer_name = "hostname%d" 210 admin_username = "testadmin" 211 admin_password = "Password1234!" 212 } 213 214 os_profile_linux_config { 215 disable_password_authentication = false 216 } 217 } 218 219 resource "azurerm_virtual_machine_extension" "test" { 220 name = "acctvme-%d" 221 location = "West US" 222 resource_group_name = "${azurerm_resource_group.test.name}" 223 virtual_machine_name = "${azurerm_virtual_machine.test.name}" 224 publisher = "Microsoft.Azure.Extensions" 225 type = "CustomScript" 226 type_handler_version = "2.0" 227 228 settings = <<SETTINGS 229 { 230 "commandToExecute": "hostname" 231 } 232 SETTINGS 233 234 tags { 235 environment = "Production" 236 } 237 } 238 ` 239 240 var testAccAzureRMVirtualMachineExtension_basicUpdate = ` 241 resource "azurerm_resource_group" "test" { 242 name = "acctestrg-%d" 243 location = "West US" 244 } 245 246 resource "azurerm_virtual_network" "test" { 247 name = "acctvn-%d" 248 address_space = ["10.0.0.0/16"] 249 location = "West US" 250 resource_group_name = "${azurerm_resource_group.test.name}" 251 } 252 253 resource "azurerm_subnet" "test" { 254 name = "acctsub-%d" 255 resource_group_name = "${azurerm_resource_group.test.name}" 256 virtual_network_name = "${azurerm_virtual_network.test.name}" 257 address_prefix = "10.0.2.0/24" 258 } 259 260 resource "azurerm_network_interface" "test" { 261 name = "acctni-%d" 262 location = "West US" 263 resource_group_name = "${azurerm_resource_group.test.name}" 264 265 ip_configuration { 266 name = "testconfiguration1" 267 subnet_id = "${azurerm_subnet.test.id}" 268 private_ip_address_allocation = "dynamic" 269 } 270 } 271 272 resource "azurerm_storage_account" "test" { 273 name = "accsa%d" 274 resource_group_name = "${azurerm_resource_group.test.name}" 275 location = "westus" 276 account_type = "Standard_LRS" 277 278 tags { 279 environment = "staging" 280 } 281 } 282 283 resource "azurerm_storage_container" "test" { 284 name = "vhds" 285 resource_group_name = "${azurerm_resource_group.test.name}" 286 storage_account_name = "${azurerm_storage_account.test.name}" 287 container_access_type = "private" 288 } 289 290 resource "azurerm_virtual_machine" "test" { 291 name = "acctvm-%d" 292 location = "West US" 293 resource_group_name = "${azurerm_resource_group.test.name}" 294 network_interface_ids = ["${azurerm_network_interface.test.id}"] 295 vm_size = "Standard_A0" 296 297 storage_image_reference { 298 publisher = "Canonical" 299 offer = "UbuntuServer" 300 sku = "14.04.2-LTS" 301 version = "latest" 302 } 303 304 storage_os_disk { 305 name = "myosdisk1" 306 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 307 caching = "ReadWrite" 308 create_option = "FromImage" 309 } 310 311 os_profile { 312 computer_name = "hostname%d" 313 admin_username = "testadmin" 314 admin_password = "Password1234!" 315 } 316 317 os_profile_linux_config { 318 disable_password_authentication = false 319 } 320 } 321 322 resource "azurerm_virtual_machine_extension" "test" { 323 name = "acctvme-%d" 324 location = "West US" 325 resource_group_name = "${azurerm_resource_group.test.name}" 326 virtual_machine_name = "${azurerm_virtual_machine.test.name}" 327 publisher = "Microsoft.Azure.Extensions" 328 type = "CustomScript" 329 type_handler_version = "2.0" 330 331 settings = <<SETTINGS 332 { 333 "commandToExecute": "whoami" 334 } 335 SETTINGS 336 337 tags { 338 environment = "Production" 339 cost_center = "MSFT" 340 } 341 } 342 ` 343 344 var testAccAzureRMVirtualMachineExtension_concurrent = ` 345 resource "azurerm_resource_group" "test" { 346 name = "acctestrg-%d" 347 location = "West US" 348 } 349 350 resource "azurerm_virtual_network" "test" { 351 name = "acctvn-%d" 352 address_space = ["10.0.0.0/16"] 353 location = "West US" 354 resource_group_name = "${azurerm_resource_group.test.name}" 355 } 356 357 resource "azurerm_subnet" "test" { 358 name = "acctsub-%d" 359 resource_group_name = "${azurerm_resource_group.test.name}" 360 virtual_network_name = "${azurerm_virtual_network.test.name}" 361 address_prefix = "10.0.2.0/24" 362 } 363 364 resource "azurerm_network_interface" "test" { 365 name = "acctni-%d" 366 location = "West US" 367 resource_group_name = "${azurerm_resource_group.test.name}" 368 369 ip_configuration { 370 name = "testconfiguration1" 371 subnet_id = "${azurerm_subnet.test.id}" 372 private_ip_address_allocation = "dynamic" 373 } 374 } 375 376 resource "azurerm_storage_account" "test" { 377 name = "accsa%d" 378 resource_group_name = "${azurerm_resource_group.test.name}" 379 location = "westus" 380 account_type = "Standard_LRS" 381 382 tags { 383 environment = "staging" 384 } 385 } 386 387 resource "azurerm_storage_container" "test" { 388 name = "vhds" 389 resource_group_name = "${azurerm_resource_group.test.name}" 390 storage_account_name = "${azurerm_storage_account.test.name}" 391 container_access_type = "private" 392 } 393 394 resource "azurerm_virtual_machine" "test" { 395 name = "acctvm-%d" 396 location = "West US" 397 resource_group_name = "${azurerm_resource_group.test.name}" 398 network_interface_ids = ["${azurerm_network_interface.test.id}"] 399 vm_size = "Standard_A0" 400 401 storage_image_reference { 402 publisher = "Canonical" 403 offer = "UbuntuServer" 404 sku = "14.04.2-LTS" 405 version = "latest" 406 } 407 408 storage_os_disk { 409 name = "myosdisk1" 410 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 411 caching = "ReadWrite" 412 create_option = "FromImage" 413 } 414 415 os_profile { 416 computer_name = "hostname%d" 417 admin_username = "testadmin" 418 admin_password = "Password1234!" 419 } 420 421 os_profile_linux_config { 422 disable_password_authentication = false 423 } 424 } 425 426 resource "azurerm_virtual_machine_extension" "test" { 427 name = "acctvme-%d" 428 location = "West US" 429 resource_group_name = "${azurerm_resource_group.test.name}" 430 virtual_machine_name = "${azurerm_virtual_machine.test.name}" 431 publisher = "Microsoft.Azure.Extensions" 432 type = "CustomScript" 433 type_handler_version = "2.0" 434 435 settings = <<SETTINGS 436 { 437 "commandToExecute": "hostname" 438 } 439 SETTINGS 440 } 441 442 resource "azurerm_virtual_machine_extension" "test2" { 443 name = "acctvme-%d-2" 444 location = "West US" 445 resource_group_name = "${azurerm_resource_group.test.name}" 446 virtual_machine_name = "${azurerm_virtual_machine.test.name}" 447 publisher = "Microsoft.OSTCExtensions" 448 type = "CustomScriptForLinux" 449 type_handler_version = "1.5" 450 451 settings = <<SETTINGS 452 { 453 "commandToExecute": "whoami" 454 } 455 SETTINGS 456 } 457 ` 458 459 var testAccAzureRMVirtualMachineExtension_linuxDiagnostics = ` 460 resource "azurerm_resource_group" "test" { 461 name = "acctestrg-%d" 462 location = "West US" 463 } 464 465 resource "azurerm_virtual_network" "test" { 466 name = "acctvn-%d" 467 address_space = ["10.0.0.0/16"] 468 location = "West US" 469 resource_group_name = "${azurerm_resource_group.test.name}" 470 } 471 472 resource "azurerm_subnet" "test" { 473 name = "acctsub-%d" 474 resource_group_name = "${azurerm_resource_group.test.name}" 475 virtual_network_name = "${azurerm_virtual_network.test.name}" 476 address_prefix = "10.0.2.0/24" 477 } 478 479 resource "azurerm_network_interface" "test" { 480 name = "acctni-%d" 481 location = "West US" 482 resource_group_name = "${azurerm_resource_group.test.name}" 483 484 ip_configuration { 485 name = "testconfiguration1" 486 subnet_id = "${azurerm_subnet.test.id}" 487 private_ip_address_allocation = "dynamic" 488 } 489 } 490 491 resource "azurerm_storage_account" "test" { 492 name = "accsa%d" 493 resource_group_name = "${azurerm_resource_group.test.name}" 494 location = "westus" 495 account_type = "Standard_LRS" 496 497 tags { 498 environment = "staging" 499 } 500 } 501 502 resource "azurerm_storage_container" "test" { 503 name = "vhds" 504 resource_group_name = "${azurerm_resource_group.test.name}" 505 storage_account_name = "${azurerm_storage_account.test.name}" 506 container_access_type = "private" 507 } 508 509 resource "azurerm_virtual_machine" "test" { 510 name = "acctvm-%d" 511 location = "West US" 512 resource_group_name = "${azurerm_resource_group.test.name}" 513 network_interface_ids = ["${azurerm_network_interface.test.id}"] 514 vm_size = "Standard_A0" 515 516 storage_image_reference { 517 publisher = "Canonical" 518 offer = "UbuntuServer" 519 sku = "14.04.2-LTS" 520 version = "latest" 521 } 522 523 storage_os_disk { 524 name = "myosdisk1" 525 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 526 caching = "ReadWrite" 527 create_option = "FromImage" 528 } 529 530 os_profile { 531 computer_name = "hostname%d" 532 admin_username = "testadmin" 533 admin_password = "Password1234!" 534 } 535 536 os_profile_linux_config { 537 disable_password_authentication = false 538 } 539 } 540 541 resource "azurerm_virtual_machine_extension" "test" { 542 name = "acctvme-%d" 543 location = "West US" 544 resource_group_name = "${azurerm_resource_group.test.name}" 545 virtual_machine_name = "${azurerm_virtual_machine.test.name}" 546 publisher = "Microsoft.OSTCExtensions" 547 type = "LinuxDiagnostic" 548 type_handler_version = "2.3" 549 550 protected_settings = <<SETTINGS 551 { 552 "storageAccountName": "${azurerm_storage_account.test.name}", 553 "storageAccountKey": "${azurerm_storage_account.test.primary_access_key}" 554 } 555 SETTINGS 556 557 tags { 558 environment = "Production" 559 } 560 } 561 `