Skip to content

Add Select-AzureProfile cmdlet #208

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 2 commits into from
Mar 2, 2015
Merged
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 src/Common/Commands.Profile/Commands.Profile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="Models\AzureProfileSettings.cs" />
<Compile Include="Models\PSAzureSubscription.cs" />
<Compile Include="Models\PSAzureSubscriptionExtended.cs" />
<Compile Include="Profile\SelectAzureProfile.cs" />
<Compile Include="Profile\NewAzureProfile.cs" />
<Compile Include="Profile\ClearAzureProfile.cs" />
<Compile Include="SwitchAzureMode.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Profile/Profile/ClearAzureProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void ExecuteCmdlet()

public void RemoveProfileProcess()
{
ProfileClient.ClearAll();
ProfileClient.ClearAll();
}
}
}
69 changes: 69 additions & 0 deletions src/Common/Commands.Profile/Profile/SelectAzureProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// ----------------------------------------------------------------------------------
//
// 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 System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using Microsoft.WindowsAzure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Management.Automation;
using System.Security.Permissions;
using Microsoft.WindowsAzure.Commands.Utilities.Profile;

namespace Microsoft.WindowsAzure.Commands.Profile
{
/// <summary>
/// Creates new Microsoft Azure profile.
/// </summary>
[Cmdlet(VerbsCommon.Select, "AzureProfile"), OutputType(typeof(AzureProfile))]
public class SelectAzureProfileCommand : SubscriptionCmdletBase
{
internal const string NewProfileParameterSet = "NewProfile";
internal const string DefaultProfilePrameterSet = "DefaultProfile";

[Parameter(ParameterSetName=NewProfileParameterSet, Mandatory=true, Position=0, ValueFromPipelineByPropertyName=true)]
public new AzureProfile Profile { get; set; }

[Parameter(ParameterSetName=DefaultProfilePrameterSet, Mandatory=true)]
public SwitchParameter Default { get; set; }

public SelectAzureProfileCommand() : base(true)
{
}

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public override void ExecuteCmdlet()
{
if (ParameterSetName == DefaultProfilePrameterSet)
{
Profile = AzurePSCmdlet.DefaultProfile;
}

if (Profile == null)
{
throw new ArgumentException("Selected profile must not be null.");
}

AzurePSCmdlet.CurrentProfile = Profile;
AzurePSCmdlet.UpdateSessionStateForProfile(AzurePSCmdlet.CurrentProfile);
WriteObject(Profile);
}
}
}
10 changes: 10 additions & 0 deletions src/ServiceManagement/Services/Commands.Test/Commands.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<Compile Include="MediaServices\NewAzureMediaServicesKeyTest.cs" />
<Compile Include="MediaServices\NewMediaServicesAccountTests.cs" />
<Compile Include="MediaServices\RemoveMediaServicesAccountTests.cs" />
<Compile Include="Profile\SelectAzureProfileTests.cs" />
<Compile Include="Profile\NewAzureProfileTests.cs" />
<Compile Include="Profile\ProfileClientHelper.cs" />
<Compile Include="Profile\ProfileCmdltsTests.cs" />
Expand Down Expand Up @@ -353,9 +354,18 @@
</Content>
<None Include="packages.config" />
<None Include="Performance\Microsoft.ServiceBus.MessagingPerformanceCounters.man" />
<None Include="Profile\SelectAzureProfileTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Profile\NewAzureProfileTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.SelectAzureProfileTests\TestMakeArmCallWithCreatedProfile.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.SelectAzureProfileTests\TestMakeRdfeCallWithCreatedProfile.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Test.Profile.NewAzureProfileTests\TestCreatesNewAzureProfileWithAccessToken.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ----------------------------------------------------------------------------------
//
// 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.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Test.Profile
{
public class SelectAzureProfileTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSelectDefaultProfile()
{
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-SelectDefaultProfile {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestMakeArmCallWithCreatedProfile()
{
ProfileTestController.NewARMInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInARMMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestMakeRdfeCallWithCreatedProfile()
{
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInARMMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------

<#
.SYNOPSIS
Tests creating new azure profile with access token
#>
function Test-SelectDefaultProfile
{
param([string] $token, [string] $user, [string] $sub)
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
Assert-AreEqual "AzureCloud" $profile.Context.Environment.Name
Select-AzureProfile $profile
Select-AzureProfile -Default
$current = [Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet]::CurrentProfile
Assert-NotNull $($current.ProfilePath)
}

<#
.SYNOPSIS
Tests using a profile to run an RDFE cmdlet
#>
function Test-NewAzureProfileInRDFEMode
{
param([string] $token, [string] $user, [string] $sub)
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
Assert-AreEqual "AzureCloud" $profile.Context.Environment.Name
Select-AzureProfile $profile
$locations = Get-AzureLocation
Assert-NotNull $locations
Assert-True {$locations.Count -gt 1}
}

<#
.SYNOPSIS
Tests using a profile to run an ARM cmdlet
#>
function Test-NewAzureProfileInARMMode
{
param([string] $token, [string] $user, [string] $sub)
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
Assert-AreEqual "AzureCloud" $($profile.Context.Environment.Name) "Expecting the azure cloud environment"
Select-AzureProfile $profile
$locations = Get-AzureLocation
Assert-NotNull $locations
Assert-True {$locations.Count -gt 1}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.