Skip to content

Commit b6f5de3

Browse files
Hao Chenyugangw-msft
authored andcommitted
[#106772718] Search for subscription in all tenants when no tenantId is specified in Get-AzureRmSubscription and Set-AzureRmContext cmdlets
1 parent 4b96991 commit b6f5de3

File tree

4 files changed

+57
-119
lines changed

4 files changed

+57
-119
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void MultipleTenantsAndSubscriptionsSucceed()
7575
tenantResults = client.ListTenants(DefaultTenant.ToString());
7676
Assert.Equal(1, tenantResults.Count());
7777
AzureSubscription subValue;
78-
Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
78+
Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
7979
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
8080
}
8181

@@ -93,7 +93,7 @@ public void SingleTenantAndSubscriptionSucceeds()
9393
tenantResults = client.ListTenants(DefaultTenant.ToString());
9494
Assert.Equal(1, tenantResults.Count());
9595
AzureSubscription subValue;
96-
Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
96+
Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
9797
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
9898
}
9999

@@ -107,7 +107,7 @@ public void SubscriptionNotFoundDoesNotThrow()
107107
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
108108
Assert.Equal(1, subResults.Count);
109109
AzureSubscription subValue;
110-
Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
110+
Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
111111
}
112112

113113
[Fact]
@@ -130,7 +130,7 @@ public void NoSubscriptionsInListDoesNotThrow()
130130
var client = SetupTestEnvironment(tenants, subscriptions);
131131
Assert.Equal(0, client.ListSubscriptions().Count());
132132
AzureSubscription subValue;
133-
Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
133+
Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
134134
}
135135

136136
[Fact]

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

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,59 +57,24 @@ protected override void ProcessRecord()
5757
AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account,
5858
Context.Environment, Context.Tenant));
5959
}
60-
else if (ParameterSetName == SubscriptionIdParameterSet)
60+
else
6161
{
62-
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
63-
profileClient.SetCurrentContext(SubscriptionId, TenantId);
64-
}
65-
else if (ParameterSetName == SubscriptionNameParameterSet)
66-
{
67-
if(string.IsNullOrWhiteSpace(TenantId) &&
68-
string.IsNullOrWhiteSpace(SubscriptionName))
62+
if (string.IsNullOrWhiteSpace(SubscriptionId) && string.IsNullOrWhiteSpace(SubscriptionName))
6963
{
7064
throw new PSInvalidOperationException(Resources.SetAzureRmContextNoParameterSet);
7165
}
7266

7367
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
74-
AzureSubscription subscription = null;
75-
string tenantId = AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString();
76-
77-
if (string.IsNullOrWhiteSpace(TenantId))
78-
{
79-
WriteVerbose(
80-
string.Format(
81-
Resources.CurrentTenantInUse,
82-
tenantId));
83-
}
84-
else
68+
if (ParameterSetName == SubscriptionIdParameterSet)
8569
{
86-
tenantId = TenantId;
87-
}
88-
89-
if (!string.IsNullOrWhiteSpace(SubscriptionName))
90-
{
91-
if (!profileClient.TryGetSubscriptionByName(
92-
tenantId,
93-
SubscriptionName,
94-
out subscription))
95-
{
96-
throw new ItemNotFoundException(
97-
string.Format(Resources.SubscriptionNameNotFoundError, SubscriptionName));
98-
}
99-
100-
profileClient.SetCurrentContext(subscription.Id.ToString(), tenantId, verifySubscription: false);
101-
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
70+
profileClient.SetCurrentContext(SubscriptionId, null, TenantId);
10271
}
103-
// Only tenantId was provided
104-
else
72+
else if (ParameterSetName == SubscriptionNameParameterSet)
10573
{
106-
// authenticate with just tenant id
107-
var accessToken = profileClient.AcureAccessToken(tenantId);
108-
profileClient.SetCurrentContext(accessToken.TenantId);
74+
profileClient.SetCurrentContext(null, SubscriptionName, TenantId);
10975
}
76+
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
11077
}
111-
112-
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
11378
}
11479
}
11580
}

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

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,30 @@ public AzureContext SetCurrentContext(string tenantId)
134134
return _profile.Context;
135135
}
136136

137-
public AzureContext SetCurrentContext(string subscriptionId, string tenantId, bool verifySubscription = true)
137+
public AzureContext SetCurrentContext(string subscriptionId, string subscriptionName, string tenantId)
138138
{
139-
if (!string.IsNullOrWhiteSpace(tenantId))
139+
AzureSubscription subscription;
140+
string subscriptionFilter = string.IsNullOrWhiteSpace(subscriptionId) ? subscriptionName : subscriptionId;
141+
if (!string.IsNullOrWhiteSpace(subscriptionId))
140142
{
141-
_profile.SetContextWithCache(new AzureContext(
142-
_profile.Context.Subscription,
143-
_profile.Context.Account,
144-
_profile.Context.Environment,
145-
CreateTenant(tenantId)));
143+
TryGetSubscriptionById(tenantId, subscriptionId, out subscription);
144+
}
145+
else
146+
{
147+
TryGetSubscriptionByName(tenantId, subscriptionName, out subscription);
148+
}
149+
150+
if (subscription == null)
151+
{
152+
throw new ArgumentException(string.Format(
153+
"Provided subscription {0} does not exist", subscriptionFilter));
154+
}
155+
else
156+
{
157+
if (string.IsNullOrWhiteSpace(tenantId))
158+
{
159+
tenantId = subscription.Properties[AzureSubscription.Property.Tenants];
160+
}
146161

147162
if (_profile.Context.Account != null)
148163
{
@@ -152,20 +167,6 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId, bo
152167
{
153168
_profile.Context.Subscription.Properties[AzureSubscription.Property.Tenants] = tenantId;
154169
}
155-
}
156-
157-
if (!string.IsNullOrWhiteSpace(subscriptionId))
158-
{
159-
var subscription = ListSubscriptions(_profile.Context.Tenant.Id.ToString())
160-
.FirstOrDefault(s =>
161-
string.Equals(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase));
162-
163-
if (verifySubscription &&
164-
subscription == null)
165-
{
166-
throw new ArgumentException(string.Format(
167-
"Provided subscription {0} does not exist under current tenant {1}", subscriptionId, _profile.Context.Tenant.Id));
168-
}
169170

170171
var newSubscription = new AzureSubscription { Id = new Guid(subscriptionId) };
171172
if (_profile.Context.Subscription != null)
@@ -180,7 +181,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId, bo
180181
newSubscription,
181182
_profile.Context.Account,
182183
_profile.Context.Environment,
183-
_profile.Context.Tenant));
184+
CreateTenant(tenantId)));
184185
}
185186

186187
return _profile.Context;
@@ -195,31 +196,35 @@ public List<AzureTenant> ListTenants(string tenant)
195196
.ToList();
196197
}
197198

198-
public bool TryGetSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription)
199+
public bool TryGetSubscriptionById(string tenantId, string subscriptionId, out AzureSubscription subscription)
199200
{
200-
if (string.IsNullOrWhiteSpace(tenantId))
201-
{
202-
throw new ArgumentNullException("Please provide a valid tenant Id.");
203-
}
201+
IEnumerable<AzureSubscription> subscriptionList = GetSubscriptions(tenantId);
204202

205-
AzureTenant tenant;
206-
var token = AcquireAccessToken(_profile.Context.Account, _profile.Context.Environment,
207-
tenantId, null, ShowDialog.Never);
208-
return TryGetTenantSubscription(token, _profile.Context.Account, _profile.Context.Environment,
209-
tenantId, subscriptionId, null, out subscription, out tenant);
203+
subscription = subscriptionList.FirstOrDefault(s => s.Id == Guid.Parse(subscriptionId));
204+
return subscription != null;
210205
}
211206

212207
public bool TryGetSubscriptionByName(string tenantId, string subscriptionName, out AzureSubscription subscription)
213208
{
209+
IEnumerable<AzureSubscription> subscriptionList = GetSubscriptions(tenantId);
210+
subscription = subscriptionList.FirstOrDefault(s => s.Name.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
211+
212+
return subscription != null;
213+
}
214+
215+
public IEnumerable<AzureSubscription> GetSubscriptions(string tenantId)
216+
{
217+
IEnumerable<AzureSubscription> subscriptionList;
214218
if (string.IsNullOrWhiteSpace(tenantId))
215219
{
216-
throw new ArgumentNullException("Please provide a valid tenant Id.");
220+
subscriptionList = ListSubscriptions();
221+
}
222+
else
223+
{
224+
subscriptionList = ListSubscriptions(tenantId);
217225
}
218226

219-
IEnumerable<AzureSubscription> subscriptionList = ListSubscriptions(tenantId);
220-
subscription = subscriptionList.FirstOrDefault(s => s.Name.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
221-
222-
return subscription != null;
227+
return subscriptionList;
223228
}
224229

225230
public AzureEnvironment AddOrSetEnvironment(AzureEnvironment environment)

src/ResourceManager/Profile/Commands.Profile/Subscription/GetAzureRMSubscription.cs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class GetAzureRMSubscriptionCommand : AzureRMCmdlet
2727
{
2828
public const string ListByIdInTenantParameterSet = "ListByIdInTenant";
2929
public const string ListByNameInTenantParameterSet = "ListByNameInTenant";
30-
public const string ListAllParameterSet = "ListAll";
3130

3231
private RMProfileClient _client;
3332

@@ -41,9 +40,6 @@ public class GetAzureRMSubscriptionCommand : AzureRMCmdlet
4140
[Parameter(ParameterSetName = ListByNameInTenantParameterSet, ValueFromPipelineByPropertyName = true, Mandatory = false)]
4241
public string TenantId { get; set; }
4342

44-
[Parameter(ParameterSetName = ListAllParameterSet, Mandatory = true)]
45-
public SwitchParameter All { get; set; }
46-
4743
protected override void BeginProcessing()
4844
{
4945
base.BeginProcessing();
@@ -53,23 +49,9 @@ protected override void BeginProcessing()
5349

5450
protected override void ProcessRecord()
5551
{
56-
if (this.ParameterSetName == ListAllParameterSet)
57-
{
58-
try
59-
{
60-
WriteObject(_client.ListSubscriptions().Select((s) => (PSAzureSubscription)(s)), enumerateCollection: true);
61-
}
62-
catch (AadAuthenticationException exception)
63-
{
64-
var account = DefaultContext.Account == null || DefaultContext.Account.Id == null
65-
? Resources.NoAccountProvided
66-
: DefaultContext.Account.Id;
67-
throw new PSInvalidOperationException(string.Format(Resources.CommonTenantAuthFailed, account), exception);
68-
}
69-
}
70-
else if (!string.IsNullOrWhiteSpace(this.SubscriptionName))
52+
var tenant = TenantId;
53+
if (!string.IsNullOrWhiteSpace(this.SubscriptionName))
7154
{
72-
var tenant = EnsureValidTenant();
7355
AzureSubscription result;
7456
try
7557
{
@@ -89,11 +71,10 @@ protected override void ProcessRecord()
8971
}
9072
else if (!string.IsNullOrWhiteSpace(this.SubscriptionId))
9173
{
92-
var tenant = EnsureValidTenant();
9374
AzureSubscription result;
9475
try
9576
{
96-
if (!this._client.TryGetSubscription(tenant, this.SubscriptionId, out result))
77+
if (!this._client.TryGetSubscriptionById(tenant, this.SubscriptionId, out result))
9778
{
9879
ThrowSubscriptionNotFoundError(this.TenantId, this.SubscriptionId);
9980
}
@@ -109,10 +90,9 @@ protected override void ProcessRecord()
10990
}
11091
else
11192
{
112-
var tenant = EnsureValidTenant();
11393
try
11494
{
115-
WriteObject(_client.ListSubscriptions(tenant).Select((s) => (PSAzureSubscription)s), enumerateCollection: true);
95+
WriteObject(_client.GetSubscriptions(tenant).Select((s) => (PSAzureSubscription)s), enumerateCollection: true);
11696
}
11797
catch (AadAuthenticationException exception)
11898
{
@@ -131,17 +111,5 @@ private void ThrowTenantAuthenticationError(string tenant, AadAuthenticationExce
131111
{
132112
throw new PSArgumentException(string.Format(Resources.TenantAuthFailed, tenant), exception);
133113
}
134-
135-
private string EnsureValidTenant()
136-
{
137-
var tenant = this.TenantId;
138-
if (string.IsNullOrWhiteSpace(tenant) && (DefaultContext.Tenant == null ||
139-
DefaultContext.Tenant.Id == null))
140-
{
141-
throw new PSArgumentException(Resources.NoValidTenant);
142-
}
143-
144-
return tenant ?? DefaultContext.Tenant.Id.ToString();
145-
}
146114
}
147115
}

0 commit comments

Comments
 (0)