6767import com .cloud .hypervisor .Hypervisor .HypervisorType ;
6868import com .cloud .hypervisor .dao .HypervisorCapabilitiesDao ;
6969import com .cloud .projects .Project .ListProjectResourcesCriteria ;
70+ import com .cloud .server .ResourceTag ;
7071import com .cloud .service .ServiceOfferingVO ;
7172import com .cloud .service .dao .ServiceOfferingDao ;
7273import com .cloud .service .dao .ServiceOfferingDetailsDao ;
8081import com .cloud .storage .dao .GuestOSDao ;
8182import com .cloud .storage .dao .SnapshotDao ;
8283import com .cloud .storage .dao .VolumeDao ;
84+ import com .cloud .tags .ResourceTagVO ;
85+ import com .cloud .tags .dao .ResourceTagDao ;
8386import com .cloud .user .Account ;
8487import com .cloud .user .AccountManager ;
8588import com .cloud .user .User ;
9396import com .cloud .utils .Ternary ;
9497import com .cloud .utils .db .EntityManager ;
9598import com .cloud .utils .db .Filter ;
99+ import com .cloud .utils .db .JoinBuilder ;
96100import com .cloud .utils .db .SearchBuilder ;
97101import com .cloud .utils .db .SearchCriteria ;
98102import 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