Skip to content

Commit 19e8291

Browse files
committed
Update RemoveAzureResourceLockCmdlet.cs
Make remove lock
1 parent 2f0f889 commit 19e8291

File tree

1 file changed

+160
-2
lines changed

1 file changed

+160
-2
lines changed

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

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,171 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17+
using System;
1718
using System.Management.Automation;
19+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1820

1921
/// <summary>
2022
/// The remove azure resource lock cmdlet.
2123
/// </summary>
2224
[Cmdlet(VerbsCommon.Remove, "AzureResourceLock", SupportsShouldProcess = true), OutputType(typeof(PSObject))]
23-
public class RemoveAzureResourceLockCmdlet : RemoveAzureResourceCmdlet
25+
public class RemoveAzureResourceLockCmdlet : ResourceManagerCmdletBase
2426
{
27+
/// <summary>
28+
/// The resource group level resource lock.
29+
/// </summary>
30+
internal const string ScopeAndName = "A lock, by the scope and name.";
31+
32+
/// <summary>
33+
/// The resource group level resource lock.
34+
/// </summary>
35+
internal const string ResourceGroupResourceLevelLock = "A lock at the resource group resource scope.";
36+
37+
/// <summary>
38+
/// The subscription level resource lock.
39+
/// </summary>
40+
internal const string SubscriptionResourceLevelLock = "A lock at the subscription resource scope.";
41+
42+
/// <summary>
43+
/// The tenant level resource lock patameter set.
44+
/// </summary>
45+
internal const string TenantResourceLevelLock = "A lock at the tenant resource scope.";
46+
47+
/// <summary>
48+
/// The resource group lock parametere set.
49+
/// </summary>
50+
internal const string ResourceGroupLevelLock = "A lock at the resource group scope.";
51+
52+
/// <summary>
53+
/// The subscription lock parameter set.
54+
/// </summary>
55+
internal const string SubscriptionLevelLock = "A lock at the subscription scope.";
56+
57+
/// <summary>
58+
/// Gets or sets the lock Id.
59+
/// </summary>
60+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ScopeAndName, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The Id of the lock. e.g. '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaserName}', to specify a resoruce group: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}'")]
61+
[ValidateNotNullOrEmpty]
62+
public string Scope { get; set; }
63+
64+
/// <summary>
65+
/// Gets or sets the extension resource name parameter.
66+
/// </summary>
67+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
68+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
69+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
70+
[ValidateNotNullOrEmpty]
71+
public string ResourceName { get; set; }
72+
73+
/// <summary>
74+
/// Gets or sets the resource type parameter.
75+
/// </summary>
76+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
77+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
78+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
79+
[ValidateNotNullOrEmpty]
80+
public string ResourceType { get; set; }
81+
82+
/// <summary>
83+
/// Gets or sets the subscription id parameter.
84+
/// </summary>
85+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
86+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
87+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.SubscriptionLevelLock, Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
88+
[ValidateNotNullOrEmpty]
89+
public Guid? SubscriptionId { get; set; }
90+
91+
/// <summary>
92+
/// Gets or sets the resource group name parameter.
93+
/// </summary>
94+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupResourceLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
95+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.ResourceGroupLevelLock, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
96+
[ValidateNotNullOrEmpty]
97+
public string ResourceGroupName { get; set; }
98+
99+
/// <summary>
100+
/// Gets or sets the tenant level parameter.
101+
/// </summary>
102+
[Parameter(ParameterSetName = ResourceLockManagementCmdletBase.TenantResourceLevelLock, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")]
103+
public SwitchParameter TenantLevel { get; set; }
104+
105+
/// <summary>
106+
/// Gets or sets the extension resource name parameter.
107+
/// </summary>
108+
[Alias("ExtensionResourceName")]
109+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the lock.")]
110+
[ValidateNotNullOrEmpty]
111+
public string LockName { get; set; }
112+
113+
/// <summary>
114+
/// Initializes the default subscription id if needed.
115+
/// </summary>
116+
protected override void OnProcessRecord()
117+
{
118+
if (string.IsNullOrWhiteSpace(this.Scope) && this.SubscriptionId == null && !this.TenantLevel)
119+
{
120+
this.SubscriptionId = this.Profile.Context.Subscription.Id;
121+
}
122+
123+
base.OnProcessRecord();
124+
}
125+
126+
/// <summary>
127+
/// Gets the resource Id from the supplied PowerShell parameters.
128+
/// </summary>
129+
protected string GetResourceId()
130+
{
131+
return !string.IsNullOrWhiteSpace(this.Scope)
132+
? ResourceIdUtility.GetResourceId(
133+
resourceId: this.Scope,
134+
extensionResourceType: Constants.MicrosoftAuthorizationLocksType,
135+
extensionResourceName: this.LockName)
136+
: ResourceIdUtility.GetResourceId(
137+
subscriptionId: this.SubscriptionId,
138+
resourceGroupName: this.ResourceGroupName,
139+
resourceType: this.ResourceType,
140+
resourceName: this.ResourceName,
141+
extensionResourceType: Constants.MicrosoftAuthorizationLocksType,
142+
extensionResourceName: this.LockName);
143+
}
144+
145+
/// <summary>
146+
/// Executes the cmdlet.
147+
/// </summary>
148+
protected override void OnProcessRecord()
149+
{
150+
base.OnProcessRecord();
151+
152+
var resourceId = this.GetResourceId();
153+
154+
this.ConfirmAction(
155+
this.Force,
156+
string.Format("Are you sure you want to delete the following lock: {0}", resourceId),
157+
"Deleting the lock...",
158+
resourceId,
159+
() =>
160+
{
161+
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
162+
163+
var operationResult = this.GetResourcesClient()
164+
.DeleteResource(
165+
resourceId: resourceId,
166+
apiVersion: apiVersion,
167+
cancellationToken: this.CancellationToken.Value)
168+
.Result;
169+
170+
var managementUri = this.GetResourcesClient()
171+
.GetResourceManagementRequestUri(
172+
resourceId: resourceId,
173+
apiVersion: apiVersion);
174+
175+
var activity = string.Format("DELETE {0}", managementUri.PathAndQuery);
176+
177+
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
178+
.WaitOnOperation(operationResult: operationResult);
179+
180+
this.WriteObject(result, ResourceObjectFormat.New);
181+
});
182+
}
25183
}
26-
}
184+
}

0 commit comments

Comments
 (0)