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

106 lines
2.6 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"
"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(
2017-03-03 23:46:33 +01:00
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d),
2017-03-03 23:31:40 +01:00
),
},
},
})
}
2017-03-03 23:46:33 +01:00
func testCheckAzureRMManagedDiskExists(name string, d *disk.Model) 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 {
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
}
*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-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 = "20"
tags {
environment = "acctest"
}
}`