implemented copy for managed disks
This commit is contained in:
parent
9158b95736
commit
a084aa08c2
|
@ -47,14 +47,22 @@ func resourceArmManagedDisk() *schema.Resource {
|
||||||
"create_option": {
|
"create_option": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
ValidateFunc: validation.StringInSlice([]string{
|
ValidateFunc: validation.StringInSlice([]string{
|
||||||
string(disk.Import),
|
string(disk.Import),
|
||||||
string(disk.Empty),
|
string(disk.Empty),
|
||||||
//todo: add support for snapshots as a source (disk.Copy)
|
string(disk.Copy),
|
||||||
}, true),
|
}, true),
|
||||||
},
|
},
|
||||||
|
|
||||||
"source_uri": {
|
"source_uri": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
"source_resource_id": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
|
@ -129,7 +137,13 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro
|
||||||
if sourceUri := d.Get("source_uri").(string); sourceUri != "" {
|
if sourceUri := d.Get("source_uri").(string); sourceUri != "" {
|
||||||
creationData.SourceURI = &sourceUri
|
creationData.SourceURI = &sourceUri
|
||||||
} else {
|
} 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)
|
d.Set("location", resp.Location)
|
||||||
|
|
||||||
if resp.Properties != nil {
|
if resp.Properties != nil {
|
||||||
if m, err := flattenAzureRmManagedDiskProperties(resp.Properties); err != nil {
|
m := flattenAzureRmManagedDiskProperties(resp.Properties)
|
||||||
return fmt.Errorf("[DEBUG] Error setting disk properties: %#v", err)
|
d.Set("storage_account_type", m["storage_account_type"])
|
||||||
} else {
|
d.Set("disk_size_gb", m["disk_size_gb"])
|
||||||
d.Set("storage_account_type", m["storage_account_type"])
|
if m["os_type"] != nil {
|
||||||
d.Set("disk_size_gb", m["disk_size_gb"])
|
d.Set("os_type", m["os_type"])
|
||||||
if m["os_type"] != nil {
|
|
||||||
d.Set("os_type", m["os_type"])
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.CreationData != nil {
|
if resp.CreationData != nil {
|
||||||
if m, err := flattenAzureRmManagedDiskCreationData(resp.CreationData); err != nil {
|
m := flattenAzureRmManagedDiskCreationData(resp.CreationData)
|
||||||
return fmt.Errorf("[DEBUG] Error setting managed disk creation data: %#v", err)
|
d.Set("create_option", m["create_option"])
|
||||||
} else {
|
if m["source_uri"] != nil {
|
||||||
d.Set("create_option", m["create_option"])
|
d.Set("source_uri", m["source_uri"])
|
||||||
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
|
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 := make(map[string]interface{})
|
||||||
result["storage_account_type"] = string(properties.AccountType)
|
result["storage_account_type"] = string(properties.AccountType)
|
||||||
result["disk_size_gb"] = *properties.DiskSizeGB
|
result["disk_size_gb"] = *properties.DiskSizeGB
|
||||||
|
@ -230,15 +240,15 @@ func flattenAzureRmManagedDiskProperties(properties *disk.Properties) (map[strin
|
||||||
result["os_type"] = string(properties.OsType)
|
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 := make(map[string]interface{})
|
||||||
result["create_option"] = string(creationData.CreateOption)
|
result["create_option"] = string(creationData.CreateOption)
|
||||||
if creationData.SourceURI != nil {
|
if creationData.SourceURI != nil {
|
||||||
result["source_uri"] = *creationData.SourceURI
|
result["source_uri"] = *creationData.SourceURI
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func TestAccAzureRMManagedDisk_update(t *testing.T) {
|
||||||
var d disk.Model
|
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 = `
|
var testAccAzureRMManagedDisk_import = `
|
||||||
resource "azurerm_resource_group" "test" {
|
resource "azurerm_resource_group" "test" {
|
||||||
name = "acctestRG-%d"
|
name = "acctestRG-%d"
|
||||||
|
@ -261,3 +261,57 @@ resource "azurerm_managed_disk" "test" {
|
||||||
environment = "acctest"
|
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"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
|
@ -152,9 +152,14 @@ func resourceArmVirtualMachine() *schema.Resource {
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Elem: &schema.Resource{
|
Elem: &schema.Resource{
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
|
"id": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
"storage_account_type": {
|
"storage_account_type": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Optional: true,
|
||||||
ValidateFunc: validation.StringInSlice([]string{
|
ValidateFunc: validation.StringInSlice([]string{
|
||||||
string(compute.PremiumLRS),
|
string(compute.PremiumLRS),
|
||||||
string(compute.StandardLRS),
|
string(compute.StandardLRS),
|
||||||
|
|
Loading…
Reference in New Issue