Skip to content

Commit b287d55

Browse files
author
Hovsep
committed
Merge pull request #208 from markcowl/selectProfile
Add Select-AzureProfile cmdlet
2 parents afbb2ff + eeffdee commit b287d55

File tree

8 files changed

+328
-1
lines changed

8 files changed

+328
-1
lines changed

src/Common/Commands.Profile/Commands.Profile.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Models\AzureProfileSettings.cs" />
131131
<Compile Include="Models\PSAzureSubscription.cs" />
132132
<Compile Include="Models\PSAzureSubscriptionExtended.cs" />
133+
<Compile Include="Profile\SelectAzureProfile.cs" />
133134
<Compile Include="Profile\NewAzureProfile.cs" />
134135
<Compile Include="Profile\ClearAzureProfile.cs" />
135136
<Compile Include="SwitchAzureMode.cs" />

src/Common/Commands.Profile/Profile/ClearAzureProfile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void ExecuteCmdlet()
4545

4646
public void RemoveProfileProcess()
4747
{
48-
ProfileClient.ClearAll();
48+
ProfileClient.ClearAll();
4949
}
5050
}
5151
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections;
17+
using System.IO;
18+
using System.Linq;
19+
using System.Security;
20+
using System.Security.Cryptography.X509Certificates;
21+
using Microsoft.Azure.Common.Authentication;
22+
using Microsoft.Azure.Common.Authentication.Models;
23+
using Microsoft.WindowsAzure.Commands.Common.Properties;
24+
using Microsoft.WindowsAzure.Commands.Profile.Models;
25+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
26+
using System.Management.Automation;
27+
using System.Security.Permissions;
28+
using Microsoft.WindowsAzure.Commands.Utilities.Profile;
29+
30+
namespace Microsoft.WindowsAzure.Commands.Profile
31+
{
32+
/// <summary>
33+
/// Creates new Microsoft Azure profile.
34+
/// </summary>
35+
[Cmdlet(VerbsCommon.Select, "AzureProfile"), OutputType(typeof(AzureProfile))]
36+
public class SelectAzureProfileCommand : SubscriptionCmdletBase
37+
{
38+
internal const string NewProfileParameterSet = "NewProfile";
39+
internal const string DefaultProfilePrameterSet = "DefaultProfile";
40+
41+
[Parameter(ParameterSetName=NewProfileParameterSet, Mandatory=true, Position=0, ValueFromPipelineByPropertyName=true)]
42+
public new AzureProfile Profile { get; set; }
43+
44+
[Parameter(ParameterSetName=DefaultProfilePrameterSet, Mandatory=true)]
45+
public SwitchParameter Default { get; set; }
46+
47+
public SelectAzureProfileCommand() : base(true)
48+
{
49+
}
50+
51+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
52+
public override void ExecuteCmdlet()
53+
{
54+
if (ParameterSetName == DefaultProfilePrameterSet)
55+
{
56+
Profile = AzurePSCmdlet.DefaultProfile;
57+
}
58+
59+
if (Profile == null)
60+
{
61+
throw new ArgumentException("Selected profile must not be null.");
62+
}
63+
64+
AzurePSCmdlet.CurrentProfile = Profile;
65+
AzurePSCmdlet.UpdateSessionStateForProfile(AzurePSCmdlet.CurrentProfile);
66+
WriteObject(Profile);
67+
}
68+
}
69+
}

src/ServiceManagement/Services/Commands.Test/Commands.Test.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
<Compile Include="MediaServices\NewAzureMediaServicesKeyTest.cs" />
229229
<Compile Include="MediaServices\NewMediaServicesAccountTests.cs" />
230230
<Compile Include="MediaServices\RemoveMediaServicesAccountTests.cs" />
231+
<Compile Include="Profile\SelectAzureProfileTests.cs" />
231232
<Compile Include="Profile\NewAzureProfileTests.cs" />
232233
<Compile Include="Profile\ProfileClientHelper.cs" />
233234
<Compile Include="Profile\ProfileCmdltsTests.cs" />
@@ -353,9 +354,18 @@
353354
</Content>
354355
<None Include="packages.config" />
355356
<None Include="Performance\Microsoft.ServiceBus.MessagingPerformanceCounters.man" />
357+
<None Include="Profile\SelectAzureProfileTests.ps1">
358+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
359+
</None>
356360
<None Include="Profile\NewAzureProfileTests.ps1">
357361
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
358362
</None>
363+
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.SelectAzureProfileTests\TestMakeArmCallWithCreatedProfile.json">
364+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
365+
</None>
366+
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.SelectAzureProfileTests\TestMakeRdfeCallWithCreatedProfile.json">
367+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
368+
</None>
359369
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.NewAzureProfileTests\TestCreatesNewAzureProfileWithAccessToken.json">
360370
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
361371
</None>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
16+
using Xunit;
17+
18+
namespace Microsoft.Azure.Commands.Test.Profile
19+
{
20+
public class SelectAzureProfileTests
21+
{
22+
[Fact]
23+
[Trait(Category.AcceptanceType, Category.CheckIn)]
24+
public void TestSelectDefaultProfile()
25+
{
26+
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-SelectDefaultProfile {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
27+
}
28+
29+
[Fact]
30+
[Trait(Category.AcceptanceType, Category.CheckIn)]
31+
public void TestMakeArmCallWithCreatedProfile()
32+
{
33+
ProfileTestController.NewARMInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInARMMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
34+
}
35+
36+
[Fact]
37+
[Trait(Category.AcceptanceType, Category.CheckIn)]
38+
public void TestMakeRdfeCallWithCreatedProfile()
39+
{
40+
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInARMMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
41+
}
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Tests creating new azure profile with access token
18+
#>
19+
function Test-SelectDefaultProfile
20+
{
21+
param([string] $token, [string] $user, [string] $sub)
22+
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
23+
Assert-AreEqual "AzureCloud" $profile.Context.Environment.Name
24+
Select-AzureProfile $profile
25+
Select-AzureProfile -Default
26+
$current = [Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet]::CurrentProfile
27+
Assert-NotNull $($current.ProfilePath)
28+
}
29+
30+
<#
31+
.SYNOPSIS
32+
Tests using a profile to run an RDFE cmdlet
33+
#>
34+
function Test-NewAzureProfileInRDFEMode
35+
{
36+
param([string] $token, [string] $user, [string] $sub)
37+
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
38+
Assert-AreEqual "AzureCloud" $profile.Context.Environment.Name
39+
Select-AzureProfile $profile
40+
$locations = Get-AzureLocation
41+
Assert-NotNull $locations
42+
Assert-True {$locations.Count -gt 1}
43+
}
44+
45+
<#
46+
.SYNOPSIS
47+
Tests using a profile to run an ARM cmdlet
48+
#>
49+
function Test-NewAzureProfileInARMMode
50+
{
51+
param([string] $token, [string] $user, [string] $sub)
52+
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
53+
Assert-AreEqual "AzureCloud" $($profile.Context.Environment.Name) "Expecting the azure cloud environment"
54+
Select-AzureProfile $profile
55+
$locations = Get-AzureLocation
56+
Assert-NotNull $locations
57+
Assert-True {$locations.Count -gt 1}
58+
}

src/ServiceManagement/Services/Commands.Test/SessionRecords/Microsoft.Azure.Commands.Test.Profile.SelectAzureProfileTests/TestMakeArmCallWithCreatedProfile.json

Lines changed: 56 additions & 0 deletions
Large diffs are not rendered by default.

src/ServiceManagement/Services/Commands.Test/SessionRecords/Microsoft.Azure.Commands.Test.Profile.SelectAzureProfileTests/TestMakeRdfeCallWithCreatedProfile.json

Lines changed: 90 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)