Skip to content

Commit a259013

Browse files
committed
Merge pull request #506 from vivsriaus/dev
Refactor resource lock cmdlets
2 parents 5007887 + 1948fb0 commit a259013

File tree

4 files changed

+173
-86
lines changed

4 files changed

+173
-86
lines changed

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/GetAzureResourceLockCmdlet.cs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1717
using System.Management.Automation;
1818
using System.Threading.Tasks;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
20-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2120
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2221
using Newtonsoft.Json.Linq;
2322

@@ -27,60 +26,37 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2726
[Cmdlet(VerbsCommon.Get, "AzureResourceLock"), OutputType(typeof(PSObject))]
2827
public class GetAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
2928
{
30-
/// <summary>
31-
/// Gets or sets the at-scope filter.
32-
/// </summary>
33-
[Parameter(Mandatory = false, HelpMessage = "When specified returns all locks at or above the specified scope, otherwise returns all locks at, above or below the scope.")]
34-
[ValidateNotNullOrEmpty]
35-
public SwitchParameter AtScope { get; set; }
36-
37-
3829
/// <summary>
3930
/// Gets or sets the extension resource name parameter.
4031
/// </summary>
4132
[Alias("ExtensionResourceName")]
42-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the lock.")]
33+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
34+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
35+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
36+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
37+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
38+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
4339
[ValidateNotNullOrEmpty]
4440
public string LockName { get; set; }
4541

4642
/// <summary>
47-
/// Gets the resource Id from the supplied PowerShell parameters.
43+
/// Gets or sets the at-scope filter.
4844
/// </summary>
49-
protected string GetResourceId()
50-
{
51-
return !string.IsNullOrWhiteSpace(this.Scope)
52-
? ResourceIdUtility.GetResourceId(
53-
resourceId: this.Scope,
54-
extensionResourceType: Constants.MicrosoftAuthorizationLocksType,
55-
extensionResourceName: this.LockName)
56-
: ResourceIdUtility.GetResourceId(
57-
subscriptionId: this.SubscriptionId,
58-
resourceGroupName: this.ResourceGroupName,
59-
resourceType: this.ResourceType,
60-
resourceName: this.ResourceName,
61-
extensionResourceType: Constants.MicrosoftAuthorizationLocksType,
62-
extensionResourceName: this.LockName);
63-
}
45+
[Parameter(Mandatory = false, HelpMessage = "When specified returns all locks at or above the specified scope, otherwise returns all locks at, above or below the scope.")]
46+
[ValidateNotNullOrEmpty]
47+
public SwitchParameter AtScope { get; set; }
6448

6549
/// <summary>
6650
/// Executes the cmdlet.
6751
/// </summary>
6852
protected override void OnProcessRecord()
6953
{
7054
base.OnProcessRecord();
71-
this.RunCmdlet();
72-
}
73-
74-
/// <summary>
75-
/// Contains the cmdlet's execution logic.
76-
/// </summary>
77-
private void RunCmdlet()
78-
{
7955
PaginatedResponseHelper.ForEach(
8056
getFirstPage: () => this.GetResources(),
8157
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
8258
cancellationToken: this.CancellationToken,
83-
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToPsObject(ResourceObjectFormat.New)), enumerateCollection: true));
59+
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects(resources), enumerateCollection: true));
8460
}
8561

8662
/// <summary>
@@ -102,7 +78,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
10278
/// </summary>
10379
private async Task<JObject> GetResource()
10480
{
105-
var resourceId = this.GetResourceId();
81+
var resourceId = this.GetResourceId(this.LockName);
10682

10783
var apiVersion = await this
10884
.DetermineApiVersion(resourceId: resourceId)
@@ -122,7 +98,7 @@ private async Task<JObject> GetResource()
12298
/// </summary>
12399
private async Task<ResponseWithContinuation<JObject[]>> ListResourcesTypeCollection()
124100
{
125-
var resourceCollectionId = this.GetResourceId();
101+
var resourceCollectionId = this.GetResourceId(this.LockName);
126102

127103
var apiVersion = await this
128104
.DetermineApiVersion(resourceId: resourceCollectionId)

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceLockCmdlet.cs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
1717
using System.Management.Automation;
18-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1918
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Locks;
20-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2119
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2220
using Newtonsoft.Json.Linq;
2321

@@ -27,6 +25,19 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2725
[Cmdlet(VerbsCommon.New, "AzureResourceLock", SupportsShouldProcess = true, DefaultParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock), OutputType(typeof(PSObject))]
2826
public class NewAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
2927
{
28+
/// <summary>
29+
/// Gets or sets the extension resource name parameter.
30+
/// </summary>
31+
[Alias("ExtensionResourceName")]
32+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
33+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
34+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
35+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
36+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
37+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
38+
[ValidateNotNullOrEmpty]
39+
public string LockName { get; set; }
40+
3041
/// <summary>
3142
/// Gets or sets the extension resource name parameter.
3243
/// </summary>
@@ -49,42 +60,13 @@ public class NewAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
4960
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
5061
public SwitchParameter Force { get; set; }
5162

52-
/// <summary>
53-
/// Gets or sets the extension resource name parameter.
54-
/// </summary>
55-
[Alias("ExtensionResourceName")]
56-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the lock.")]
57-
[ValidateNotNullOrEmpty]
58-
public string LockName { get; set; }
59-
60-
/// <summary>
61-
/// Gets the resource Id from the supplied PowerShell parameters.
62-
/// </summary>
63-
protected string GetResourceId()
64-
{
65-
return !string.IsNullOrWhiteSpace(this.Scope)
66-
? ResourceIdUtility.GetResourceId(
67-
resourceId: this.Scope,
68-
extensionResourceType: Constants.MicrosoftAuthorizationLocksType,
69-
extensionResourceName: this.LockName)
70-
: ResourceIdUtility.GetResourceId(
71-
subscriptionId: this.SubscriptionId,
72-
resourceGroupName: this.ResourceGroupName,
73-
resourceType: this.ResourceType,
74-
resourceName: this.ResourceName,
75-
extensionResourceType: Constants.MicrosoftAuthorizationLocksType,
76-
extensionResourceName: this.LockName);
77-
}
78-
7963
/// <summary>
8064
/// Executes the cmdlet.
8165
/// </summary>
8266
protected override void OnProcessRecord()
8367
{
8468
base.OnProcessRecord();
85-
86-
var resourceId = this.GetResourceId();
87-
69+
var resourceId = this.GetResourceId(this.LockName);
8870
this.ConfirmAction(
8971
this.Force,
9072
this.GetActionMessage(resourceId),
@@ -112,7 +94,7 @@ protected override void OnProcessRecord()
11294
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
11395
.WaitOnOperation(operationResult: operationResult);
11496

115-
this.WriteObject(result, ResourceObjectFormat.New);
97+
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
11698
});
11799
}
118100

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceLockCmdlet.cs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,68 @@
1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
1717
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1819

1920
/// <summary>
2021
/// The remove azure resource lock cmdlet.
2122
/// </summary>
2223
[Cmdlet(VerbsCommon.Remove, "AzureResourceLock", SupportsShouldProcess = true), OutputType(typeof(PSObject))]
23-
public class RemoveAzureResourceLockCmdlet : RemoveAzureResourceCmdlet
24+
public class RemoveAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
2425
{
26+
/// <summary>
27+
/// Gets or sets the extension resource name parameter.
28+
/// </summary>
29+
[Alias("ExtensionResourceName")]
30+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
31+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
32+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
33+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
34+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
35+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")]
36+
[ValidateNotNullOrEmpty]
37+
public string LockName { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets the force parameter.
41+
/// </summary>
42+
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
43+
public SwitchParameter Force { get; set; }
44+
45+
/// <summary>
46+
/// Executes the cmdlet.
47+
/// </summary>
48+
protected override void OnProcessRecord()
49+
{
50+
base.OnProcessRecord();
51+
var resourceId = this.GetResourceId(this.LockName);
52+
this.ConfirmAction(
53+
this.Force,
54+
string.Format("Are you sure you want to delete the following lock: {0}", resourceId),
55+
"Deleting the lock...",
56+
resourceId,
57+
() =>
58+
{
59+
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
60+
61+
var operationResult = this.GetResourcesClient()
62+
.DeleteResource(
63+
resourceId: resourceId,
64+
apiVersion: apiVersion,
65+
cancellationToken: this.CancellationToken.Value)
66+
.Result;
67+
68+
var managementUri = this.GetResourcesClient()
69+
.GetResourceManagementRequestUri(
70+
resourceId: resourceId,
71+
apiVersion: apiVersion);
72+
73+
var activity = string.Format("DELETE {0}", managementUri.PathAndQuery);
74+
75+
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
76+
.WaitOnOperation(operationResult: operationResult);
77+
78+
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
79+
});
80+
}
2581
}
26-
}
82+
}

0 commit comments

Comments
 (0)