2015-05-28 00:50:45 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachinedisk"
|
2015-05-28 00:50:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureDataDisk_basic(t *testing.T) {
|
|
|
|
var disk virtualmachinedisk.DataDiskResponse
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureDataDiskDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureDataDisk_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDataDiskExists(
|
|
|
|
"azure_data_disk.foo", &disk),
|
|
|
|
testAccCheckAzureDataDiskAttributes(&disk),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "label", "terraform-test-0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "size", "10"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureDataDisk_update(t *testing.T) {
|
|
|
|
var disk virtualmachinedisk.DataDiskResponse
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAzureDataDiskDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureDataDisk_advanced,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDataDiskExists(
|
|
|
|
"azure_data_disk.foo", &disk),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "label", "terraform-test1-1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "lun", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "size", "10"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "caching", "ReadOnly"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "virtual_machine", "terraform-test1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureDataDisk_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureDataDiskExists(
|
|
|
|
"azure_data_disk.foo", &disk),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "label", "terraform-test1-1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "lun", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "size", "20"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "caching", "ReadWrite"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_data_disk.foo", "virtual_machine", "terraform-test2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureDataDiskExists(
|
|
|
|
n string,
|
|
|
|
disk *virtualmachinedisk.DataDiskResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Data Disk ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := rs.Primary.Attributes["virtual_machine"]
|
|
|
|
lun, err := strconv.Atoi(rs.Primary.Attributes["lun"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:10:21 +02:00
|
|
|
mc := testAccProvider.Meta().(*Client).mgmtClient
|
2015-05-28 00:50:45 +02:00
|
|
|
d, err := virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.DiskName != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Data Disk not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*disk = d
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureDataDiskAttributes(
|
|
|
|
disk *virtualmachinedisk.DataDiskResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if disk.Lun != 0 {
|
|
|
|
return fmt.Errorf("Bad lun: %d", disk.Lun)
|
|
|
|
}
|
|
|
|
|
|
|
|
if disk.LogicalDiskSizeInGB != 10 {
|
|
|
|
return fmt.Errorf("Bad size: %d", disk.LogicalDiskSizeInGB)
|
|
|
|
}
|
|
|
|
|
|
|
|
if disk.HostCaching != "None" {
|
|
|
|
return fmt.Errorf("Bad caching: %s", disk.HostCaching)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureDataDiskDestroy(s *terraform.State) error {
|
2015-05-29 00:10:21 +02:00
|
|
|
mc := testAccProvider.Meta().(*Client).mgmtClient
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azure_data_disk" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Disk ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := rs.Primary.Attributes["virtual_machine"]
|
|
|
|
lun, err := strconv.Atoi(rs.Primary.Attributes["lun"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-04 17:11:07 +02:00
|
|
|
_, err = virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun)
|
|
|
|
if err == nil {
|
2015-06-09 12:38:05 +02:00
|
|
|
return fmt.Errorf("Data disk %s still exists", rs.Primary.ID)
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
2015-06-04 17:11:07 +02:00
|
|
|
if !management.IsResourceNotFoundError(err) {
|
|
|
|
return err
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureDataDisk_basic = fmt.Sprintf(`
|
|
|
|
resource "azure_instance" "foo" {
|
|
|
|
name = "terraform-test"
|
|
|
|
image = "Ubuntu Server 14.04 LTS"
|
|
|
|
size = "Basic_A1"
|
2015-06-05 16:12:21 +02:00
|
|
|
storage_service_name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_data_disk" "foo" {
|
|
|
|
lun = 0
|
|
|
|
size = 10
|
2015-06-11 20:26:49 +02:00
|
|
|
storage_service_name = "${azure_instance.foo.storage_service_name}"
|
2015-05-28 00:50:45 +02:00
|
|
|
virtual_machine = "${azure_instance.foo.id}"
|
2015-06-05 16:12:21 +02:00
|
|
|
}`, testAccStorageServiceName)
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
var testAccAzureDataDisk_advanced = fmt.Sprintf(`
|
|
|
|
resource "azure_instance" "foo" {
|
|
|
|
name = "terraform-test1"
|
|
|
|
image = "Ubuntu Server 14.04 LTS"
|
|
|
|
size = "Basic_A1"
|
2015-06-05 16:12:21 +02:00
|
|
|
storage_service_name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_data_disk" "foo" {
|
|
|
|
lun = 1
|
|
|
|
size = 10
|
|
|
|
caching = "ReadOnly"
|
2015-06-11 20:26:49 +02:00
|
|
|
storage_service_name = "${azure_instance.foo.storage_service_name}"
|
2015-05-28 00:50:45 +02:00
|
|
|
virtual_machine = "${azure_instance.foo.id}"
|
2015-06-05 16:12:21 +02:00
|
|
|
}`, testAccStorageServiceName)
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
var testAccAzureDataDisk_update = fmt.Sprintf(`
|
|
|
|
resource "azure_instance" "foo" {
|
|
|
|
name = "terraform-test1"
|
|
|
|
image = "Ubuntu Server 14.04 LTS"
|
|
|
|
size = "Basic_A1"
|
2015-06-05 16:12:21 +02:00
|
|
|
storage_service_name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_instance" "bar" {
|
|
|
|
name = "terraform-test2"
|
|
|
|
image = "Ubuntu Server 14.04 LTS"
|
|
|
|
size = "Basic_A1"
|
2015-06-11 20:26:49 +02:00
|
|
|
storage_service_name = "${azure_instance.foo.storage_service_name}"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_data_disk" "foo" {
|
|
|
|
lun = 2
|
|
|
|
size = 20
|
|
|
|
caching = "ReadWrite"
|
2015-06-11 20:26:49 +02:00
|
|
|
storage_service_name = "${azure_instance.bar.storage_service_name}"
|
2015-05-28 00:50:45 +02:00
|
|
|
virtual_machine = "${azure_instance.bar.id}"
|
2015-06-05 16:12:21 +02:00
|
|
|
}`, testAccStorageServiceName)
|