2727import javax .inject .Inject ;
2828import javax .naming .ConfigurationException ;
2929
30+ import org .apache .cloudstack .api .ApiConstants ;
31+ import org .apache .commons .collections .MapUtils ;
3032import org .apache .log4j .Logger ;
3133import org .springframework .stereotype .Component ;
3234import org .apache .cloudstack .api .command .user .vmsnapshot .ListVMSnapshotCmd ;
6769import com .cloud .hypervisor .Hypervisor .HypervisorType ;
6870import com .cloud .hypervisor .dao .HypervisorCapabilitiesDao ;
6971import com .cloud .projects .Project .ListProjectResourcesCriteria ;
72+ import com .cloud .server .ResourceTag ;
7073import com .cloud .service .ServiceOfferingVO ;
7174import com .cloud .service .dao .ServiceOfferingDao ;
7275import com .cloud .service .dao .ServiceOfferingDetailsDao ;
8083import com .cloud .storage .dao .GuestOSDao ;
8184import com .cloud .storage .dao .SnapshotDao ;
8285import com .cloud .storage .dao .VolumeDao ;
86+ import com .cloud .tags .ResourceTagVO ;
87+ import com .cloud .tags .dao .ResourceTagDao ;
8388import com .cloud .user .Account ;
8489import com .cloud .user .AccountManager ;
8590import com .cloud .user .User ;
9398import com .cloud .utils .Ternary ;
9499import com .cloud .utils .db .EntityManager ;
95100import com .cloud .utils .db .Filter ;
101+ import com .cloud .utils .db .JoinBuilder ;
96102import com .cloud .utils .db .SearchBuilder ;
97103import com .cloud .utils .db .SearchCriteria ;
98104import com .cloud .utils .db .Transaction ;
@@ -147,6 +153,8 @@ public class VMSnapshotManagerImpl extends MutualExclusiveIdsManagerBase impleme
147153 EntityManager _entityMgr ;
148154 @ Inject
149155 AsyncJobManager _jobMgr ;
156+ @ Inject
157+ ResourceTagDao _resourceTagDao ;
150158
151159 @ Inject
152160 VmWorkJobDao _workJobDao ;
@@ -206,6 +214,7 @@ public Pair<List<? extends VMSnapshot>, Integer> listVMSnapshots(ListVMSnapshotC
206214 String keyword = cmd .getKeyword ();
207215 String name = cmd .getVmSnapshotName ();
208216 String accountName = cmd .getAccountName ();
217+ Map <String , String > tags = cmd .getTags ();
209218
210219 List <Long > ids = getIdsListFromCmd (cmd .getId (), cmd .getIds ());
211220
@@ -229,11 +238,32 @@ public Pair<List<? extends VMSnapshot>, Integer> listVMSnapshots(ListVMSnapshotC
229238 sb .and ("idIN" , sb .entity ().getId (), SearchCriteria .Op .IN );
230239 sb .and ("display_name" , sb .entity ().getDisplayName (), SearchCriteria .Op .EQ );
231240 sb .and ("account_id" , sb .entity ().getAccountId (), SearchCriteria .Op .EQ );
232- sb .done ();
241+
242+ if (MapUtils .isNotEmpty (tags )) {
243+ SearchBuilder <ResourceTagVO > tagSearch = _resourceTagDao .createSearchBuilder ();
244+ for (int count = 0 ; count < tags .size (); count ++) {
245+ tagSearch .or ().op (ApiConstants .KEY + String .valueOf (count ), tagSearch .entity ().getKey (), SearchCriteria .Op .EQ );
246+ tagSearch .and (ApiConstants .VALUE + String .valueOf (count ), tagSearch .entity ().getValue (), SearchCriteria .Op .EQ );
247+ tagSearch .cp ();
248+ }
249+ tagSearch .and (ApiConstants .RESOURCE_TYPE , tagSearch .entity ().getResourceType (), SearchCriteria .Op .EQ );
250+ sb .groupBy (sb .entity ().getId ());
251+ sb .join ("tagSearch" , tagSearch , sb .entity ().getId (), tagSearch .entity ().getResourceId (), JoinBuilder .JoinType .INNER );
252+ }
233253
234254 SearchCriteria <VMSnapshotVO > sc = sb .create ();
235255 _accountMgr .buildACLSearchCriteria (sc , domainId , isRecursive , permittedAccounts , listProjectResourcesCriteria );
236256
257+ if (MapUtils .isNotEmpty (tags )) {
258+ int count = 0 ;
259+ sc .setJoinParameters ("tagSearch" , ApiConstants .RESOURCE_TYPE , ResourceTag .ResourceObjectType .VMSnapshot .toString ());
260+ for (String key : tags .keySet ()) {
261+ sc .setJoinParameters ("tagSearch" , ApiConstants .KEY + String .valueOf (count ), key );
262+ sc .setJoinParameters ("tagSearch" , ApiConstants .VALUE + String .valueOf (count ), tags .get (key ));
263+ count ++;
264+ }
265+ }
266+
237267 if (accountName != null && cmd .getDomainId () != null ) {
238268 Account account = _accountMgr .getActiveAccountByName (accountName , cmd .getDomainId ());
239269 sc .setParameters ("account_id" , account .getId ());
0 commit comments