Skip to content

Commit dd2b55a

Browse files
committed
Add -WhatIf to Remove-AzureVM
1 parent d01407d commit dd2b55a

File tree

6 files changed

+54
-31
lines changed

6 files changed

+54
-31
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/BVTTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ public void AzureIaaSBVT()
379379
//
380380
// Remove-AzureVM
381381
//
382+
vmPowershellCmdlets.RemoveAzureVM(newAzureVMName, serviceName, false, true);
383+
Assert.AreNotEqual(null, vmPowershellCmdlets.GetAzureVM(newAzureVMName, serviceName));
384+
382385
vmPowershellCmdlets.RemoveAzureVM(newAzureVMName, serviceName);
383386

384387
RecordTimeTaken(ref prevTime);

src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/IaasCmdletInfo/RemoveAzureVMCmdletInfo.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests
1919
{
2020
public class RemoveAzureVMCmdletInfo : CmdletsInfo
2121
{
22-
public RemoveAzureVMCmdletInfo(string vmName, string serviceName, bool deleteVhd)
22+
public RemoveAzureVMCmdletInfo(string vmName, string serviceName, bool deleteVhd, bool whatif)
2323
{
2424
this.cmdletName = Utilities.RemoveAzureVMCmdletName;
2525
this.cmdletParams.Add(new CmdletParam("Name", vmName));
@@ -29,6 +29,11 @@ public RemoveAzureVMCmdletInfo(string vmName, string serviceName, bool deleteVhd
2929
{
3030
this.cmdletParams.Add(new CmdletParam("DeleteVhd"));
3131
}
32+
33+
if (whatif)
34+
{
35+
this.cmdletParams.Add(new CmdletParam("WhatIf"));
36+
}
3237
}
3338
}
3439
}

src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,9 +1446,9 @@ public SM.PersistentVMRoleContext GetAzureVM(string vmName, string serviceName)
14461446
return RunPSCmdletAndReturnFirst<SM.PersistentVMRoleContext>(new GetAzureVMCmdletInfo(vmName, serviceName));
14471447
}
14481448

1449-
public ManagementOperationContext RemoveAzureVM(string vmName, string serviceName, bool deleteVhd = false)
1449+
public ManagementOperationContext RemoveAzureVM(string vmName, string serviceName, bool deleteVhd = false, bool whatif = false)
14501450
{
1451-
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureVMCmdletInfo(vmName, serviceName, deleteVhd));
1451+
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureVMCmdletInfo(vmName, serviceName, deleteVhd, whatif));
14521452
}
14531453

14541454
public ManagementOperationContext StartAzureVM(string vmName, string serviceName)

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/PersistentVMs/RemoveAzureVM.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS
2525
{
26-
[Cmdlet(VerbsCommon.Remove, "AzureVM"), OutputType(typeof(ManagementOperationContext))]
26+
[Cmdlet(VerbsCommon.Remove, "AzureVM", SupportsShouldProcess = true), OutputType(typeof(ManagementOperationContext))]
2727
public class RemoveAzureVMCommand : IaaSDeploymentManagementCmdletBase
2828
{
2929
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the role to remove.")]
@@ -43,38 +43,41 @@ public SwitchParameter DeleteVHD
4343

4444
protected override void ExecuteCommand()
4545
{
46-
ServiceManagementProfile.Initialize();
47-
48-
base.ExecuteCommand();
49-
if (CurrentDeploymentNewSM == null)
46+
if (this.ShouldProcess(String.Format("Service: {0}, VM: {1}", this.ServiceName, this.Name), Resources.RemoveAzureVMShouldProcessAction))
5047
{
51-
return;
52-
}
48+
ServiceManagementProfile.Initialize();
5349

54-
DeploymentGetResponse deploymentGetResponse = this.ComputeClient.Deployments.GetBySlot(this.ServiceName, DeploymentSlot.Production);
55-
if (deploymentGetResponse.Roles.FirstOrDefault(r => r.RoleName.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) == null)
56-
{
57-
throw new ArgumentOutOfRangeException(String.Format(Resources.RoleInstanceCanNotBeFoundWithName, Name));
58-
}
50+
base.ExecuteCommand();
51+
if (CurrentDeploymentNewSM == null)
52+
{
53+
return;
54+
}
5955

60-
if (deploymentGetResponse.RoleInstances.Count > 1)
61-
{
62-
ExecuteClientActionNewSM(
63-
null,
64-
CommandRuntime.ToString(),
65-
() => this.ComputeClient.VirtualMachines.Delete(this.ServiceName, CurrentDeploymentNewSM.Name, Name, DeleteVHD.IsPresent));
66-
}
67-
else
68-
{
69-
if (deploymentGetResponse != null && !string.IsNullOrEmpty(deploymentGetResponse.ReservedIPName))
56+
DeploymentGetResponse deploymentGetResponse = this.ComputeClient.Deployments.GetBySlot(this.ServiceName, DeploymentSlot.Production);
57+
if (deploymentGetResponse.Roles.FirstOrDefault(r => r.RoleName.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) == null)
7058
{
71-
WriteVerboseWithTimestamp(string.Format(Resources.ReservedIPNameNoLongerInUseByDeletingLastVMButStillBeingReserved, deploymentGetResponse.ReservedIPName));
59+
throw new ArgumentOutOfRangeException(String.Format(Resources.RoleInstanceCanNotBeFoundWithName, Name));
7260
}
7361

74-
ExecuteClientActionNewSM<AzureOperationResponse>(
75-
null,
76-
CommandRuntime.ToString(),
77-
() => this.ComputeClient.Deployments.DeleteByName(this.ServiceName, CurrentDeploymentNewSM.Name, DeleteVHD.IsPresent));
62+
if (deploymentGetResponse.RoleInstances.Count > 1)
63+
{
64+
ExecuteClientActionNewSM(
65+
null,
66+
CommandRuntime.ToString(),
67+
() => this.ComputeClient.VirtualMachines.Delete(this.ServiceName, CurrentDeploymentNewSM.Name, Name, DeleteVHD.IsPresent));
68+
}
69+
else
70+
{
71+
if (deploymentGetResponse != null && !string.IsNullOrEmpty(deploymentGetResponse.ReservedIPName))
72+
{
73+
WriteVerboseWithTimestamp(string.Format(Resources.ReservedIPNameNoLongerInUseByDeletingLastVMButStillBeingReserved, deploymentGetResponse.ReservedIPName));
74+
}
75+
76+
ExecuteClientActionNewSM<AzureOperationResponse>(
77+
null,
78+
CommandRuntime.ToString(),
79+
() => this.ComputeClient.Deployments.DeleteByName(this.ServiceName, CurrentDeploymentNewSM.Name, DeleteVHD.IsPresent));
80+
}
7881
}
7982
}
8083
}

src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,4 +753,7 @@
753753
<value>Upload '{0}'</value>
754754
<comment>{0} is the name of an storage blob</comment>
755755
</data>
756+
<data name="RemoveAzureVMShouldProcessAction" xml:space="preserve">
757+
<value>Deleting VM</value>
758+
</data>
756759
</root>

0 commit comments

Comments
 (0)