Skip to content

Commit

Permalink
Add Deep delete options for Disks and NICs (#15177)
Browse files Browse the repository at this point in the history
* DeleteOptions for Disks and NetworkInterfaces

* DeleteOption fixes

* Upgrade to Microsoft.Azure.Management.Compute Version 47.0.0

* Updated help files

* temporyr change to move Compute package to version 47

Co-authored-by: Bashar Gharaibeh <basharghar@live.com>
  • Loading branch information
basharg and Bashar Gharaibeh authored Jun 8, 2021
1 parent 7307e5e commit 5498b82
Show file tree
Hide file tree
Showing 22 changed files with 10,003 additions and 2,941 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public void TestSimpleNewVm()
TestRunner.RunTestScript("Test-SimpleNewVm");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSimpleNewVmWithDeleteOptions()
{
TestRunner.RunTestScript("Test-SimpleNewVmWithDeleteOptions");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSimpleNewVmFromSIGImage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ function Test-SimpleNewVm
}
}

<#
.SYNOPSIS
Test Simple Paremeter Set for New Vm
#>
function Test-SimpleNewVmWithDeleteOptions
{
# Setup
$vmname = Get-ResourceName

try
{
$username = "admin01"
$password = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
[string]$domainNameLabel = "$vmname-$vmname".tolower();

# Common
$x = New-AzVM -Name $vmname -Credential $cred -DomainNameLabel $domainNameLabel -NetworkInterfaceDeleteOption "Delete" -OSDiskDeleteOption "Detach" -DataDiskSizeInGb 32 -DataDiskDeleteOption "Delete"

Assert-AreEqual $vmname $x.Name;
Assert-Null $x.Identity
Assert-False { $x.AdditionalCapabilities.UltraSSDEnabled };

Assert-AreEqual $x.NetworkProfile.NetworkInterfaces[0].DeleteOption "Delete"
Assert-AreEqual $x.StorageProfile.OSDisk.DeleteOption "Detach"

}
finally
{
# Cleanup
Clean-ResourceGroup $vmname
}
}

<#
.SYNOPSIS
Test Simple Paremeter Set for New Vm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,11 @@ function Test-AddNetworkInterface

$nicList = Get-AzNetworkInterface -ResourceGroupName $rgname;
$nicList[0].Primary = $true;
$p = Add-AzVMNetworkInterface -VM $p -NetworkInterface $nicList;
$p = Add-AzVMNetworkInterface -VM $p -NetworkInterface $nicList -DeleteOption "Detach";
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicList[0].Id;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Primary $true;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].DeleteOption "Detach";

# Storage Account (SA)
$stoname = 'sto' + $rgname;
Expand Down Expand Up @@ -453,6 +454,7 @@ function Test-AddNetworkInterface
Assert-AreEqual $vm1.Name $vmname;
Assert-AreEqual $vm1.NetworkProfile.NetworkInterfaces.Count 1;
Assert-AreEqual $vm1.NetworkProfile.NetworkInterfaces[0].Id $nicId;
Assert-AreEqual $vm1.NetworkProfile.NetworkInterfaces[0].DeleteOption "Detach";
}
finally
{
Expand Down Expand Up @@ -840,3 +842,5 @@ function Test-VMNicWithAcceleratedNetworkingValidations
Clean-ResourceGroup $rgname
}
}


7 changes: 7 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ public void TestVirtualMachineManagedDiskConversion()
TestRunner.RunTestScript("Test-VirtualMachineManagedDiskConversion");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVirtualMachineDiskDeleteOption()
{
TestRunner.RunTestScript("Test-VirtualMachineDiskDeleteOption");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVirtualMachinePerformanceMaintenance()
Expand Down
147 changes: 147 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3328,6 +3328,153 @@ function Test-VirtualMachineManagedDiskConversion
}
}

<#
.SYNOPSIS
Test Virtual Machine managed disk delete option
#>
function Test-VirtualMachineDiskDeleteOption
{
# Setup
$rgname = Get-ComputeTestResourceName

try
{
# Common
if ($loc -eq $null)
{
$loc = Get-ComputeVMLocation;
}
New-AzResourceGroup -Name $rgname -Location $loc -Force;

# VM Profile & Hardware
$vmsize = 'Standard_A4';
$vmname = 'vm' + $rgname;
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize;
Assert-AreEqual $p.HardwareProfile.VmSize $vmsize;

# NRP
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
$subnetId = $vnet.Subnets[0].Id;
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname);
$pubipId = $pubip.Id;
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
$nicId = $nic.Id;

$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId;

# Adding the same Nic but not set it Primary
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId -Primary;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Primary $true;

# Storage Account (SA)
$stoname = 'sto' + $rgname;
$stotype = 'Standard_GRS';
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
$stoaccount = Get-AzStorageAccount -ResourceGroupName $rgname -Name $stoname;

$osDiskName = 'osDisk';
$osDiskCaching = 'ReadWrite';
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";
$dataDiskVhdUri1 = "https://$stoname.blob.core.windows.net/test/data1.vhd";
$dataDiskVhdUri2 = "https://$stoname.blob.core.windows.net/test/data2.vhd";
$dataDiskVhdUri3 = "https://$stoname.blob.core.windows.net/test/data3.vhd";

$p = Set-AzVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage -DeleteOption "Delete";

$p = Add-AzVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 1 -VhdUri $dataDiskVhdUri1 -CreateOption Empty -DeleteOption "Delete";
$p = Add-AzVMDataDisk -VM $p -Name 'testDataDisk2' -Caching 'ReadOnly' -DiskSizeInGB 11 -Lun 2 -VhdUri $dataDiskVhdUri2 -CreateOption Empty -DeleteOption "Detach";
$p = Add-AzVMDataDisk -VM $p -Name 'testDataDisk3' -Caching 'ReadOnly' -DiskSizeInGB 12 -Lun 3 -VhdUri $dataDiskVhdUri3 -CreateOption Empty;
$p = Remove-AzVMDataDisk -VM $p -Name 'testDataDisk3';

Assert-AreEqual $p.StorageProfile.OSDisk.Caching $osDiskCaching;
Assert-AreEqual $p.StorageProfile.OSDisk.Name $osDiskName;
Assert-AreEqual $p.StorageProfile.OSDisk.Vhd.Uri $osDiskVhdUri;
Assert-AreEqual $p.StorageProfile.OSDisk.DeleteOption "Delete";
Assert-AreEqual $p.StorageProfile.DataDisks.Count 2;
Assert-AreEqual $p.StorageProfile.DataDisks[0].Caching 'ReadOnly';
Assert-AreEqual $p.StorageProfile.DataDisks[0].DiskSizeGB 10;
Assert-AreEqual $p.StorageProfile.DataDisks[0].Lun 1;
Assert-AreEqual $p.StorageProfile.DataDisks[0].Vhd.Uri $dataDiskVhdUri1;
Assert-AreEqual $p.StorageProfile.DataDisks[0].DeleteOption "Delete";
Assert-AreEqual $p.StorageProfile.DataDisks[1].Caching 'ReadOnly';
Assert-AreEqual $p.StorageProfile.DataDisks[1].DiskSizeGB 11;
Assert-AreEqual $p.StorageProfile.DataDisks[1].Lun 2;
Assert-AreEqual $p.StorageProfile.DataDisks[1].Vhd.Uri $dataDiskVhdUri2;
Assert-AreEqual $p.StorageProfile.DataDisks[1].DeleteOption "Detach";

# OS & Image
$user = "Foo12";
$password = $PLACEHOLDER;
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
$computerName = 'test';
$vhdContainer = "https://$stoname.blob.core.windows.net/test";

# $p.StorageProfile.OSDisk = $null;
$p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred;

$imgRef = Get-DefaultCRPImage -loc $loc;
$p = ($imgRef | Set-AzVMSourceImage -VM $p);

Assert-AreEqual $p.OSProfile.AdminUsername $user;
Assert-AreEqual $p.OSProfile.ComputerName $computerName;
Assert-AreEqual $p.OSProfile.AdminPassword $password;

Assert-AreEqual $p.StorageProfile.ImageReference.Offer $imgRef.Offer;
Assert-AreEqual $p.StorageProfile.ImageReference.Publisher $imgRef.PublisherName;
Assert-AreEqual $p.StorageProfile.ImageReference.Sku $imgRef.Skus;
Assert-AreEqual $p.StorageProfile.ImageReference.Version $imgRef.Version;

# Virtual Machine
New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p;

$vm2 = Get-AzVM -Name $vmname -ResourceGroupName $rgname;

Assert-AreEqual $vm2.NetworkProfile.NetworkInterfaces.Count 1;
Assert-AreEqual $vm2.NetworkProfile.NetworkInterfaces[0].Id $nicId;
Assert-AreEqual $vm2.StorageProfile.DataDisks.Count 2;

Assert-AreEqual $vm2.OSProfile.AdminUsername $user;
Assert-AreEqual $vm2.OSProfile.ComputerName $computerName;
Assert-AreEqual $vm2.HardwareProfile.VmSize $vmsize;
Assert-NotNull $vm2.Location;

Assert-Null $vm2.StorageProfile.OSDisk.ManagedDisk
Assert-Null $vm2.StorageProfile.DataDisks[0].ManagedDisk
Assert-Null $vm2.StorageProfile.DataDisks[1].ManagedDisk

# Deallocate the VM before conversion
Stop-AzVM -ResourceGroupName $rgname -Name $vmname -Force

# Convert VM to managed disks
$job = ConvertTo-AzVMManagedDisk -ResourceGroupName $rgname -VMName $vmname -AsJob;
$result = $job | Wait-Job;
Assert-AreEqual "Completed" $result.State;

$vm2 = Get-AzVM -Name $vmname -ResourceGroupName $rgname;

Assert-NotNull $vm2.StorageProfile.OSDisk.ManagedDisk
Assert-AreEqual $vm2.StorageProfile.OSDisk.DeleteOption "Delete"
Assert-NotNull $vm2.StorageProfile.DataDisks[0].ManagedDisk
Assert-AreEqual $vm2.StorageProfile.DataDisks[0].DeleteOption "Delete"
Assert-NotNull $vm2.StorageProfile.DataDisks[1].ManagedDisk
Assert-AreEqual $vm2.StorageProfile.DataDisks[1].DeleteOption "Detach"

# Remove
Remove-AzVM -ResourceGroupName $rgname -Name $vmname -Force;
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Test Virtual Machine Performance Maintenance
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public partial class AddAzureRmVmssNetworkInterfaceConfigurationCommand : Micros
[Alias("DnsServer")]
public string[] DnsSettingsDnsServer { get; set; }

[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
public string NetworkApiVersion { get; set; }

protected override void ProcessRecord()
{
if (ShouldProcess("VirtualMachineScaleSet", "Add"))
Expand All @@ -106,6 +111,18 @@ private void Run()
this.VirtualMachineScaleSet.VirtualMachineProfile.NetworkProfile = new VirtualMachineScaleSetNetworkProfile();
}

if (this.IsParameterBound(c => c.NetworkApiVersion))
{
this.VirtualMachineScaleSet.VirtualMachineProfile.NetworkProfile.NetworkApiVersion = this.NetworkApiVersion;
}
else
{
// If networkApiVersion is not specified, reuse the existing one in network profile if not null,
// else use default version
this.VirtualMachineScaleSet.VirtualMachineProfile.NetworkProfile.NetworkApiVersion = this.VirtualMachineScaleSet.VirtualMachineProfile.NetworkProfile.NetworkApiVersion
?? Microsoft.Azure.Management.Compute.Models.NetworkApiVersion.TwoZeroTwoZeroHyphenMinusOneOneHyphenMinusZeroOne;
}

// NetworkInterfaceConfigurations
if (this.VirtualMachineScaleSet.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ public override void ExecuteCmdlet()
WriteWarning("You are deploying VMSS pinned to a specific image version from Azure Marketplace. \n" +
"Consider using \"latest\" as the image version. This allows VMSS to auto upgrade when a newer version is available.");
}
if (parameters?.OrchestrationMode == OrchestrationMode.Flexible)
{
if (parameters?.VirtualMachineProfile?.NetworkProfile?.NetworkInterfaceConfigurations != null)
{
foreach (var nicConfig in parameters.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations)
{
if (nicConfig.IpConfigurations != null)
{
foreach (var ipConfig in nicConfig.IpConfigurations)
{
ipConfig.LoadBalancerInboundNatPools = null;
}
}
}
}
parameters.UpgradePolicy = null;
}
var result = VirtualMachineScaleSetsClient.CreateOrUpdate(resourceGroupName, vmScaleSetName, parameters);
var psObject = new PSVirtualMachineScaleSet();
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
Expand Down
4 changes: 3 additions & 1 deletion src/Compute/Compute/Strategies/ComputeRp/DataDiskStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static IList<T> CreateDataDisks<T>(

public static IList<DataDisk> CreateDataDisks(
IEnumerable<int> imageDataDiskLuns,
IEnumerable<int> dataDiskSizes)
IEnumerable<int> dataDiskSizes,
string deleteOption = null)
=> CreateDataDisks(
imageDataDiskLuns,
dataDiskSizes,
Expand All @@ -42,6 +43,7 @@ public static IList<DataDisk> CreateDataDisks(
CreateOption = createOption,
Lun = lun,
DiskSizeGB = size,
DeleteOption = deleteOption
});

public static IList<VirtualMachineScaleSetDataDisk> CreateVmssDataDisks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.Azure.Commands.Compute.Strategies.ComputeRp
static class NetworkInterfaceReferenceStrategy
{
public static NetworkInterfaceReference GetReference(
this IEngine engine, ResourceConfig<NetworkInterface> networkInterface)
=> new NetworkInterfaceReference { Id = engine.GetId(networkInterface) };
this IEngine engine, ResourceConfig<NetworkInterface> networkInterface, string deleteOption = null)
=> new NetworkInterfaceReference { Id = engine.GetId(networkInterface), DeleteOption = deleteOption };
}
}
19 changes: 13 additions & 6 deletions src/Compute/Compute/Strategies/ComputeRp/VirtualMachineStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
string priority,
string evictionPolicy,
double? maxPrice,
bool encryptionAtHostPresent)
bool encryptionAtHostPresent,
string networkInterfaceDeleteOption = null,
string osDiskDeleteOption = null,
string dataDiskDeleteOption = null)

=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
Expand All @@ -79,7 +82,7 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
{
NetworkInterfaces = new[]
{
engine.GetReference(networkInterface)
engine.GetReference(networkInterface, networkInterfaceDeleteOption)
}
},
HardwareProfile = new HardwareProfile
Expand All @@ -90,7 +93,7 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
{
ImageReference = imageAndOsType?.Image,
DataDisks = DataDiskStrategy.CreateDataDisks(
imageAndOsType?.DataDiskLuns, dataDisks)
imageAndOsType?.DataDiskLuns, dataDisks, dataDiskDeleteOption)
},
AvailabilitySet = engine.GetReference(availabilitySet),
Zones = zones,
Expand Down Expand Up @@ -124,7 +127,10 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
string priority,
string evictionPolicy,
double? maxPrice,
bool encryptionAtHostPresent
bool encryptionAtHostPresent,
string networkInterfaceDeleteOption = null,
string osDiskDeleteOption = null,
string dataDiskDeleteOption = null
)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
Expand All @@ -135,7 +141,7 @@ bool encryptionAtHostPresent
{
NetworkInterfaces = new[]
{
engine.GetReference(networkInterface)
engine.GetReference(networkInterface, networkInterfaceDeleteOption)
}
},
HardwareProfile = new HardwareProfile
Expand All @@ -150,8 +156,9 @@ bool encryptionAtHostPresent
CreateOption = DiskCreateOptionTypes.Attach,
OsType = osType,
ManagedDisk = engine.GetReference(disk, ultraSSDEnabled ? StorageAccountTypes.UltraSSDLRS : StorageAccountTypes.PremiumLRS),
DeleteOption = osDiskDeleteOption
},
DataDisks = DataDiskStrategy.CreateDataDisks(null, dataDisks)
DataDisks = DataDiskStrategy.CreateDataDisks(null, dataDisks, dataDiskDeleteOption)
},
Identity = identity,
AvailabilitySet = engine.GetReference(availabilitySet),
Expand Down
Loading

0 comments on commit 5498b82

Please sign in to comment.