|
39 | 39 | import javax.inject.Inject; |
40 | 40 | import javax.naming.ConfigurationException; |
41 | 41 |
|
| 42 | +import com.cloud.server.ResourceTag; |
| 43 | +import com.cloud.server.TaggedResourceService; |
42 | 44 | import org.apache.cloudstack.acl.ControlledEntity.ACLType; |
43 | 45 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
44 | 46 | import org.apache.cloudstack.affinity.AffinityGroupService; |
@@ -472,6 +474,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir |
472 | 474 | private TemplateApiService _tmplService; |
473 | 475 | @Inject |
474 | 476 | private ConfigurationDao _configDao; |
| 477 | + @Inject |
| 478 | + private TaggedResourceService _taggedResourceService; |
475 | 479 |
|
476 | 480 | private ScheduledExecutorService _executor = null; |
477 | 481 | private ScheduledExecutorService _vmIpFetchExecutor = null; |
@@ -2818,6 +2822,28 @@ public UserVm destroyVm(DestroyVMCmd cmd) throws ResourceUnavailableException, C |
2818 | 2822 | return destroyedVm; |
2819 | 2823 | } |
2820 | 2824 |
|
| 2825 | + protected Boolean removeTagsFromVm(long vmId) { |
| 2826 | + UserVmVO vm = _vmDao.findById(vmId); |
| 2827 | + Long resourceId = _taggedResourceService.getResourceId(vm.getUuid(), ResourceTag.ResourceObjectType.UserVm); |
| 2828 | + List<? extends ResourceTag> resourceTags = _taggedResourceService.listByResourceTypeAndId(ResourceTag.ResourceObjectType.UserVm, resourceId); |
| 2829 | + if (resourceTags.size() > 0) { |
| 2830 | + List<String> resourceIds = Arrays.asList(vm.getUuid()); |
| 2831 | + Map<String, String> tags = new HashMap<>(); |
| 2832 | + return _taggedResourceService.deleteTags(resourceIds, ResourceTag.ResourceObjectType.UserVm, tags); |
| 2833 | + } else { |
| 2834 | + return false; |
| 2835 | + } |
| 2836 | + |
| 2837 | + } |
| 2838 | + |
| 2839 | + public void setTaggedResourceService(TaggedResourceService taggedResourceServiceMock) { |
| 2840 | + _taggedResourceService = taggedResourceServiceMock; |
| 2841 | + } |
| 2842 | + |
| 2843 | + public void setVmDao(UserVmDao userVmMock) { |
| 2844 | + _vmDao = userVmMock; |
| 2845 | + } |
| 2846 | + |
2821 | 2847 | private List<VolumeVO> getVolumesFromIds(DestroyVMCmd cmd) { |
2822 | 2848 | List<VolumeVO> volumes = new ArrayList<>(); |
2823 | 2849 | if (cmd.getVolumeIds() != null) { |
@@ -4601,17 +4627,23 @@ public UserVm destroyVm(long vmId, boolean expunge) throws ResourceUnavailableEx |
4601 | 4627 | } |
4602 | 4628 |
|
4603 | 4629 | boolean status; |
| 4630 | + boolean resultRemovedTags; |
4604 | 4631 | State vmState = vm.getState(); |
4605 | 4632 |
|
4606 | 4633 | try { |
4607 | 4634 | VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid()); |
4608 | 4635 | status = vmEntity.destroy(Long.toString(userId), expunge); |
| 4636 | + resultRemovedTags = removeTagsFromVm(vmId); |
4609 | 4637 | } catch (CloudException e) { |
4610 | 4638 | CloudRuntimeException ex = new CloudRuntimeException("Unable to destroy with specified vmId", e); |
4611 | 4639 | ex.addProxyObject(vm.getUuid(), "vmId"); |
4612 | 4640 | throw ex; |
4613 | 4641 | } |
4614 | 4642 |
|
| 4643 | + if (!resultRemovedTags) { |
| 4644 | + s_logger.error("Error to delete tags or tags not exists, for the vm with id " + vmId); |
| 4645 | + } |
| 4646 | + |
4615 | 4647 | if (status) { |
4616 | 4648 | // Mark the account's volumes as destroyed |
4617 | 4649 | List<VolumeVO> volumes = _volsDao.findByInstance(vmId); |
|
0 commit comments