From 491a10be0c77716ead01961f50328f5d81c6497c Mon Sep 17 00:00:00 2001 From: Rohit Yadav Date: Wed, 10 Apr 2019 17:12:55 +0530 Subject: [PATCH] storage: publish delete usage event for snapshot deletion (#3212) Problem: Users are billed for destroyed VMs with VM snapshots because usage records don't get that the VM and VM snapshots are removed. Root Cause: The destroyVirtualMachine and expungeVirtualMachine APIs were removing VM snapshots but not generating VMSNAPSHOT.DELETE usage event due to which the VM snapshots were not marked as removed in the usage_vmsnapshot table. Solution: The issue was fixed by emitting the proper usage event for all the VM snapshots of a VM that is destroyed. --- .../storage/vmsnapshot/DefaultVMSnapshotStrategy.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/vmsnapshot/DefaultVMSnapshotStrategy.java b/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/vmsnapshot/DefaultVMSnapshotStrategy.java index ebe8b27b207..19777f08e3f 100644 --- a/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/vmsnapshot/DefaultVMSnapshotStrategy.java +++ b/engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/vmsnapshot/DefaultVMSnapshotStrategy.java @@ -423,6 +423,12 @@ public class DefaultVMSnapshotStrategy extends ManagerBase implements VMSnapshot s_logger.debug("Failed to change vm snapshot state with event ExpungeRequested"); throw new CloudRuntimeException("Failed to change vm snapshot state with event ExpungeRequested: " + e.getMessage()); } + UserVm userVm = userVmDao.findById(vmSnapshot.getVmId()); + List volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId()); + for (VolumeObjectTO volumeTo: volumeTOs) { + volumeTo.setSize(0); + publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_DELETE, vmSnapshot, userVm, volumeTo); + } return vmSnapshotDao.remove(vmSnapshot.getId()); } }