Skip to content

Commit

Permalink
rename data disk blocks to create and attach
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops committed Feb 15, 2021
1 parent b28a4fb commit 78f1e6d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ func resourceLinuxVirtualMachineUpdate(d *schema.ResourceData, meta interface{})
if features.VMDataDiskBeta() && d.HasChange("data_disks") {
shouldUpdate = true

oldRaw, newRaw := d.GetChange("data_disks.0.local")
oldRaw, newRaw := d.GetChange("data_disks.0.create")
oldDisks := oldRaw.(*schema.Set).List()
newDisks := newRaw.(*schema.Set).List()
for _, o := range oldDisks {
Expand All @@ -867,7 +867,7 @@ func resourceLinuxVirtualMachineUpdate(d *schema.ResourceData, meta interface{})
newDisk := n.(map[string]interface{})
if newDiskName, ok := newDisk["name"]; ok && oldDiskName.(string) == newDiskName.(string) {
if newDisk["disk_size_gb"].(int) < oldDisk["disk_size_gb"].(int) {
return fmt.Errorf("new disk size cannot be smaller than existing for %q, in Virtual Machine %q (resource group %q)", oldDisk["name"], id.Name, id.ResourceGroup)
return fmt.Errorf("new disk size cannot be smaller than current size for %q, in Virtual Machine %q (resource group %q)", oldDisk["name"], id.Name, id.ResourceGroup)
} else if newDisk["disk_size_gb"].(int) > oldDisk["disk_size_gb"].(int) {
shouldShutDown = true
shouldDeallocate = true
Expand All @@ -881,13 +881,13 @@ func resourceLinuxVirtualMachineUpdate(d *schema.ResourceData, meta interface{})
}
}
// EncryptionSet changes for "existing" disks
oldExistingRaw, newExistingRaw := d.GetChange("data_disks.0.existing")
oldExisting := oldExistingRaw.(*schema.Set).List()
newExisting := newExistingRaw.(*schema.Set).List()
for _, o := range oldExisting {
oldAttachedRaw, newAttachedRaw := d.GetChange("data_disks.0.attach")
oldAttached := oldAttachedRaw.(*schema.Set).List()
newAttached := newAttachedRaw.(*schema.Set).List()
for _, o := range oldAttached {
oldDisk := o.(map[string]interface{})
if oldDiskID, ok := oldDisk["managed_disk_id"]; ok {
for _, n := range newExisting {
for _, n := range newAttached {
newDisk := n.(map[string]interface{})
if newDiskID, ok := newDisk["managed_disk_id"]; ok && oldDiskID.(string) == newDiskID.(string) {
if newDisk["disk_encryption_set_id"].(string) != oldDisk["disk_encryption_set_id"].(string) {
Expand Down Expand Up @@ -1103,7 +1103,7 @@ func resourceLinuxVirtualMachineUpdate(d *schema.ResourceData, meta interface{})
}
}

if features.VMDataDiskBeta() && d.HasChanges("data_disks.0.local", "data_disks.0.existing") {
if features.VMDataDiskBeta() && d.HasChanges("data_disks.0.create", "data_disks.0.attach") {
shouldUpdate = true
dataDisks, err := expandVirtualMachineDataDisks(ctx, d, meta)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "None"
Expand Down Expand Up @@ -383,38 +383,38 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "None"
storage_account_type = "Standard_LRS"
disk_size_gb = 1
}
local {
create {
name = "acctest-localdisk2"
lun = 2
caching = "ReadOnly"
storage_account_type = "Standard_LRS"
disk_size_gb = 2
}
local {
create {
name = "acctest-localdisk3"
lun = 3
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
disk_size_gb = 3
}
existing {
attach {
managed_disk_id = azurerm_managed_disk.test1.id
lun = 10
caching = "None"
storage_account_type = "Standard_LRS"
}
existing {
attach {
managed_disk_id = azurerm_managed_disk.test2.id
lun = 11
caching = "ReadOnly"
Expand Down Expand Up @@ -492,30 +492,30 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "None"
storage_account_type = "Standard_LRS"
disk_size_gb = 1
}
local {
create {
name = "acctest-localdisk2"
lun = 2
caching = "ReadOnly"
storage_account_type = "Standard_LRS"
disk_size_gb = 4
}
existing {
attach {
managed_disk_id = azurerm_managed_disk.test1.id
lun = 10
caching = "None"
storage_account_type = "Standard_LRS"
}
existing {
attach {
managed_disk_id = azurerm_managed_disk.test2.id
lun = 11
caching = "ReadWrite"
Expand Down Expand Up @@ -564,7 +564,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "ReadOnly"
Expand Down Expand Up @@ -610,7 +610,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "ReadOnly"
Expand Down Expand Up @@ -683,7 +683,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "ReadOnly"
Expand Down Expand Up @@ -734,15 +734,15 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
local {
create {
name = "acctest-localdisk"
lun = 1
caching = "ReadOnly"
storage_account_type = "Standard_LRS"
disk_size_gb = 2
}
local {
create {
name = "acctest-localdisk2"
lun = 2
caching = "ReadOnly"
Expand Down Expand Up @@ -806,7 +806,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
existing {
attach {
managed_disk_id = azurerm_managed_disk.test.id
lun = 1
caching = "None"
Expand Down Expand Up @@ -869,7 +869,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}
data_disks {
existing {
attach {
managed_disk_id = azurerm_managed_disk.test.id
lun = 1
caching = "None"
Expand Down
32 changes: 16 additions & 16 deletions azurerm/internal/services/compute/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ func virtualMachineDataDiskSchema() *schema.Schema {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"local": virtualMachineLocalDataDiskSchema(),
"create": virtualMachineLocalDataDiskSchema(),

"existing": virtualMachineExistingDataDiskSchema(),
"attach": virtualMachineExistingDataDiskSchema(),
},
},
}
Expand Down Expand Up @@ -561,7 +561,7 @@ func expandVirtualMachineDataDisks(ctx context.Context, d *schema.ResourceData,

result := make([]compute.DataDisk, 0)
dataDisks := dataDisksRaw[0].(map[string]interface{})
if newDisksRaw, ok := dataDisks["local"]; ok {
if newDisksRaw, ok := dataDisks["create"]; ok {
var newDisks []compute.DataDisk
if d.IsNewResource() {
newDisks = expandVirtualMachineNewDataDisksForCreate(newDisksRaw)
Expand All @@ -574,13 +574,13 @@ func expandVirtualMachineDataDisks(ctx context.Context, d *schema.ResourceData,
result = append(result, newDisks...)
}

if existingDisksRaw, ok := dataDisks["existing"]; ok {
existingDisks, err := expandVirtualMachineExistingDataDisksForCreate(ctx, existingDisksRaw, d, meta)
if attachDisksRaw, ok := dataDisks["attach"]; ok {
attachDisks, err := expandVirtualMachineExistingDataDisksForCreate(ctx, attachDisksRaw, d, meta)
if err != nil {
return nil, err
}

result = append(result, existingDisks...)
result = append(result, attachDisks...)
}

return &result, nil
Expand Down Expand Up @@ -688,18 +688,18 @@ func expandVirtualMachineExistingDataDisksForCreate(ctx context.Context, input i
}
continue
}
existing, err := disksClient.Get(ctx, diskID.ResourceGroup, diskID.DiskName)
attach, err := disksClient.Get(ctx, diskID.ResourceGroup, diskID.DiskName)
if err != nil {
return nil, fmt.Errorf("failed retrieving details for existing Managed Disk %q (resource group %q: %+v", diskID.DiskName, diskID.ResourceGroup, err)
return nil, fmt.Errorf("failed retrieving details for attached Managed Disk %q (resource group %q: %+v", diskID.DiskName, diskID.ResourceGroup, err)
}
dataDisk := compute.DataDisk{}
dataDisk.Name = &diskID.DiskName
dataDisk.CreateOption = compute.DiskCreateOptionTypesAttach

if existing.DiskSizeGB == nil {
return nil, fmt.Errorf("failed reading `disk_size_gb` from existing Managed Disk %q (resource group %q)", diskID.DiskName, diskID.ResourceGroup)
if attach.DiskSizeGB == nil {
return nil, fmt.Errorf("failed reading `disk_size_gb` from attached Managed Disk %q (resource group %q)", diskID.DiskName, diskID.ResourceGroup)
}
dataDisk.DiskSizeGB = existing.DiskSizeGB
dataDisk.DiskSizeGB = attach.DiskSizeGB

dataDisk.Caching = compute.CachingTypes(disk["caching"].(string))
dataDisk.Lun = utils.Int32(int32(disk["lun"].(int)))
Expand All @@ -724,8 +724,8 @@ func flattenVirtualMachineDataDisks(input *[]compute.DataDisk) ([]interface{}, e
}

var newDisks []interface{}
var existingDisks []interface{}
// we need to split into new and "existing", we can use `createOption` as indicator
var attachDisks []interface{}
// we need to split into new and "attach", we can use `createOption` as indicator

for _, v := range *input {
dataDisk := make(map[string]interface{})
Expand Down Expand Up @@ -784,16 +784,16 @@ func flattenVirtualMachineDataDisks(input *[]compute.DataDisk) ([]interface{}, e
dataDisk["storage_account_type"] = storageAccountType

dataDisk["managed_disk_id"] = managedDiskID
existingDisks = append(existingDisks, dataDisk)
attachDisks = append(attachDisks, dataDisk)

default:
return nil, fmt.Errorf("unsupported `createOption` type while flattening: %s", string(createOption))
}
}
return []interface{}{
map[string]interface{}{
"local": schema.NewSet(resourceArmVirtualMachineNewDataDiskHash, newDisks),
"existing": existingDisks,
"create": schema.NewSet(resourceArmVirtualMachineNewDataDiskHash, newDisks),
"attach": attachDisks,
},
}, nil
}
Expand Down
Loading

0 comments on commit 78f1e6d

Please sign in to comment.