From 1948fb0cff62d85cd69e5bf12f7eef76891ebcb1 Mon Sep 17 00:00:00 2001 From: vivsriaus Date: Mon, 15 Jun 2015 15:29:55 -0700 Subject: [PATCH] Refactor resource lock cmdlets --- .../GetAzureResourceLockCmdlet.cs | 50 +++------ .../NewAzureResourceLockCmdlet.cs | 48 +++------ .../RemoveAzureResourceLockCmdlet.cs | 60 ++++++++++- .../ResourceLockManagementCmdletBase.cs | 101 +++++++++++++++--- 4 files changed, 173 insertions(+), 86 deletions(-) diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/GetAzureResourceLockCmdlet.cs b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/GetAzureResourceLockCmdlet.cs index b562e849ffcf..cca6cb33ab7f 100644 --- a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/GetAzureResourceLockCmdlet.cs +++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/GetAzureResourceLockCmdlet.cs @@ -17,7 +17,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation using System.Management.Automation; using System.Threading.Tasks; using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components; - using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources; using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; using Newtonsoft.Json.Linq; @@ -27,40 +26,25 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation [Cmdlet(VerbsCommon.Get, "AzureResourceLock"), OutputType(typeof(PSObject))] public class GetAzureResourceLockCmdlet : ResourceLockManagementCmdletBase { - /// - /// Gets or sets the at-scope filter. - /// - [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.")] - [ValidateNotNullOrEmpty] - public SwitchParameter AtScope { get; set; } - - /// /// Gets or sets the extension resource name parameter. /// [Alias("ExtensionResourceName")] - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] [ValidateNotNullOrEmpty] public string LockName { get; set; } /// - /// Gets the resource Id from the supplied PowerShell parameters. + /// Gets or sets the at-scope filter. /// - protected string GetResourceId() - { - return !string.IsNullOrWhiteSpace(this.Scope) - ? ResourceIdUtility.GetResourceId( - resourceId: this.Scope, - extensionResourceType: Constants.MicrosoftAuthorizationLocksType, - extensionResourceName: this.LockName) - : ResourceIdUtility.GetResourceId( - subscriptionId: this.SubscriptionId, - resourceGroupName: this.ResourceGroupName, - resourceType: this.ResourceType, - resourceName: this.ResourceName, - extensionResourceType: Constants.MicrosoftAuthorizationLocksType, - extensionResourceName: this.LockName); - } + [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.")] + [ValidateNotNullOrEmpty] + public SwitchParameter AtScope { get; set; } /// /// Executes the cmdlet. @@ -68,19 +52,11 @@ protected string GetResourceId() protected override void OnProcessRecord() { base.OnProcessRecord(); - this.RunCmdlet(); - } - - /// - /// Contains the cmdlet's execution logic. - /// - private void RunCmdlet() - { PaginatedResponseHelper.ForEach( getFirstPage: () => this.GetResources(), getNextPage: nextLink => this.GetNextLink(nextLink), cancellationToken: this.CancellationToken, - action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToPsObject(ResourceObjectFormat.New)), enumerateCollection: true)); + action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects(resources), enumerateCollection: true)); } /// @@ -102,7 +78,7 @@ private async Task> GetResources() /// private async Task GetResource() { - var resourceId = this.GetResourceId(); + var resourceId = this.GetResourceId(this.LockName); var apiVersion = await this .DetermineApiVersion(resourceId: resourceId) @@ -122,7 +98,7 @@ private async Task GetResource() /// private async Task> ListResourcesTypeCollection() { - var resourceCollectionId = this.GetResourceId(); + var resourceCollectionId = this.GetResourceId(this.LockName); var apiVersion = await this .DetermineApiVersion(resourceId: resourceCollectionId) diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceLockCmdlet.cs b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceLockCmdlet.cs index b670dc2c48f6..c07f9f786951 100644 --- a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceLockCmdlet.cs +++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceLockCmdlet.cs @@ -15,9 +15,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation { using System.Management.Automation; - using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components; using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Locks; - using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources; using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; using Newtonsoft.Json.Linq; @@ -27,6 +25,19 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation [Cmdlet(VerbsCommon.New, "AzureResourceLock", SupportsShouldProcess = true, DefaultParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock), OutputType(typeof(PSObject))] public class NewAzureResourceLockCmdlet : ResourceLockManagementCmdletBase { + /// + /// Gets or sets the extension resource name parameter. + /// + [Alias("ExtensionResourceName")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [ValidateNotNullOrEmpty] + public string LockName { get; set; } + /// /// Gets or sets the extension resource name parameter. /// @@ -49,42 +60,13 @@ public class NewAzureResourceLockCmdlet : ResourceLockManagementCmdletBase [Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")] public SwitchParameter Force { get; set; } - /// - /// Gets or sets the extension resource name parameter. - /// - [Alias("ExtensionResourceName")] - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the lock.")] - [ValidateNotNullOrEmpty] - public string LockName { get; set; } - - /// - /// Gets the resource Id from the supplied PowerShell parameters. - /// - protected string GetResourceId() - { - return !string.IsNullOrWhiteSpace(this.Scope) - ? ResourceIdUtility.GetResourceId( - resourceId: this.Scope, - extensionResourceType: Constants.MicrosoftAuthorizationLocksType, - extensionResourceName: this.LockName) - : ResourceIdUtility.GetResourceId( - subscriptionId: this.SubscriptionId, - resourceGroupName: this.ResourceGroupName, - resourceType: this.ResourceType, - resourceName: this.ResourceName, - extensionResourceType: Constants.MicrosoftAuthorizationLocksType, - extensionResourceName: this.LockName); - } - /// /// Executes the cmdlet. /// protected override void OnProcessRecord() { base.OnProcessRecord(); - - var resourceId = this.GetResourceId(); - + var resourceId = this.GetResourceId(this.LockName); this.ConfirmAction( this.Force, this.GetActionMessage(resourceId), @@ -112,7 +94,7 @@ protected override void OnProcessRecord() var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true) .WaitOnOperation(operationResult: operationResult); - this.WriteObject(result, ResourceObjectFormat.New); + this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true); }); } diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceLockCmdlet.cs b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceLockCmdlet.cs index c9ebee95ed8d..bbd552ff4b0e 100644 --- a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceLockCmdlet.cs +++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceLockCmdlet.cs @@ -15,12 +15,68 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation { using System.Management.Automation; + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; /// /// The remove azure resource lock cmdlet. /// [Cmdlet(VerbsCommon.Remove, "AzureResourceLock", SupportsShouldProcess = true), OutputType(typeof(PSObject))] - public class RemoveAzureResourceLockCmdlet : RemoveAzureResourceCmdlet + public class RemoveAzureResourceLockCmdlet : ResourceLockManagementCmdletBase { + /// + /// Gets or sets the extension resource name parameter. + /// + [Alias("ExtensionResourceName")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The name of the lock.")] + [ValidateNotNullOrEmpty] + public string LockName { get; set; } + + /// + /// Gets or sets the force parameter. + /// + [Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")] + public SwitchParameter Force { get; set; } + + /// + /// Executes the cmdlet. + /// + protected override void OnProcessRecord() + { + base.OnProcessRecord(); + var resourceId = this.GetResourceId(this.LockName); + this.ConfirmAction( + this.Force, + string.Format("Are you sure you want to delete the following lock: {0}", resourceId), + "Deleting the lock...", + resourceId, + () => + { + var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result; + + var operationResult = this.GetResourcesClient() + .DeleteResource( + resourceId: resourceId, + apiVersion: apiVersion, + cancellationToken: this.CancellationToken.Value) + .Result; + + var managementUri = this.GetResourcesClient() + .GetResourceManagementRequestUri( + resourceId: resourceId, + apiVersion: apiVersion); + + var activity = string.Format("DELETE {0}", managementUri.PathAndQuery); + + var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false) + .WaitOnOperation(operationResult: operationResult); + + this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true); + }); + } } -} \ No newline at end of file +} diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/ResourceLockManagementCmdletBase.cs b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/ResourceLockManagementCmdletBase.cs index 8a02e36c0f5e..19d01241f5e1 100644 --- a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/ResourceLockManagementCmdletBase.cs +++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/ResourceLockManagementCmdletBase.cs @@ -15,13 +15,23 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation { using System; + using System.Linq; using System.Management.Automation; + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components; + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources; + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; + using Newtonsoft.Json.Linq; /// /// Base class for resource lock management cmdlets. /// public abstract class ResourceLockManagementCmdletBase : ResourceManagerCmdletBase { + /// + /// The Id parameter set. + /// + internal const string LockIdParameterSet = "A lock, by Id."; + /// /// The resource group level resource lock. /// @@ -55,43 +65,44 @@ public abstract class ResourceLockManagementCmdletBase : ResourceManagerCmdletBa /// /// Gets or sets the scope. /// - [Alias("Id")] + [Alias("Id", "ResourceId")] [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The scope. e.g. to specify a database '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaserName}', to specify a resoruce group: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}'")] [ValidateNotNullOrEmpty] public string Scope { get; set; } /// - /// Gets or sets the extension resource name parameter. + /// Gets or sets the resource name parameter. /// - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")] [ValidateNotNullOrEmpty] public string ResourceName { get; set; } /// /// Gets or sets the resource type parameter. /// - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")] [ValidateNotNullOrEmpty] public string ResourceType { get; set; } /// /// Gets or sets the subscription id parameter. /// - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The subscription to use.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The subscription to use.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The subscription to use.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The subscription to use.")] [ValidateNotNullOrEmpty] public Guid? SubscriptionId { get; set; } /// /// Gets or sets the resource group name parameter. /// - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")] - [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource group name.")] + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource group name.")] [ValidateNotNullOrEmpty] public string ResourceGroupName { get; set; } @@ -101,17 +112,79 @@ public abstract class ResourceLockManagementCmdletBase : ResourceManagerCmdletBa [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")] public SwitchParameter TenantLevel { get; set; } + /// + /// The Id of the lock. + /// + [Parameter(ParameterSetName = ResourceLockManagementCmdletBase.LockIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The Id of the lock.")] + [ValidateNotNullOrEmpty] + public string LockId { get; set; } + /// /// Initializes the default subscription id if needed. /// protected override void OnProcessRecord() { - if (string.IsNullOrWhiteSpace(this.Scope) && this.SubscriptionId == null && !this.TenantLevel) + if (string.IsNullOrWhiteSpace(this.LockId) && + string.IsNullOrWhiteSpace(this.Scope) && + this.SubscriptionId == null && + !this.TenantLevel) { this.SubscriptionId = this.Profile.Context.Subscription.Id; } base.OnProcessRecord(); } + + /// + /// Gets the resource Id from the supplied PowerShell parameters. + /// + /// The name of the lock. + protected string GetResourceId(string lockName) + { + if (!string.IsNullOrWhiteSpace(this.LockId)) + { + var resourceType = ResourceIdUtility.GetResourceType(this.LockId); + var extensionResourceType = ResourceIdUtility.GetExtensionResourceType(this.LockId); + + if ((resourceType.EqualsInsensitively(Constants.MicrosoftAuthorizationLocksType) && + string.IsNullOrWhiteSpace(extensionResourceType)) || + extensionResourceType.EqualsInsensitively(Constants.MicrosoftAuthorizationLocksType)) + { + return this.LockId; + } + + throw new InvalidOperationException(string.Format("The Id '{0}' does not belong to a lock.", this.LockId)); + } + + return !string.IsNullOrWhiteSpace(this.Scope) + ? ResourceIdUtility.GetResourceId( + resourceId: this.Scope, + extensionResourceType: Constants.MicrosoftAuthorizationLocksType, + extensionResourceName: lockName) + : ResourceIdUtility.GetResourceId( + subscriptionId: this.SubscriptionId, + resourceGroupName: this.ResourceGroupName, + resourceType: this.ResourceType, + resourceName: this.ResourceName, + extensionResourceType: Constants.MicrosoftAuthorizationLocksType, + extensionResourceName: lockName); + } + + /// + /// Converts the resource object to output that can be piped to the lock cmdlets. + /// + /// The lock resource object. + protected PSObject[] GetOutputObjects(params JToken[] resources) + { + return resources + .CoalesceEnumerable() + .Where(resource => resource != null) + .SelectArray(resource => + { + var psobject = resource.ToResource().ToPsObject(ResourceObjectFormat.New); + psobject.Properties.Add(new PSNoteProperty("LockId", psobject.Properties["ResourceId"].Value)); + return psobject; + }); + } } } \ No newline at end of file