github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_managed_disk_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/Azure/azure-sdk-for-go/arm/disk"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAzureRMManagedDisk_empty(t *testing.T) {
    16  	var d disk.Model
    17  	ri := acctest.RandInt()
    18  	config := fmt.Sprintf(testAccAzureRMManagedDisk_empty, ri, ri)
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testCheckAzureRMManagedDiskDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: config,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureRMManagedDisk_import(t *testing.T) {
    35  	var d disk.Model
    36  	var vm compute.VirtualMachine
    37  	ri := acctest.RandInt()
    38  	vmConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri)
    39  	config := fmt.Sprintf(testAccAzureRMManagedDisk_import, ri, ri, ri)
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testCheckAzureRMManagedDiskDestroy,
    44  		Steps: []resource.TestStep{
    45  			{
    46  				//need to create a vm and then delete it so we can use the vhd to test import
    47  				Config:             vmConfig,
    48  				Destroy:            false,
    49  				ExpectNonEmptyPlan: true,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
    52  					testDeleteAzureRMVirtualMachine("azurerm_virtual_machine.test"),
    53  				),
    54  			},
    55  			{
    56  				Config: config,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestAccAzureRMManagedDisk_copy(t *testing.T) {
    66  	var d disk.Model
    67  	ri := acctest.RandInt()
    68  	config := fmt.Sprintf(testAccAzureRMManagedDisk_copy, ri, ri, ri)
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testCheckAzureRMManagedDiskDestroy,
    73  		Steps: []resource.TestStep{
    74  			{
    75  				Config: config,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccAzureRMManagedDisk_update(t *testing.T) {
    85  	var d disk.Model
    86  
    87  	ri := acctest.RandInt()
    88  	preConfig := fmt.Sprintf(testAccAzureRMManagedDisk_empty, ri, ri)
    89  	postConfig := fmt.Sprintf(testAccAzureRMManagedDisk_empty_updated, ri, ri)
    90  	resource.Test(t, resource.TestCase{
    91  		PreCheck:     func() { testAccPreCheck(t) },
    92  		Providers:    testAccProviders,
    93  		CheckDestroy: testCheckAzureRMManagedDiskDestroy,
    94  		Steps: []resource.TestStep{
    95  			{
    96  				Config: preConfig,
    97  				Check: resource.ComposeTestCheckFunc(
    98  					testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
    99  					resource.TestCheckResourceAttr(
   100  						"azurerm_managed_disk.test", "tags.%", "2"),
   101  					resource.TestCheckResourceAttr(
   102  						"azurerm_managed_disk.test", "tags.environment", "acctest"),
   103  					resource.TestCheckResourceAttr(
   104  						"azurerm_managed_disk.test", "tags.cost-center", "ops"),
   105  					resource.TestCheckResourceAttr(
   106  						"azurerm_managed_disk.test", "disk_size_gb", "1"),
   107  					resource.TestCheckResourceAttr(
   108  						"azurerm_managed_disk.test", "storage_account_type", string(disk.StandardLRS)),
   109  				),
   110  			},
   111  			{
   112  				Config: postConfig,
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
   115  					resource.TestCheckResourceAttr(
   116  						"azurerm_managed_disk.test", "tags.%", "1"),
   117  					resource.TestCheckResourceAttr(
   118  						"azurerm_managed_disk.test", "tags.environment", "acctest"),
   119  					resource.TestCheckResourceAttr(
   120  						"azurerm_managed_disk.test", "disk_size_gb", "2"),
   121  					resource.TestCheckResourceAttr(
   122  						"azurerm_managed_disk.test", "storage_account_type", string(disk.PremiumLRS)),
   123  				),
   124  			},
   125  		},
   126  	})
   127  }
   128  
   129  func testCheckAzureRMManagedDiskExists(name string, d *disk.Model, shouldExist bool) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		rs, ok := s.RootModule().Resources[name]
   132  		if !ok {
   133  			return fmt.Errorf("Not found: %s", name)
   134  		}
   135  
   136  		dName := rs.Primary.Attributes["name"]
   137  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   138  		if !hasResourceGroup {
   139  			return fmt.Errorf("Bad: no resource group found in state for disk: %s", dName)
   140  		}
   141  
   142  		conn := testAccProvider.Meta().(*ArmClient).diskClient
   143  
   144  		resp, err := conn.Get(resourceGroup, dName)
   145  		if err != nil {
   146  			return fmt.Errorf("Bad: Get on diskClient: %s", err)
   147  		}
   148  
   149  		if resp.StatusCode == http.StatusNotFound && shouldExist {
   150  			return fmt.Errorf("Bad: ManagedDisk %q (resource group %q) does not exist", dName, resourceGroup)
   151  		}
   152  		if resp.StatusCode != http.StatusNotFound && !shouldExist {
   153  			return fmt.Errorf("Bad: ManagedDisk %q (resource group %q) still exists", dName, resourceGroup)
   154  		}
   155  
   156  		*d = resp
   157  
   158  		return nil
   159  	}
   160  }
   161  
   162  func testCheckAzureRMManagedDiskDestroy(s *terraform.State) error {
   163  	conn := testAccProvider.Meta().(*ArmClient).diskClient
   164  
   165  	for _, rs := range s.RootModule().Resources {
   166  		if rs.Type != "azurerm_managed_disk" {
   167  			continue
   168  		}
   169  
   170  		name := rs.Primary.Attributes["name"]
   171  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   172  
   173  		resp, err := conn.Get(resourceGroup, name)
   174  
   175  		if err != nil {
   176  			return nil
   177  		}
   178  
   179  		if resp.StatusCode != http.StatusNotFound {
   180  			return fmt.Errorf("Managed Disk still exists: \n%#v", resp.Properties)
   181  		}
   182  	}
   183  
   184  	return nil
   185  }
   186  
   187  func testDeleteAzureRMVirtualMachine(name string) resource.TestCheckFunc {
   188  	return func(s *terraform.State) error {
   189  		rs, ok := s.RootModule().Resources[name]
   190  		if !ok {
   191  			return fmt.Errorf("Not found: %s", name)
   192  		}
   193  
   194  		vmName := rs.Primary.Attributes["name"]
   195  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   196  		if !hasResourceGroup {
   197  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName)
   198  		}
   199  
   200  		conn := testAccProvider.Meta().(*ArmClient).vmClient
   201  
   202  		_, err := conn.Delete(resourceGroup, vmName, make(chan struct{}))
   203  		if err != nil {
   204  			return fmt.Errorf("Bad: Delete on vmClient: %s", err)
   205  		}
   206  
   207  		return nil
   208  	}
   209  }
   210  
   211  var testAccAzureRMManagedDisk_empty = `
   212  resource "azurerm_resource_group" "test" {
   213      name = "acctestRG-%d"
   214      location = "West US 2"
   215  }
   216  
   217  resource "azurerm_managed_disk" "test" {
   218      name = "acctestd-%d"
   219      location = "West US 2"
   220      resource_group_name = "${azurerm_resource_group.test.name}"
   221      storage_account_type = "Standard_LRS"
   222      create_option = "Empty"
   223      disk_size_gb = "1"
   224  
   225      tags {
   226          environment = "acctest"
   227          cost-center = "ops"
   228      }
   229  }`
   230  
   231  var testAccAzureRMManagedDisk_import = `
   232  resource "azurerm_resource_group" "test" {
   233      name = "acctestRG-%d"
   234      location = "West US 2"
   235  }
   236  
   237  resource "azurerm_storage_account" "test" {
   238      name = "accsa%d"
   239      resource_group_name = "${azurerm_resource_group.test.name}"
   240      location = "West US 2"
   241      account_type = "Standard_LRS"
   242  
   243      tags {
   244          environment = "staging"
   245      }
   246  }
   247  
   248  resource "azurerm_storage_container" "test" {
   249      name = "vhds"
   250      resource_group_name = "${azurerm_resource_group.test.name}"
   251      storage_account_name = "${azurerm_storage_account.test.name}"
   252      container_access_type = "private"
   253  }
   254  
   255  resource "azurerm_managed_disk" "test" {
   256      name = "acctestd-%d"
   257      location = "West US 2"
   258      resource_group_name = "${azurerm_resource_group.test.name}"
   259      storage_account_type = "Standard_LRS"
   260      create_option = "Import"
   261      source_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   262      disk_size_gb = "45"
   263  
   264      tags {
   265          environment = "acctest"
   266      }
   267  }`
   268  
   269  var testAccAzureRMManagedDisk_copy = `
   270  resource "azurerm_resource_group" "test" {
   271      name = "acctestRG-%d"
   272      location = "West US 2"
   273  }
   274  
   275  resource "azurerm_managed_disk" "source" {
   276      name = "acctestd1-%d"
   277      location = "West US 2"
   278      resource_group_name = "${azurerm_resource_group.test.name}"
   279      storage_account_type = "Standard_LRS"
   280      create_option = "Empty"
   281      disk_size_gb = "1"
   282  
   283      tags {
   284          environment = "acctest"
   285          cost-center = "ops"
   286      }
   287  }
   288  
   289  resource "azurerm_managed_disk" "test" {
   290      name = "acctestd2-%d"
   291      location = "West US 2"
   292      resource_group_name = "${azurerm_resource_group.test.name}"
   293      storage_account_type = "Standard_LRS"
   294      create_option = "Copy"
   295      source_resource_id = "${azurerm_managed_disk.source.id}"
   296      disk_size_gb = "1"
   297  
   298      tags {
   299          environment = "acctest"
   300          cost-center = "ops"
   301      }
   302  }`
   303  
   304  var testAccAzureRMManagedDisk_empty_updated = `
   305  resource "azurerm_resource_group" "test" {
   306      name = "acctestRG-%d"
   307      location = "West US 2"
   308  }
   309  
   310  resource "azurerm_managed_disk" "test" {
   311      name = "acctestd-%d"
   312      location = "West US 2"
   313      resource_group_name = "${azurerm_resource_group.test.name}"
   314      storage_account_type = "Premium_LRS"
   315      create_option = "Empty"
   316      disk_size_gb = "2"
   317  
   318      tags {
   319          environment = "acctest"
   320      }
   321  }`