terraform/builtin/providers/azurerm/resource_arm_managed_disk_t...

367 lines
10 KiB
Go
Raw Normal View History

2017-03-03 21:12:00 +01:00
package azurerm
2017-03-03 23:31:40 +01:00
import (
"fmt"
"net/http"
"testing"
2017-03-04 03:15:05 +01:00
"github.com/Azure/azure-sdk-for-go/arm/compute"
2017-03-03 23:31:40 +01:00
"github.com/Azure/azure-sdk-for-go/arm/disk"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
2017-03-03 23:46:33 +01:00
func TestAccAzureRMManagedDisk_empty(t *testing.T) {
2017-03-03 23:31:40 +01:00
var d disk.Model
ri := acctest.RandInt()
2017-03-03 23:46:33 +01:00
config := fmt.Sprintf(testAccAzureRMManagedDisk_empty, ri, ri)
2017-03-03 23:31:40 +01:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
2017-03-03 23:46:33 +01:00
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
2017-03-03 23:31:40 +01:00
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
2017-03-03 23:31:40 +01:00
),
},
},
})
}
2017-03-04 03:15:05 +01:00
func TestAccAzureRMManagedDisk_import(t *testing.T) {
var d disk.Model
var vm compute.VirtualMachine
ri := acctest.RandInt()
vmConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri)
config := fmt.Sprintf(testAccAzureRMManagedDisk_import, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
{
//need to create a vm and then delete it so we can use the vhd to test import
Config: vmConfig,
Destroy: false,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
testDeleteAzureRMVirtualMachine("azurerm_virtual_machine.test"),
),
},
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
2017-03-04 03:15:05 +01:00
),
},
},
})
}
2017-03-04 04:33:04 +01:00
func TestAccAzureRMManagedDisk_copy(t *testing.T) {
var d disk.Model
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMManagedDisk_copy, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
2017-03-04 04:33:04 +01:00
),
},
},
})
}
2017-03-04 03:15:05 +01:00
func TestAccAzureRMManagedDisk_update(t *testing.T) {
var d disk.Model
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMManagedDisk_empty, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMManagedDisk_empty_updated, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
2017-03-04 03:15:05 +01:00
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.%", "2"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.environment", "acctest"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.cost-center", "ops"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "disk_size_gb", "1"),
2017-03-04 03:15:05 +01:00
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "storage_account_type", string(disk.StandardLRS)),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
2017-03-04 03:15:05 +01:00
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.%", "1"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.environment", "acctest"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "disk_size_gb", "2"),
2017-03-04 03:15:05 +01:00
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "storage_account_type", string(disk.PremiumLRS)),
),
},
},
})
}
func TestAccAzureRMManagedDisk_NonStandardCasing(t *testing.T) {
var d disk.Model
ri := acctest.RandInt()
config := testAccAzureRMManagedDiskNonStandardCasing(ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d, true),
),
},
resource.TestStep{
Config: config,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}
func testCheckAzureRMManagedDiskExists(name string, d *disk.Model, shouldExist bool) resource.TestCheckFunc {
2017-03-03 23:31:40 +01:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
dName := 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 disk: %s", dName)
}
conn := testAccProvider.Meta().(*ArmClient).diskClient
resp, err := conn.Get(resourceGroup, dName)
if err != nil {
return fmt.Errorf("Bad: Get on diskClient: %s", err)
}
if resp.StatusCode == http.StatusNotFound && shouldExist {
2017-03-03 23:46:33 +01:00
return fmt.Errorf("Bad: ManagedDisk %q (resource group %q) does not exist", dName, resourceGroup)
2017-03-03 23:31:40 +01:00
}
if resp.StatusCode != http.StatusNotFound && !shouldExist {
return fmt.Errorf("Bad: ManagedDisk %q (resource group %q) still exists", dName, resourceGroup)
}
2017-03-03 23:31:40 +01:00
*d = resp
return nil
}
}
2017-03-03 23:46:33 +01:00
func testCheckAzureRMManagedDiskDestroy(s *terraform.State) error {
2017-03-03 23:31:40 +01:00
conn := testAccProvider.Meta().(*ArmClient).diskClient
for _, rs := range s.RootModule().Resources {
2017-03-03 23:46:33 +01:00
if rs.Type != "azurerm_managed_disk" {
2017-03-03 23:31:40 +01:00
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 {
2017-03-03 23:46:33 +01:00
return fmt.Errorf("Managed Disk still exists: \n%#v", resp.Properties)
2017-03-03 23:31:40 +01:00
}
}
return nil
}
2017-03-04 03:15:05 +01:00
func testDeleteAzureRMVirtualMachine(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
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
_, error := conn.Delete(resourceGroup, vmName, make(chan struct{}))
err := <-error
2017-03-04 03:15:05 +01:00
if err != nil {
return fmt.Errorf("Bad: Delete on vmClient: %s", err)
}
return nil
}
}
2017-03-03 23:46:33 +01:00
var testAccAzureRMManagedDisk_empty = `
2017-03-03 23:31:40 +01:00
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
2017-03-03 23:46:33 +01:00
resource "azurerm_managed_disk" "test" {
2017-03-03 23:31:40 +01:00
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1"
2017-03-03 23:31:40 +01:00
2017-03-04 03:15:05 +01:00
tags {
environment = "acctest"
cost-center = "ops"
}
}`
2017-03-04 04:33:04 +01:00
var testAccAzureRMManagedDisk_import = `
2017-03-04 03:15:05 +01:00
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
2017-03-04 04:33:04 +01:00
resource "azurerm_storage_account" "test" {
name = "accsa%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US 2"
account_type = "Standard_LRS"
tags {
environment = "staging"
}
}
resource "azurerm_storage_container" "test" {
name = "vhds"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
}
2017-03-04 03:15:05 +01:00
resource "azurerm_managed_disk" "test" {
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
2017-03-04 04:33:04 +01:00
storage_account_type = "Standard_LRS"
create_option = "Import"
source_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
disk_size_gb = "45"
2017-03-04 03:15:05 +01:00
tags {
environment = "acctest"
}
}`
2017-03-04 04:33:04 +01:00
var testAccAzureRMManagedDisk_copy = `
2017-03-04 03:15:05 +01:00
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
2017-03-04 04:33:04 +01:00
resource "azurerm_managed_disk" "source" {
name = "acctestd1-%d"
2017-03-04 03:15:05 +01:00
location = "West US 2"
2017-03-04 04:33:04 +01:00
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1"
2017-03-04 03:15:05 +01:00
tags {
2017-03-04 04:33:04 +01:00
environment = "acctest"
cost-center = "ops"
2017-03-04 03:15:05 +01:00
}
}
2017-03-04 04:33:04 +01:00
resource "azurerm_managed_disk" "test" {
name = "acctestd2-%d"
location = "West US 2"
2017-03-04 03:15:05 +01:00
resource_group_name = "${azurerm_resource_group.test.name}"
2017-03-04 04:33:04 +01:00
storage_account_type = "Standard_LRS"
create_option = "Copy"
source_resource_id = "${azurerm_managed_disk.source.id}"
disk_size_gb = "1"
tags {
environment = "acctest"
cost-center = "ops"
}
}`
var testAccAzureRMManagedDisk_empty_updated = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
2017-03-04 03:15:05 +01:00
}
resource "azurerm_managed_disk" "test" {
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
2017-03-04 04:33:04 +01:00
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = "2"
2017-03-04 03:15:05 +01:00
2017-03-03 23:31:40 +01:00
tags {
environment = "acctest"
}
}`
func testAccAzureRMManagedDiskNonStandardCasing(ri int) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
resource "azurerm_managed_disk" "test" {
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "standard_lrs"
create_option = "Empty"
disk_size_gb = "1"
tags {
environment = "acctest"
cost-center = "ops"
}
}`, ri, ri)
}