github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMVirtualMachine_basicLinuxMachine(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri)
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: config,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccAzureRMVirtualMachine_basicWindowsMachine(t *testing.T) {
    32  	ri := acctest.RandInt()
    33  	config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicWindowsMachine, ri, ri, ri, ri, ri, ri)
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: config,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testCheckAzureRMVirtualMachineExists(name string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		// Ensure we have enough information in state to look up in API
    52  		rs, ok := s.RootModule().Resources[name]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", name)
    55  		}
    56  
    57  		vmName := rs.Primary.Attributes["name"]
    58  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    59  		if !hasResourceGroup {
    60  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName)
    61  		}
    62  
    63  		conn := testAccProvider.Meta().(*ArmClient).vmClient
    64  
    65  		resp, err := conn.Get(resourceGroup, vmName, "")
    66  		if err != nil {
    67  			return fmt.Errorf("Bad: Get on vmClient: %s", err)
    68  		}
    69  
    70  		if resp.StatusCode == http.StatusNotFound {
    71  			return fmt.Errorf("Bad: VirtualMachine %q (resource group: %q) does not exist", vmName, resourceGroup)
    72  		}
    73  
    74  		return nil
    75  	}
    76  }
    77  
    78  func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error {
    79  	conn := testAccProvider.Meta().(*ArmClient).vmClient
    80  
    81  	for _, rs := range s.RootModule().Resources {
    82  		if rs.Type != "azurerm_virtual_machine" {
    83  			continue
    84  		}
    85  
    86  		name := rs.Primary.Attributes["name"]
    87  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    88  
    89  		resp, err := conn.Get(resourceGroup, name, "")
    90  
    91  		if err != nil {
    92  			return nil
    93  		}
    94  
    95  		if resp.StatusCode != http.StatusNotFound {
    96  			return fmt.Errorf("Virtual Machine still exists:\n%#v", resp.Properties)
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  var testAccAzureRMVirtualMachine_basicLinuxMachine = `
   104  resource "azurerm_resource_group" "test" {
   105      name = "acctestrg-%d"
   106      location = "West US"
   107  }
   108  
   109  resource "azurerm_virtual_network" "test" {
   110      name = "acctvn-%d"
   111      address_space = ["10.0.0.0/16"]
   112      location = "West US"
   113      resource_group_name = "${azurerm_resource_group.test.name}"
   114  }
   115  
   116  resource "azurerm_subnet" "test" {
   117      name = "acctsub-%d"
   118      resource_group_name = "${azurerm_resource_group.test.name}"
   119      virtual_network_name = "${azurerm_virtual_network.test.name}"
   120      address_prefix = "10.0.2.0/24"
   121  }
   122  
   123  resource "azurerm_network_interface" "test" {
   124      name = "acctni-%d"
   125      location = "West US"
   126      resource_group_name = "${azurerm_resource_group.test.name}"
   127  
   128      ip_configuration {
   129      	name = "testconfiguration1"
   130      	subnet_id = "${azurerm_subnet.test.id}"
   131      	private_ip_address_allocation = "dynamic"
   132      }
   133  }
   134  
   135  resource "azurerm_storage_account" "test" {
   136      name = "accsa%d"
   137      resource_group_name = "${azurerm_resource_group.test.name}"
   138      location = "westus"
   139      account_type = "Standard_LRS"
   140  
   141      tags {
   142          environment = "staging"
   143      }
   144  }
   145  
   146  resource "azurerm_storage_container" "test" {
   147      name = "vhds"
   148      resource_group_name = "${azurerm_resource_group.test.name}"
   149      storage_account_name = "${azurerm_storage_account.test.name}"
   150      container_access_type = "private"
   151  }
   152  
   153  resource "azurerm_virtual_machine" "test" {
   154      name = "acctvm-%d"
   155      location = "West US"
   156      resource_group_name = "${azurerm_resource_group.test.name}"
   157      network_interface_ids = ["${azurerm_network_interface.test.id}"]
   158      vm_size = "Standard_A0"
   159  
   160      storage_image_reference {
   161  	publisher = "Canonical"
   162  	offer = "UbuntuServer"
   163  	sku = "14.04.2-LTS"
   164  	version = "latest"
   165      }
   166  
   167      storage_os_disk {
   168          name = "myosdisk1"
   169          vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   170          caching = "ReadWrite"
   171          create_option = "FromImage"
   172      }
   173  
   174      os_profile {
   175  	computer_name = "hostname%d"
   176  	admin_username = "testadmin"
   177  	admin_password = "Password1234!"
   178      }
   179  
   180      os_profile_linux_config {
   181  	disable_password_authentication = false
   182      }
   183  }
   184  `
   185  
   186  var testAccAzureRMVirtualMachine_basicWindowsMachine = `
   187  resource "azurerm_resource_group" "test" {
   188      name = "acctestrg-%d"
   189      location = "West US"
   190  }
   191  
   192  resource "azurerm_virtual_network" "test" {
   193      name = "acctvn-%d"
   194      address_space = ["10.0.0.0/16"]
   195      location = "West US"
   196      resource_group_name = "${azurerm_resource_group.test.name}"
   197  }
   198  
   199  resource "azurerm_subnet" "test" {
   200      name = "acctsub-%d"
   201      resource_group_name = "${azurerm_resource_group.test.name}"
   202      virtual_network_name = "${azurerm_virtual_network.test.name}"
   203      address_prefix = "10.0.2.0/24"
   204  }
   205  
   206  resource "azurerm_network_interface" "test" {
   207      name = "acctni-%d"
   208      location = "West US"
   209      resource_group_name = "${azurerm_resource_group.test.name}"
   210  
   211      ip_configuration {
   212      	name = "testconfiguration1"
   213      	subnet_id = "${azurerm_subnet.test.id}"
   214      	private_ip_address_allocation = "dynamic"
   215      }
   216  }
   217  
   218  resource "azurerm_storage_account" "test" {
   219      name = "accsa%d"
   220      resource_group_name = "${azurerm_resource_group.test.name}"
   221      location = "westus"
   222      account_type = "Standard_LRS"
   223  
   224      tags {
   225          environment = "staging"
   226      }
   227  }
   228  
   229  resource "azurerm_storage_container" "test" {
   230      name = "vhds"
   231      resource_group_name = "${azurerm_resource_group.test.name}"
   232      storage_account_name = "${azurerm_storage_account.test.name}"
   233      container_access_type = "private"
   234  }
   235  
   236  resource "azurerm_virtual_machine" "test" {
   237      name = "acctvm-%d"
   238      location = "West US"
   239      resource_group_name = "${azurerm_resource_group.test.name}"
   240      network_interface_ids = ["${azurerm_network_interface.test.id}"]
   241      vm_size = "Standard_A0"
   242  
   243      storage_image_reference {
   244  	publisher = "MicrosoftWindowsServer"
   245  	offer = "WindowsServer"
   246  	sku = "2012-Datacenter"
   247  	version = "latest"
   248      }
   249  
   250      storage_os_disk {
   251          name = "myosdisk1"
   252          vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   253          caching = "ReadWrite"
   254          create_option = "FromImage"
   255      }
   256  
   257      os_profile {
   258  	computer_name = "winhost01"
   259  	admin_username = "testadmin"
   260  	admin_password = "Password1234!"
   261      }
   262  
   263      os_profile_windows_config {
   264  	enable_automatic_upgrades = false
   265  	provision_vm_agent = true
   266      }
   267  }
   268  `