Skip to content

Add Get/Set/Clear-AzureRmDefault Cmdlets #4707

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 28 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
95cb16c
Initial work on defaults
azdevxps Sep 26, 2017
53b4af1
Add all cmdlets
Sep 27, 2017
f443ef6
Changes to cmdlets and adding tests
azdevxps Sep 29, 2017
0529806
add tests and help files
azdevxps Oct 2, 2017
07fcecd
Merge branch 'preview' of https://github.com/Azure/azure-powershell i…
azdevxps Oct 2, 2017
26202e9
Add scenario tests
azdevxps Oct 3, 2017
29f9957
cleanup
azdevxps Oct 3, 2017
c286f39
include all changes in shouldprocess
azdevxps Oct 3, 2017
a995b7f
add changes to changelog
azdevxps Oct 3, 2017
09c92ab
update csproj
Oct 3, 2017
73cb902
text error
Oct 3, 2017
cbe3a6a
add missing sessionfiles
azdevxps Oct 4, 2017
8181bba
Merge branch 'Default' of https://github.com/maddieclayton/azure-powe…
azdevxps Oct 4, 2017
4e3bc99
add required files
Oct 4, 2017
779ab67
change session files
azdevxps Oct 4, 2017
a093169
csproj changes
azdevxps Oct 4, 2017
7eda1b6
fix build error
Oct 4, 2017
2d781fd
Partially enlightened after reviewing another cmdlet pr
azdevxps Oct 4, 2017
e6a3a92
Merge branch 'Default' of https://github.com/maddieclayton/azure-powe…
azdevxps Oct 4, 2017
a827f10
some fixes
azdevxps Oct 6, 2017
6317ceb
changes to cmdlets
azdevxps Oct 9, 2017
84fe87d
fix tests
azdevxps Oct 10, 2017
05fe7c2
fix session records
azdevxps Oct 10, 2017
bd4cb0a
update help files
azdevxps Oct 10, 2017
2db8da3
fix yaml bug
azdevxps Oct 10, 2017
58d2cbd
Merge branch 'preview' into Default
Oct 18, 2017
a815e19
Add requested changes
azdevxps Oct 18, 2017
c481a9a
Merge branch 'Default' of https://github.com/maddieclayton/azure-powe…
azdevxps Oct 18, 2017
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
3 changes: 2 additions & 1 deletion src/ResourceManager/Profile/AzureRM.Profile.Netcore.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ CmdletsToExport = 'Disable-AzureRmDataCollection', 'Enable-AzureRmDataCollection
'Set-AzureRmEnvironment', 'Add-AzureRmEnvironment',
'Get-AzureRmSubscription', 'Add-AzureRmAccount', 'Get-AzureRmContext',
'Set-AzureRmContext', 'Import-AzureRmContext', 'Save-AzureRmContext',
'Get-AzureRmTenant', 'Send-Feedback', 'Resolve-AzureRmError'
'Get-AzureRmTenant', 'Send-Feedback', 'Resolve-AzureRmError',
'Set-AzureRmDefault', 'Get-AzureRmDefault', 'Clear-AzureRmDefault'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
3 changes: 2 additions & 1 deletion src/ResourceManager/Profile/AzureRM.Profile.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ CmdletsToExport = 'Disable-AzureRmDataCollection', 'Disable-AzureRmContextAutosa
'Get-AzureRmTenant', 'Send-Feedback', 'Resolve-AzureRmError',
'Select-AzureRmContext', 'Rename-AzureRmContext',
'Remove-AzureRmContext', 'Clear-AzureRmContext',
'Remove-AzureRmAccount', 'Get-AzureRmContextAutosaveSetting'
'Remove-AzureRmAccount', 'Get-AzureRmContextAutosaveSetting',
'Set-AzureRmDefault', 'Get-AzureRmDefault', 'Clear-AzureRmDefault'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
10 changes: 10 additions & 0 deletions src/ResourceManager/Profile/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
- Additional information about change #1
-->
## Current Release
- Set-AzureRmDefault
- Use this cmdlet to set a default resource group. This will make the -ResourceGroup parameter optional for some cmdlets, and will use the default when a resource group is not specified
- ```Set-AzureRmDefault -ResourceGroupName "ExampleResourceGroup"```
- If resource group specified exists in the subscription, this resource group will be set to default. Otherwise, the resource group will be created and then set to default.
- Get-AzureRmDefault
- Use this cmdlet to get the current default resource group (and other defaults in the future).
- ```Get-AzureRmDefault -ResourceGroup```
- Clear-AzureRmDefault
- Use this cmdlet to remove the current default resource group
- ```Clear-AzureRmDefault -ResourceGroup```

## Version 3.4.0
* Start-Job Support for AzureRm cmdlets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<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.Management.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ResourceManager.1.6.0-preview\lib\net452\Microsoft.Azure.Management.ResourceManager.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Test.Framework">
<HintPath>..\..\..\packages\Microsoft.Azure.Test.Framework.1.0.6179.26854-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -187,6 +191,7 @@
<Compile Include="AzureRmProfileTests.cs" />
<Compile Include="ClientFactoryTests.cs" />
<Compile Include="CommonDataCmdletTests.cs" />
<Compile Include="DefaultCmdletTests.cs" />
<Compile Include="EnvironmentCmdletTests.cs" />
<Compile Include="ErrorResolutionTests.cs" />
<Compile Include="MockDataStore.cs" />
Expand All @@ -211,12 +216,20 @@
<Content Include="SubscriptionCmdletTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="DefaultCmdletTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="App.config">
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="MSSharedLibKey.snk" />
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.DefaultCmdletTests\DefaultResourceGroup.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.ProfileModuleTests\WarningOnIncompatibleVersions.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -270,4 +283,4 @@
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------------
//
// 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.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.Resources.Test.ScenarioTests;
using Microsoft.Azure.Commands.ScenarioTest;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using System.Threading;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.Profile.Test
{
public class DefaultCmdletTests : RMTestBase
{
private XunitTracingInterceptor xunitLogger;

public DefaultCmdletTests(ITestOutputHelper output)
{
TestExecutionHelpers.SetUpSessionAndProfile();
ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);

xunitLogger = new XunitTracingInterceptor(output);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void DefaultResourceGroup()
{
ProfileController.NewInstance.RunPsTest(xunitLogger, "72f988bf-86f1-41af-91ab-2d7cd011db47", "Test-DefaultResourceGroup");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# ----------------------------------------------------------------------------------
#
# 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 cmdlets surrounding default resource group
#>
function Test-DefaultResourceGroup
{
# Setup
$rgname = Get-ResourceGroupName
Clear-AzureRmDefault -ResourceGroup

try
{
# Test GetDefault when default not set
$output = Get-AzureRmDefault
Assert-Null($output)
$output = Get-AzureRmDefault -ResourceGroup
Assert-Null($output)
$storedValue = (Get-AzureRmContext).ExtendedProperties["Default Resource Group"]
Assert-Null($storedValue)

# Test Resoure Group created when it doesn't exist
$output = Set-AzureRmDefault -ResourceGroupName $rgname -Force
$resourcegroup = Get-AzureRmResourceGroup -Name $rgname
Assert-AreEqual $output.Name $resourcegroup.ResourceGroupName
$context = Get-AzureRmContext
$storedValue = $context.ExtendedProperties["Default Resource Group"]
Assert-AreEqual $storedValue $output.Name

# Test GetDefault when default is set
$output = Get-AzureRmDefault
Assert-AreEqual $output.Name $resourceGroup.ResourceGroupName
$output = Get-AzureRmDefault -ResourceGroup
Assert-AreEqual $output.Name $resourceGroup.ResourceGroupName

# Test Clear-AzureRmDefault (no parameters shown)
Clear-AzureRmDefault -Force
$output = Get-AzureRmDefault
Assert-Null($output)
$context = Get-AzureRmContext
$storedValue = $context.ExtendedProperties["Default Resource Group"]
Assert-Null($storedValue)

# Test SetDefault when resource group exists
$output = Set-AzureRmDefault -ResourceGroupName $rgname
Assert-AreEqual $output.Name $resourcegroup.ResourceGroupName
$context = Get-AzureRmContext
$storedValue = $context.ExtendedProperties["Default Resource Group"]
Assert-AreEqual $storedValue $output.Name

# Test Clear-AzureRmDefault (no parameters shown)
Clear-AzureRmDefault -ResourceGroup
$output = Get-AzureRmDefault
Assert-Null($output)
$context = Get-AzureRmContext
$storedValue = $context.ExtendedProperties["Default Resource Group"]
Assert-Null($storedValue)
}
finally
{
Clean-ResourceGroup($rgname)
}
}

<#
.SYNOPSIS
Gets valid resource group name
#>
function Get-ResourceGroupName
{
return getAssetName
}

<#
.SYNOPSIS
Cleans the created resource groups
#>
function Clean-ResourceGroup($rgname)
{
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback) {
Remove-AzureRmResourceGroup -Name $rgname -Force
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Internal.Subscriptions;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
Expand All @@ -37,7 +38,11 @@ public sealed class ProfileController
private const string SubscriptionIdKey = "SubscriptionId";


public SubscriptionClient SubscriptionClient { get; private set; }
public Internal.Subscriptions.SubscriptionClient SubscriptionClient { get; private set; }

public ResourceManagementClient ResourceManagementClient { get; private set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all of these resourcemanagemrnt clients? Seems like we can simplify to a single one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do need both of them - one is being used in the test code, and one is being used in the cmdlet code.


public Management.Internal.Resources.ResourceManagementClient NewResourceManagementClient { get; private set; }

public string UserDomain { get; private set; }

Expand Down Expand Up @@ -133,13 +138,25 @@ private void RunPsTestWorkflow(
private void SetupManagementClients(MockContext context)
{
SubscriptionClient = GetSubscriptionClient(context);
ResourceManagementClient = GetResourceManagementClient(context);
NewResourceManagementClient = GetNewResourceManagementClient(context);

helper.SetupManagementClients(SubscriptionClient, ResourceManagementClient, NewResourceManagementClient);
}

helper.SetupManagementClients(SubscriptionClient);
private Internal.Subscriptions.SubscriptionClient GetSubscriptionClient(MockContext context)
{
return context.GetServiceClient<Internal.Subscriptions.SubscriptionClient>(TestEnvironmentFactory.GetTestEnvironment());
}

private ResourceManagementClient GetResourceManagementClient(MockContext context)
{
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}

private SubscriptionClient GetSubscriptionClient(MockContext context)
private Management.Internal.Resources.ResourceManagementClient GetNewResourceManagementClient(MockContext context)
{
return context.GetServiceClient<SubscriptionClient>(TestEnvironmentFactory.GetTestEnvironment());
return context.GetServiceClient<Management.Internal.Resources.ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
}
}
Loading