4848import org .apache .cloudstack .api .command .admin .zone .AddVmwareDcCmd ;
4949import org .apache .cloudstack .api .command .admin .zone .ImportVsphereStoragePoliciesCmd ;
5050import org .apache .cloudstack .api .command .admin .zone .ListVmwareDcVmsCmd ;
51+ import org .apache .cloudstack .api .command .admin .zone .ListVmwareDcHostsCmd ;
52+ import org .apache .cloudstack .api .command .admin .zone .ListVmwareDcItems ;
5153import org .apache .cloudstack .api .command .admin .zone .ListVmwareDcsCmd ;
5254import org .apache .cloudstack .api .command .admin .zone .ListVsphereStoragePoliciesCmd ;
5355import org .apache .cloudstack .api .command .admin .zone .ListVsphereStoragePolicyCompatiblePoolsCmd ;
179181import com .vmware .vim25 .InvalidLoginFaultMsg ;
180182import com .vmware .vim25 .RuntimeFaultFaultMsg ;
181183import com .vmware .vim25 .InvalidPropertyFaultMsg ;
184+ import org .jetbrains .annotations .NotNull ;
182185
183186public class VmwareManagerImpl extends ManagerBase implements VmwareManager , VmwareStorageMount , Listener , VmwareDatacenterService , Configurable {
184187 private static final Logger s_logger = Logger .getLogger (VmwareManagerImpl .class );
@@ -1093,6 +1096,7 @@ public List<Class<?>> getCommands() {
10931096 cmdList .add (ListVsphereStoragePoliciesCmd .class );
10941097 cmdList .add (ListVsphereStoragePolicyCompatiblePoolsCmd .class );
10951098 cmdList .add (ListVmwareDcVmsCmd .class );
1099+ cmdList .add (ListVmwareDcHostsCmd .class );
10961100 return cmdList ;
10971101 }
10981102
@@ -1561,15 +1565,79 @@ public List<StoragePool> listVsphereStoragePolicyCompatibleStoragePools(ListVsph
15611565 return compatiblePools ;
15621566 }
15631567
1568+ @ Override
1569+ public List <HostMO > listHostsInDatacenter (ListVmwareDcHostsCmd cmd ) {
1570+ Integer maxObjects = cmd .getBatchSize ();
1571+ String token = cmd .getToken ();
1572+
1573+ VcenterData vmwaredc = getVcenterData (cmd );
1574+
1575+ try {
1576+ VmwareContext context = getVmwareContext (vmwaredc );
1577+ DatacenterMO dcMo = getDatacenterMO (context , vmwaredc );
1578+ return dcMo .getAllHostsOnDatacenter ();
1579+ } catch (RuntimeFaultFaultMsg | URISyntaxException | VmwareClientException | InvalidLocaleFaultMsg |
1580+ InvalidLoginFaultMsg | InvalidPropertyFaultMsg e ) {
1581+ String errorMsg = String .format ("Error retrieving stopped VMs from the VMware VC %s datacenter %s: %s" ,
1582+ vmwaredc .vcenter , vmwaredc .datacenterName , e .getMessage ());
1583+ s_logger .error (errorMsg , e );
1584+ throw new CloudRuntimeException (errorMsg );
1585+ }
1586+
1587+ }
1588+
15641589 @ Override
15651590 public Pair <String , List <UnmanagedInstanceTO >> listVMsInDatacenter (ListVmwareDcVmsCmd cmd ) {
1591+ Integer maxObjects = cmd .getBatchSize ();
1592+ String token = cmd .getToken ();
1593+
1594+ VcenterData vmwaredc = getVcenterData (cmd );
1595+
1596+ try {
1597+ VmwareContext context = getVmwareContext (vmwaredc );
1598+
1599+ DatacenterMO dcMo = getDatacenterMO (context , vmwaredc );
1600+ return dcMo .getVmsOnDatacenter (maxObjects , token );
1601+ } catch (InvalidParameterValueException | VmwareClientException | InvalidLocaleFaultMsg | InvalidLoginFaultMsg |
1602+ RuntimeFaultFaultMsg | URISyntaxException | InvalidPropertyFaultMsg | InvocationTargetException |
1603+ NoSuchMethodException | IllegalAccessException e ) {
1604+ String errorMsg = String .format ("Error retrieving stopped VMs from the VMware VC %s datacenter %s: %s" ,
1605+ vmwaredc .vcenter , vmwaredc .datacenterName , e .getMessage ());
1606+ s_logger .error (errorMsg , e );
1607+ throw new CloudRuntimeException (errorMsg );
1608+ }
1609+ }
1610+
1611+ @ NotNull
1612+ private static DatacenterMO getDatacenterMO (VmwareContext context , VcenterData vmwaredc ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
1613+ DatacenterMO dcMo = new DatacenterMO (context , vmwaredc .datacenterName );
1614+ ManagedObjectReference dcMor = dcMo .getMor ();
1615+ if (dcMor == null ) {
1616+ String msg = String .format ("Unable to find VMware datacenter %s in vCenter %s" ,
1617+ vmwaredc .datacenterName , vmwaredc .vcenter );
1618+ s_logger .error (msg );
1619+ throw new InvalidParameterValueException (msg );
1620+ }
1621+ return dcMo ;
1622+ }
1623+
1624+ @ NotNull
1625+ private static VmwareContext getVmwareContext (VcenterData vmwaredc ) throws RuntimeFaultFaultMsg , URISyntaxException , VmwareClientException , InvalidLocaleFaultMsg , InvalidLoginFaultMsg {
1626+ s_logger .debug (String .format ("Connecting to the VMware datacenter %s at vCenter %s to retrieve VMs" ,
1627+ vmwaredc .datacenterName , vmwaredc .vcenter ));
1628+ String serviceUrl = String .format ("https://%s/sdk/vimService" , vmwaredc .vcenter );
1629+ VmwareClient vimClient = new VmwareClient (vmwaredc .vcenter );
1630+ vimClient .connect (serviceUrl , vmwaredc .username , vmwaredc .password );
1631+ VmwareContext context = new VmwareContext (vimClient , vmwaredc .vcenter );
1632+ return context ;
1633+ }
1634+
1635+ @ NotNull
1636+ private VcenterData getVcenterData (ListVmwareDcItems cmd ) {
15661637 String vcenter = cmd .getVcenter ();
15671638 String datacenterName = cmd .getDatacenterName ();
15681639 String username = cmd .getUsername ();
15691640 String password = cmd .getPassword ();
1570- Integer maxObjects = cmd .getBatchSize ();
1571- String token = cmd .getToken ();
1572-
15731641 Long existingVcenterId = cmd .getExistingVcenterId ();
15741642
15751643 if ((existingVcenterId == null && StringUtils .isBlank (vcenter )) ||
@@ -1591,31 +1659,21 @@ public Pair<String, List<UnmanagedInstanceTO>> listVMsInDatacenter(ListVmwareDcV
15911659 username = vmwareDc .getUser ();
15921660 password = vmwareDc .getPassword ();
15931661 }
1662+ VcenterData vmwaredc = new VcenterData (vcenter , datacenterName , username , password );
1663+ return vmwaredc ;
1664+ }
15941665
1595- s_logger .debug (String .format ("Connecting to the VMware datacenter %s at vCenter %s to retrieve VMs" ,
1596- datacenterName , vcenter ));
1597- String serviceUrl = String .format ("https://%s/sdk/vimService" , vcenter );
1598- VmwareClient vimClient = new VmwareClient (vcenter );
1599- try {
1600- vimClient .connect (serviceUrl , username , password );
1601- VmwareContext context = new VmwareContext (vimClient , vcenter );
1666+ private static class VcenterData {
1667+ public final String vcenter ;
1668+ public final String datacenterName ;
1669+ public final String username ;
1670+ public final String password ;
16021671
1603- DatacenterMO dcMo = new DatacenterMO (context , datacenterName );
1604- ManagedObjectReference dcMor = dcMo .getMor ();
1605- if (dcMor == null ) {
1606- String msg = String .format ("Unable to find VMware datacenter %s in vCenter %s" ,
1607- datacenterName , vcenter );
1608- s_logger .error (msg );
1609- throw new InvalidParameterValueException (msg );
1610- }
1611- return dcMo .getVmsOnDatacenter (maxObjects , token );
1612- } catch (InvalidParameterValueException | VmwareClientException | InvalidLocaleFaultMsg | InvalidLoginFaultMsg |
1613- RuntimeFaultFaultMsg | URISyntaxException | InvalidPropertyFaultMsg | InvocationTargetException |
1614- NoSuchMethodException | IllegalAccessException e ) {
1615- String errorMsg = String .format ("Error retrieving stopped VMs from the VMware VC %s datacenter %s: %s" ,
1616- vcenter , datacenterName , e .getMessage ());
1617- s_logger .error (errorMsg , e );
1618- throw new CloudRuntimeException (errorMsg );
1672+ public VcenterData (String vcenter , String datacenterName , String username , String password ) {
1673+ this .vcenter = vcenter ;
1674+ this .datacenterName = datacenterName ;
1675+ this .username = username ;
1676+ this .password = password ;
16191677 }
16201678 }
16211679
@@ -1671,7 +1729,7 @@ private void startTemplateCleanJobSchedule() {
16711729 }
16721730
16731731 /**
1674- * This task is to clean-up templates from primary storage that are otherwise not cleaned by the {@link com.cloud.storage.StorageManagerImpl.StorageGarbageCollector}.
1732+ * This task is to clean-up templates from primary storage that are otherwise not cleaned by the {@see com.cloud.storage.StorageManagerImpl.StorageGarbageCollector}.
16751733 * it is called at regular intervals when storage.template.cleanup.enabled == true
16761734 * It collect all templates that
16771735 * - are deleted from cloudstack
0 commit comments