Skip to content

Commit ae7607e

Browse files
authored
Merge pull request #9403 from rileymckenna/preview
Added Better Error Handling
2 parents 78da0d0 + a2416b9 commit ae7607e

16 files changed

+161
-136
lines changed

src/Peering/Peering.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScenarioTest.ResourceManage
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFx", "..\..\tools\TestFx\TestFx.csproj", "{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}"
2323
EndProject
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Resources", "..\Resources\Resources\Resources.csproj", "{E1F5201D-6067-430E-B303-4E367652991B}"
25+
EndProject
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResourceManager", "..\Resources\ResourceManager\ResourceManager.csproj", "{8058D403-06E3-4BED-8924-D166CE303961}"
27+
EndProject
28+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tags", "..\Resources\Tags\Tags.csproj", "{6BD4D521-DAFB-472B-A803-81F053AB1396}"
29+
EndProject
2430
Global
2531
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2632
Debug|Any CPU = Debug|Any CPU
@@ -55,6 +61,18 @@ Global
5561
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Debug|Any CPU.Build.0 = Debug|Any CPU
5662
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Release|Any CPU.ActiveCfg = Release|Any CPU
5763
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{E1F5201D-6067-430E-B303-4E367652991B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{E1F5201D-6067-430E-B303-4E367652991B}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{E1F5201D-6067-430E-B303-4E367652991B}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{E1F5201D-6067-430E-B303-4E367652991B}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{8058D403-06E3-4BED-8924-D166CE303961}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{8058D403-06E3-4BED-8924-D166CE303961}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{8058D403-06E3-4BED-8924-D166CE303961}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{8058D403-06E3-4BED-8924-D166CE303961}.Release|Any CPU.Build.0 = Release|Any CPU
72+
{6BD4D521-DAFB-472B-A803-81F053AB1396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
73+
{6BD4D521-DAFB-472B-A803-81F053AB1396}.Debug|Any CPU.Build.0 = Debug|Any CPU
74+
{6BD4D521-DAFB-472B-A803-81F053AB1396}.Release|Any CPU.ActiveCfg = Release|Any CPU
75+
{6BD4D521-DAFB-472B-A803-81F053AB1396}.Release|Any CPU.Build.0 = Release|Any CPU
5876
EndGlobalSection
5977
GlobalSection(SolutionProperties) = preSolution
6078
HideSolutionNode = FALSE

src/Peering/Peering/Az.Peering.psd1

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,25 @@ NestedModules = @('.\Microsoft.Azure.PowerShell.Cmdlets.Peering.dll')
7474
FunctionsToExport = @()
7575

7676
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
77-
CmdletsToExport = 'Get-AzPeering', 'Get-AzPeerAsn', 'New-AzPeerAsn', 'New-AzPeering',
78-
'Update-AzPeering', 'Set-AzPeeringExchangeConnectionObject',
79-
'New-AzPeeringExchangeConnectionObject',
80-
'Set-AzPeeringDirectConnectionObject',
81-
'New-AzPeeringDirectConnectionObject', 'Get-AzPeeringLocation',
82-
'Get-AzLegacyPeering', 'Set-AzPeerAsn', 'Remove-AzPeerAsn'
77+
CmdletsToExport = 'Get-AzPeering',
78+
'Get-AzPeerAsn',
79+
'New-AzPeerAsn',
80+
'New-AzPeering',
81+
'Update-AzPeering',
82+
'Set-AzPeeringExchangeConnectionObject',
83+
'New-AzPeeringExchangeConnectionObject',
84+
'Set-AzPeeringDirectConnectionObject',
85+
'New-AzPeeringDirectConnectionObject',
86+
'Get-AzPeeringLocation',
87+
'Get-AzLegacyPeering',
88+
'Set-AzPeerAsn',
89+
'Remove-AzPeerAsn'
8390

8491
# Variables to export from this module
8592
# VariablesToExport = @()
8693

8794
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
88-
AliasesToExport = @()
95+
#AliasesToExport = @()
8996

9097
# DSC resources to export from this module
9198
# DscResourcesToExport = @()
@@ -102,7 +109,7 @@ PrivateData = @{
102109
PSData = @{
103110

104111
# Tags applied to this module. These help with module discovery in online galleries.
105-
Tags = 'Azure','ARM','Network','Peering','PSModule','Exchange','Direct','PNI','Peer','Asn'
112+
Tags = 'Azure','ARM','Network','Peering','PSModule', 'Exchange', 'Direct', 'PNI', 'Peer', 'Asn'
106113

107114
# A URL to the license for this module.
108115
LicenseUri = 'https://aka.ms/azps-license'

src/Peering/Peering/ChangeLog.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21-
21+
* Bug fix in IP Prefix validation to allow 0/30 for DirectConnection
22+
* Bug fix to handle error messages from ARM and ERM
23+
* Added Verbose comments for debugging
2224
## Version 0.1.1
2325
* Update version of AutoMapper library used
2426
* Fix case sensitivity bug in `Get-AzPeeringLocation` for `-Kind` parameter
25-
2627
## Version 0.1.0
27-
* Preview release for Microsoft Peering Service module
28+
* Preview release for Microsoft Peering Service module

src/Peering/Peering/Common/PeeringBaseCmdlet.cs

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Azure.PowerShell.Cmdlets.Peering.Common
1818
using System.Collections.Generic;
1919
using System.Linq;
2020
using System.Management.Automation;
21+
using System.Net;
2122
using System.Net.Sockets;
2223

2324
using Microsoft.Azure.Commands.Common.Authentication;
@@ -184,6 +185,7 @@ public PSPeeringLocation TopSPeeringLocation(object peering)
184185
/// </exception>
185186
public bool ValidBandwidth(int? bandwidthInMbps)
186187
{
188+
this.WriteVerbose($"validating bandwidth: {bandwidthInMbps}");
187189
if (bandwidthInMbps <= 0)
188190
throw new PSArgumentException(string.Format(Resources.Error_BandwidthTooLow, bandwidthInMbps));
189191
if (bandwidthInMbps % Constants.MinRange != 0)
@@ -211,6 +213,7 @@ public bool ValidBandwidth(int? bandwidthInMbps)
211213
/// </exception>
212214
public bool ValidUpgradeBandwidth(int? startingBandwidth, int? newBandwidth)
213215
{
216+
this.WriteVerbose($"Starting bandwidth: {startingBandwidth} new bandwidth: {newBandwidth}");
214217
if (!this.ValidBandwidth(newBandwidth))
215218
return false;
216219
if (newBandwidth <= (startingBandwidth ?? 0))
@@ -225,21 +228,40 @@ public bool ValidUpgradeBandwidth(int? startingBandwidth, int? newBandwidth)
225228
/// <param name="routePrefix">
226229
/// The route prefix.
227230
/// </param>
228-
/// <param name="PeeringType">
231+
/// <param name="peeringType">
229232
/// The InputObject Type.
230233
/// </param>
231234
/// <returns>
232235
/// The <see cref="string"/>.
233236
/// </returns>
234-
public string ValidatePrefix(string routePrefix, string PeeringType)
237+
public string ValidatePrefix(string routePrefix, string peeringType)
235238
{
236239
if (routePrefix != null)
237240
{
241+
this.WriteVerbose($"Validating route prefix: {routePrefix} for PeeringType:{peeringType}");
242+
if (peeringType.Equals(Constants.Exchange))
243+
{
244+
if (routePrefix.Split('/').Length != 2) {
245+
// Prefix if missing
246+
var newRouteWithPrefix = IPAddress.Parse(routePrefix);
247+
if(newRouteWithPrefix.AddressFamily == AddressFamily.InterNetwork)
248+
{
249+
routePrefix += "/32";
250+
this.WriteVerbose($"Validating route prefix: {routePrefix} for PeeringType:{peeringType}");
251+
}
252+
if(newRouteWithPrefix.AddressFamily == AddressFamily.InterNetworkV6)
253+
{
254+
routePrefix += "/128";
255+
this.WriteVerbose($"Validating route prefix: {routePrefix} for PeeringType:{peeringType}");
256+
}
257+
258+
}
259+
}
238260
var prefix = RoutePrefix.GetValidPrefix(routePrefix);
239261
switch (prefix.PrefixAddressFamily)
240262
{
241263
case AddressFamily.InterNetwork:
242-
if (PeeringType.Equals(Constants.Exchange, StringComparison.OrdinalIgnoreCase))
264+
if (peeringType.Equals(Constants.Exchange, StringComparison.OrdinalIgnoreCase))
243265
{
244266
if (prefix.PrefixMaskWidth != 32)
245267
{
@@ -259,16 +281,14 @@ public string ValidatePrefix(string routePrefix, string PeeringType)
259281
var actualPrefixBigInt = prefix.ActualPrefixBigInt;
260282
if (prefix.Length == 4)
261283
{
262-
return prefix.StartOfPrefixBigInt + 1 == actualPrefixBigInt
284+
return prefix.StartOfPrefixBigInt == actualPrefixBigInt
263285
? routePrefix
264286
: throw new PSArgumentException(
265287
string.Format(
266288
Resources.Error_InvalidPrefixRange,
267289
routePrefix,
268-
(prefix.StartOfPrefixBigInt + 1).ToIpAddress(
269-
AddressFamily.InterNetwork),
270-
(prefix.EndOfPrefixBigInt).ToIpAddress(
271-
AddressFamily.InterNetwork)));
290+
(prefix.StartOfPrefixBigInt).ToIpAddress(
291+
AddressFamily.InterNetwork) + "/30"));
272292
}
273293
else if (prefix.Length == 2)
274294
{
@@ -289,7 +309,7 @@ public string ValidatePrefix(string routePrefix, string PeeringType)
289309

290310
return routePrefix;
291311
case AddressFamily.InterNetworkV6:
292-
if (PeeringType.Equals(Constants.Exchange, StringComparison.OrdinalIgnoreCase))
312+
if (peeringType.Equals(Constants.Exchange, StringComparison.OrdinalIgnoreCase))
293313
{
294314
if (prefix.PrefixMaskWidth != 128)
295315
{
@@ -383,6 +403,7 @@ public string GetAzureRegion(string peeringLocation, string kind)
383403
{
384404
if (location.Name == peeringLocation)
385405
{
406+
this.WriteVerbose($"Region: {location.AzureRegion}");
386407
return location.AzureRegion;
387408
}
388409
}
@@ -391,10 +412,7 @@ public string GetAzureRegion(string peeringLocation, string kind)
391412
}
392413
catch (ErrorResponseException ex)
393414
{
394-
var error = ex.Response.Content.Contains("\"error\": \"")
395-
? JsonConvert.DeserializeObject<Dictionary<string, ErrorResponse>>(ex.Response.Content)
396-
.FirstOrDefault().Value
397-
: JsonConvert.DeserializeObject<ErrorResponse>(ex.Response.Content);
415+
var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
398416
throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
399417
}
400418
}
@@ -420,26 +438,6 @@ public bool IsValidConnection(PSExchangeConnection connection)
420438

421439
if (connection.BgpSession == null)
422440
throw new PSArgumentNullException(string.Format(Resources.Error_NullSession));
423-
if (connection.BgpSession.PeerSessionIPv4Address == null
424-
&& connection.BgpSession.MaxPrefixesAdvertisedV4 != null)
425-
throw new PSArgumentException(
426-
string.Format(Resources.Error_MustBeNull, "4", connection.BgpSession.MaxPrefixesAdvertisedV4));
427-
if (connection.BgpSession.PeerSessionIPv6Address == null
428-
&& connection.BgpSession.MaxPrefixesAdvertisedV6 != null)
429-
throw new PSArgumentException(
430-
string.Format(Resources.Error_MustBeNull, "6", connection.BgpSession.MaxPrefixesAdvertisedV6));
431-
if (connection.BgpSession.MaxPrefixesAdvertisedV4 <= 0 && connection.BgpSession.SessionPrefixV4 != null)
432-
throw new PSArgumentException(
433-
string.Format(
434-
Resources.Error_MustBeGreaterThanZero,
435-
"4",
436-
connection.BgpSession.MaxPrefixesAdvertisedV4));
437-
if (connection.BgpSession.MaxPrefixesAdvertisedV6 <= 0 && connection.BgpSession.SessionPrefixV6 != null)
438-
throw new PSArgumentException(
439-
string.Format(
440-
Resources.Error_MustBeGreaterThanZero,
441-
"6",
442-
connection.BgpSession.MaxPrefixesAdvertisedV6));
443441
return true;
444442
}
445443

@@ -461,10 +459,47 @@ public bool IsValidConnection(PSDirectConnection connection)
461459
throw new PSArgumentException(
462460
string.Format(Resources.Error_InvalidFacilityId, connection.PeeringDBFacilityId));
463461
}
464-
465462
if (connection.BgpSession == null)
466463
throw new PSArgumentNullException(string.Format(Resources.Error_NullSession));
467464
return this.ValidBandwidth(connection.BandwidthInMbps);
468465
}
466+
467+
/// <summary>
468+
/// The get error code and message from arm or erm.
469+
/// </summary>
470+
/// <param name="ex">
471+
/// The ex.
472+
/// </param>
473+
/// <returns>
474+
/// The <see cref="ErrorResponse"/>.
475+
/// </returns>
476+
public ErrorResponse GetErrorCodeAndMessageFromArmOrErm(ErrorResponseException ex)
477+
{
478+
ErrorResponse error = null;
479+
try
480+
{
481+
var armError = JsonConvert.DeserializeObject<Dictionary<string, ErrorResponse>>(ex.Response.Content);
482+
if (armError.Values.FirstOrDefault()?.Code != null)
483+
{
484+
error = new ErrorResponse(code: armError.Values.FirstOrDefault()?.Code, message: armError.Values.FirstOrDefault()?.Message);
485+
}
486+
}
487+
catch
488+
{
489+
try
490+
{
491+
var ermError = JsonConvert.DeserializeObject<ErrorResponse>(ex.Response.Content);
492+
if (ermError.Code != null)
493+
{
494+
error = new ErrorResponse(code: ermError.Code, message: ermError.Message);
495+
}
496+
}
497+
catch
498+
{
499+
throw ex;
500+
}
501+
}
502+
return error;
503+
}
469504
}
470505
}

src/Peering/Peering/Direct/SetAzureDirectPeeringConnectionCommand.cs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public override void Execute()
163163
}
164164
catch (ErrorResponseException ex)
165165
{
166-
var error = ex.Response.Content.Contains("\"error\\\":") ? JsonConvert.DeserializeObject<Dictionary<string, ErrorResponse>>(JsonConvert.DeserializeObject(ex.Response.Content).ToString()).FirstOrDefault().Value : JsonConvert.DeserializeObject<ErrorResponse>(ex.Response.Content);
166+
var error = this.GetErrorCodeAndMessageFromArmOrErm(ex);
167167
throw new ErrorResponseException(string.Format(Resources.Error_CloudError, error.Code, error.Message));
168168
}
169169
}
@@ -176,15 +176,12 @@ public override void Execute()
176176
/// </returns>
177177
private PSDirectConnection UpdatePeeringOffer()
178178
{
179-
if (this.InputObject is PSDirectConnection inputObject)
180-
{
181-
inputObject.BandwidthInMbps =
182-
this.ValidUpgradeBandwidth(inputObject.BandwidthInMbps, this.BandwidthInMbps)
179+
this.InputObject.BandwidthInMbps =
180+
this.ValidUpgradeBandwidth(this.InputObject.BandwidthInMbps, this.BandwidthInMbps)
183181
? this.BandwidthInMbps
184-
: inputObject.BandwidthInMbps;
185-
if (this.IsValidConnection(inputObject))
186-
return inputObject;
187-
}
182+
: this.InputObject.BandwidthInMbps;
183+
if (this.IsValidConnection(this.InputObject))
184+
return this.InputObject;
188185

189186
throw new InvalidOperationException(string.Format(Resources.Error_WrongCommandForDirectObject));
190187
}
@@ -197,12 +194,9 @@ private PSDirectConnection UpdatePeeringOffer()
197194
/// </returns>
198195
private PSDirectConnection UpdateMD5Authentication()
199196
{
200-
if (this.InputObject is PSDirectConnection inputObject)
201-
{
202-
inputObject.BgpSession.Md5AuthenticationKey = this.MD5AuthenticationKey;
203-
if (this.IsValidConnection(inputObject))
204-
return inputObject;
205-
}
197+
this.InputObject.BgpSession.Md5AuthenticationKey = this.MD5AuthenticationKey;
198+
if (this.IsValidConnection(this.InputObject))
199+
return this.InputObject;
206200

207201
throw new InvalidOperationException(string.Format(Resources.Error_WrongCommandForDirectObject));
208202
}
@@ -215,14 +209,11 @@ private PSDirectConnection UpdateMD5Authentication()
215209
/// </returns>
216210
private PSDirectConnection UpdateIpV4Prefix()
217211
{
218-
if (this.InputObject is PSDirectConnection inputObject)
219-
{
220-
inputObject.BgpSession.MaxPrefixesAdvertisedV4 =
221-
this.MaxPrefixesAdvertisedIPv4 == null ? inputObject.BgpSession.MaxPrefixesAdvertisedV4 : 20000;
222-
inputObject.BgpSession.SessionPrefixV4 = this.ValidatePrefix(this.SessionPrefixV4, Constants.Direct);
223-
if (this.IsValidConnection(inputObject))
224-
return inputObject;
225-
}
212+
this.InputObject.BgpSession.MaxPrefixesAdvertisedV4 =
213+
this.MaxPrefixesAdvertisedIPv4 == null ? this.InputObject.BgpSession.MaxPrefixesAdvertisedV4 : 20000;
214+
this.InputObject.BgpSession.SessionPrefixV4 = this.ValidatePrefix(this.SessionPrefixV4, Constants.Direct);
215+
if (this.IsValidConnection(this.InputObject))
216+
return this.InputObject;
226217

227218
throw new InvalidOperationException(string.Format(Resources.Error_WrongCommandForDirectObject));
228219
}
@@ -235,14 +226,11 @@ private PSDirectConnection UpdateIpV4Prefix()
235226
/// </returns>
236227
private PSDirectConnection UpdateIpV6Prefix()
237228
{
238-
if (this.InputObject is PSDirectConnection inputObject)
239-
{
240-
inputObject.BgpSession.MaxPrefixesAdvertisedV6 =
241-
this.MaxPrefixesAdvertisedIPv6 == null ? inputObject.BgpSession.MaxPrefixesAdvertisedV6 : 2000;
242-
inputObject.BgpSession.SessionPrefixV6 = this.ValidatePrefix(this.SessionPrefixV6, Constants.Direct);
243-
if (this.IsValidConnection(inputObject))
244-
return inputObject;
245-
}
229+
this.InputObject.BgpSession.MaxPrefixesAdvertisedV6 =
230+
this.MaxPrefixesAdvertisedIPv6 == null ? this.InputObject.BgpSession.MaxPrefixesAdvertisedV6 : 2000;
231+
this.InputObject.BgpSession.SessionPrefixV6 = this.ValidatePrefix(this.SessionPrefixV6, Constants.Direct);
232+
if (this.IsValidConnection(this.InputObject))
233+
return this.InputObject;
246234

247235
throw new InvalidOperationException(string.Format(Resources.Error_WrongCommandForDirectObject));
248236
}

0 commit comments

Comments
 (0)