package azurerm import ( "fmt" "net/http" "testing" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAzureRMVirtualMachine_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMVirtualMachineDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAzureRMVirtualMachine_basic, Check: resource.ComposeTestCheckFunc( testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), ), }, }, }) } func testCheckAzureRMVirtualMachineExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } vmName := rs.Primary.Attributes["name"] resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] if !hasResourceGroup { return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName) } conn := testAccProvider.Meta().(*ArmClient).vmClient resp, err := conn.Get(resourceGroup, vmName, "") if err != nil { return fmt.Errorf("Bad: Get on vmClient: %s", err) } if resp.StatusCode == http.StatusNotFound { return fmt.Errorf("Bad: VirtualMachine %q (resource group: %q) does not exist", vmName, resourceGroup) } return nil } } func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*ArmClient).vmClient for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_virtual_machine" { continue } name := rs.Primary.Attributes["name"] resourceGroup := rs.Primary.Attributes["resource_group_name"] resp, err := conn.Get(resourceGroup, name, "") if err != nil { return nil } if resp.StatusCode != http.StatusNotFound { return fmt.Errorf("Virtual Machine still exists:\n%#v", resp.Properties) } } return nil } var testAccAzureRMVirtualMachine_basic = ` resource "azurerm_resource_group" "test" { name = "acceptanceTestResourceGroup1" location = "West US" } resource "azurerm_virtual_network" "test" { name = "acceptanceTestVirtualNetwork1" address_space = ["10.0.0.0/16"] location = "West US" resource_group_name = "${azurerm_resource_group.test.name}" } resource "azurerm_subnet" "test" { name = "testsubnet" resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" address_prefix = "10.0.2.0/24" } resource "azurerm_network_interface" "test" { name = "acceptanceTestNetworkInterface1" location = "West US" resource_group_name = "${azurerm_resource_group.test.name}" ip_configuration { name = "testconfiguration1" subnet_id = "${azurerm_subnet.test.id}" private_ip_address_allocation = "dynamic" } } resource "azurerm_virtual_machine" "test" { name = "acceptanceTestVirtualMachine1" location = "West US" resource_group_name = "${azurerm_resource_group.test.name}" network_interface_ids = ["${azurerm_network_interface.test.id}"] vm_size = "Standard_A0" storage_os_disk { name = "myosdisk1" vhd_uri = "http://mystorage1.blob.core.windows.net/vhds/myosdisk1.vhd" caching = "ReadWrite" create_option = "FromImage" } os_profile { computer_name = "my_computer" admin_username = "testadmin" admin_password = "Password1234!" } } `