Skip to content

. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 17, 2015
Merged

. #91

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AzurePowershell.Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<XUnitTests Include=".\src\ResourceManager\TrafficManager\Commands.TrafficManager2.Test\bin\Debug\Microsoft.Azure.Commands.TrafficManager.Test.ScenarioTests.dll"/>
<XUnitTests Include=".\src\ServiceManagement\Network\Commands.Network.Test\bin\Debug\Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.dll"/>
<XUnitTests Include=".\src\ResourceManager\ApiManagement\Commands.ApiManagement.Test\bin\Debug\Microsoft.Azure.Commands.ApiManagement.Test.dll"/>
<XUnitTests Include=".\src\Common\Commands.ResourceManager.Profile.Test\bin\Debug\Microsoft.Azure.Commands.Profile.Test.dll"/>
</ItemGroup>

<Target Name="InvokeMSTest">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.KeyVault.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.ResourceManager, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Management.Resources.2.18.7-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
Expand Down
3 changes: 2 additions & 1 deletion src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
Expand All @@ -21,4 +22,4 @@
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="System.Spatial" version="5.6.4" targetFramework="net45" />
<package id="WindowsAzure.Storage" version="5.0.0" targetFramework="net45" />
</packages>
</packages>
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="0.9.3" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management" version="4.1.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -184,6 +184,7 @@
</Compile>
<Compile Include="AccessTokenExtensions.cs" />
<Compile Include="AzureRMCmdlet.cs" />
<Compile Include="ModelExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
26 changes: 26 additions & 0 deletions src/Common/Commands.ResourceManager.Common/ModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Azure.Subscriptions.Models;

namespace Microsoft.Azure.Commands.ResourceManager.Common
{
public static class ModelExtensions
{

public static AzureSubscription ToAzureSubscription(this Subscription other, AzureContext context)
{
var subscription = new AzureSubscription();
subscription.Account = context.Account != null ? context.Account.Id : null;
subscription.Environment = context.Environment != null ? context.Environment.Name : EnvironmentName.AzureCloud;
subscription.Id = new Guid(other.SubscriptionId);
subscription.Name = other.DisplayName;
subscription.SetProperty(AzureSubscription.Property.Tenants,
context.Tenant.Id.ToString());
return subscription;
}
}
}
94 changes: 93 additions & 1 deletion src/Common/Commands.ResourceManager.Common/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ public List<AzureTenant> ListTenants(string tenant)
.ToList();
}

public bool TryGetSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription)
{
if (string.IsNullOrWhiteSpace(tenantId))
{
throw new ArgumentNullException("Please provide a valid tenant Id");
}

AzureTenant tenant;
return TryGetTenantSubscription(_profile.Context.Account, _profile.Context.Environment,
tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant);
}

private bool TryGetTenantSubscription(
AzureAccount account,
AzureEnvironment environment,
Expand Down Expand Up @@ -177,7 +189,8 @@ private bool TryGetTenantSubscription(
if (subscriptions.Count > 1)
{
WriteWarningMessage(string.Format(
"Tenant '{0}' contains more than one subscription. First one will be selected for further use.",
"Tenant '{0}' contains more than one subscription. First one will be selected for further use. " +
"To select another subscription, use Set-AzureRMContext.",
tenantId));
}
subscriptionFromServer = subscriptions.First();
Expand Down Expand Up @@ -233,12 +246,91 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
}
}

/// <summary>
/// List all tenants for the account in the profile context
/// </summary>
/// <returns>The list of tenants for the default account.</returns>
public IEnumerable<AzureTenant> ListTenants()
{
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never);
}

private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
SecureString password, ShowDialog promptBehavior, string tenantId)
{
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
account,
environment,
tenantId,
password,
promptBehavior);
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
new TokenCloudCredentials(accessToken.AccessToken),
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
{
var subscriptions = subscriptionClient.Subscriptions.List();
if (subscriptions != null && subscriptions.Subscriptions != null)
{
return
subscriptions.Subscriptions.Select(
(s) =>
s.ToAzureSubscription(new AzureContext(_profile.Context.Subscription, account,
environment, CreateTenantFromString(tenantId))));
}

return null;
}
}

public IEnumerable<AzureSubscription> ListSubscriptions(string tenant)
{
return ListSubscriptionsForTenant(_profile.Context.Account, _profile.Context.Environment, null,
ShowDialog.Never, tenant);
}

public IEnumerable<AzureSubscription> ListSubscriptions()
{
List<AzureSubscription> subscriptions = new List<AzureSubscription>();
foreach (var tenant in ListTenants())
{
try
{
subscriptions.AddRange(ListSubscriptions(tenant.Id.ToString()));
}
catch (AadAuthenticationException)
{
WriteWarningMessage(string.Format("Could not authenticate user account {0} with tenant {1}. " +
"Subscriptions in this tenant will not be listed. Please login again using Login-AzureRMAccount " +
"to view the subscriptions in this tenant.", _profile.Context.Account, tenant));
}

}

return subscriptions;
}

private void WriteWarningMessage(string message)
{
if (WarningLog != null)
{
WarningLog(message);
}
}

private static AzureTenant CreateTenantFromString(string tenantOrDomain)
{
AzureTenant result = new AzureTenant();
Guid id;
if (Guid.TryParse(tenantOrDomain, out id))
{
result.Id = id;
}
else
{
result.Domain = tenantOrDomain;
}

return result;
}
}
}
2 changes: 1 addition & 1 deletion src/Common/Commands.ResourceManager.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="0.9.3" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management" version="4.1.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using System.Linq;
using Xunit;
using System;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
{

public class AzureRMProfileTests
{
private const string DefaultAccount = "admin@contoso.com";
private static Guid DefaultSubscription = Guid.NewGuid();
private static string DefaultSubscriptionName = "Contoso Subscription";
private static string DefaultDomain = "contoso.com";
private static Guid DefaultTenant = Guid.NewGuid();

private static RMProfileClient SetupTestEnvironment(List<string> tenants, params List<string>[] subscriptionLists)
{
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(DefaultAccount,
Guid.NewGuid().ToString(), DefaultTenant.ToString());
var subscriptionList = new Queue<List<string>>(subscriptionLists);
var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList);
var mock = new MockClientFactory(new List<object>
{
clientFactory.GetSubscriptionClient()
}, true);
mock.MoqClients = true;
AzureSession.ClientFactory = mock;
var context = new AzureContext(new AzureSubscription()
{
Account = DefaultAccount,
Environment = EnvironmentName.AzureCloud,
Id = DefaultSubscription,
Name = DefaultSubscriptionName
},
new AzureAccount() { Id = DefaultAccount, Type = AzureAccount.AccountType.User },
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
new AzureTenant() { Domain = DefaultDomain, Id = DefaultTenant });
var profile = new AzureRMProfile();
profile.Context = context;
return new RMProfileClient(profile);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void MultipleTenantsAndSubscriptionsSucceed()
{
var tenants = new List<string> {Guid.NewGuid().ToString(), DefaultTenant.ToString()};
var firstList = new List<string> { DefaultSubscription.ToString(), Guid.NewGuid().ToString() };
var secondList = new List<string> { Guid.NewGuid().ToString()};
var client = SetupTestEnvironment(tenants, firstList, secondList);
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
Assert.Equal(3, subResults.Count);
var tenantResults = client.ListTenants();
Assert.Equal(2, tenantResults.Count());
tenantResults = client.ListTenants(DefaultTenant.ToString());
Assert.Equal(1, tenantResults.Count());
AzureSubscription subValue;
Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SingleTenantAndSubscriptionSucceeds()
{
var tenants = new List<string> {DefaultTenant.ToString()};
var subscriptions = new List<string> {DefaultSubscription.ToString()};
var client = SetupTestEnvironment(tenants, subscriptions);
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
Assert.Equal(1, subResults.Count);
var tenantResults = client.ListTenants();
Assert.Equal(1, tenantResults.Count());
tenantResults = client.ListTenants(DefaultTenant.ToString());
Assert.Equal(1, tenantResults.Count());
AzureSubscription subValue;
Assert.True(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SubscriptionNotFoundDoesNotThrow()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var subscriptions = new List<string> { Guid.NewGuid().ToString() };
var client = SetupTestEnvironment(tenants, subscriptions);
var subResults = new List<AzureSubscription>(client.ListSubscriptions());
Assert.Equal(1, subResults.Count);
AzureSubscription subValue;
Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NoTenantsDoesNotThrow()
{
var tenants = new List<string> { };
var subscriptions = new List<string> { Guid.NewGuid().ToString() };
var client = SetupTestEnvironment(tenants, subscriptions);
Assert.Equal(0, client.ListSubscriptions().Count());
Assert.Equal(0, client.ListTenants().Count());
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NoSubscriptionsInListThrows()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var subscriptions = new List<string> () ;
var client = SetupTestEnvironment(tenants, subscriptions);
Assert.Equal(0, client.ListSubscriptions().Count());
AzureSubscription subValue;
Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
}
}
}
Loading