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