@@ -356,55 +356,57 @@ public bool TryFindContext(IAzureContext context, out string name)
356
356
{
357
357
bool result = false ;
358
358
name = null ;
359
- var foundContext = Contexts . FirstOrDefault ( ( c ) =>
360
- c . Value != null
361
- && (
362
- ( c . Value . Account != null && context . Account != null && string . Equals ( c . Value . Account . Id , context . Account . Id , StringComparison . OrdinalIgnoreCase ) )
363
- || ( c . Value . Account == context . Account ) )
364
- && (
365
- ( c . Value . Tenant != null && context . Tenant != null && c . Value . Tenant . GetId ( ) == context . Tenant . GetId ( ) )
366
- || ( c . Value . Tenant == context . Tenant ) )
367
- && (
368
- ( c . Value . Subscription != null && context . Subscription != null && c . Value . Subscription . GetId ( ) == context . Subscription . GetId ( ) )
369
- || ( c . Value . Subscription == context . Subscription ) ) ) ;
370
- if ( ! string . IsNullOrEmpty ( foundContext . Key ) )
371
- {
372
- name = foundContext . Key ;
373
- result = true ;
359
+ if ( context != null )
360
+ {
361
+ var foundContext = Contexts . FirstOrDefault ( ( c ) =>
362
+ c . Value != null
363
+ && (
364
+ ( c . Value . Account != null && context . Account != null && string . Equals ( c . Value . Account . Id , context . Account . Id , StringComparison . OrdinalIgnoreCase ) )
365
+ || ( c . Value . Account == context . Account ) )
366
+ && (
367
+ ( c . Value . Tenant != null && context . Tenant != null && c . Value . Tenant . GetId ( ) == context . Tenant . GetId ( ) )
368
+ || ( c . Value . Tenant == context . Tenant ) )
369
+ && (
370
+ ( c . Value . Subscription != null && context . Subscription != null && c . Value . Subscription . GetId ( ) == context . Subscription . GetId ( ) )
371
+ || ( c . Value . Subscription == context . Subscription ) ) ) ;
372
+ if ( ! string . IsNullOrEmpty ( foundContext . Key ) )
373
+ {
374
+ name = foundContext . Key ;
375
+ result = true ;
376
+ }
374
377
}
375
378
376
379
return result ;
377
380
}
378
381
379
382
public bool TryGetContextName ( IAzureContext context , out string name )
380
383
{
381
- if ( context == null )
382
- {
383
- throw new ArgumentNullException ( nameof ( context ) ) ;
384
- }
385
-
386
384
bool result = false ;
387
385
name = null ;
388
- if ( ( context . Account != null && ! string . IsNullOrWhiteSpace ( context . Account . Id ) ) || context . Subscription != null )
386
+ if ( context != null )
389
387
{
390
- List < string > components = new List < string > ( ) ;
391
- if ( context . Account != null && ! string . IsNullOrWhiteSpace ( context . Account . Id ) )
388
+
389
+ if ( ( context . Account != null && ! string . IsNullOrWhiteSpace ( context . Account . Id ) ) || context . Subscription != null )
392
390
{
393
- components . Add ( context . Account . Id ) ;
391
+ List < string > components = new List < string > ( ) ;
392
+ if ( context . Account != null && ! string . IsNullOrWhiteSpace ( context . Account . Id ) )
393
+ {
394
+ components . Add ( context . Account . Id ) ;
395
+ }
396
+
397
+ if ( context . Subscription != null )
398
+ {
399
+ components . Add ( context . Subscription . GetId ( ) . ToString ( ) ) ;
400
+ }
401
+
402
+ name = string . Format ( "[{0}]" , string . Join ( ", " , components ) ) ;
403
+ result = true ;
394
404
}
395
-
396
- if ( context . Subscription != null )
405
+ else
397
406
{
398
- components . Add ( context . Subscription . GetId ( ) . ToString ( ) ) ;
407
+ name = "Default" ;
408
+ result = true ;
399
409
}
400
-
401
- name = string . Format ( "[{0}]" , string . Join ( ", " , components ) ) ;
402
- result = true ;
403
- }
404
- else
405
- {
406
- name = "Default" ;
407
- result = true ;
408
410
}
409
411
410
412
return result ;
@@ -478,7 +480,8 @@ public bool TrySetDefaultContext(IAzureContext context)
478
480
{
479
481
bool result = false ;
480
482
string contextName ;
481
- if ( TryFindContext ( context , out contextName ) || TryAddContext ( context , out contextName ) )
483
+
484
+ if ( context != null && ( TryFindContext ( context , out contextName ) || TryAddContext ( context , out contextName ) ) )
482
485
{
483
486
result = TrySetDefaultContext ( contextName ) ;
484
487
}
@@ -489,13 +492,16 @@ public bool TrySetDefaultContext(IAzureContext context)
489
492
public bool TrySetDefaultContext ( string name , IAzureContext context )
490
493
{
491
494
bool result = false ;
492
- if ( string . IsNullOrWhiteSpace ( name ) )
495
+ if ( context != null )
493
496
{
494
- result = TrySetDefaultContext ( context ) ;
495
- }
496
- else if ( TrySetContext ( name , context ) )
497
- {
498
- result = TrySetDefaultContext ( name ) ;
497
+ if ( string . IsNullOrWhiteSpace ( name ) )
498
+ {
499
+ result = TrySetDefaultContext ( context ) ;
500
+ }
501
+ else if ( TrySetContext ( name , context ) )
502
+ {
503
+ result = TrySetDefaultContext ( name ) ;
504
+ }
499
505
}
500
506
501
507
return result ;
@@ -562,6 +568,11 @@ public bool TryCopyProfile(AzureRmProfile other)
562
568
TrySetContext ( context . Key , context . Value ) ;
563
569
}
564
570
571
+ if ( other . DefaultContext != null )
572
+ {
573
+ this . TrySetDefaultContext ( other . DefaultContext ) ;
574
+ }
575
+
565
576
this . CopyPropertiesFrom ( other ) ;
566
577
return true ;
567
578
}
0 commit comments