Skip to content

Commit 0674f22

Browse files
authored
[Resources] Remove-AzResourceGroup - support parameter 'forceDeletion' (#25819)
* Remove-AzResourceGroup - support parameter 'forceDeletion' * update changelog * update logic in remove-azresourcegroup * update unit test coverage * Remove unnecessary updates
1 parent 6ffb4c9 commit 0674f22

File tree

5 files changed

+102
-19
lines changed

5 files changed

+102
-19
lines changed

src/Resources/ResourceManager/Implementation/ResourceGroups/RemoveAzureResourceGroupCmdlet.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using System.Linq;
1920
using System.Management.Automation;
2021
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
2122

@@ -48,6 +49,9 @@ public class RemoveAzureResourceGroupCmdlet : ResourceManagerCmdletBaseWithApiVe
4849
[ValidateNotNullOrEmpty]
4950
public string Id { get; set; }
5051

52+
[Parameter(Mandatory = false, HelpMessage = "The resource types you want to force delete.Currently, only the following is supported: forceDeletionTypes=Microsoft.Compute/virtualMachineScaleSets,Microsoft.Compute/virtualMachines,Microsoft.Databricks/workspaces")]
53+
public string ForceDeletionType { get; set; }
54+
5155
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
5256
public SwitchParameter Force { get; set; }
5357

@@ -58,12 +62,24 @@ protected override void OnProcessRecord()
5862
{
5963
Name = Name ?? ResourceIdentifier.FromResourceGroupIdentifier(this.Id).ResourceGroupName;
6064

61-
ConfirmAction(
62-
Force.IsPresent,
63-
string.Format(ProjectResources.RemovingResourceGroup, Name),
64-
ProjectResources.RemoveResourceGroupMessage,
65-
Name,
66-
() => ResourceManagerSdkClient.DeleteResourceGroup(Name));
65+
if (string.IsNullOrWhiteSpace(ForceDeletionType))
66+
{
67+
ConfirmAction(
68+
Force.IsPresent,
69+
string.Format(ProjectResources.RemovingResourceGroup, Name),
70+
ProjectResources.RemoveResourceGroupMessage,
71+
Name,
72+
() => ResourceManagerSdkClient.DeleteResourceGroup(Name));
73+
}
74+
else
75+
{
76+
ConfirmAction(
77+
Force.IsPresent,
78+
string.Format(ProjectResources.RemovingResourceGroup, Name),
79+
ProjectResources.RemoveResourceGroupMessage,
80+
Name,
81+
() => NewResourceManagerSdkClient.DeleteResourceGroup(Name, ForceDeletionType));
82+
}
6783

6884
WriteObject(true);
6985
}

src/Resources/ResourceManager/SdkClient/NewResourceManagerSdkClient.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public DeploymentExtended ProvisionDeploymentStatus(PSDeploymentCmdletParameters
208208

209209
Action writeProgressAction = () => this.WriteDeploymentProgress(parameters, deployment, deploymentOperationError);
210210

211-
var deploymentExtended = this.WaitDeploymentStatus(
211+
var deploymentExtended = this.WaitDeploymentStatus(
212212
getDeploymentFunc,
213213
writeProgressAction,
214214
ProvisioningState.Canceled,
@@ -217,8 +217,8 @@ public DeploymentExtended ProvisionDeploymentStatus(PSDeploymentCmdletParameters
217217

218218
if (deploymentOperationError.ErrorMessages.Count > 0)
219219
{
220-
WriteError(GetDeploymentErrorMessagesWithOperationId(deploymentOperationError,
221-
parameters.DeploymentName,
220+
WriteError(GetDeploymentErrorMessagesWithOperationId(deploymentOperationError,
221+
parameters.DeploymentName,
222222
deploymentExtended?.Properties?.CorrelationId));
223223
}
224224

@@ -260,11 +260,11 @@ private void WriteDeploymentProgress(PSDeploymentCmdletParameters parameters, De
260260
}
261261
else
262262
{
263-
deploymentOperationError.ProcessError(operation);
263+
deploymentOperationError.ProcessError(operation);
264264
}
265265
}
266266
}
267-
267+
268268
private DeploymentExtended WaitDeploymentStatus(
269269
Func<Task<AzureOperationResponse<DeploymentExtended>>> getDeployment,
270270
Action listDeploymentOperations,
@@ -680,7 +680,7 @@ private Dictionary<string, List<string>> ConvertAuxTenantDictionary(IDictionary<
680680
{
681681
if (auxTenants == null) return null;
682682

683-
var headers = new Dictionary<string, List<string>> ();
683+
var headers = new Dictionary<string, List<string>>();
684684
foreach (KeyValuePair<string, IList<string>> entry in auxTenants)
685685
{
686686
headers[entry.Key] = entry.Value.ToList();
@@ -950,6 +950,27 @@ public virtual void DeleteResourceGroup(string name)
950950
}
951951
}
952952

953+
/// <summary>
954+
/// Deletes a given resource group
955+
/// </summary>
956+
/// <param name="name">The resource group name</param>
957+
/// <param name="forceDeletionTypes">
958+
/// The resource types you want to force delete. Currently, only the following
959+
/// is supported:
960+
/// forceDeletionTypes=Microsoft.Compute/virtualMachines,Microsoft.Compute/virtualMachineScaleSets
961+
/// </param>
962+
public virtual void DeleteResourceGroup(string name, string forceDeletionTypes)
963+
{
964+
if (!ResourceManagementClient.ResourceGroups.CheckExistence(name))
965+
{
966+
WriteError(ProjectResources.ResourceGroupDoesntExists);
967+
}
968+
else
969+
{
970+
ResourceManagementClient.ResourceGroups.Delete(name, forceDeletionTypes);
971+
}
972+
}
973+
953974
/// <summary>
954975
/// Filters the resource group deployments with provided options
955976
/// </summary>
@@ -1691,7 +1712,8 @@ private void CancelDeploymentAtResourceGroup(List<PSDeployment> deployments, str
16911712
/// <returns>The validation errors if there's any, or empty list otherwise.</returns>
16921713
public virtual List<PSResourceManagerError> ValidateDeployment(PSDeploymentCmdletParameters parameters)
16931714
{
1694-
if (parameters.DeploymentName == null){
1715+
if (parameters.DeploymentName == null)
1716+
{
16951717
parameters.DeploymentName = GenerateDeploymentName(parameters);
16961718
}
16971719
Deployment deployment = CreateBasicDeployment(parameters, parameters.DeploymentMode, null);
@@ -1729,7 +1751,7 @@ public string GetDeploymentErrorMessagesWithOperationId(DeploymentOperationError
17291751
.AppendLine());
17301752

17311753
// Add correlationId
1732-
sb.AppendLine().AppendFormat(ProjectResources.DeploymentCorrelationId, correlationId);
1754+
sb.AppendLine().AppendFormat(ProjectResources.DeploymentCorrelationId, correlationId);
17331755

17341756
return sb.ToString();
17351757
}

src/Resources/Resources.Test/ResourceGroups/RemoveAzureResourceGroupCommandTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ public class RemoveAzureResourceGroupCommandTests : RMTestBase
3030

3131
private Mock<ResourceManagerSdkClient> resourcesClientMock;
3232

33+
private Mock<NewResourceManagerSdkClient> newResourceClientMock;
34+
3335
private Mock<ICommandRuntime> commandRuntimeMock;
3436

3537
private string resourceGroupName = "myResourceGroup";
3638
private string resourceGroupId = "/subscriptions/subId/resourceGroups/myResourceGroup";
3739
private string resourceId = "/subscriptions/subId/resourceGroups/myResourceGroup/providers/myResourceProvider/resourceType/myResource";
40+
private string resourceForceDeletionType = "Microsoft.Compute/virtualMachineScaleSets,Microsoft.Compute/virtualMachines,Microsoft.Databricks/workspaces";
3841

3942
public RemoveAzureResourceGroupCommandTests(ITestOutputHelper output)
4043
{
4144
resourcesClientMock = new Mock<ResourceManagerSdkClient>();
45+
newResourceClientMock = new Mock<NewResourceManagerSdkClient>();
4246
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
4347
commandRuntimeMock = new Mock<ICommandRuntime>();
4448
cmdlet = new RemoveAzureResourceGroupCmdlet()
4549
{
4650
CommandRuntime = commandRuntimeMock.Object,
47-
ResourceManagerSdkClient = resourcesClientMock.Object
51+
ResourceManagerSdkClient = resourcesClientMock.Object,
52+
NewResourceManagerSdkClient = newResourceClientMock.Object
4853
};
4954
}
5055

@@ -99,5 +104,21 @@ public void RemovesResourceGroupFromResourceId()
99104

100105
resourcesClientMock.Verify(f => f.DeleteResourceGroup(resourceGroupName), Times.Never());
101106
}
107+
108+
[Fact]
109+
[Trait(Category.AcceptanceType, Category.CheckIn)]
110+
public void RemovesResourceGroupFromForceDeletionType()
111+
{
112+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
113+
newResourceClientMock.Setup(f => f.DeleteResourceGroup(resourceGroupName, resourceForceDeletionType));
114+
115+
cmdlet.Name = resourceGroupName;
116+
cmdlet.ForceDeletionType = resourceForceDeletionType;
117+
cmdlet.Force = true;
118+
119+
cmdlet.ExecuteCmdlet();
120+
121+
newResourceClientMock.Verify(f => f.DeleteResourceGroup(resourceGroupName, resourceForceDeletionType), Times.Once());
122+
}
102123
}
103124
}

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* `Remove-AzResourceGroup` - support parameter "[-ForceDeletionType]".
2223
* Removed specific characters from the codebase to unblock digital signature verification.
2324

2425
## Version 7.3.0

src/Resources/Resources/help/Remove-AzResourceGroup.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Removes a resource group.
1515

1616
### RemoveByResourceGroupName (Default)
1717
```
18-
Remove-AzResourceGroup [-Name] <String> [-Force] [-AsJob] [-ApiVersion <String>] [-Pre]
19-
[-DefaultProfile <IAzureContextContainer>] [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm]
18+
Remove-AzResourceGroup [-Name] <String> [-ForceDeletionType <String>] [-Force] [-AsJob] [-ApiVersion <String>]
19+
[-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
2020
[<CommonParameters>]
2121
```
2222

2323
### RemoveByResourceGroupId
2424
```
25-
Remove-AzResourceGroup -Id <String> [-Force] [-AsJob] [-ApiVersion <String>] [-Pre]
26-
[-DefaultProfile <IAzureContextContainer>] [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm]
25+
Remove-AzResourceGroup -Id <String> [-ForceDeletionType <String>] [-Force] [-AsJob] [-ApiVersion <String>]
26+
[-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
2727
[<CommonParameters>]
2828
```
2929

@@ -55,6 +55,14 @@ Get-AzResourceGroup | Remove-AzResourceGroup
5555

5656
This command uses the **Get-AzResourceGroup** cmdlet to get all resource groups, and then passes them to **Remove-AzResourceGroup** by using the pipeline operator.
5757

58+
### Example 4: Remove a resource groups use ForceDeletionType
59+
```powershell
60+
Remove-AzResourceGroup -Name "ContosoRG01" -ForceDeletionType "Microsoft.Compute/virtualMachineScaleSets,Microsoft.Compute/virtualMachines,Microsoft.Databricks/workspaces"
61+
```
62+
63+
This command removes the ContosoRG01 resource group use the ForceDeletionType.
64+
The cmdlet prompts you for confirmation and returns no output.
65+
5866
## PARAMETERS
5967

6068
### -ApiVersion
@@ -118,6 +126,21 @@ Accept pipeline input: False
118126
Accept wildcard characters: False
119127
```
120128
129+
### -ForceDeletionType
130+
The resource types you want to force delete.Currently, only the following is supported: forceDeletionTypes=Microsoft.Compute/virtualMachines,Microsoft.Compute/virtualMachineScaleSets
131+
132+
```yaml
133+
Type: System.String
134+
Parameter Sets: (All)
135+
Aliases:
136+
137+
Required: False
138+
Position: Named
139+
Default value: None
140+
Accept pipeline input: False
141+
Accept wildcard characters: False
142+
```
143+
121144
### -Id
122145
Specifies the ID of resource group to remove.
123146
Wildcard characters are not permitted.

0 commit comments

Comments
 (0)