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

status 6451: resolved fixed
This commit is contained in:
Abhinandan Prateek 2011-05-03 09:45:49 +05:30
parent 57b08cb76b
commit db916401a0
3 changed files with 96 additions and 2 deletions

View File

@ -89,7 +89,13 @@ public class UpdateHostPasswordCmd extends BaseCmd {
@Override
public void execute() {
_mgr.updateHostPassword(this);
this.setResponseObject(new SuccessResponse());
boolean result = _resourceService.updateHostPassword(this);
if (result){
_mgr.updateHostPassword(this);
this.setResponseObject(new SuccessResponse(getCommandName()));
}
else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update host(s) password. Please, check the username and password.");
}
}
}

View File

@ -27,6 +27,7 @@ import com.cloud.api.commands.DeleteClusterCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
import com.cloud.api.commands.ReconnectHostCmd;
import com.cloud.api.commands.UpdateHostCmd;
import com.cloud.api.commands.UpdateHostPasswordCmd;
import com.cloud.exception.AgentUnavailableException;
import com.cloud.exception.DiscoveryException;
import com.cloud.exception.InvalidParameterValueException;
@ -79,6 +80,8 @@ public interface ResourceService {
* @param true if deleted, false otherwise
*/
boolean deleteHost(long hostId, boolean isForced);
boolean updateHostPassword(UpdateHostPasswordCmd upasscmd);
Host getHost(long hostId);

View File

@ -82,6 +82,7 @@ import com.cloud.api.commands.DeleteClusterCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
import com.cloud.api.commands.ReconnectHostCmd;
import com.cloud.api.commands.UpdateHostCmd;
import com.cloud.api.commands.UpdateHostPasswordCmd;
import com.cloud.capacity.Capacity;
import com.cloud.capacity.CapacityVO;
import com.cloud.capacity.dao.CapacityDao;
@ -1245,6 +1246,90 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
_accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(), host.getDataCenterId());
return deleteHost(hostId, isForced, caller);
}
@Override
public boolean updateHostPassword(UpdateHostPasswordCmd cmd){
List<HostVO> hosts = _hostDao.listByCluster(cmd.getClusterId());
for (HostVO host : hosts) {
String resourceName = host.getResource();
ServerResource resource = null;
try {
Class<?> clazz = Class.forName(resourceName);
Constructor constructor = clazz.getConstructor();
resource = (ServerResource) constructor.newInstance();
} catch (ClassNotFoundException e) {
s_logger.warn("Unable to find class " + host.getResource(), e);
return false;
} catch (InstantiationException e) {
s_logger.warn("Unablet to instantiate class " + host.getResource(), e);
return false;
} catch (IllegalAccessException e) {
s_logger.warn("Illegal access " + host.getResource(), e);
return false;
} catch (SecurityException e) {
s_logger.warn("Security error on " + host.getResource(), e);
return false;
} catch (NoSuchMethodException e) {
s_logger.warn("NoSuchMethodException error on " + host.getResource(), e);
return false;
} catch (IllegalArgumentException e) {
s_logger.warn("IllegalArgumentException error on " + host.getResource(), e);
return false;
} catch (InvocationTargetException e) {
s_logger.warn("InvocationTargetException error on " + host.getResource(), e);
return false;
}
_hostDao.loadDetails(host);
HashMap<String, Object> params = new HashMap<String, Object>(host.getDetails().size() + 5);
params.putAll(host.getDetails());
params.put("guid", host.getGuid());
params.put("zone", Long.toString(host.getDataCenterId()));
if (host.getPodId() != null) {
params.put("pod", Long.toString(host.getPodId()));
}
if (host.getClusterId() != null) {
params.put("cluster", Long.toString(host.getClusterId()));
String guid = null;
ClusterVO cluster = _clusterDao.findById(host.getClusterId());
if (cluster.getGuid() == null) {
guid = host.getDetail("pool");
} else {
guid = cluster.getGuid();
}
if (guid == null || guid.isEmpty()) {
throw new CloudRuntimeException("Can not find guid for cluster " + cluster.getId() + " name " + cluster.getName());
}
params.put("pool", guid);
}
params.put("ipaddress", host.getPrivateIpAddress());
params.put("secondary.storage.vm", "false");
params.put("max.template.iso.size", _configDao.getValue("max.template.iso.size"));
params.put("username", cmd.getUsername());
params.put("password", cmd.getPassword());
try {
resource.configure(host.getName(), params);
} catch (ConfigurationException e) {
s_logger.warn("Unable to configure resource due to ", e);
return false;
}
if (!resource.start()) {
s_logger.warn("Unable to start the resource");
return false;
}
host.setLastPinged(System.currentTimeMillis() >> 10);
host.setManagementServerId(_nodeId);
_hostDao.update(host.getId(), host);
_executor.execute(new SimulateStartTask(host.getId(), resource, host.getDetails(), null));
}
return true;
}
@DB
protected boolean deleteSecondaryStorageHost(HostVO secStorageHost) {