Skip to content

Commit 2a5b533

Browse files
author
Pearl Dsilva
committed
search vm snapshots with tags
1 parent d76caa7 commit 2a5b533

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

server/src/main/java/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import com.cloud.hypervisor.Hypervisor.HypervisorType;
6868
import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao;
6969
import com.cloud.projects.Project.ListProjectResourcesCriteria;
70+
import com.cloud.server.ResourceTag;
7071
import com.cloud.service.ServiceOfferingVO;
7172
import com.cloud.service.dao.ServiceOfferingDao;
7273
import com.cloud.service.dao.ServiceOfferingDetailsDao;
@@ -80,6 +81,8 @@
8081
import com.cloud.storage.dao.GuestOSDao;
8182
import com.cloud.storage.dao.SnapshotDao;
8283
import com.cloud.storage.dao.VolumeDao;
84+
import com.cloud.tags.ResourceTagVO;
85+
import com.cloud.tags.dao.ResourceTagDao;
8386
import com.cloud.user.Account;
8487
import com.cloud.user.AccountManager;
8588
import com.cloud.user.User;
@@ -93,6 +96,7 @@
9396
import com.cloud.utils.Ternary;
9497
import com.cloud.utils.db.EntityManager;
9598
import com.cloud.utils.db.Filter;
99+
import com.cloud.utils.db.JoinBuilder;
96100
import com.cloud.utils.db.SearchBuilder;
97101
import com.cloud.utils.db.SearchCriteria;
98102
import com.cloud.utils.db.Transaction;
@@ -147,6 +151,8 @@ public class VMSnapshotManagerImpl extends MutualExclusiveIdsManagerBase impleme
147151
EntityManager _entityMgr;
148152
@Inject
149153
AsyncJobManager _jobMgr;
154+
@Inject
155+
ResourceTagDao _resourceTagDao;
150156

151157
@Inject
152158
VmWorkJobDao _workJobDao;
@@ -206,6 +212,7 @@ public Pair<List<? extends VMSnapshot>, Integer> listVMSnapshots(ListVMSnapshotC
206212
String keyword = cmd.getKeyword();
207213
String name = cmd.getVmSnapshotName();
208214
String accountName = cmd.getAccountName();
215+
Map<String, String> tags = cmd.getTags();
209216

210217
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
211218

@@ -229,11 +236,32 @@ public Pair<List<? extends VMSnapshot>, Integer> listVMSnapshots(ListVMSnapshotC
229236
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
230237
sb.and("display_name", sb.entity().getDisplayName(), SearchCriteria.Op.EQ);
231238
sb.and("account_id", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
232-
sb.done();
239+
240+
if (tags != null && !tags.isEmpty()) {
241+
SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
242+
for (int count = 0; count < tags.size(); count++) {
243+
tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
244+
tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
245+
tagSearch.cp();
246+
}
247+
tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
248+
sb.groupBy(sb.entity().getId());
249+
sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
250+
}
233251

234252
SearchCriteria<VMSnapshotVO> sc = sb.create();
235253
_accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
236254

255+
if (tags != null && !tags.isEmpty()) {
256+
int count = 0;
257+
sc.setJoinParameters("tagSearch", "resourceType", ResourceTag.ResourceObjectType.VMSnapshot.toString());
258+
for (String key : tags.keySet()) {
259+
sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
260+
sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
261+
count++;
262+
}
263+
}
264+
237265
if (accountName != null && cmd.getDomainId() != null) {
238266
Account account = _accountMgr.getActiveAccountByName(accountName, cmd.getDomainId());
239267
sc.setParameters("account_id", account.getId());

0 commit comments

Comments
 (0)