Skip to content

Commit e7d7339

Browse files
committed
Merge branch 'context' of github.com:yugangw-msft/azure-powershell into context
2 parents 7789968 + 083c743 commit e7d7339

File tree

7 files changed

+54
-19
lines changed

7 files changed

+54
-19
lines changed

src/ResourceManager/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6161
// (tenant is not provided and subscription is not provided)
6262
else
6363
{
64-
foreach(var tenant in ListAccountTenants(account, environment, password, promptBehavior))
64+
foreach (var tenant in ListAccountTenants(account, environment, password, promptBehavior))
6565
{
6666
AzureTenant tempTenant;
6767
AzureSubscription tempSubscription;
6868
var token = AcquireAccessToken(account, environment, tenant.Id.ToString(), password,
6969
ShowDialog.Auto);
7070
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, out tempSubscription, out tempTenant) &&
7171
newTenant == null)
72-
{
72+
{
7373
newTenant = tempTenant;
7474
newSubscription = tempSubscription;
7575
}
@@ -93,8 +93,8 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
9393
{
9494
_profile.Context = new AzureContext(
9595
_profile.Context.Subscription,
96-
_profile.Context.Account,
97-
_profile.Context.Environment,
96+
_profile.Context.Account,
97+
_profile.Context.Environment,
9898
new AzureTenant() { Id = new Guid(tenantId) });
9999

100100
if (_profile.Context.Account != null)
@@ -107,10 +107,17 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
107107
}
108108
}
109109

110-
if(!string.IsNullOrWhiteSpace(subscriptionId))
110+
if (!string.IsNullOrWhiteSpace(subscriptionId))
111111
{
112+
if (ListSubscriptions(_profile.Context.Tenant.Id.ToString()).FirstOrDefault(s =>
113+
String.Compare(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase) == 0) == null)
114+
{
115+
throw new ArgumentException(string.Format(
116+
"Provided subscription {0} does not exist under current tenant {1}", subscriptionId, _profile.Context.Tenant.Id));
117+
}
118+
112119
var newSubscription = new AzureSubscription { Id = new Guid(subscriptionId) };
113-
if(_profile.Context.Subscription != null)
120+
if (_profile.Context.Subscription != null)
114121
{
115122
newSubscription.Account = _profile.Context.Subscription.Account;
116123
newSubscription.Environment = _profile.Context.Subscription.Environment;
@@ -121,7 +128,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
121128
_profile.Context = new AzureContext(
122129
newSubscription,
123130
_profile.Context.Account,
124-
_profile.Context.Environment,
131+
_profile.Context.Environment,
125132
_profile.Context.Tenant);
126133
}
127134

@@ -131,7 +138,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
131138
public List<AzureTenant> ListTenants(string tenant)
132139
{
133140
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Auto)
134-
.Where(t => tenant == null ||
141+
.Where(t => tenant == null ||
135142
tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) ||
136143
tenant.Equals(t.Domain, StringComparison.OrdinalIgnoreCase))
137144
.ToList();
@@ -279,7 +286,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
279286
out AzureSubscription subscription,
280287
out AzureTenant tenant)
281288
{
282-
289+
283290
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
284291
new TokenCloudCredentials(accessToken.AccessToken),
285292
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
@@ -339,15 +346,15 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
339346

340347
private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
341348
{
342-
var commonTenantToken = AcquireAccessToken( account, environment, AuthenticationFactory.CommonAdTenant,
349+
var commonTenantToken = AcquireAccessToken(account, environment, AuthenticationFactory.CommonAdTenant,
343350
password, promptBehavior);
344351

345352
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
346353
new TokenCloudCredentials(commonTenantToken.AccessToken),
347354
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
348355
{
349356
return subscriptionClient.Tenants.List().TenantIds
350-
.Select(ti => new AzureTenant() { Id = new Guid(ti.TenantId), Domain = commonTenantToken.GetDomain() } )
357+
.Select(ti => new AzureTenant() { Id = new Guid(ti.TenantId), Domain = commonTenantToken.GetDomain() })
351358
.ToList();
352359
}
353360
}
@@ -361,7 +368,7 @@ public IEnumerable<AzureTenant> ListTenants()
361368
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never);
362369
}
363370

364-
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
371+
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
365372
SecureString password, ShowDialog promptBehavior, string tenantId)
366373
{
367374
var accessToken = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="xunit.runner.console" version="2.0.0" />
4+
</packages>

src/ResourceManager/HDInsight/Commands.HDInsight.Test/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
2020
<package id="Microsoft.Rest.ClientRuntime" version="1.2.0" targetFramework="net45" />
2121
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="0.9.3" targetFramework="net45" />
22+
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
2223
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
2324
<package id="xunit" version="1.9.2" targetFramework="net45" />
2425
<package id="xunit.extensions" version="1.9.2" targetFramework="net45" />
26+
<package id="xunit.runner.visualstudio" version="2.1.0-beta4-build1109" targetFramework="net45" />
2527
</packages>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ public void AllParameterSetsSucceed()
2626
{
2727
ProfileController.NewInstance.RunPsTest("db1ab6f0-4769-4b27-930e-01e2ef9c123c", "Test-GetSubscriptionsEndToEnd");
2828
}
29+
30+
[Fact]
31+
[Trait(Category.AcceptanceType, Category.CheckIn)]
32+
public void SetAzureRmContextWorks()
33+
{
34+
ProfileController.NewInstance.RunPsTest("db1ab6f0-4769-4b27-930e-01e2ef9c123c", "Test-SetAzureRmContextEndToEnd");
35+
}
2936
}
3037
}

src/ResourceManager/Profile/Commands.Profile.Test/SubscriptionCmdletTests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,19 @@ function Test-GetSubscriptionsEndToEnd
4242
Assert-True {$mostSubscriptions.Count -gt 0}
4343
$tenantSubscriptions = Get-AzureRmSubscription -Tenant $tenant
4444
Assert-True {$tenantSubscriptions.Count -gt 0}
45+
}
46+
47+
function Test-SetAzureRmContextEndToEnd
48+
{
49+
# This test requires that the tenant contains atleast two subscriptions
50+
$allSubscriptions = Get-AzureRmSubscription
51+
$secondSubscription = $allSubscriptions[1]
52+
Assert-True { $allSubscriptions[0] -ne $null }
53+
Assert-True { $secondSubscription -ne $null }
54+
Set-AzureRmContext -SubscriptionId $secondSubscription.SubscriptionId
55+
$context = Get-AzureRmContext
56+
57+
Assert-AreEqual $context.subscriptionId $secondSubscription.SubscriptionId
58+
59+
Assert-ThrowsContains {Set-AzureRmContext -SubscriptionId 'junk-subscription-id'} "does not exist under current tenant"
4560
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class SetAzureRMContextCommand : AzureRMCmdlet
3232
private const string TenantIdAndSubscriptionIdParameterSet = "TenantIdAndSubscriptionId";
3333
private const string TenantAndSubscriptionParameterSet = "TenantAndSubscription";
3434

35-
[Parameter(ParameterSetName = TenantIdParameterSet, Mandatory = true, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
36-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = true, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
35+
[Parameter(ParameterSetName = TenantIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
36+
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
3737
[ValidateNotNullOrEmpty]
3838
public string TenantId { get; set; }
3939

@@ -42,7 +42,7 @@ public class SetAzureRMContextCommand : AzureRMCmdlet
4242
[ValidateNotNullOrEmpty]
4343
public string SubscriptionId { get; set; }
4444

45-
[Parameter(ParameterSetName = TenantAndSubscriptionParameterSet, Mandatory = true, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
45+
[Parameter(ParameterSetName = TenantAndSubscriptionParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
4646
public AzureTenant Tenant { get; set; }
4747

4848
[Parameter(ParameterSetName = TenantAndSubscriptionParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]

tools/PublishModules.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ if ([string]::IsNullOrEmpty($scope))
4141
$scope = 'All'
4242
}
4343

44-
Write-Host "Publishing $scope package(s)"
44+
Write-Host "Publishing $scope package(and its dependencies)"
4545

4646
$packageFolder = "$PSScriptRoot\..\src\Package"
4747

@@ -54,9 +54,9 @@ if ($repo -ne $null) {
5454
}
5555

5656
$resourceManagerRootFolder = "$packageFolder\$buildConfig\ResourceManager\AzureResourceManager"
57-
58-
if ($scope -eq 'All') {
59-
# Publish AzureRM.Profile first which is the common dependency
57+
$publishToLocal = test-path $repositoryLocation
58+
if (($scope -eq 'All') -or $publishToLocal ) {
59+
# If we publish 'All' or to local folder, publish AzureRM.Profile first, becasue it is the common dependency
6060
Write-Host "Publishing profile module"
6161
Publish-Module -Path "$resourceManagerRootFolder\AzureRM.Profile" -NuGetApiKey $apiKey -Repository $repoName
6262
Write-Host "Published profile module"

0 commit comments

Comments
 (0)