Skip to content

Commit 8646e8a

Browse files
authored
Merge pull request #6860 from ethanmoffat/etmoffat/subnet_better_errormsg
Add a better error message when attempting to get a subnet that doesn't exist
2 parents 34c679e + 1182653 commit 8646e8a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/ResourceManager/Network/Commands.Network.Test/ScenarioTests/VirtualNetworkTests.ps1

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,29 @@ function Test-subnetCRUD
150150
Assert-AreEqual $subnetName $vnetExpected.Subnets[0].Name
151151
Assert-AreEqual $subnet2Name $vnetExpected.Subnets[1].Name
152152
Assert-AreEqual "10.0.3.0/24" $vnetExpected.Subnets[1].AddressPrefix
153-
153+
154154
# Get subnet
155155
$subnet2 = Get-AzureRmvirtualNetwork -Name $vnetName -ResourceGroupName $rgname | Get-AzureRmVirtualNetworkSubnetConfig -Name $subnet2Name
156156
$subnetAll = Get-AzureRmvirtualNetwork -Name $vnetName -ResourceGroupName $rgname | Get-AzureRmVirtualNetworkSubnetConfig
157-
157+
158158
Assert-AreEqual 2 @($subnetAll).Count
159159
Assert-AreEqual $subnetName $subnetAll[0].Name
160160
Assert-AreEqual $subnet2Name $subnetAll[1].Name
161161
Assert-AreEqual $subnet2Name $subnet2.Name
162162

163+
# Get non-existing subnet
164+
try
165+
{
166+
$subnetNotExists = $vnetExpected | Get-AzureRmVirtualNetworkSubnetConfig -Name "Subnet-DoesNotExist"
167+
}
168+
catch
169+
{
170+
if ($_.Exception.GetType() -ne [System.ArgumentException])
171+
{
172+
throw;
173+
}
174+
}
175+
163176
# Remove a subnet
164177
Get-AzureRmvirtualNetwork -Name $vnetName -ResourceGroupName $rgname | Remove-AzureRmVirtualNetworkSubnetConfig -Name $subnet2Name | Set-AzureRmVirtualNetwork
165178

src/ResourceManager/Network/Commands.Network/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Added example for Set-AzureRmVirtualNetworkGatewayConnectionSharedKey
2727
* Added example for Set-AzureRmVirtualNetworkGatewayConnection
2828
* Re-generated cmdlets for ApplicationSecurityGroup, RouteTable and Usage using latest code generator
29+
* Clarified error message for Get-AzureRmVirtualNetworkSubnetConfig when attempting to get a subnet that does not exitc
2930

3031
## Version 6.4.1
3132
* Updated all help files to include full parameter types and correct input/output types.

src/ResourceManager/Network/Commands.Network/VirtualNetwork/Subnet/GetAzureVirtualNetworkSubnetConfigCommand.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Network.Models;
16+
using System;
1617
using System.Linq;
1718
using System.Management.Automation;
1819

@@ -34,14 +35,18 @@ public class GetAzureVirtualNetworkSubnetConfigCommand : NetworkBaseCmdlet
3435

3536
public override void Execute()
3637
{
37-
3838
base.Execute();
3939
if (!string.IsNullOrEmpty(this.Name))
4040
{
4141
var subnet =
42-
this.VirtualNetwork.Subnets.First(
42+
this.VirtualNetwork.Subnets.FirstOrDefault(
4343
resource =>
44-
string.Equals(resource.Name, this.Name, System.StringComparison.CurrentCultureIgnoreCase));
44+
string.Equals(resource.Name, this.Name, StringComparison.CurrentCultureIgnoreCase));
45+
46+
if (subnet == null)
47+
{
48+
throw new ArgumentException(string.Format(Properties.Resources.ResourceNotFound, this.Name));
49+
}
4550

4651
WriteObject(subnet);
4752
}
@@ -50,7 +55,6 @@ public override void Execute()
5055
var subnets = this.VirtualNetwork.Subnets;
5156
WriteObject(subnets, true);
5257
}
53-
5458
}
5559
}
5660
}

0 commit comments

Comments
 (0)