Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,60 +26,37 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
[Cmdlet(VerbsCommon.Get, "AzureResourceLock"), OutputType(typeof(PSObject))]
public class GetAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
{
/// <summary>
/// Gets or sets the at-scope filter.
/// </summary>
[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; }


/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
[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; }

/// <summary>
/// Gets the resource Id from the supplied PowerShell parameters.
/// Gets or sets the at-scope filter.
/// </summary>
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; }

/// <summary>
/// Executes the cmdlet.
/// </summary>
protected override void OnProcessRecord()
{
base.OnProcessRecord();
this.RunCmdlet();
}

/// <summary>
/// Contains the cmdlet's execution logic.
/// </summary>
private void RunCmdlet()
{
PaginatedResponseHelper.ForEach(
getFirstPage: () => this.GetResources(),
getNextPage: nextLink => this.GetNextLink<JObject>(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));
}

/// <summary>
Expand All @@ -102,7 +78,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
/// </summary>
private async Task<JObject> GetResource()
{
var resourceId = this.GetResourceId();
var resourceId = this.GetResourceId(this.LockName);

var apiVersion = await this
.DetermineApiVersion(resourceId: resourceId)
Expand All @@ -122,7 +98,7 @@ private async Task<JObject> GetResource()
/// </summary>
private async Task<ResponseWithContinuation<JObject[]>> ListResourcesTypeCollection()
{
var resourceCollectionId = this.GetResourceId();
var resourceCollectionId = this.GetResourceId(this.LockName);

var apiVersion = await this
.DetermineApiVersion(resourceId: resourceCollectionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
[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; }

/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
Expand All @@ -49,42 +60,13 @@ public class NewAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
public SwitchParameter Force { get; set; }

/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
[Alias("ExtensionResourceName")]
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the lock.")]
[ValidateNotNullOrEmpty]
public string LockName { get; set; }

/// <summary>
/// Gets the resource Id from the supplied PowerShell parameters.
/// </summary>
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);
}

/// <summary>
/// Executes the cmdlet.
/// </summary>
protected override void OnProcessRecord()
{
base.OnProcessRecord();

var resourceId = this.GetResourceId();

var resourceId = this.GetResourceId(this.LockName);
this.ConfirmAction(
this.Force,
this.GetActionMessage(resourceId),
Expand Down Expand Up @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,68 @@
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;

/// <summary>
/// The remove azure resource lock cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.Remove, "AzureResourceLock", SupportsShouldProcess = true), OutputType(typeof(PSObject))]
public class RemoveAzureResourceLockCmdlet : RemoveAzureResourceCmdlet
public class RemoveAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
{
/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
[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; }

/// <summary>
/// Gets or sets the force parameter.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
public SwitchParameter Force { get; set; }

/// <summary>
/// Executes the cmdlet.
/// </summary>
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);
});
}
}
}
}
Loading