Skip to content

Commit 5e57026

Browse files
committed
Fix piping issues for context cmdlets, fix minor clear and remove issues, regenerate help
1 parent 448f36c commit 5e57026

26 files changed

+403
-159
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/ContextCmdletTests.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using Microsoft.Azure.Commands.Profile.Context;
3030
using System.Linq;
3131
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
32+
using Microsoft.Azure.Commands.Profile.Common;
3233

3334
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
3435
{
@@ -227,7 +228,7 @@ public void RemoveDefaultContext()
227228
cmdlet.CommandRuntime = commandRuntimeMock;
228229
cmdlet.DefaultProfile = profile;
229230
cmdlet.Force = true;
230-
cmdlet.PassThrough = true;
231+
cmdlet.PassThru = true;
231232
cmdlet.MyInvocation.BoundParameters.Add("Name", profile.DefaultContextKey);
232233
cmdlet.InvokeBeginProcessing();
233234
cmdlet.ExecuteCmdlet();
@@ -254,7 +255,7 @@ public void RemoveNonDefaultContext()
254255
var defaultContextKey = profile.DefaultContextKey;
255256
cmdlet.CommandRuntime = commandRuntimeMock;
256257
cmdlet.DefaultProfile = profile;
257-
cmdlet.PassThrough = true;
258+
cmdlet.PassThru = true;
258259
cmdlet.MyInvocation.BoundParameters.Add("Name", removedContextKey);
259260
cmdlet.InvokeBeginProcessing();
260261
cmdlet.ExecuteCmdlet();
@@ -280,7 +281,7 @@ public void RemoveNonExistentContext()
280281
var contextCount = profile.Contexts.Count;
281282
cmdlet.CommandRuntime = commandRuntimeMock;
282283
cmdlet.DefaultProfile = profile;
283-
cmdlet.PassThrough = true;
284+
cmdlet.PassThru = true;
284285
cmdlet.MyInvocation.BoundParameters.Add("Name", removedContextKey);
285286
cmdlet.InvokeBeginProcessing();
286287
cmdlet.ExecuteCmdlet();
@@ -301,7 +302,7 @@ public void RemoveContextNoLogin()
301302
var contextCount = profile.Contexts.Count;
302303
cmdlet.CommandRuntime = commandRuntimeMock;
303304
cmdlet.DefaultProfile = profile;
304-
cmdlet.PassThrough = true;
305+
cmdlet.PassThru = true;
305306
cmdlet.MyInvocation.BoundParameters.Add("Name", removedContextKey);
306307
cmdlet.InvokeBeginProcessing();
307308
cmdlet.ExecuteCmdlet();
@@ -403,7 +404,7 @@ public void RenameDefaultContext()
403404
cmdlet.DefaultProfile = profile;
404405
cmdlet.MyInvocation.BoundParameters.Add("SourceName", profile.DefaultContextKey);
405406
cmdlet.MyInvocation.BoundParameters.Add("TargetName", newContextName);
406-
cmdlet.PassThrough = true;
407+
cmdlet.PassThru = true;
407408
cmdlet.InvokeBeginProcessing();
408409
cmdlet.ExecuteCmdlet();
409410
cmdlet.InvokeEndProcessing();
@@ -436,7 +437,7 @@ public void RenameNonDefaultContext()
436437
cmdlet.DefaultProfile = profile;
437438
cmdlet.MyInvocation.BoundParameters.Add("SourceName", contextNameToRename);
438439
cmdlet.MyInvocation.BoundParameters.Add("TargetName", newContextName);
439-
cmdlet.PassThrough = true;
440+
cmdlet.PassThru = true;
440441
cmdlet.InvokeBeginProcessing();
441442
cmdlet.ExecuteCmdlet();
442443
cmdlet.InvokeEndProcessing();
@@ -470,7 +471,7 @@ public void RenameOverwritesDefaultContext()
470471
cmdlet.DefaultProfile = profile;
471472
cmdlet.MyInvocation.BoundParameters.Add("SourceName", contextNameToRename);
472473
cmdlet.MyInvocation.BoundParameters.Add("TargetName", newContextName);
473-
cmdlet.PassThrough = true;
474+
cmdlet.PassThru = true;
474475
cmdlet.Force = true;
475476
cmdlet.InvokeBeginProcessing();
476477
cmdlet.ExecuteCmdlet();
@@ -504,7 +505,7 @@ public void RenameNonExistentContext()
504505
cmdlet.DefaultProfile = profile;
505506
cmdlet.MyInvocation.BoundParameters.Add("SourceName", "This context does not exist");
506507
cmdlet.MyInvocation.BoundParameters.Add("TargetName", contextNameToRename);
507-
cmdlet.PassThrough = true;
508+
cmdlet.PassThru = true;
508509
cmdlet.InvokeBeginProcessing();
509510
cmdlet.ExecuteCmdlet();
510511
cmdlet.InvokeEndProcessing();
@@ -534,7 +535,7 @@ public void RenameContextSameName()
534535
cmdlet.DefaultProfile = profile;
535536
cmdlet.MyInvocation.BoundParameters.Add("SourceName", contextNameToRename);
536537
cmdlet.MyInvocation.BoundParameters.Add("TargetName", newContextName);
537-
cmdlet.PassThrough = true;
538+
cmdlet.PassThru = true;
538539
cmdlet.InvokeBeginProcessing();
539540
cmdlet.ExecuteCmdlet();
540541
cmdlet.InvokeEndProcessing();
@@ -560,7 +561,7 @@ public void RenameContextNoLogin()
560561
cmdlet.DefaultProfile = profile;
561562
cmdlet.MyInvocation.BoundParameters.Add("SourceName", "Default");
562563
cmdlet.MyInvocation.BoundParameters.Add("TargetName", "target context");
563-
cmdlet.PassThrough = true;
564+
cmdlet.PassThru = true;
564565
cmdlet.InvokeBeginProcessing();
565566
cmdlet.ExecuteCmdlet();
566567
cmdlet.InvokeEndProcessing();
@@ -580,7 +581,8 @@ public void ClearMultipleContexts()
580581
var defaultContext = profile.DefaultContext;
581582
cmdlet.CommandRuntime = commandRuntimeMock;
582583
cmdlet.DefaultProfile = profile;
583-
cmdlet.PassThrough = true;
584+
cmdlet.Scope = ContextModificationScope.Process;
585+
cmdlet.PassThru = true;
584586
cmdlet.InvokeBeginProcessing();
585587
cmdlet.ExecuteCmdlet();
586588
cmdlet.InvokeEndProcessing();
@@ -602,7 +604,8 @@ public void ClearContextNoLogin()
602604
var profile = new AzureRmProfile();
603605
cmdlet.CommandRuntime = commandRuntimeMock;
604606
cmdlet.DefaultProfile = profile;
605-
cmdlet.PassThrough = true;
607+
cmdlet.PassThru = true;
608+
cmdlet.Scope = ContextModificationScope.Process;
606609
cmdlet.InvokeBeginProcessing();
607610
cmdlet.ExecuteCmdlet();
608611
cmdlet.InvokeEndProcessing();

src/ResourceManager/Profile/Commands.Profile/Account/RemoveAzureRmAccount.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public override void ExecuteCmdlet()
112112

113113
if (ShouldProcess(string.Format("Log out principal '{0}'", azureAccount.Id), "log out"))
114114
{
115-
AzureSession.Instance.AuthenticationFactory.RemoveUser(azureAccount, AzureSession.Instance.TokenCache);
115+
if (GetContextModificationScope() == ContextModificationScope.CurrentUser)
116+
{
117+
AzureSession.Instance.AuthenticationFactory.RemoveUser(azureAccount, AzureSession.Instance.TokenCache);
118+
}
119+
116120
if (AzureRmProfileProvider.Instance.Profile != null)
117121
{
118122

src/ResourceManager/Profile/Commands.Profile/Common/AzureContextModificationCmdlet.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,33 @@ protected bool TryGetExistingContextNameParameter(string name, out RuntimeDefin
154154

155155
return result;
156156
}
157+
158+
/// <summary>
159+
/// Generate a runtime parameter with ValidateSet matching the current context
160+
/// </summary>
161+
/// <param name="name">The name of the parameter</param>
162+
/// <param name="runtimeParameter">The returned runtime parameter for context, with appropriate validate set</param>
163+
/// <returns>True if one or more contexts were found, otherwise false</returns>
164+
protected bool TryGetExistingContextNameParameter(string name, string parameterSetName, out RuntimeDefinedParameter runtimeParameter)
165+
{
166+
var result = false;
167+
var profile = DefaultProfile as AzureRmProfile;
168+
runtimeParameter = null;
169+
if (profile != null && profile.Contexts != null && profile.Contexts.Any())
170+
{
171+
runtimeParameter = new RuntimeDefinedParameter(
172+
name, typeof(string),
173+
new Collection<Attribute>()
174+
{
175+
new ParameterAttribute { Position =0, Mandatory=true, HelpMessage="The name of the context", ParameterSetName = parameterSetName },
176+
new ValidateSetAttribute(profile.Contexts.Keys.ToArray())
177+
}
178+
);
179+
result = true;
180+
}
181+
182+
return result;
183+
}
184+
157185
}
158186
}

src/ResourceManager/Profile/Commands.Profile/Context/ClearAzureRmContext.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17+
using Microsoft.Azure.Commands.Common.Authentication.Models;
1718
using Microsoft.Azure.Commands.Profile.Common;
1819
using Microsoft.Azure.Commands.Profile.Properties;
1920
using Microsoft.Azure.Commands.ResourceManager.Common;
@@ -26,11 +27,11 @@ namespace Microsoft.Azure.Commands.Profile.Context
2627
[OutputType(typeof(bool))]
2728
public class ClearAzureRmContext : AzureRMCmdlet
2829
{
29-
[Parameter(Mandatory = false, HelpMessage ="Clear the context only for the current PowerShell session, or for all sessions.")]
30+
[Parameter(Mandatory = true, HelpMessage ="Clear the context only for the current PowerShell session, or for all sessions.")]
3031
public ContextModificationScope Scope { get; set; } = ContextModificationScope.Process;
3132

3233
[Parameter(Mandatory = false, HelpMessage="Return a value indicating success or failure")]
33-
public SwitchParameter PassThrough { get; set; }
34+
public SwitchParameter PassThru { get; set; }
3435

3536
[Parameter(Mandatory = false, HelpMessage = "Delete all users and groups from the global scope without prompting")]
3637
public SwitchParameter Force { get; set; }
@@ -44,16 +45,13 @@ public override void ExecuteCmdlet()
4445
() =>
4546
{
4647
bool result = false;
47-
if (AzureSession.Instance.TokenCache != null)
48+
var profile = DefaultProfile as AzureRmProfile;
49+
if (profile != null)
4850
{
49-
AzureSession.Instance.TokenCache.Clear();
50-
}
51-
if (DefaultProfile != null)
52-
{
53-
DefaultProfile.Clear();
51+
profile.Clear();
5452
result = true;
5553
}
56-
if (PassThrough.IsPresent)
54+
if (PassThru.IsPresent)
5755
{
5856
WriteObject(result);
5957
}
@@ -81,7 +79,7 @@ public override void ExecuteCmdlet()
8179
}
8280
}
8381

84-
if (PassThrough.IsPresent)
82+
if (PassThru.IsPresent)
8583
{
8684
WriteObject(result);
8785
}

src/ResourceManager/Profile/Commands.Profile/Context/RemoveAzureRmContext.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,31 @@
1717
using Microsoft.Azure.Commands.Profile.Models;
1818
using Microsoft.Azure.Commands.Profile.Properties;
1919
using System;
20+
using System.Linq;
2021
using System.Management.Automation;
2122

2223
namespace Microsoft.Azure.Commands.Profile.Context
2324
{
24-
[Cmdlet(VerbsCommon.Remove, "AzureRmContext", SupportsShouldProcess = true)]
25+
[Cmdlet(VerbsCommon.Remove, "AzureRmContext", SupportsShouldProcess = true, DefaultParameterSetName = InputObjectParameterSet)]
2526
[OutputType(typeof(PSAzureContext))]
2627
public class RemoveAzureRmContext : AzureContextModificationCmdlet, IDynamicParameters
2728
{
29+
const string NamedContextParameterSet = "Named Context", InputObjectParameterSet = "Input Object";
30+
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "A context object, normally passed through the pipeline.")]
31+
[ValidateNotNullOrEmpty]
32+
public PSAzureContext InputObject { get; set; }
33+
2834
[Parameter(Mandatory = false, HelpMessage = "Remove context even if it is the defualt")]
2935
public SwitchParameter Force { get; set; }
3036

3137
[Parameter(Mandatory = false, HelpMessage = "Return the removed context")]
32-
public SwitchParameter PassThrough { get; set; }
38+
public SwitchParameter PassThru { get; set; }
3339

3440
public object GetDynamicParameters()
3541
{
3642
var parameters = new RuntimeDefinedParameterDictionary();
3743
RuntimeDefinedParameter namedParameter;
38-
if (TryGetExistingContextNameParameter("Name", out namedParameter))
44+
if (TryGetExistingContextNameParameter("Name", NamedContextParameterSet, out namedParameter))
3945
{
4046
parameters.Add(namedParameter.Name, namedParameter);
4147
}
@@ -45,34 +51,40 @@ public object GetDynamicParameters()
4551

4652
public override void ExecuteCmdlet()
4753
{
48-
if (MyInvocation.BoundParameters.ContainsKey("Name"))
54+
string name = null;
55+
if (ParameterSetName == InputObjectParameterSet)
56+
{
57+
name = InputObject?.Name;
58+
}
59+
else if (MyInvocation.BoundParameters.ContainsKey("Name"))
60+
{
61+
name = MyInvocation.BoundParameters["Name"] as string;
62+
}
63+
64+
if (!string.IsNullOrWhiteSpace(name))
4965
{
50-
string name = MyInvocation.BoundParameters["Name"] as string;
51-
if (name != null)
52-
{
53-
var defaultProfile = DefaultProfile as AzureRmProfile;
54-
ConfirmAction(Force.IsPresent,
55-
string.Format(Resources.RemoveDefaultContextQuery, name),
56-
string.Format(Resources.RemoveContextMessage, name),
57-
Resources.RemoveContextTarget,
58-
() =>
66+
var defaultProfile = DefaultProfile as AzureRmProfile;
67+
ConfirmAction(Force.IsPresent,
68+
string.Format(Resources.RemoveDefaultContextQuery, name),
69+
string.Format(Resources.RemoveContextMessage, name),
70+
Resources.RemoveContextTarget,
71+
() =>
72+
{
73+
ModifyContext((profile, client) =>
5974
{
60-
ModifyContext((profile, client) =>
75+
if (profile.Contexts.ContainsKey(name))
6176
{
62-
if (profile.Contexts.ContainsKey(name))
77+
var removedContext = profile.Contexts[name];
78+
if (client.TryRemoveContext(name) && PassThru.IsPresent)
6379
{
64-
var removedContext = profile.Contexts[name];
65-
if (client.TryRemoveContext(name) && PassThrough.IsPresent)
66-
{
67-
var outContext = new PSAzureContext(removedContext);
68-
outContext.Name = name;
69-
WriteObject(outContext);
70-
}
80+
var outContext = new PSAzureContext(removedContext);
81+
outContext.Name = name;
82+
WriteObject(outContext);
7183
}
72-
});
73-
},
74-
() => string.Equals(defaultProfile.DefaultContextKey, name, StringComparison.OrdinalIgnoreCase));
75-
}
84+
}
85+
});
86+
},
87+
() => string.Equals(defaultProfile.DefaultContextKey, name, StringComparison.OrdinalIgnoreCase));
7688
}
7789
}
7890
}

src/ResourceManager/Profile/Commands.Profile/Context/RenameAzureRmContext.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@
2222

2323
namespace Microsoft.Azure.Commands.Profile.Context
2424
{
25-
[Cmdlet(VerbsCommon.Rename, "AzureRmContext", SupportsShouldProcess = true)]
25+
[Cmdlet(VerbsCommon.Rename, "AzureRmContext", SupportsShouldProcess = true, DefaultParameterSetName = InputObjectParameterSet)]
2626
[OutputType(typeof(PSAzureContext))]
2727
public class RenameAzureRmContext : AzureContextModificationCmdlet, IDynamicParameters
2828
{
29-
const string SourceParameterName = "SourceName", TargetParameterName = "TargetName";
29+
const string SourceParameterName = "SourceName", TargetParameterName = "TargetName", InputObjectParameterSet = "Input Object", NameParameterSet = "Context Name";
30+
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "A context object, normally passed through the pipeline.")]
31+
[ValidateNotNullOrEmpty]
32+
public PSAzureContext InputObject { get; set; }
33+
3034
[Parameter( Mandatory=false, HelpMessage="Rename the context even if the target context already exists")]
3135
public SwitchParameter Force { get; set; }
3236

3337
[Parameter(Mandatory=false, HelpMessage="Return the renamed context")]
34-
public SwitchParameter PassThrough { get; set; }
38+
public SwitchParameter PassThru { get; set; }
3539

3640
public object GetDynamicParameters()
3741
{
3842
var parameters = new RuntimeDefinedParameterDictionary();
3943
RuntimeDefinedParameter sourceNameParameter;
40-
if (TryGetExistingContextNameParameter(SourceParameterName, out sourceNameParameter))
44+
if (TryGetExistingContextNameParameter(SourceParameterName, NameParameterSet, out sourceNameParameter))
4145
{
4246
parameters.Add(sourceNameParameter.Name, sourceNameParameter);
4347
var attributes = new Collection<Attribute>()
@@ -54,9 +58,18 @@ public object GetDynamicParameters()
5458

5559
public override void ExecuteCmdlet()
5660
{
57-
if (MyInvocation.BoundParameters.ContainsKey(SourceParameterName) && MyInvocation.BoundParameters.ContainsKey(TargetParameterName))
61+
string sourceName = null;
62+
if (ParameterSetName == InputObjectParameterSet)
63+
{
64+
sourceName = InputObject?.Name;
65+
}
66+
else if (MyInvocation.BoundParameters.ContainsKey(SourceParameterName))
67+
{
68+
sourceName = MyInvocation.BoundParameters[SourceParameterName] as string;
69+
}
70+
71+
if (!string.IsNullOrWhiteSpace(sourceName) && MyInvocation.BoundParameters.ContainsKey(TargetParameterName))
5872
{
59-
var sourceName = MyInvocation.BoundParameters[SourceParameterName] as string;
6073
var targetName = MyInvocation.BoundParameters[TargetParameterName] as string;
6174
var defaultProfile = DefaultProfile as AzureRmProfile;
6275
if (!string.IsNullOrWhiteSpace(sourceName) && !string.IsNullOrWhiteSpace(targetName) && defaultProfile != null && !string.Equals(sourceName, targetName, StringComparison.OrdinalIgnoreCase))
@@ -77,7 +90,7 @@ public override void ExecuteCmdlet()
7790
if (client.TryRenameContext(sourceName, targetName)
7891
&& (!string.Equals(targetName, defaultContextName, StringComparison.OrdinalIgnoreCase)
7992
|| client.TrySetDefaultContext(targetName))
80-
&& PassThrough.IsPresent)
93+
&& PassThru.IsPresent)
8194
{
8295
var outContext = new PSAzureContext(profile.Contexts[targetName]);
8396
outContext.Name = targetName;

0 commit comments

Comments
 (0)