From 9a2d235f7660f81214acdefb19d9369d26d4fba3 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Fri, 17 Jul 2020 10:22:15 +0530 Subject: [PATCH 1/2] search vm snapshots with tags --- .../vm/snapshot/VMSnapshotManagerImpl.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java b/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java index 86357f7700fc..950c820921ec 100644 --- a/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java @@ -67,6 +67,7 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao; import com.cloud.projects.Project.ListProjectResourcesCriteria; +import com.cloud.server.ResourceTag; import com.cloud.service.ServiceOfferingVO; import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.service.dao.ServiceOfferingDetailsDao; @@ -80,6 +81,8 @@ import com.cloud.storage.dao.GuestOSDao; import com.cloud.storage.dao.SnapshotDao; import com.cloud.storage.dao.VolumeDao; +import com.cloud.tags.ResourceTagVO; +import com.cloud.tags.dao.ResourceTagDao; import com.cloud.user.Account; import com.cloud.user.AccountManager; import com.cloud.user.User; @@ -93,6 +96,7 @@ import com.cloud.utils.Ternary; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Filter; +import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; @@ -147,6 +151,8 @@ public class VMSnapshotManagerImpl extends MutualExclusiveIdsManagerBase impleme EntityManager _entityMgr; @Inject AsyncJobManager _jobMgr; + @Inject + ResourceTagDao _resourceTagDao; @Inject VmWorkJobDao _workJobDao; @@ -206,6 +212,7 @@ public Pair, Integer> listVMSnapshots(ListVMSnapshotC String keyword = cmd.getKeyword(); String name = cmd.getVmSnapshotName(); String accountName = cmd.getAccountName(); + Map tags = cmd.getTags(); List ids = getIdsListFromCmd(cmd.getId(), cmd.getIds()); @@ -229,11 +236,32 @@ public Pair, Integer> listVMSnapshots(ListVMSnapshotC sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN); sb.and("display_name", sb.entity().getDisplayName(), SearchCriteria.Op.EQ); sb.and("account_id", sb.entity().getAccountId(), SearchCriteria.Op.EQ); - sb.done(); + + if (tags != null && !tags.isEmpty()) { + SearchBuilder tagSearch = _resourceTagDao.createSearchBuilder(); + for (int count = 0; count < tags.size(); count++) { + tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ); + tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ); + tagSearch.cp(); + } + tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ); + sb.groupBy(sb.entity().getId()); + sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER); + } SearchCriteria sc = sb.create(); _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); + if (tags != null && !tags.isEmpty()) { + int count = 0; + sc.setJoinParameters("tagSearch", "resourceType", ResourceTag.ResourceObjectType.VMSnapshot.toString()); + for (String key : tags.keySet()) { + sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key); + sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key)); + count++; + } + } + if (accountName != null && cmd.getDomainId() != null) { Account account = _accountMgr.getActiveAccountByName(accountName, cmd.getDomainId()); sc.setParameters("account_id", account.getId()); From 2f01c8bb5681c2c9800227092e7bd04e51d01e39 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Fri, 17 Jul 2020 10:22:15 +0530 Subject: [PATCH 2/2] Review comments --- .../vm/snapshot/VMSnapshotManagerImpl.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java b/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java index 950c820921ec..39d753034ac9 100644 --- a/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java @@ -27,6 +27,8 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; +import org.apache.cloudstack.api.ApiConstants; +import org.apache.commons.collections.MapUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import org.apache.cloudstack.api.command.user.vmsnapshot.ListVMSnapshotCmd; @@ -237,14 +239,14 @@ public Pair, Integer> listVMSnapshots(ListVMSnapshotC sb.and("display_name", sb.entity().getDisplayName(), SearchCriteria.Op.EQ); sb.and("account_id", sb.entity().getAccountId(), SearchCriteria.Op.EQ); - if (tags != null && !tags.isEmpty()) { + if (MapUtils.isNotEmpty(tags)) { SearchBuilder tagSearch = _resourceTagDao.createSearchBuilder(); for (int count = 0; count < tags.size(); count++) { - tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ); - tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ); + tagSearch.or().op(ApiConstants.KEY + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ); + tagSearch.and(ApiConstants.VALUE + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ); tagSearch.cp(); } - tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ); + tagSearch.and(ApiConstants.RESOURCE_TYPE, tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ); sb.groupBy(sb.entity().getId()); sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER); } @@ -252,12 +254,12 @@ public Pair, Integer> listVMSnapshots(ListVMSnapshotC SearchCriteria sc = sb.create(); _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); - if (tags != null && !tags.isEmpty()) { + if (MapUtils.isNotEmpty(tags)) { int count = 0; - sc.setJoinParameters("tagSearch", "resourceType", ResourceTag.ResourceObjectType.VMSnapshot.toString()); + sc.setJoinParameters("tagSearch", ApiConstants.RESOURCE_TYPE, ResourceTag.ResourceObjectType.VMSnapshot.toString()); for (String key : tags.keySet()) { - sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key); - sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key)); + sc.setJoinParameters("tagSearch", ApiConstants.KEY + String.valueOf(count), key); + sc.setJoinParameters("tagSearch", ApiConstants.VALUE + String.valueOf(count), tags.get(key)); count++; } }