bug 6451: XenServer username/password should be changable through API

This commit is contained in:
Abhinandan Prateek 2011-04-27 11:07:43 +05:30 committed by root
parent dce4a8ec2e
commit 0fc005f20a
4 changed files with 158 additions and 2 deletions

View File

@ -0,0 +1,95 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.api.commands;
import org.apache.log4j.Logger;
import com.cloud.api.ApiConstants;
import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.BaseCmd.CommandType;
import com.cloud.api.response.RegisterResponse;
import com.cloud.api.response.SuccessResponse;
import com.cloud.user.Account;
@Implementation(description = "Update password of a host/pool on management server.", responseObject = SuccessResponse.class)
public class UpdateHostPasswordCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(UpdateHostPasswordCmd.class.getName());
private static final String s_name = "updatehostpasswordresponse";
// ///////////////////////////////////////////////////
// ////////////// API parameters /////////////////////
// ///////////////////////////////////////////////////
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID")
private Long hostId;
@Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="the cluster ID for the host")
private Long clusterId;
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=true, description="the username for the host/cluster")
private String username;
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required=true, description="the password for the host/cluster")
private String password;
// ///////////////////////////////////////////////////
// ///////////////// Accessors ///////////////////////
// ///////////////////////////////////////////////////
public Long getHostId() {
return hostId;
}
public Long getClusterId() {
return clusterId;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public String getCommandName() {
return s_name;
}
@Override
public long getEntityOwnerId() {
return Account.ACCOUNT_ID_SYSTEM;
}
@Override
public void execute() {
_mgr.updateHostPassword(this);
this.setResponseObject(new SuccessResponse());
}
}

View File

@ -68,6 +68,7 @@ import com.cloud.api.commands.RegisterSSHKeyPairCmd;
import com.cloud.api.commands.StartSystemVMCmd;
import com.cloud.api.commands.StopSystemVmCmd;
import com.cloud.api.commands.UpdateDomainCmd;
import com.cloud.api.commands.UpdateHostPasswordCmd;
import com.cloud.api.commands.UpdateIsoCmd;
import com.cloud.api.commands.UpdateIsoPermissionsCmd;
import com.cloud.api.commands.UpdateTemplateCmd;
@ -370,6 +371,8 @@ public interface ManagementService {
String[] createApiKeyAndSecretKey(RegisterCmd cmd);
boolean updateHostPassword(UpdateHostPasswordCmd cmd);
InstanceGroup updateVmGroup(UpdateVMGroupCmd cmd);
List<? extends InstanceGroup> searchForVmGroups(ListVMGroupsCmd cmd);

View File

@ -180,6 +180,7 @@ prepareHostForMaintenance=com.cloud.api.commands.PrepareForMaintenanceCmd;1
cancelHostMaintenance=com.cloud.api.commands.CancelMaintenanceCmd;1
listHosts=com.cloud.api.commands.ListHostsCmd;3
addSecondaryStorage=com.cloud.api.commands.AddSecondaryStorageCmd;1
updateHostPassword=com.cloud.api.commands.UpdateHostPasswordCmd;1
#### volume commands
attachVolume=com.cloud.api.commands.AttachVolumeCmd;15

View File

@ -61,6 +61,7 @@ import com.cloud.alert.Alert;
import com.cloud.alert.AlertManager;
import com.cloud.alert.AlertVO;
import com.cloud.alert.dao.AlertDao;
import com.cloud.api.ApiConstants;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.commands.CreateDomainCmd;
import com.cloud.api.commands.CreateSSHKeyPairCmd;
@ -105,6 +106,7 @@ import com.cloud.api.commands.RegisterSSHKeyPairCmd;
import com.cloud.api.commands.StartSystemVMCmd;
import com.cloud.api.commands.StopSystemVmCmd;
import com.cloud.api.commands.UpdateDomainCmd;
import com.cloud.api.commands.UpdateHostPasswordCmd;
import com.cloud.api.commands.UpdateIsoCmd;
import com.cloud.api.commands.UpdateIsoPermissionsCmd;
import com.cloud.api.commands.UpdateTemplateCmd;
@ -165,8 +167,10 @@ import com.cloud.exception.ResourceUnavailableException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.Host;
import com.cloud.host.Host.Type;
import com.cloud.host.DetailVO;
import com.cloud.host.HostVO;
import com.cloud.host.Status;
import com.cloud.host.dao.DetailsDao;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.info.ConsoleProxyInfo;
@ -285,7 +289,8 @@ public class ManagementServerImpl implements ManagementServer {
private final VlanDao _vlanDao;
private final AccountVlanMapDao _accountVlanMapDao;
private final PodVlanMapDao _podVlanMapDao;
private final HostDao _hostDao;
private final HostDao _hostDao;
private final DetailsDao _detailsDao;
private final UserDao _userDao;
private final UserVmDao _userVmDao;
private final ConfigurationDao _configDao;
@ -352,7 +357,8 @@ public class ManagementServerImpl implements ManagementServer {
_vlanDao = locator.getDao(VlanDao.class);
_accountVlanMapDao = locator.getDao(AccountVlanMapDao.class);
_podVlanMapDao = locator.getDao(PodVlanMapDao.class);
_hostDao = locator.getDao(HostDao.class);
_hostDao = locator.getDao(HostDao.class);
_detailsDao = locator.getDao(DetailsDao.class);
_hostPodDao = locator.getDao(HostPodDao.class);
_jobDao = locator.getDao(AsyncJobDao.class);
_clusterDao = locator.getDao(ClusterDao.class);
@ -4756,5 +4762,56 @@ public class ManagementServerImpl implements ManagementServer {
return password;
}
@DB
public boolean updateHostPassword(UpdateHostPasswordCmd cmd) {
if (cmd.getClusterId()==null && cmd.getHostId() == null){
throw new InvalidParameterValueException("You should provide one of cluster id or a host id.");
}
else if (cmd.getClusterId()==null){
HostVO h = _hostDao.findById(cmd.getHostId());
if (h.getHypervisorType() == HypervisorType.XenServer){
throw new InvalidParameterValueException("You should provide cluster id for Xenserver cluster.");
}
DetailVO nv= _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME);
if (nv.getValue().equals(cmd.getUsername())){
DetailVO nvp= _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD);
nvp.setValue(cmd.getPassword());
_detailsDao.persist(nvp);
}
else {
throw new InvalidParameterValueException("The username is not under use by management server.");
}
}
else {
//get all the hosts in this cluster
List<HostVO> hosts = _hostDao.listByCluster(cmd.getClusterId());
Transaction txn = Transaction.currentTxn();
txn.start();
for (HostVO h : hosts) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Changing password for host name = " + h.getName());
}
//update password for this host
DetailVO nv= _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME);
if (nv.getValue().equals(cmd.getUsername())){
DetailVO nvp= _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD);
nvp.setValue(cmd.getPassword());
_detailsDao.persist(nvp);
}
else { //if one host in the cluster has diff username then rollback to maintain consistency
txn.rollback();
throw new InvalidParameterValueException("The username is not same for all hosts, please modify passwords for individual hosts.");
}
}
txn.commit();
// if hypervisor is xenserver then we update it in CitrixResourceBase
}
return true;
}
}