Skip to content

Commit 0fc005f

Browse files
Abhinandan Prateekroot
authored andcommitted
bug 6451: XenServer username/password should be changable through API
1 parent dce4a8e commit 0fc005f

4 files changed

Lines changed: 158 additions & 2 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
19+
package com.cloud.api.commands;
20+
21+
import org.apache.log4j.Logger;
22+
23+
24+
import com.cloud.api.ApiConstants;
25+
import com.cloud.api.BaseCmd;
26+
import com.cloud.api.Implementation;
27+
import com.cloud.api.Parameter;
28+
import com.cloud.api.ServerApiException;
29+
import com.cloud.api.BaseCmd.CommandType;
30+
import com.cloud.api.response.RegisterResponse;
31+
import com.cloud.api.response.SuccessResponse;
32+
import com.cloud.user.Account;
33+
34+
@Implementation(description = "Update password of a host/pool on management server.", responseObject = SuccessResponse.class)
35+
public class UpdateHostPasswordCmd extends BaseCmd {
36+
public static final Logger s_logger = Logger.getLogger(UpdateHostPasswordCmd.class.getName());
37+
38+
private static final String s_name = "updatehostpasswordresponse";
39+
40+
// ///////////////////////////////////////////////////
41+
// ////////////// API parameters /////////////////////
42+
// ///////////////////////////////////////////////////
43+
44+
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID")
45+
private Long hostId;
46+
47+
@Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="the cluster ID for the host")
48+
private Long clusterId;
49+
50+
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=true, description="the username for the host/cluster")
51+
private String username;
52+
53+
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required=true, description="the password for the host/cluster")
54+
private String password;
55+
56+
// ///////////////////////////////////////////////////
57+
// ///////////////// Accessors ///////////////////////
58+
// ///////////////////////////////////////////////////
59+
60+
public Long getHostId() {
61+
return hostId;
62+
}
63+
64+
public Long getClusterId() {
65+
return clusterId;
66+
}
67+
68+
public String getPassword() {
69+
return password;
70+
}
71+
72+
public String getUsername() {
73+
return username;
74+
}
75+
76+
// ///////////////////////////////////////////////////
77+
// ///////////// API Implementation///////////////////
78+
// ///////////////////////////////////////////////////
79+
80+
@Override
81+
public String getCommandName() {
82+
return s_name;
83+
}
84+
85+
@Override
86+
public long getEntityOwnerId() {
87+
return Account.ACCOUNT_ID_SYSTEM;
88+
}
89+
90+
@Override
91+
public void execute() {
92+
_mgr.updateHostPassword(this);
93+
this.setResponseObject(new SuccessResponse());
94+
}
95+
}

‎api/src/com/cloud/server/ManagementService.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.cloud.api.commands.StartSystemVMCmd;
6969
import com.cloud.api.commands.StopSystemVmCmd;
7070
import com.cloud.api.commands.UpdateDomainCmd;
71+
import com.cloud.api.commands.UpdateHostPasswordCmd;
7172
import com.cloud.api.commands.UpdateIsoCmd;
7273
import com.cloud.api.commands.UpdateIsoPermissionsCmd;
7374
import com.cloud.api.commands.UpdateTemplateCmd;
@@ -370,6 +371,8 @@ public interface ManagementService {
370371

371372
String[] createApiKeyAndSecretKey(RegisterCmd cmd);
372373

374+
boolean updateHostPassword(UpdateHostPasswordCmd cmd);
375+
373376
InstanceGroup updateVmGroup(UpdateVMGroupCmd cmd);
374377

375378
List<? extends InstanceGroup> searchForVmGroups(ListVMGroupsCmd cmd);

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ prepareHostForMaintenance=com.cloud.api.commands.PrepareForMaintenanceCmd;1
180180
cancelHostMaintenance=com.cloud.api.commands.CancelMaintenanceCmd;1
181181
listHosts=com.cloud.api.commands.ListHostsCmd;3
182182
addSecondaryStorage=com.cloud.api.commands.AddSecondaryStorageCmd;1
183+
updateHostPassword=com.cloud.api.commands.UpdateHostPasswordCmd;1
183184

184185
#### volume commands
185186
attachVolume=com.cloud.api.commands.AttachVolumeCmd;15

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.cloud.alert.AlertManager;
6262
import com.cloud.alert.AlertVO;
6363
import com.cloud.alert.dao.AlertDao;
64+
import com.cloud.api.ApiConstants;
6465
import com.cloud.api.ApiDBUtils;
6566
import com.cloud.api.commands.CreateDomainCmd;
6667
import com.cloud.api.commands.CreateSSHKeyPairCmd;
@@ -105,6 +106,7 @@
105106
import com.cloud.api.commands.StartSystemVMCmd;
106107
import com.cloud.api.commands.StopSystemVmCmd;
107108
import com.cloud.api.commands.UpdateDomainCmd;
109+
import com.cloud.api.commands.UpdateHostPasswordCmd;
108110
import com.cloud.api.commands.UpdateIsoCmd;
109111
import com.cloud.api.commands.UpdateIsoPermissionsCmd;
110112
import com.cloud.api.commands.UpdateTemplateCmd;
@@ -165,8 +167,10 @@
165167
import com.cloud.exception.StorageUnavailableException;
166168
import com.cloud.host.Host;
167169
import com.cloud.host.Host.Type;
170+
import com.cloud.host.DetailVO;
168171
import com.cloud.host.HostVO;
169172
import com.cloud.host.Status;
173+
import com.cloud.host.dao.DetailsDao;
170174
import com.cloud.host.dao.HostDao;
171175
import com.cloud.hypervisor.Hypervisor.HypervisorType;
172176
import com.cloud.info.ConsoleProxyInfo;
@@ -285,7 +289,8 @@ public class ManagementServerImpl implements ManagementServer {
285289
private final VlanDao _vlanDao;
286290
private final AccountVlanMapDao _accountVlanMapDao;
287291
private final PodVlanMapDao _podVlanMapDao;
288-
private final HostDao _hostDao;
292+
private final HostDao _hostDao;
293+
private final DetailsDao _detailsDao;
289294
private final UserDao _userDao;
290295
private final UserVmDao _userVmDao;
291296
private final ConfigurationDao _configDao;
@@ -352,7 +357,8 @@ protected ManagementServerImpl() {
352357
_vlanDao = locator.getDao(VlanDao.class);
353358
_accountVlanMapDao = locator.getDao(AccountVlanMapDao.class);
354359
_podVlanMapDao = locator.getDao(PodVlanMapDao.class);
355-
_hostDao = locator.getDao(HostDao.class);
360+
_hostDao = locator.getDao(HostDao.class);
361+
_detailsDao = locator.getDao(DetailsDao.class);
356362
_hostPodDao = locator.getDao(HostPodDao.class);
357363
_jobDao = locator.getDao(AsyncJobDao.class);
358364
_clusterDao = locator.getDao(ClusterDao.class);
@@ -4756,5 +4762,56 @@ public String getVMPassword(GetVMPasswordCmd cmd) {
47564762

47574763
return password;
47584764
}
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+
47594816

47604817
}

0 commit comments

Comments
 (0)