4343import javax .naming .ConfigurationException ;
4444import javax .persistence .EntityExistsException ;
4545
46+ import com .cloud .hypervisor .vmware .mo .VirtualMachineMO ;
4647import com .cloud .hypervisor .vmware .util .VmwareClient ;
4748import org .apache .cloudstack .api .command .admin .zone .AddVmwareDcCmd ;
4849import org .apache .cloudstack .api .command .admin .zone .ImportVsphereStoragePoliciesCmd ;
171172import com .vmware .pbm .PbmProfile ;
172173import com .vmware .vim25 .AboutInfo ;
173174import com .vmware .vim25 .ManagedObjectReference ;
175+ import org .apache .logging .log4j .LogManager ;
176+ import org .apache .logging .log4j .Logger ;
174177
175178public class VmwareManagerImpl extends ManagerBase implements VmwareManager , VmwareStorageMount , Listener , VmwareDatacenterService , Configurable {
179+ protected static Logger static_logger = LogManager .getLogger (VmwareManagerImpl .class );
176180
177181 private static final long SECONDS_PER_MINUTE = 60 ;
178182 private static final int DEFAULT_PORTS_PER_DV_PORT_GROUP_VSPHERE4_x = 256 ;
@@ -1585,14 +1589,26 @@ public List<StoragePool> listVsphereStoragePolicyCompatibleStoragePools(ListVsph
15851589 return compatiblePools ;
15861590 }
15871591
1588- @ Override
1589- public List <UnmanagedInstanceTO > listVMsInDatacenter (ListVmwareDcVmsCmd cmd ) {
1592+ private static class VcenterData {
1593+ public final String vcenter ;
1594+ public final String datacenterName ;
1595+ public final String username ;
1596+ public final String password ;
1597+
1598+ public VcenterData (String vcenter , String datacenterName , String username , String password ) {
1599+ this .vcenter = vcenter ;
1600+ this .datacenterName = datacenterName ;
1601+ this .username = username ;
1602+ this .password = password ;
1603+ }
1604+ }
1605+
1606+ private VcenterData getVcenterData (ListVmwareDcVmsCmd cmd ) {
15901607 String vcenter = cmd .getVcenter ();
15911608 String datacenterName = cmd .getDatacenterName ();
15921609 String username = cmd .getUsername ();
15931610 String password = cmd .getPassword ();
15941611 Long existingVcenterId = cmd .getExistingVcenterId ();
1595- String keyword = cmd .getKeyword ();
15961612
15971613 if ((existingVcenterId == null && StringUtils .isBlank (vcenter )) ||
15981614 (existingVcenterId != null && StringUtils .isNotBlank (vcenter ))) {
@@ -1613,34 +1629,69 @@ public List<UnmanagedInstanceTO> listVMsInDatacenter(ListVmwareDcVmsCmd cmd) {
16131629 username = vmwareDc .getUser ();
16141630 password = vmwareDc .getPassword ();
16151631 }
1632+ VcenterData vmwaredc = new VcenterData (vcenter , datacenterName , username , password );
1633+ return vmwaredc ;
1634+ }
1635+
1636+ private static VmwareContext getVmwareContext (String vcenter , String username , String password ) throws Exception {
1637+ static_logger .debug (String .format ("Connecting to the VMware vCenter %s" , vcenter ));
1638+ String serviceUrl = String .format ("https://%s/sdk/vimService" , vcenter );
1639+ VmwareClient vimClient = new VmwareClient (vcenter );
1640+ vimClient .connect (serviceUrl , username , password );
1641+ return new VmwareContext (vimClient , vcenter );
1642+ }
1643+
1644+ @ Override
1645+ public List <UnmanagedInstanceTO > listVMsInDatacenter (ListVmwareDcVmsCmd cmd ) {
1646+ VcenterData vmwareDC = getVcenterData (cmd );
1647+ String vcenter = vmwareDC .vcenter ;
1648+ String username = vmwareDC .username ;
1649+ String password = vmwareDC .password ;
1650+ String datacenterName = vmwareDC .datacenterName ;
1651+ String keyword = cmd .getKeyword ();
1652+ String esxiHostName = cmd .getHostName ();
1653+ String virtualMachineName = cmd .getInstanceName ();
16161654
16171655 try {
16181656 logger .debug (String .format ("Connecting to the VMware datacenter %s at vCenter %s to retrieve VMs" ,
16191657 datacenterName , vcenter ));
1620- String serviceUrl = String .format ("https://%s/sdk/vimService" , vcenter );
1621- VmwareClient vimClient = new VmwareClient (vcenter );
1622- vimClient .connect (serviceUrl , username , password );
1623- VmwareContext context = new VmwareContext (vimClient , vcenter );
1624-
1625- DatacenterMO dcMo = new DatacenterMO (context , datacenterName );
1626- ManagedObjectReference dcMor = dcMo .getMor ();
1627- if (dcMor == null ) {
1628- String msg = String .format ("Unable to find VMware datacenter %s in vCenter %s" ,
1629- datacenterName , vcenter );
1630- logger .error (msg );
1631- throw new InvalidParameterValueException (msg );
1658+ VmwareContext context = getVmwareContext (vcenter , username , password );
1659+ DatacenterMO dcMo = getDatacenterMO (context , vcenter , datacenterName );
1660+
1661+ List <UnmanagedInstanceTO > instances ;
1662+ if (StringUtils .isNotBlank (esxiHostName ) && StringUtils .isNotBlank (virtualMachineName )) {
1663+ ManagedObjectReference hostMor = dcMo .findHost (esxiHostName );
1664+ if (hostMor == null ) {
1665+ String errorMsg = String .format ("Cannot find a host with name %s on vcenter %s" , esxiHostName , vcenter );
1666+ logger .error (errorMsg );
1667+ throw new CloudRuntimeException (errorMsg );
1668+ }
1669+ HostMO hostMO = new HostMO (context , hostMor );
1670+ VirtualMachineMO vmMo = hostMO .findVmOnHyperHost (virtualMachineName );
1671+ instances = Collections .singletonList (VmwareHelper .getUnmanagedInstance (hostMO , vmMo ));
1672+ } else {
1673+ instances = dcMo .getAllVmsOnDatacenter (keyword );
16321674 }
1633- List <UnmanagedInstanceTO > instances = dcMo .getAllVmsOnDatacenter ();
1634- return StringUtils .isBlank (keyword ) ? instances :
1635- instances .stream ().filter (x -> x .getName ().toLowerCase ().contains (keyword .toLowerCase ())).collect (Collectors .toList ());
1675+ return instances ;
16361676 } catch (Exception e ) {
1637- String errorMsg = String .format ("Error retrieving stopped VMs from the VMware VC %s datacenter %s: %s" ,
1677+ String errorMsg = String .format ("Error retrieving VMs from the VMware VC %s datacenter %s: %s" ,
16381678 vcenter , datacenterName , e .getMessage ());
16391679 logger .error (errorMsg , e );
16401680 throw new CloudRuntimeException (errorMsg );
16411681 }
16421682 }
16431683
1684+ private static DatacenterMO getDatacenterMO (VmwareContext context , String vcenter , String datacenterName ) throws Exception {
1685+ DatacenterMO dcMo = new DatacenterMO (context , datacenterName );
1686+ ManagedObjectReference dcMor = dcMo .getMor ();
1687+ if (dcMor == null ) {
1688+ String msg = String .format ("Unable to find VMware datacenter %s in vCenter %s" , datacenterName , vcenter );
1689+ static_logger .error (msg );
1690+ throw new InvalidParameterValueException (msg );
1691+ }
1692+ return dcMo ;
1693+ }
1694+
16441695 @ Override
16451696 public boolean hasNexusVSM (Long clusterId ) {
16461697 ClusterVSMMapVO vsmMapVo = null ;
@@ -1693,7 +1744,7 @@ private void startTemplateCleanJobSchedule() {
16931744 }
16941745
16951746 /**
1696- * This task is to cleanup templates from primary storage that are otherwise not cleaned by the {@link com.cloud.storage.StorageManagerImpl.StorageGarbageCollector }.
1747+ * This task is to cleanup templates from primary storage that are otherwise not cleaned by the {code}StorageGarbageCollector{code} from { @link com.cloud.storage.StorageManagerImpl}.
16971748 * it is called at regular intervals when storage.template.cleanup.enabled == true
16981749 * It collect all templates that
16991750 * - are deleted from cloudstack
0 commit comments