Skip to content

Commit e2956c8

Browse files
committed
Improve exception handling in Cortex cmdlets
1 parent f0bc23a commit e2956c8

File tree

10 files changed

+14509
-10223
lines changed

10 files changed

+14509
-10223
lines changed

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.CortexTests/TestCortexCRUD.json

Lines changed: 8065 additions & 6913 deletions
Large diffs are not rendered by default.

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.CortexTests/TestCortexExpressRouteCRUD.json

Lines changed: 6395 additions & 3230 deletions
Large diffs are not rendered by default.

src/Network/Network/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
--->
2020

2121
## Upcoming Release
22+
* Improve handling of exceptions in Cortex cmdlets
2223
* Fix incorrect example in `New-AzApplicationGateway` reference documentation
2324
* Add note in `Get-AzNetworkWatcherPacketCapture` reference documentation about retrieving all properties for a packet capture
2425
* Fixed example in `Test-AzNetworkWatcherIPFlow` reference documentation to correctly enumerate NICs

src/Network/Network/Common/NetworkBaseCmdlet.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,39 @@ public static string GetResourceGroup(string resourceId)
8181

8282
return resourceId.Substring(startIndex, endIndex - startIndex);
8383
}
84+
85+
public static bool IsResourcePresent(Action fn)
86+
{
87+
try
88+
{
89+
fn();
90+
}
91+
catch (Rest.Azure.CloudException exception)
92+
{
93+
if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
94+
{
95+
return false;
96+
}
97+
throw;
98+
}
99+
catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
100+
{
101+
if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
102+
{
103+
return false;
104+
}
105+
throw;
106+
}
107+
catch (Microsoft.Azure.Management.Network.Models.ErrorResponseException exception)
108+
{
109+
if (exception.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
110+
{
111+
return false;
112+
}
113+
throw;
114+
}
115+
116+
return true;
117+
}
84118
}
85119
}

src/Network/Network/Cortex/HubVnetConnection/HubVnetConnectionBaseCmdlet.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,7 @@ public List<PSHubVirtualNetworkConnection> ListHubVnetConnections(string resourc
6565

6666
public bool IsHubVnetConnectionPresent(string resourceGroupName, string parentHubName, string name)
6767
{
68-
try
69-
{
70-
this.GetHubVirtualNetworkConnection(resourceGroupName, parentHubName, name);
71-
}
72-
catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
73-
{
74-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
75-
{
76-
// Resource is not present
77-
return false;
78-
}
79-
}
80-
81-
return true;
68+
return NetworkBaseCmdlet.IsResourcePresent(() => { GetHubVirtualNetworkConnection(resourceGroupName, parentHubName, name); });
8269
}
8370
}
8471
}

src/Network/Network/Cortex/VirtualHub/VirtualHubBaseCmdlet.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,23 @@ public PSVirtualHub ToPsVirtualHub(Management.Network.Models.VirtualHub virtualH
4545

4646
public PSVirtualHub GetVirtualHub(string resourceGroupName, string name)
4747
{
48-
MNM.VirtualHub virtualHub = null;
49-
PSVirtualHub psVirtualHub = null;
50-
5148
try
5249
{
5350
//// The following code will throw if resource is not found
54-
virtualHub = this.VirtualHubClient.Get(resourceGroupName, name);
51+
MNM.VirtualHub virtualHub = this.VirtualHubClient.Get(resourceGroupName, name);
5552

56-
psVirtualHub = ToPsVirtualHub(virtualHub);
53+
PSVirtualHub psVirtualHub = ToPsVirtualHub(virtualHub);
5754
psVirtualHub.ResourceGroupName = resourceGroupName;
55+
return psVirtualHub;
5856
}
59-
catch (Microsoft.Azure.Management.Network.Models.ErrorException)
57+
catch (Exception ex)
6058
{
61-
// Resource is not present
62-
return psVirtualHub;
59+
if (ex is Microsoft.Azure.Management.Network.Models.ErrorException || ex is Rest.Azure.CloudException)
60+
{
61+
return null;
62+
}
63+
throw;
6364
}
64-
65-
return psVirtualHub;
6665
}
6766

6867
public bool IsVirtualHubPresent(string resourceGroupName, string name)

src/Network/Network/Cortex/VirtualWan/VirtualWanBaseCmdlet.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,7 @@ public List<PSVirtualWan> ListVirtualWans(string resourceGroupName)
107107

108108
public bool IsVirtualWanPresent(string resourceGroupName, string name)
109109
{
110-
try
111-
{
112-
GetVirtualWan(resourceGroupName, name);
113-
}
114-
catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
115-
{
116-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
117-
{
118-
// Resource is not present
119-
return false;
120-
}
121-
}
122-
123-
return true;
110+
return NetworkBaseCmdlet.IsResourcePresent(() => { GetVirtualWan(resourceGroupName, name); });
124111
}
125112
}
126113
}

src/Network/Network/Cortex/VpnConnection/VpnConnectionBaseCmdlet.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,7 @@ public List<PSVpnConnection> ListVpnConnections(string resourceGroupName, string
6363

6464
public bool IsVpnConnectionPresent(string resourceGroupName, string parentVpnGatewayName, string name)
6565
{
66-
try
67-
{
68-
this.GetVpnConnection(resourceGroupName, parentVpnGatewayName, name);
69-
}
70-
catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
71-
{
72-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
73-
{
74-
// Resource is not present
75-
return false;
76-
}
77-
}
78-
79-
return true;
66+
return NetworkBaseCmdlet.IsResourcePresent(() => { GetVpnConnection(resourceGroupName, parentVpnGatewayName, name); });
8067
}
8168
}
8269
}

src/Network/Network/Cortex/VpnGateway/VpnGatewayBaseCmdlet.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,7 @@ public PSVpnGateway CreateOrUpdateVpnGateway(string resourceGroupName, string vp
8686

8787
public bool IsVpnGatewayPresent(string resourceGroupName, string name)
8888
{
89-
try
90-
{
91-
GetVpnGateway(resourceGroupName, name);
92-
}
93-
catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
94-
{
95-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
96-
{
97-
// Resource is not present
98-
return false;
99-
}
100-
}
101-
102-
return true;
89+
return NetworkBaseCmdlet.IsResourcePresent(() => { GetVpnGateway(resourceGroupName, name); });
10390
}
10491
}
10592
}

src/Network/Network/Cortex/VpnSite/VpnSiteBaseCmdlet.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,7 @@ public List<PSVpnSite> ListVpnSites(string resourceGroupName)
113113

114114
public bool IsVpnSitePresent(string resourceGroupName, string name)
115115
{
116-
try
117-
{
118-
GetVpnSite(resourceGroupName, name);
119-
}
120-
catch (Microsoft.Azure.Management.Network.Models.ErrorException exception)
121-
{
122-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
123-
{
124-
// Resource is not present
125-
return false;
126-
}
127-
}
128-
129-
return true;
116+
return NetworkBaseCmdlet.IsResourcePresent( () => { GetVpnSite(resourceGroupName, name); } );
130117
}
131118
}
132119
}

0 commit comments

Comments
 (0)