@@ -316,7 +316,63 @@ public UserVmResponse importUnmanagedInstance(ImportUnmanageInstanceCmd cmd) {
316316
317317 UserVm userVm = null ;
318318
319+ List <String > templatesFilterList = new ArrayList <>();
320+
321+ if (cluster .getHypervisorType () == Hypervisor .HypervisorType .VMware ) { // Add filter for templates for VMware
322+ List <VMTemplateStoragePoolVO > templates = templatePoolDao .listAll ();
323+ for (VMTemplateStoragePoolVO templateStoragePoolVO : templates ) {
324+ templatesFilterList .add (templateStoragePoolVO .getInstallPath ());
325+ }
326+ }
327+
319328 for (HostVO host : hosts ) {
329+ List <String > managedVms = new ArrayList <>();
330+ managedVms .addAll (templatesFilterList );
331+ try {
332+ ListVMsCmdByAdmin vmsCmd = new ListVMsCmdByAdmin ();
333+ vmsCmd = ComponentContext .inject (vmsCmd );
334+ Field hostField = vmsCmd .getClass ().getDeclaredField ("hostId" );
335+ hostField .setAccessible (true );
336+ hostField .set (vmsCmd , host .getId ());
337+ Field listAllField = BaseListDomainResourcesCmd .class .getDeclaredField ("listAll" );
338+ listAllField .setAccessible (true );
339+ listAllField .set (vmsCmd , true );
340+ ListResponse <UserVmResponse > vmsResponse = queryService .searchForUserVMs (vmsCmd );
341+ for (UserVmResponse vmResponse : vmsResponse .getResponses ()) {
342+ managedVms .add (vmResponse .getInstanceName ());
343+ }
344+ } catch (Exception e ) {
345+ LOGGER .warn (String .format ("Unable to retrieve user vms for host ID: %s" , host .getUuid ()));
346+ }
347+ try {
348+ ListSystemVMsCmd vmsCmd = new ListSystemVMsCmd ();
349+ vmsCmd = ComponentContext .inject (vmsCmd );
350+ Field hostField = vmsCmd .getClass ().getDeclaredField ("hostId" );
351+ hostField .setAccessible (true );
352+ hostField .set (vmsCmd , host .getId ());
353+ Pair <List <? extends VirtualMachine >, Integer > systemVMs = managementService .searchForSystemVm (vmsCmd );
354+ for (VirtualMachine systemVM : systemVMs .first ()) {
355+ managedVms .add (systemVM .getInstanceName ());
356+ }
357+ } catch (Exception e ) {
358+ LOGGER .warn (String .format ("Unable to retrieve system vms for host ID: %s" , host .getUuid ()));
359+ }
360+ try {
361+ ListRoutersCmd vmsCmd = new ListRoutersCmd ();
362+ vmsCmd = ComponentContext .inject (vmsCmd );
363+ Field hostField = vmsCmd .getClass ().getDeclaredField ("hostId" );
364+ hostField .setAccessible (true );
365+ hostField .set (vmsCmd , host .getId ());
366+ Field listAllField = BaseListDomainResourcesCmd .class .getDeclaredField ("listAll" );
367+ listAllField .setAccessible (true );
368+ listAllField .set (vmsCmd , true );
369+ ListResponse <DomainRouterResponse > routersResponse = queryService .searchForRouters (vmsCmd );
370+ for (DomainRouterResponse routerResponse : routersResponse .getResponses ()) {
371+ managedVms .add (routerResponse .getName ());
372+ }
373+ } catch (Exception e ) {
374+ LOGGER .warn (String .format ("Unable to retrieve virtual router vms for host ID: %s" , host .getUuid ()));
375+ }
320376 GetUnmanagedInstancesCommand command = new GetUnmanagedInstancesCommand ();
321377 command .setInstanceName (instanceName );
322378 Answer answer = agentManager .easySend (host .getId (), command );
0 commit comments