Skip to content

Commit 342cf64

Browse files
author
maddieclayton
authored
Merge pull request Azure#4707 from maddieclayton/Default
Add Get/Set/Clear-AzureRmDefault Cmdlets
2 parents 0dc60c7 + c481a9a commit 342cf64

File tree

18 files changed

+19407
-8
lines changed

18 files changed

+19407
-8
lines changed

src/ResourceManager/Profile/AzureRM.Profile.Netcore.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ CmdletsToExport = 'Disable-AzureRmDataCollection', 'Enable-AzureRmDataCollection
7777
'Set-AzureRmEnvironment', 'Add-AzureRmEnvironment',
7878
'Get-AzureRmSubscription', 'Add-AzureRmAccount', 'Get-AzureRmContext',
7979
'Set-AzureRmContext', 'Import-AzureRmContext', 'Save-AzureRmContext',
80-
'Get-AzureRmTenant', 'Send-Feedback', 'Resolve-AzureRmError'
80+
'Get-AzureRmTenant', 'Send-Feedback', 'Resolve-AzureRmError',
81+
'Set-AzureRmDefault', 'Get-AzureRmDefault', 'Clear-AzureRmDefault'
8182

8283
# Variables to export from this module
8384
# VariablesToExport = @()

src/ResourceManager/Profile/AzureRM.Profile.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ CmdletsToExport = 'Disable-AzureRmDataCollection', 'Disable-AzureRmContextAutosa
8282
'Get-AzureRmTenant', 'Send-Feedback', 'Resolve-AzureRmError',
8383
'Select-AzureRmContext', 'Rename-AzureRmContext',
8484
'Remove-AzureRmContext', 'Clear-AzureRmContext',
85-
'Remove-AzureRmAccount', 'Get-AzureRmContextAutosaveSetting'
85+
'Remove-AzureRmAccount', 'Get-AzureRmContextAutosaveSetting',
86+
'Set-AzureRmDefault', 'Get-AzureRmDefault', 'Clear-AzureRmDefault'
8687

8788
# Variables to export from this module
8889
# VariablesToExport = @()

src/ResourceManager/Profile/ChangeLog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
- Set-AzureRmDefault
22+
- 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
23+
- ```Set-AzureRmDefault -ResourceGroupName "ExampleResourceGroup"```
24+
- 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.
25+
- Get-AzureRmDefault
26+
- Use this cmdlet to get the current default resource group (and other defaults in the future).
27+
- ```Get-AzureRmDefault -ResourceGroup```
28+
- Clear-AzureRmDefault
29+
- Use this cmdlet to remove the current default resource group
30+
- ```Clear-AzureRmDefault -ResourceGroup```
2131

2232
## Version 3.4.1
2333
* LocationCompleterAttribute added and available for cmdlets which use the -Location parameter

src/ResourceManager/Profile/Commands.Profile.Test/Commands.Profile.Test.csproj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
<HintPath>..\..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
6565
<Private>True</Private>
6666
</Reference>
67+
<Reference Include="Microsoft.Azure.Management.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
68+
<SpecificVersion>False</SpecificVersion>
69+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ResourceManager.1.6.0-preview\lib\net452\Microsoft.Azure.Management.ResourceManager.dll</HintPath>
70+
</Reference>
6771
<Reference Include="Microsoft.Azure.Test.Framework">
6872
<HintPath>..\..\..\packages\Microsoft.Azure.Test.Framework.1.0.6179.26854-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
6973
<Private>True</Private>
@@ -188,6 +192,7 @@
188192
<Compile Include="ClientFactoryTests.cs" />
189193
<Compile Include="CommonDataCmdletTests.cs" />
190194
<Compile Include="ContextModelTests.cs" />
195+
<Compile Include="DefaultCmdletTests.cs" />
191196
<Compile Include="EnvironmentCmdletTests.cs" />
192197
<Compile Include="ErrorResolutionTests.cs" />
193198
<Compile Include="LocationCompleterUnitTests.cs" />
@@ -214,12 +219,20 @@
214219
<Content Include="SubscriptionCmdletTests.ps1">
215220
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
216221
</Content>
222+
<Content Include="DefaultCmdletTests.ps1">
223+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
224+
</Content>
217225
<None Include="App.config">
218226
<SubType>Designer</SubType>
219227
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
220228
</None>
221229
<None Include="MSSharedLibKey.snk" />
222-
<None Include="packages.config" />
230+
<None Include="packages.config">
231+
<SubType>Designer</SubType>
232+
</None>
233+
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.DefaultCmdletTests\DefaultResourceGroup.json">
234+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
235+
</None>
223236
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.ProfileModuleTests\WarningOnIncompatibleVersions.json">
224237
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
225238
</None>
@@ -273,4 +286,4 @@
273286
<ItemGroup />
274287
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
275288
<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')" />
276-
</Project>
289+
</Project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.Azure.Commands.ResourceManager.Common;
16+
using Microsoft.Azure.Commands.Resources.Test.ScenarioTests;
17+
using Microsoft.Azure.Commands.ScenarioTest;
18+
using Microsoft.Azure.ServiceManagemenet.Common.Models;
19+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
20+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
21+
using System.Threading;
22+
using Xunit;
23+
using Xunit.Abstractions;
24+
25+
namespace Microsoft.Azure.Commands.Profile.Test
26+
{
27+
public class DefaultCmdletTests : RMTestBase
28+
{
29+
private XunitTracingInterceptor xunitLogger;
30+
31+
public DefaultCmdletTests(ITestOutputHelper output)
32+
{
33+
TestExecutionHelpers.SetUpSessionAndProfile();
34+
ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
35+
36+
xunitLogger = new XunitTracingInterceptor(output);
37+
}
38+
39+
[Fact]
40+
[Trait(Category.AcceptanceType, Category.CheckIn)]
41+
public void DefaultResourceGroup()
42+
{
43+
ProfileController.NewInstance.RunPsTest(xunitLogger, "72f988bf-86f1-41af-91ab-2d7cd011db47", "Test-DefaultResourceGroup");
44+
}
45+
}
46+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 cmdlets surrounding default resource group
18+
#>
19+
function Test-DefaultResourceGroup
20+
{
21+
# Setup
22+
$rgname = Get-ResourceGroupName
23+
Clear-AzureRmDefault -ResourceGroup
24+
25+
try
26+
{
27+
# Test GetDefault when default not set
28+
$output = Get-AzureRmDefault
29+
Assert-Null($output)
30+
$output = Get-AzureRmDefault -ResourceGroup
31+
Assert-Null($output)
32+
$storedValue = (Get-AzureRmContext).ExtendedProperties["Default Resource Group"]
33+
Assert-Null($storedValue)
34+
35+
# Test Resoure Group created when it doesn't exist
36+
$output = Set-AzureRmDefault -ResourceGroupName $rgname -Force
37+
$resourcegroup = Get-AzureRmResourceGroup -Name $rgname
38+
Assert-AreEqual $output.Name $resourcegroup.ResourceGroupName
39+
$context = Get-AzureRmContext
40+
$storedValue = $context.ExtendedProperties["Default Resource Group"]
41+
Assert-AreEqual $storedValue $output.Name
42+
43+
# Test GetDefault when default is set
44+
$output = Get-AzureRmDefault
45+
Assert-AreEqual $output.Name $resourceGroup.ResourceGroupName
46+
$output = Get-AzureRmDefault -ResourceGroup
47+
Assert-AreEqual $output.Name $resourceGroup.ResourceGroupName
48+
49+
# Test Clear-AzureRmDefault (no parameters shown)
50+
Clear-AzureRmDefault -Force
51+
$output = Get-AzureRmDefault
52+
Assert-Null($output)
53+
$context = Get-AzureRmContext
54+
$storedValue = $context.ExtendedProperties["Default Resource Group"]
55+
Assert-Null($storedValue)
56+
57+
# Test SetDefault when resource group exists
58+
$output = Set-AzureRmDefault -ResourceGroupName $rgname
59+
Assert-AreEqual $output.Name $resourcegroup.ResourceGroupName
60+
$context = Get-AzureRmContext
61+
$storedValue = $context.ExtendedProperties["Default Resource Group"]
62+
Assert-AreEqual $storedValue $output.Name
63+
64+
# Test Clear-AzureRmDefault (no parameters shown)
65+
Clear-AzureRmDefault -ResourceGroup
66+
$output = Get-AzureRmDefault
67+
Assert-Null($output)
68+
$context = Get-AzureRmContext
69+
$storedValue = $context.ExtendedProperties["Default Resource Group"]
70+
Assert-Null($storedValue)
71+
}
72+
finally
73+
{
74+
Clean-ResourceGroup($rgname)
75+
}
76+
}
77+
78+
<#
79+
.SYNOPSIS
80+
Gets valid resource group name
81+
#>
82+
function Get-ResourceGroupName
83+
{
84+
return getAssetName
85+
}
86+
87+
<#
88+
.SYNOPSIS
89+
Cleans the created resource groups
90+
#>
91+
function Clean-ResourceGroup($rgname)
92+
{
93+
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback) {
94+
Remove-AzureRmResourceGroup -Name $rgname -Force
95+
}
96+
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Internal.Subscriptions;
17+
using Microsoft.Azure.Management.ResourceManager;
1718
using Microsoft.Azure.ServiceManagemenet.Common.Models;
1819
using Microsoft.Azure.Test.HttpRecorder;
1920
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
@@ -37,7 +38,11 @@ public sealed class ProfileController
3738
private const string SubscriptionIdKey = "SubscriptionId";
3839

3940

40-
public SubscriptionClient SubscriptionClient { get; private set; }
41+
public Internal.Subscriptions.SubscriptionClient SubscriptionClient { get; private set; }
42+
43+
public ResourceManagementClient ResourceManagementClient { get; private set; }
44+
45+
public Management.Internal.Resources.ResourceManagementClient NewResourceManagementClient { get; private set; }
4146

4247
public string UserDomain { get; private set; }
4348

@@ -133,13 +138,25 @@ private void RunPsTestWorkflow(
133138
private void SetupManagementClients(MockContext context)
134139
{
135140
SubscriptionClient = GetSubscriptionClient(context);
141+
ResourceManagementClient = GetResourceManagementClient(context);
142+
NewResourceManagementClient = GetNewResourceManagementClient(context);
143+
144+
helper.SetupManagementClients(SubscriptionClient, ResourceManagementClient, NewResourceManagementClient);
145+
}
136146

137-
helper.SetupManagementClients(SubscriptionClient);
147+
private Internal.Subscriptions.SubscriptionClient GetSubscriptionClient(MockContext context)
148+
{
149+
return context.GetServiceClient<Internal.Subscriptions.SubscriptionClient>(TestEnvironmentFactory.GetTestEnvironment());
150+
}
151+
152+
private ResourceManagementClient GetResourceManagementClient(MockContext context)
153+
{
154+
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
138155
}
139156

140-
private SubscriptionClient GetSubscriptionClient(MockContext context)
157+
private Management.Internal.Resources.ResourceManagementClient GetNewResourceManagementClient(MockContext context)
141158
{
142-
return context.GetServiceClient<SubscriptionClient>(TestEnvironmentFactory.GetTestEnvironment());
159+
return context.GetServiceClient<Management.Internal.Resources.ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
143160
}
144161
}
145162
}

0 commit comments

Comments
 (0)