|
61 | 61 | import com.cloud.alert.AlertManager; |
62 | 62 | import com.cloud.alert.AlertVO; |
63 | 63 | import com.cloud.alert.dao.AlertDao; |
| 64 | +import com.cloud.api.ApiConstants; |
64 | 65 | import com.cloud.api.ApiDBUtils; |
65 | 66 | import com.cloud.api.commands.CreateDomainCmd; |
66 | 67 | import com.cloud.api.commands.CreateSSHKeyPairCmd; |
|
105 | 106 | import com.cloud.api.commands.StartSystemVMCmd; |
106 | 107 | import com.cloud.api.commands.StopSystemVmCmd; |
107 | 108 | import com.cloud.api.commands.UpdateDomainCmd; |
| 109 | +import com.cloud.api.commands.UpdateHostPasswordCmd; |
108 | 110 | import com.cloud.api.commands.UpdateIsoCmd; |
109 | 111 | import com.cloud.api.commands.UpdateIsoPermissionsCmd; |
110 | 112 | import com.cloud.api.commands.UpdateTemplateCmd; |
|
165 | 167 | import com.cloud.exception.StorageUnavailableException; |
166 | 168 | import com.cloud.host.Host; |
167 | 169 | import com.cloud.host.Host.Type; |
| 170 | +import com.cloud.host.DetailVO; |
168 | 171 | import com.cloud.host.HostVO; |
169 | 172 | import com.cloud.host.Status; |
| 173 | +import com.cloud.host.dao.DetailsDao; |
170 | 174 | import com.cloud.host.dao.HostDao; |
171 | 175 | import com.cloud.hypervisor.Hypervisor.HypervisorType; |
172 | 176 | import com.cloud.info.ConsoleProxyInfo; |
@@ -285,7 +289,8 @@ public class ManagementServerImpl implements ManagementServer { |
285 | 289 | private final VlanDao _vlanDao; |
286 | 290 | private final AccountVlanMapDao _accountVlanMapDao; |
287 | 291 | private final PodVlanMapDao _podVlanMapDao; |
288 | | - private final HostDao _hostDao; |
| 292 | + private final HostDao _hostDao; |
| 293 | + private final DetailsDao _detailsDao; |
289 | 294 | private final UserDao _userDao; |
290 | 295 | private final UserVmDao _userVmDao; |
291 | 296 | private final ConfigurationDao _configDao; |
@@ -352,7 +357,8 @@ protected ManagementServerImpl() { |
352 | 357 | _vlanDao = locator.getDao(VlanDao.class); |
353 | 358 | _accountVlanMapDao = locator.getDao(AccountVlanMapDao.class); |
354 | 359 | _podVlanMapDao = locator.getDao(PodVlanMapDao.class); |
355 | | - _hostDao = locator.getDao(HostDao.class); |
| 360 | + _hostDao = locator.getDao(HostDao.class); |
| 361 | + _detailsDao = locator.getDao(DetailsDao.class); |
356 | 362 | _hostPodDao = locator.getDao(HostPodDao.class); |
357 | 363 | _jobDao = locator.getDao(AsyncJobDao.class); |
358 | 364 | _clusterDao = locator.getDao(ClusterDao.class); |
@@ -4756,5 +4762,56 @@ public String getVMPassword(GetVMPasswordCmd cmd) { |
4756 | 4762 |
|
4757 | 4763 | return password; |
4758 | 4764 | } |
| 4765 | + |
| 4766 | + @DB |
| 4767 | + public boolean updateHostPassword(UpdateHostPasswordCmd cmd) { |
| 4768 | + if (cmd.getClusterId()==null && cmd.getHostId() == null){ |
| 4769 | + throw new InvalidParameterValueException("You should provide one of cluster id or a host id."); |
| 4770 | + } |
| 4771 | + else if (cmd.getClusterId()==null){ |
| 4772 | + HostVO h = _hostDao.findById(cmd.getHostId()); |
| 4773 | + if (h.getHypervisorType() == HypervisorType.XenServer){ |
| 4774 | + throw new InvalidParameterValueException("You should provide cluster id for Xenserver cluster."); |
| 4775 | + } |
| 4776 | + DetailVO nv= _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); |
| 4777 | + if (nv.getValue().equals(cmd.getUsername())){ |
| 4778 | + DetailVO nvp= _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); |
| 4779 | + nvp.setValue(cmd.getPassword()); |
| 4780 | + _detailsDao.persist(nvp); |
| 4781 | + } |
| 4782 | + else { |
| 4783 | + throw new InvalidParameterValueException("The username is not under use by management server."); |
| 4784 | + } |
| 4785 | + } |
| 4786 | + else { |
| 4787 | + //get all the hosts in this cluster |
| 4788 | + List<HostVO> hosts = _hostDao.listByCluster(cmd.getClusterId()); |
| 4789 | + Transaction txn = Transaction.currentTxn(); |
| 4790 | + txn.start(); |
| 4791 | + for (HostVO h : hosts) { |
| 4792 | + if (s_logger.isDebugEnabled()) { |
| 4793 | + s_logger.debug("Changing password for host name = " + h.getName()); |
| 4794 | + } |
| 4795 | + //update password for this host |
| 4796 | + DetailVO nv= _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); |
| 4797 | + if (nv.getValue().equals(cmd.getUsername())){ |
| 4798 | + DetailVO nvp= _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); |
| 4799 | + nvp.setValue(cmd.getPassword()); |
| 4800 | + _detailsDao.persist(nvp); |
| 4801 | + } |
| 4802 | + else { //if one host in the cluster has diff username then rollback to maintain consistency |
| 4803 | + txn.rollback(); |
| 4804 | + throw new InvalidParameterValueException("The username is not same for all hosts, please modify passwords for individual hosts."); |
| 4805 | + } |
| 4806 | + } |
| 4807 | + txn.commit(); |
| 4808 | + // if hypervisor is xenserver then we update it in CitrixResourceBase |
| 4809 | + } |
| 4810 | + return true; |
| 4811 | + } |
| 4812 | + |
| 4813 | + |
| 4814 | + |
| 4815 | + |
4759 | 4816 |
|
4760 | 4817 | } |
0 commit comments