Skip to content

Commit fd831ff

Browse files
CLOUDSTACK-5193 [Hyper-V] VHDs not deleted post VM destroy and expunge
1 parent 93ea31c commit fd831ff

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/HypervResourceController.cs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,64 @@ public JContainer DestroyCommand([FromBody]dynamic cmd)
419419
}
420420
}
421421

422+
// POST api/HypervResource/DeleteCommand
423+
[HttpPost]
424+
[ActionName(CloudStackTypes.DeleteCommand)]
425+
public JContainer DeleteCommand([FromBody]dynamic cmd)
426+
{
427+
using (log4net.NDC.Push(Guid.NewGuid().ToString()))
428+
{
429+
logger.Info(CloudStackTypes.DestroyCommand + cmd.ToString());
430+
431+
string details = null;
432+
bool result = false;
433+
434+
try
435+
{
436+
// Assert
437+
String errMsg = "No 'volume' details in " + CloudStackTypes.DestroyCommand + " " + cmd.ToString();
438+
VolumeObjectTO destVolumeObjectTO = VolumeObjectTO.ParseJson(cmd.data);
439+
440+
if (destVolumeObjectTO.name == null)
441+
{
442+
logger.Error(errMsg);
443+
throw new ArgumentException(errMsg);
444+
}
445+
446+
String path = destVolumeObjectTO.FullFileName;
447+
if (!File.Exists(path))
448+
{
449+
logger.Info(CloudStackTypes.DestroyCommand + ", but volume at pass already deleted " + path);
450+
}
451+
452+
string vmName = (string)cmd.vmName;
453+
if (!string.IsNullOrEmpty(vmName) && File.Exists(path))
454+
{
455+
// Make sure that this resource is removed from the VM
456+
wmiCallsV2.DetachDisk(vmName, path);
457+
}
458+
459+
File.Delete(path);
460+
result = true;
461+
}
462+
catch (Exception sysEx)
463+
{
464+
details = CloudStackTypes.DestroyCommand + " failed due to " + sysEx.Message;
465+
logger.Error(details, sysEx);
466+
}
467+
468+
object ansContent = new
469+
{
470+
result = result,
471+
details = details,
472+
contextMap = contextMap
473+
};
474+
475+
return ReturnCloudStackTypedJArray(ansContent, CloudStackTypes.Answer);
476+
}
477+
}
478+
479+
422480
private static JArray ReturnCloudStackTypedJArray(object ansContent, string ansType)
423481
{
424482
JObject ansObj = Utils.CreateCloudStackObject(ansType, ansContent);

0 commit comments

Comments
 (0)