Skip to content

Commit 7eb8900

Browse files
committed
Add switch parameter to opt out of context population
1 parent 5dd1b24 commit 7eb8900

File tree

3 files changed

+94
-55
lines changed

3 files changed

+94
-55
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,42 +51,42 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
5151
[ValidateNotNullOrEmpty]
5252
public string Environment { get; set; }
5353

54-
55-
[Parameter(ParameterSetName = UserParameterSet,
54+
55+
[Parameter(ParameterSetName = UserParameterSet,
5656
Mandatory = false, HelpMessage = "Optional credential", Position = 0)]
57-
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
57+
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
5858
Mandatory = true, HelpMessage = "Credential")]
5959
public PSCredential Credential { get; set; }
6060

61-
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
61+
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
6262
Mandatory = true, HelpMessage = "Certificate Hash (Thumbprint)")]
6363
public string CertificateThumbprint { get; set; }
64-
65-
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
64+
65+
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
6666
Mandatory = true, HelpMessage = "SPN")]
6767
public string ApplicationId { get; set; }
6868

69-
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
69+
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
7070
Mandatory = true)]
71-
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
71+
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
7272
Mandatory = true)]
7373
public SwitchParameter ServicePrincipal { get; set; }
74-
75-
[Parameter(ParameterSetName = UserParameterSet,
74+
75+
[Parameter(ParameterSetName = UserParameterSet,
7676
Mandatory = false, HelpMessage = "Optional tenant name or ID")]
77-
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
77+
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
7878
Mandatory = true, HelpMessage = "Tenant name or ID")]
79-
[Parameter(ParameterSetName = AccessTokenParameterSet,
79+
[Parameter(ParameterSetName = AccessTokenParameterSet,
8080
Mandatory = false, HelpMessage = "Tenant name or ID")]
81-
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
81+
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
8282
Mandatory = true, HelpMessage = "Tenant name or ID")]
8383
[Parameter(ParameterSetName = ManagedServiceParameterSet,
8484
Mandatory = false, HelpMessage = "Optional tenant name or ID")]
8585
[Alias("Domain")]
8686
[ValidateNotNullOrEmpty]
8787
public string TenantId { get; set; }
88-
89-
[Parameter(ParameterSetName = AccessTokenParameterSet,
88+
89+
[Parameter(ParameterSetName = AccessTokenParameterSet,
9090
Mandatory = true, HelpMessage = "AccessToken for Azure Resource Manager")]
9191
[ValidateNotNullOrEmpty]
9292
public string AccessToken { get; set; }
@@ -100,8 +100,8 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
100100
Mandatory = false, HelpMessage = "AccessToken for KeyVault Service")]
101101
[ValidateNotNullOrEmpty]
102102
public string KeyVaultAccessToken { get; set; }
103-
104-
[Parameter(ParameterSetName = AccessTokenParameterSet,
103+
104+
[Parameter(ParameterSetName = AccessTokenParameterSet,
105105
Mandatory = true, HelpMessage = "Account Id for access token")]
106106
[Parameter(ParameterSetName = ManagedServiceParameterSet,
107107
Mandatory = false, HelpMessage = "Account Id for managed service. Can be a managed service resource Id, or the associated client id. To use the SyatemAssigned identity, leave this field blank.")]
@@ -147,6 +147,9 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
147147
Mandatory = false, HelpMessage = "Skip validation for access token")]
148148
public SwitchParameter SkipValidation { get; set; }
149149

150+
[Parameter(Mandatory = false, HelpMessage = "Skips context population if no contexts are found.")]
151+
public SwitchParameter SkipContextPopulation { get; set; }
152+
150153
[Parameter(Mandatory = false, HelpMessage = "Overwrite the existing context with the same name, if any.")]
151154
public SwitchParameter Force { get; set; }
152155

@@ -213,8 +216,8 @@ public override void ExecuteCmdlet()
213216
builder.Port = ManagedServicePort;
214217
builder.Path = "/oauth2/token";
215218

216-
string msiSecret = this.IsBound(nameof(ManagedServiceSecret))
217-
? ManagedServiceSecret.ConvertToString()
219+
string msiSecret = this.IsBound(nameof(ManagedServiceSecret))
220+
? ManagedServiceSecret.ConvertToString()
218221
: System.Environment.GetEnvironmentVariable(MSISecretVariable);
219222

220223
string suppliedUri = this.IsBound(nameof(ManagedServiceHostName))
@@ -225,7 +228,7 @@ public override void ExecuteCmdlet()
225228
{
226229
azureAccount.SetProperty(AzureAccount.Property.MSILoginSecret, msiSecret);
227230
}
228-
231+
229232
if (!string.IsNullOrWhiteSpace(suppliedUri))
230233
{
231234
azureAccount.SetProperty(AzureAccount.Property.MSILoginUri, suppliedUri);
@@ -254,7 +257,7 @@ public override void ExecuteCmdlet()
254257
{
255258
azureAccount.Id = ApplicationId;
256259
}
257-
260+
258261
if (!string.IsNullOrWhiteSpace(CertificateThumbprint))
259262
{
260263
azureAccount.SetThumbprint(CertificateThumbprint);
@@ -283,7 +286,8 @@ public override void ExecuteCmdlet()
283286
password,
284287
SkipValidation,
285288
(s) => WriteWarning(s),
286-
name));
289+
name,
290+
!this.SkipContextPopulation.IsPresent));
287291
});
288292
}
289293
}
@@ -329,7 +333,7 @@ public void OnImport()
329333
#if DEBUG
330334
}
331335
#endif
332-
336+
333337
bool autoSaveEnabled = AzureSession.Instance.ARMContextSaveMode == ContextSaveMode.CurrentUser;
334338
var autosaveVariable = System.Environment.GetEnvironmentVariable(AzureProfileConstants.AzureAutosaveVariable);
335339
bool localAutosave;

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public AzureRmProfile Login(
109109
SecureString password,
110110
bool skipValidation,
111111
Action<string> promptAction,
112-
string name = null)
112+
string name = null,
113+
bool shouldPopulateContextList = true)
113114
{
114115
IAzureSubscription newSubscription = null;
115116
IAzureTenant newTenant = null;
@@ -226,7 +227,7 @@ public AzureRmProfile Login(
226227
}
227228
}
228229

229-
var shouldPopulateContextList = _profile.DefaultContext?.Account == null;
230+
shouldPopulateContextList &= _profile.DefaultContext?.Account == null;
230231
if (newSubscription == null)
231232
{
232233
if (subscriptionId != null)
@@ -264,7 +265,7 @@ public AzureRmProfile Login(
264265
if (shouldPopulateContextList)
265266
{
266267
var defaultContext = _profile.DefaultContext;
267-
var subscriptions = ListSubscriptions(tenantId);
268+
var subscriptions = ListSubscriptions(tenantId).Take(25);
268269
foreach (var subscription in subscriptions)
269270
{
270271
IAzureTenant tempTenant = new AzureTenant()

0 commit comments

Comments
 (0)