implemented copy for managed disks

This commit is contained in:
Brandon Tosch 2017-03-03 19:33:04 -08:00
parent 9158b95736
commit a084aa08c2
3 changed files with 111 additions and 42 deletions

View File

@ -47,14 +47,22 @@ func resourceArmManagedDisk() *schema.Resource {
"create_option": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(disk.Import),
string(disk.Empty),
//todo: add support for snapshots as a source (disk.Copy)
string(disk.Copy),
}, true),
},
"source_uri": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"source_resource_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
@ -129,7 +137,13 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro
if sourceUri := d.Get("source_uri").(string); sourceUri != "" {
creationData.SourceURI = &sourceUri
} else {
return fmt.Errorf("[ERROR] source_uri must be specified when create_option is `%s` or `%s`", disk.Import, disk.Copy)
return fmt.Errorf("[ERROR] source_uri must be specified when create_option is `%s`", disk.Import)
}
} else if strings.EqualFold(createOption, string(disk.Copy)) {
if sourceResourceId := d.Get("source_resource_id").(string); sourceResourceId != "" {
creationData.SourceResourceID = &sourceResourceId
} else {
return fmt.Errorf("[ERROR] source_resource_id must be specified when create_option is `%s`", disk.Copy)
}
}
@ -177,26 +191,22 @@ func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error
d.Set("location", resp.Location)
if resp.Properties != nil {
if m, err := flattenAzureRmManagedDiskProperties(resp.Properties); err != nil {
return fmt.Errorf("[DEBUG] Error setting disk properties: %#v", err)
} else {
d.Set("storage_account_type", m["storage_account_type"])
d.Set("disk_size_gb", m["disk_size_gb"])
if m["os_type"] != nil {
d.Set("os_type", m["os_type"])
}
m := flattenAzureRmManagedDiskProperties(resp.Properties)
d.Set("storage_account_type", m["storage_account_type"])
d.Set("disk_size_gb", m["disk_size_gb"])
if m["os_type"] != nil {
d.Set("os_type", m["os_type"])
}
}
if resp.CreationData != nil {
if m, err := flattenAzureRmManagedDiskCreationData(resp.CreationData); err != nil {
return fmt.Errorf("[DEBUG] Error setting managed disk creation data: %#v", err)
} else {
d.Set("create_option", m["create_option"])
if m["source_uri"] != nil {
d.Set("source_uri", m["source_uri"])
}
m := flattenAzureRmManagedDiskCreationData(resp.CreationData)
d.Set("create_option", m["create_option"])
if m["source_uri"] != nil {
d.Set("source_uri", m["source_uri"])
}
if m["source_resource_id"] != nil {
d.Set("source_resource_id", m["source_resource_id"])
}
}
@ -222,7 +232,7 @@ func resourceArmManagedDiskDelete(d *schema.ResourceData, meta interface{}) erro
return nil
}
func flattenAzureRmManagedDiskProperties(properties *disk.Properties) (map[string]interface{}, error) {
func flattenAzureRmManagedDiskProperties(properties *disk.Properties) map[string]interface{} {
result := make(map[string]interface{})
result["storage_account_type"] = string(properties.AccountType)
result["disk_size_gb"] = *properties.DiskSizeGB
@ -230,15 +240,15 @@ func flattenAzureRmManagedDiskProperties(properties *disk.Properties) (map[strin
result["os_type"] = string(properties.OsType)
}
return result, nil
return result
}
func flattenAzureRmManagedDiskCreationData(creationData *disk.CreationData) (map[string]interface{}, error) {
func flattenAzureRmManagedDiskCreationData(creationData *disk.CreationData) map[string]interface{} {
result := make(map[string]interface{})
result["create_option"] = string(creationData.CreateOption)
if creationData.SourceURI != nil {
result["source_uri"] = *creationData.SourceURI
}
return result, nil
return result
}

View File

@ -62,6 +62,25 @@ func TestAccAzureRMManagedDisk_import(t *testing.T) {
})
}
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),
),
},
},
})
}
func TestAccAzureRMManagedDisk_update(t *testing.T) {
var d disk.Model
@ -206,25 +225,6 @@ resource "azurerm_managed_disk" "test" {
}
}`
var testAccAzureRMManagedDisk_empty_updated = `
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 = "Premium_LRS"
create_option = "Empty"
disk_size_gb = "30"
tags {
environment = "acctest"
}
}`
var testAccAzureRMManagedDisk_import = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
@ -261,3 +261,57 @@ resource "azurerm_managed_disk" "test" {
environment = "acctest"
}
}`
var testAccAzureRMManagedDisk_copy = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
resource "azurerm_managed_disk" "source" {
name = "acctestd1-%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"
}
}
resource "azurerm_managed_disk" "test" {
name = "acctestd2-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
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"
}
resource "azurerm_managed_disk" "test" {
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = "30"
tags {
environment = "acctest"
}
}`

View File

@ -152,9 +152,14 @@ func resourceArmVirtualMachine() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"storage_account_type": {
Type: schema.TypeString,
Required: true,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.PremiumLRS),
string(compute.StandardLRS),