diff --git a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs index 7ccd45e3dc6b..7afd0b604c94 100644 --- a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs +++ b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs @@ -311,5 +311,12 @@ public void TestSetAzVMOperatingSystemError() { TestRunner.RunTestScript("Test-SetAzVMOperatingSystemError"); } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void TestHostGroupPropertySetOnVirtualMachine() + { + TestRunner.RunTestScript("Test-HostGroupPropertySetOnVirtualMachine"); + } } } diff --git a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 index d61bc8023b78..62a7cec72af2 100644 --- a/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 +++ b/src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1 @@ -4171,3 +4171,47 @@ function Test-SetAzVMOperatingSystemError } } +<# +.SYNOPSIS +Test HostGroup property is set on a VM correctly when HostGroup.Id is passed as a parameter. +#> +function Test-HostGroupPropertySetOnVirtualMachine +{ + # Setup + $rgname = Get-ComputeTestResourceName + + try + { + # Common + [string]$loc = Get-Location "Microsoft.Resources" "resourceGroups" "East US 2 EUAP"; + $loc = $loc.Replace(' ', ''); + + New-AzResourceGroup -Name $rgname -Location $loc -Force; + + # Create a VM first + $hostGroupName = $rgname + 'hostgroup' + $hostGroup = New-AzHostGroup -ResourceGroupName $rgname -Name $hostGroupName -Location $loc -PlatformFaultDomain 2 -Zone "2"; + + $hostName = $rgname + 'host' + New-AzHost -ResourceGroupName $rgname -HostGroupName $hostGroupName -Name $hostName -Location $loc -Sku "ESv3-Type1" -PlatformFaultDomain 1; + + # VM Profile & Hardware + $vmsize = 'Standard_E2s_v3'; + $vmname0 = 'v' + $rgname; + + # Creating a VM using simple parameter set + $username = "admin01" + $password = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force + $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password + + $vm0 = New-AzVM -ResourceGroupName $rgname -Location $loc -Name $vmname0 -Credential $cred -Zone "2" -Size $vmsize -HostGroupId $hostGroup.Id; + + Assert-AreEqual $hostGroup.Id $vm0.HostGroup.Id; + } + finally + { + # Cleanup + Clean-ResourceGroup $rgname + } +} + diff --git a/src/Compute/Compute/ChangeLog.md b/src/Compute/Compute/ChangeLog.md index 1d83d190a21e..9ebc0e2db1bd 100644 --- a/src/Compute/Compute/ChangeLog.md +++ b/src/Compute/Compute/ChangeLog.md @@ -23,6 +23,7 @@ * Added '-EncryptionAtHost' parameter to New-AzVm, New-AzVmss, New-AzVMConfig, New-AzVmssConfig, Update-AzVM, and Update-AzVmss * Added 'SecurityProfile' to Get-AzVM and Get-AzVmss return object * Added the '-InstanceView' switch as optional parameter to Get-AzHostGroup +* Added the '-HostGroupId' parameter to New-AzVm. ## Version 4.2.1 * Added warning when using `New-AzVmss` without "latest" image version diff --git a/src/Compute/Compute/Models/PSVirtualMachine.cs b/src/Compute/Compute/Models/PSVirtualMachine.cs index 6100caacb539..6f9cc20c4c0a 100644 --- a/src/Compute/Compute/Models/PSVirtualMachine.cs +++ b/src/Compute/Compute/Models/PSVirtualMachine.cs @@ -124,5 +124,8 @@ public string ResourceGroupName // Gets or sets the Priority public string Priority { get; set; } + + // Gets or sets the HostGroup + public SubResource HostGroup { get; set; } } } diff --git a/src/Compute/Compute/Strategies/ComputeRp/VirtualMachineStrategy.cs b/src/Compute/Compute/Strategies/ComputeRp/VirtualMachineStrategy.cs index e51320269e62..8ef58660aeba 100644 --- a/src/Compute/Compute/Strategies/ComputeRp/VirtualMachineStrategy.cs +++ b/src/Compute/Compute/Strategies/ComputeRp/VirtualMachineStrategy.cs @@ -53,6 +53,7 @@ public static ResourceConfig CreateVirtualMachineConfig( bool ultraSSDEnabled, Func proximityPlacementGroup, string hostId, + string hostGroupId, string priority, string evictionPolicy, double? maxPrice, @@ -94,6 +95,7 @@ public static ResourceConfig CreateVirtualMachineConfig( AdditionalCapabilities = ultraSSDEnabled ? new AdditionalCapabilities(true) : null, ProximityPlacementGroup = proximityPlacementGroup(engine), Host = string.IsNullOrEmpty(hostId) ? null : new SubResource(hostId), + HostGroup = string.IsNullOrEmpty(hostId) ? null : new SubResource(hostGroupId), Priority = priority, EvictionPolicy = evictionPolicy, BillingProfile = (maxPrice == null) ? null : new BillingProfile(maxPrice), @@ -114,6 +116,7 @@ public static ResourceConfig CreateVirtualMachineConfig( bool ultraSSDEnabled, Func proximityPlacementGroup, string hostId, + string hostGroupId, string priority, string evictionPolicy, double? maxPrice, @@ -152,6 +155,7 @@ public static ResourceConfig CreateVirtualMachineConfig( AdditionalCapabilities = ultraSSDEnabled ? new AdditionalCapabilities(true) : null, ProximityPlacementGroup = proximityPlacementGroup(engine), Host = string.IsNullOrEmpty(hostId) ? null : new SubResource(hostId), + HostGroup = string.IsNullOrEmpty(hostId) ? null : new SubResource(hostGroupId), Priority = priority, EvictionPolicy = evictionPolicy, BillingProfile = (maxPrice == null) ? null : new BillingProfile(maxPrice), diff --git a/src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs b/src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs index 09de335ddc50..a7532428e9f1 100644 --- a/src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs +++ b/src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs @@ -259,6 +259,14 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet [Parameter(ParameterSetName = DiskFileParameterSet, Mandatory = false, HelpMessage = "EncryptionAtHost property can be used by user in the request to enable or disable the Host Encryption for the virtual machine. This will enable the encryption for all the disks including Resource/Temp disk at host itself.")] public SwitchParameter EncryptionAtHost { get; set; } = false; + + [Parameter(ParameterSetName = SimpleParameterSet, Mandatory = false, + HelpMessage = "The resource id of the dedicated host group, on which the customer wants their VM placed using automatic placement.", + ValueFromPipelineByPropertyName = true)] + [Parameter(ParameterSetName = DiskFileParameterSet, Mandatory = false, + HelpMessage = "The resource id of the dedicated host group, on which the customer wants their VM placed using automatic placement.", + ValueFromPipelineByPropertyName = true)] + public string HostGroupId { get; set; } public override void ExecuteCmdlet() { @@ -392,6 +400,7 @@ public async Task> CreateConfigAsync() identity: _cmdlet.GetVMIdentityFromArgs(), proximityPlacementGroup: ppgSubResourceFunc, hostId: _cmdlet.HostId, + hostGroupId: _cmdlet.HostGroupId, priority: _cmdlet.Priority, evictionPolicy: _cmdlet.EvictionPolicy, maxPrice: _cmdlet.IsParameterBound(c => c.MaxPrice) ? _cmdlet.MaxPrice : (double?)null, @@ -417,6 +426,7 @@ public async Task> CreateConfigAsync() identity: _cmdlet.GetVMIdentityFromArgs(), proximityPlacementGroup: ppgSubResourceFunc, hostId: _cmdlet.HostId, + hostGroupId: _cmdlet.HostGroupId, priority: _cmdlet.Priority, evictionPolicy: _cmdlet.EvictionPolicy, maxPrice: _cmdlet.IsParameterBound(c => c.MaxPrice) ? _cmdlet.MaxPrice : (double?)null,