@@ -18,6 +18,7 @@ namespace Microsoft.Azure.PowerShell.Cmdlets.Peering.Common
18
18
using System . Collections . Generic ;
19
19
using System . Linq ;
20
20
using System . Management . Automation ;
21
+ using System . Net ;
21
22
using System . Net . Sockets ;
22
23
23
24
using Microsoft . Azure . Commands . Common . Authentication ;
@@ -184,6 +185,7 @@ public PSPeeringLocation TopSPeeringLocation(object peering)
184
185
/// </exception>
185
186
public bool ValidBandwidth ( int ? bandwidthInMbps )
186
187
{
188
+ this . WriteVerbose ( $ "validating bandwidth: { bandwidthInMbps } ") ;
187
189
if ( bandwidthInMbps <= 0 )
188
190
throw new PSArgumentException ( string . Format ( Resources . Error_BandwidthTooLow , bandwidthInMbps ) ) ;
189
191
if ( bandwidthInMbps % Constants . MinRange != 0 )
@@ -211,6 +213,7 @@ public bool ValidBandwidth(int? bandwidthInMbps)
211
213
/// </exception>
212
214
public bool ValidUpgradeBandwidth ( int ? startingBandwidth , int ? newBandwidth )
213
215
{
216
+ this . WriteVerbose ( $ "Starting bandwidth: { startingBandwidth } new bandwidth: { newBandwidth } ") ;
214
217
if ( ! this . ValidBandwidth ( newBandwidth ) )
215
218
return false ;
216
219
if ( newBandwidth <= ( startingBandwidth ?? 0 ) )
@@ -225,21 +228,40 @@ public bool ValidUpgradeBandwidth(int? startingBandwidth, int? newBandwidth)
225
228
/// <param name="routePrefix">
226
229
/// The route prefix.
227
230
/// </param>
228
- /// <param name="PeeringType ">
231
+ /// <param name="peeringType ">
229
232
/// The InputObject Type.
230
233
/// </param>
231
234
/// <returns>
232
235
/// The <see cref="string"/>.
233
236
/// </returns>
234
- public string ValidatePrefix ( string routePrefix , string PeeringType )
237
+ public string ValidatePrefix ( string routePrefix , string peeringType )
235
238
{
236
239
if ( routePrefix != null )
237
240
{
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
+ }
238
260
var prefix = RoutePrefix . GetValidPrefix ( routePrefix ) ;
239
261
switch ( prefix . PrefixAddressFamily )
240
262
{
241
263
case AddressFamily . InterNetwork :
242
- if ( PeeringType . Equals ( Constants . Exchange , StringComparison . OrdinalIgnoreCase ) )
264
+ if ( peeringType . Equals ( Constants . Exchange , StringComparison . OrdinalIgnoreCase ) )
243
265
{
244
266
if ( prefix . PrefixMaskWidth != 32 )
245
267
{
@@ -259,16 +281,14 @@ public string ValidatePrefix(string routePrefix, string PeeringType)
259
281
var actualPrefixBigInt = prefix . ActualPrefixBigInt ;
260
282
if ( prefix . Length == 4 )
261
283
{
262
- return prefix . StartOfPrefixBigInt + 1 == actualPrefixBigInt
284
+ return prefix . StartOfPrefixBigInt == actualPrefixBigInt
263
285
? routePrefix
264
286
: throw new PSArgumentException (
265
287
string . Format (
266
288
Resources . Error_InvalidPrefixRange ,
267
289
routePrefix ,
268
- ( prefix . StartOfPrefixBigInt + 1 ) . ToIpAddress (
269
- AddressFamily . InterNetwork ) ,
270
- ( prefix . EndOfPrefixBigInt ) . ToIpAddress (
271
- AddressFamily . InterNetwork ) ) ) ;
290
+ ( prefix . StartOfPrefixBigInt ) . ToIpAddress (
291
+ AddressFamily . InterNetwork ) + "/30" ) ) ;
272
292
}
273
293
else if ( prefix . Length == 2 )
274
294
{
@@ -289,7 +309,7 @@ public string ValidatePrefix(string routePrefix, string PeeringType)
289
309
290
310
return routePrefix ;
291
311
case AddressFamily . InterNetworkV6 :
292
- if ( PeeringType . Equals ( Constants . Exchange , StringComparison . OrdinalIgnoreCase ) )
312
+ if ( peeringType . Equals ( Constants . Exchange , StringComparison . OrdinalIgnoreCase ) )
293
313
{
294
314
if ( prefix . PrefixMaskWidth != 128 )
295
315
{
@@ -383,6 +403,7 @@ public string GetAzureRegion(string peeringLocation, string kind)
383
403
{
384
404
if ( location . Name == peeringLocation )
385
405
{
406
+ this . WriteVerbose ( $ "Region: { location . AzureRegion } ") ;
386
407
return location . AzureRegion ;
387
408
}
388
409
}
@@ -391,10 +412,7 @@ public string GetAzureRegion(string peeringLocation, string kind)
391
412
}
392
413
catch ( ErrorResponseException ex )
393
414
{
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 ) ;
398
416
throw new ErrorResponseException ( string . Format ( Resources . Error_CloudError , error . Code , error . Message ) ) ;
399
417
}
400
418
}
@@ -420,26 +438,6 @@ public bool IsValidConnection(PSExchangeConnection connection)
420
438
421
439
if ( connection . BgpSession == null )
422
440
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 ) ) ;
443
441
return true ;
444
442
}
445
443
@@ -461,10 +459,47 @@ public bool IsValidConnection(PSDirectConnection connection)
461
459
throw new PSArgumentException (
462
460
string . Format ( Resources . Error_InvalidFacilityId , connection . PeeringDBFacilityId ) ) ;
463
461
}
464
-
465
462
if ( connection . BgpSession == null )
466
463
throw new PSArgumentNullException ( string . Format ( Resources . Error_NullSession ) ) ;
467
464
return this . ValidBandwidth ( connection . BandwidthInMbps ) ;
468
465
}
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
+ }
469
504
}
470
505
}
0 commit comments