Skip to content

Commit 0e6d853

Browse files
authored
Get-AzVm filter by Name prior to checking for throttling (#13437)
* practice * practice * practice * practicing * more practice * attempting NextLink * successful RG iteration * attempt to list multiple VMs with same name in multiple RGs. * attempts at using Get * now attempting moving filtering earlier * mapping works? paging unsure * compile errors for list trying to access power state data. * fewest errors version * save old changes and new functional * new functional, existing tests pass * dev and testing completed * cleaning and test * cleanup
1 parent 7984951 commit 0e6d853

File tree

5 files changed

+10407
-46
lines changed

5 files changed

+10407
-46
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,19 @@ public void TestVirtualMachineImageListTopOrderExpand()
346346
{
347347
TestRunner.RunTestScript("Test-VirtualMachineImageListTopOrderExpand");
348348
}
349-
349+
350350
[Fact]
351351
[Trait(Category.AcceptanceType, Category.LiveOnly)]
352352
public void TestVirtualMachineBootDiagnostics()
353353
{
354354
TestRunner.RunTestScript("Test-VirtualMachineBootDiagnostics");
355355
}
356+
357+
[Fact]
358+
[Trait(Category.AcceptanceType, Category.CheckIn)]
359+
public void TestVirtualMachineGetVMNameAcrossResourceGroups()
360+
{
361+
TestRunner.RunTestScript("Test-VirtualMachineGetVMNameAcrossResourceGroups");
362+
}
356363
}
357364
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4374,7 +4374,7 @@ function Test-HostGroupPropertySetOnVirtualMachine
43744374

43754375
<#
43764376
.SYNOPSIS
4377-
Test Virtual Machine Size and Usage
4377+
Test Get-AzVMImage new parameters -OrderBy and -Top.
43784378
#>
43794379
function Test-VirtualMachineImageListTopOrderExpand
43804380
{
@@ -4414,7 +4414,7 @@ function Test-VirtualMachineImageListTopOrderExpand
44144414

44154415
<#
44164416
.SYNOPSIS
4417-
This test can only run in Record mode. SEveral lines need to be uncommented for it to test the cmdlet.
4417+
This test can only run in Record mode. Several lines need to be uncommented for it to test the cmdlet.
44184418
Downloads the managed boot diagnostics of a Windows machine to a local file path.
44194419
#>
44204420
function Test-VirtualMachineBootDiagnostics
@@ -4432,9 +4432,9 @@ function Test-VirtualMachineBootDiagnostics
44324432
$vmname = 'vm' + $rgname;
44334433
# Create the file path on your machine, then set this variable to it.
44344434
# $localPath = "C:\Users\adsandor\Documents\bootDiags"
4435-
4435+
44364436
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize;
4437-
4437+
44384438
# NRP
44394439
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
44404440
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
@@ -4447,19 +4447,19 @@ function Test-VirtualMachineBootDiagnostics
44474447
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
44484448
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
44494449
$nicId = $nic.Id;
4450-
4450+
44514451
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
4452-
4452+
44534453
# OS & Image
44544454
$user = "Foo2";
44554455
$password = $PLACEHOLDER;
44564456
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
44574457
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
44584458
$computerName = 'test';
4459-
4459+
44604460
# Windows OS test case.
44614461
$p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred;
4462-
4462+
44634463
$imgRef = Get-DefaultCRPImage -loc $loc;
44644464
$p = ($imgRef | Set-AzVMSourceImage -VM $p);
44654465

@@ -4475,6 +4475,52 @@ function Test-VirtualMachineBootDiagnostics
44754475
{
44764476
# Cleanup
44774477
Clean-ResourceGroup $rgname
4478-
}
4478+
}
44794479
}
44804480

4481+
<#
4482+
.SYNOPSIS
4483+
Test the Get-AzVm cmdlet when using VMs with the
4484+
same name across multiple Resource Groups.
4485+
#>
4486+
function Test-VirtualMachineGetVMNameAcrossResourceGroups
4487+
{
4488+
# Setup
4489+
$loc = "eastus";
4490+
$rgname = Get-ComputeTestResourceName;
4491+
$rgname2 = Get-ComputeTestResourceName;
4492+
4493+
try
4494+
{
4495+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
4496+
New-AzResourceGroup -Name $rgname2 -Location $loc -Force;
4497+
4498+
# VM Profile & Hardware
4499+
$vmsize = 'Standard_E2s_v3';
4500+
$vmname1 = 'v' + $rgname;
4501+
$vmname3 = 'v3' + $rgname;
4502+
4503+
# Creating a VM using simple parameter set
4504+
$username = "admin01"
4505+
$password = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force
4506+
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
4507+
4508+
$domainNameLabel1 = "domain1" + $rgname;
4509+
$domainNameLabel2 = "domain2" + $rgname;
4510+
$domainNameLabel3 = "domain3" + $rgname;
4511+
4512+
$vm1 = New-AzVM -ResourceGroupName $rgname -Location $loc -Name $vmname1 -Credential $cred -Zone "2" -Size $vmsize -DomainNameLabel $domainNameLabel1;
4513+
$vm2 = New-AzVM -ResourceGroupName $rgname2 -Location $loc -Name $vmname1 -Credential $cred -Zone "2" -Size $vmsize -DomainNameLabel $domainNameLabel2;
4514+
$vm3 = New-AzVM -ResourceGroupName $rgname2 -Location $loc -Name $vmname3 -Credential $cred -Zone "2" -Size $vmsize -DomainNameLabel $domainNameLabel3;
4515+
4516+
$vms = Get-AzVm -Name $vmname1 -Status;
4517+
4518+
Assert-AreEqual 2 $vms.Count;
4519+
}
4520+
finally
4521+
{
4522+
# Cleanup
4523+
Clean-ResourceGroup $rgname;
4524+
Clean-ResourceGroup $rgname2;
4525+
}
4526+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests/TestVirtualMachineGetVMNameAcrossResourceGroups.json

Lines changed: 10301 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
-->
2222
## Upcoming Release
23+
* Edited Get-AzVm to filter by `-Name` prior to checking for throttling due to too many resources.
2324

2425
## Version 4.6.0
2526
* Added `-VmssId` parameter to `New-AzVm`

src/Compute/Compute/VirtualMachine/Operation/GetAzureVMCommand.cs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2222
using Microsoft.Azure.Management.Compute;
2323
using Microsoft.Azure.Management.Compute.Models;
24+
using Microsoft.Azure.Management.Internal.Resources;
2425
using Microsoft.Rest.Azure;
26+
using Microsoft.Azure.Management.Authorization.Version2015_07_01;
2527

2628
namespace Microsoft.Azure.Commands.Compute
2729
{
@@ -117,7 +119,7 @@ public override void ExecuteCmdlet()
117119
{
118120
ReturnListVMObject(
119121
this.VirtualMachineClient.ListAllWithHttpMessagesAsync().GetAwaiter().GetResult(),
120-
this.VirtualMachineClient.ListAllNextWithHttpMessagesAsync);
122+
this.VirtualMachineClient.ListAllNextWithHttpMessagesAsync);
121123
}
122124
else if (ShouldGetByName(ResourceGroupName, Name))
123125
{
@@ -149,14 +151,51 @@ public override void ExecuteCmdlet()
149151
});
150152
}
151153

154+
private void ReturnListVMObject(AzureOperationResponse<IPage<VirtualMachine>> vmListResult,
155+
Func<string, Dictionary<string, List<string>>, CancellationToken, Task<AzureOperationResponse<IPage<VirtualMachine>>>> listNextFunction)
156+
{
157+
var psResultListStatus = new List<PSVirtualMachineListStatus>();
158+
159+
while (vmListResult != null)
160+
{
161+
psResultListStatus = GetPowerstate(vmListResult, psResultListStatus);
162+
163+
if (!string.IsNullOrEmpty(vmListResult.Body.NextPageLink))
164+
{
165+
vmListResult = listNextFunction(vmListResult.Body.NextPageLink, null, default(CancellationToken)).GetAwaiter().GetResult();
166+
}
167+
else
168+
{
169+
vmListResult = null;
170+
}
171+
}
172+
173+
if (this.Status.IsPresent)
174+
{
175+
WriteObject(TopLevelWildcardFilter(ResourceGroupName, Name, psResultListStatus), true);
176+
}
177+
else
178+
{
179+
var psResultList = new List<PSVirtualMachineList>();
180+
foreach (var item in psResultListStatus)
181+
{
182+
var psItem = ComputeAutoMapperProfile.Mapper.Map<PSVirtualMachineList>(item);
183+
psResultList.Add(psItem);
184+
}
185+
WriteObject(TopLevelWildcardFilter(ResourceGroupName, Name, psResultList), true);
186+
}
187+
}
188+
152189
private List<PSVirtualMachineListStatus> GetPowerstate(
153190
AzureOperationResponse<IPage<VirtualMachine>> vmListResult,
154191
List<PSVirtualMachineListStatus> psResultListStatus)
155192
{
193+
156194
if (vmListResult.Body != null)
157195
{
158196
int vm_count = 0;
159-
foreach (var item in vmListResult.Body)
197+
var filteredList = TopLevelWildcardFilter(ResourceGroupName, Name, resources: vmListResult.Body);
198+
foreach (var item in filteredList)
160199
{
161200
vm_count++;
162201
var psItem = ComputeAutoMapperProfile.Mapper.Map<PSVirtualMachineListStatus>(vmListResult);
@@ -206,39 +245,6 @@ private List<PSVirtualMachineListStatus> GetPowerstate(
206245
return psResultListStatus;
207246
}
208247

209-
private void ReturnListVMObject(AzureOperationResponse<IPage<VirtualMachine>> vmListResult,
210-
Func<string, Dictionary<string, List<string>>, CancellationToken, Task<AzureOperationResponse<IPage<VirtualMachine>>>> listNextFunction)
211-
{
212-
var psResultListStatus = new List<PSVirtualMachineListStatus>();
213-
214-
while (vmListResult != null)
215-
{
216-
psResultListStatus = GetPowerstate(vmListResult, psResultListStatus);
217-
218-
if (!string.IsNullOrEmpty(vmListResult.Body.NextPageLink))
219-
{
220-
vmListResult = listNextFunction(vmListResult.Body.NextPageLink, null, default(CancellationToken)).GetAwaiter().GetResult();
221-
}
222-
else
223-
{
224-
vmListResult = null;
225-
}
226-
}
227-
228-
if (this.Status.IsPresent)
229-
{
230-
WriteObject(TopLevelWildcardFilter(ResourceGroupName, Name, psResultListStatus), true);
231-
}
232-
else
233-
{
234-
var psResultList = new List<PSVirtualMachineList>();
235-
foreach (var item in psResultListStatus)
236-
{
237-
var psItem = ComputeAutoMapperProfile.Mapper.Map<PSVirtualMachineList>(item);
238-
psResultList.Add(psItem);
239-
}
240-
WriteObject(TopLevelWildcardFilter(ResourceGroupName, Name, psResultList), true);
241-
}
242-
}
248+
243249
}
244250
}

0 commit comments

Comments
 (0)