Skip to content

Commit a815e19

Browse files
committed
Add requested changes
1 parent 2db8da3 commit a815e19

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

src/ResourceManager/Profile/Commands.Profile/Default/ClearAzureRmDefault.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17+
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
18+
using Microsoft.Azure.Commands.Profile.Common;
1719
using Microsoft.Azure.Commands.Profile.Properties;
1820
using Microsoft.Azure.Commands.ResourceManager.Common;
1921
using Microsoft.Azure.Management.Internal.Resources;
@@ -28,7 +30,7 @@ namespace Microsoft.Azure.Commands.Profile.Default
2830
[Cmdlet(VerbsCommon.Clear, "AzureRmDefault", DefaultParameterSetName = ResourceGroupParameterSet,
2931
SupportsShouldProcess = true)]
3032
[OutputType(typeof(bool))]
31-
public class ClearAzureRMDefaultCommand : AzureRMCmdlet
33+
public class ClearAzureRMDefaultCommand : AzureContextModificationCmdlet
3234
{
3335
private const string ResourceGroupParameterSet = "ResourceGroup";
3436

@@ -47,21 +49,23 @@ public override void ExecuteCmdlet()
4749
// If no parameters are specified, clear all defaults
4850
if (!ResourceGroup)
4951
{
50-
if (Force.IsPresent || ShouldContinue(Resources.RemoveDefaultsMessage, Resources.RemoveDefaultsCaption))
51-
{
52-
if (context.IsPropertySet(Resources.DefaultResourceGroupKey))
52+
if (ShouldProcess(string.Format(Resources.DefaultResourceGroupTarget), Resources.DefaultResourceGroupRemovalWarning)) {
53+
if (Force.IsPresent || ShouldContinue(Resources.RemoveDefaultsMessage, Resources.RemoveDefaultsCaption))
5354
{
54-
context.ExtendedProperties.Remove(Resources.DefaultResourceGroupKey);
55-
if (PassThru.IsPresent)
55+
if (context.IsPropertySet(Resources.DefaultResourceGroupKey))
5656
{
57-
WriteObject(true);
57+
ModifyContext((profile, client) => RemoveDefaultProperty(profile));
58+
if (PassThru.IsPresent)
59+
{
60+
WriteObject(true);
61+
}
5862
}
59-
}
60-
else
61-
{
62-
if (PassThru.IsPresent)
63+
else
6364
{
64-
WriteObject(false);
65+
if (PassThru.IsPresent)
66+
{
67+
WriteObject(false);
68+
}
6569
}
6670
}
6771
}
@@ -74,7 +78,7 @@ public override void ExecuteCmdlet()
7478
{
7579
if (ShouldProcess(string.Format(Resources.DefaultResourceGroupTarget), Resources.DefaultResourceGroupRemovalWarning))
7680
{
77-
context.ExtendedProperties.Remove(Resources.DefaultResourceGroupKey);
81+
ModifyContext((profile, client) => RemoveDefaultProperty(profile));
7882
if (PassThru.IsPresent)
7983
{
8084
WriteObject(true);
@@ -90,5 +94,11 @@ public override void ExecuteCmdlet()
9094
}
9195
}
9296
}
97+
98+
private void RemoveDefaultProperty(IProfileOperations profile)
99+
{
100+
var context = profile.DefaultContext;
101+
context.ExtendedProperties.Remove(Resources.DefaultResourceGroupKey);
102+
}
93103
}
94104
}

src/ResourceManager/Profile/Commands.Profile/Default/SetAzureRMDefault.cs

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

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17+
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
18+
using Microsoft.Azure.Commands.Profile.Common;
1719
using Microsoft.Azure.Commands.Profile.Models;
1820
using Microsoft.Azure.Commands.Profile.Properties;
1921
using Microsoft.Azure.Commands.ResourceManager.Common;
@@ -29,7 +31,7 @@ namespace Microsoft.Azure.Commands.Profile.Default
2931
[Cmdlet(VerbsCommon.Set, "AzureRmDefault", DefaultParameterSetName = ResourceGroupNameParameterSet,
3032
SupportsShouldProcess = true)]
3133
[OutputType(typeof(PSResourceGroup))]
32-
public class SetAzureRMDefaultCommand : AzureRMCmdlet
34+
public class SetAzureRMDefaultCommand : AzureContextModificationCmdlet
3335
{
3436
private const string ResourceGroupNameParameterSet = "ResourceGroupName";
3537

@@ -43,28 +45,34 @@ public class SetAzureRMDefaultCommand : AzureRMCmdlet
4345
public override void ExecuteCmdlet()
4446
{
4547
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
46-
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
48+
IResourceManagementClient resourceManagementclient = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
4749
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
4850
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),
4951
AzureSession.Instance.ClientFactory.GetCustomHandlers());
50-
client.SubscriptionId = context.Subscription.Id;
52+
resourceManagementclient.SubscriptionId = context.Subscription.Id;
5153

5254
if (ResourceGroupName != null)
5355
{
5456
ResourceGroup defaultResourceGroup;
5557
if (ShouldProcess(Resources.DefaultResourceGroupTarget, string.Format(Resources.DefaultResourceGroupChangeWarning, ResourceGroupName)))
5658
{
57-
if (!client.ResourceGroups.CheckExistence(ResourceGroupName) && (Force.IsPresent || ShouldContinue(string.Format(Resources.CreateResourceGroupMessage, ResourceGroupName), Resources.CreateResourceGroupCaption)))
59+
if (!resourceManagementclient.ResourceGroups.CheckExistence(ResourceGroupName) && (Force.IsPresent || ShouldContinue(string.Format(Resources.CreateResourceGroupMessage, ResourceGroupName), Resources.CreateResourceGroupCaption)))
5860
{
5961
ResourceGroup parameters = new ResourceGroup("West US");
60-
client.ResourceGroups.CreateOrUpdate(ResourceGroupName, parameters);
62+
resourceManagementclient.ResourceGroups.CreateOrUpdate(ResourceGroupName, parameters);
6163
}
62-
defaultResourceGroup = client.ResourceGroups.Get(ResourceGroupName);
63-
context.SetProperty(Resources.DefaultResourceGroupKey, defaultResourceGroup.Name);
6464

65+
defaultResourceGroup = resourceManagementclient.ResourceGroups.Get(ResourceGroupName);
66+
ModifyContext((profile, client) => SetDefaultProperty(profile));
6567
WriteObject(defaultResourceGroup);
6668
}
6769
}
6870
}
71+
72+
private void SetDefaultProperty(IProfileOperations profile)
73+
{
74+
var context = profile.DefaultContext;
75+
context.SetProperty(Resources.DefaultResourceGroupKey, ResourceGroupName);
76+
}
6977
}
7078
}

0 commit comments

Comments
 (0)