adding some error return handling to api dispatcher, and cleaning up some of the code paths to return the right error messages

This commit is contained in:
abhishek 2010-10-26 12:03:15 -07:00
parent 42f1ff92a9
commit c6588c0e25
6 changed files with 64 additions and 29 deletions

View File

@ -36,6 +36,7 @@ import com.cloud.configuration.ConfigurationManager;
import com.cloud.consoleproxy.ConsoleProxyManager;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.network.DomainRouterService;
import com.cloud.network.NetworkManager;
import com.cloud.network.security.NetworkGroupManager;
@ -154,6 +155,8 @@ public class ApiDispatcher {
throw new ServerApiException(BaseCmd.PARAM_ERROR, cause.getMessage());
} else if (cause instanceof PermissionDeniedException) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, cause.getMessage());
} else if (cause instanceof ResourceAllocationException){
throw new ServerApiException(BaseCmd.UNSUPPORTED_ACTION_ERROR, cause.getMessage());
}
s_logger.warn("Exception executing method " + methodName + " for command " + cmd.getClass().getSimpleName(), ite);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Unable to execute method " + methodName + " for command " + cmd.getClass().getSimpleName() + ", internal error in the implementation.");

View File

@ -19,13 +19,15 @@ package com.cloud.api.commands;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseAsyncCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.response.StatusResponse;
import com.cloud.event.EventTypes;
import com.cloud.user.Account;
@Implementation(method="uploadCertificate")
public class UploadCustomCertificateCmd extends BaseCmd {
public class UploadCustomCertificateCmd extends BaseAsyncCmd {
public static final Logger s_logger = Logger.getLogger(UploadCustomCertificateCmd.class.getName());
private static final String s_name = "uploadcustomcertificateresponse";
@ -37,11 +39,7 @@ public class UploadCustomCertificateCmd extends BaseCmd {
return path;
}
@Override
public String getName() {
return s_name;
}
@Override @SuppressWarnings("unchecked")
public StatusResponse getResponse() {
Boolean status = (Boolean)getResponseObject();
@ -51,4 +49,29 @@ public class UploadCustomCertificateCmd extends BaseCmd {
response.setResponseName(getName());
return response;
}
@Override
public String getEventType() {
return EventTypes.EVENT_VOLUME_CREATE;
}
@Override
public String getEventDescription() {
return ("Uploading custom certificate to the db, and applying it to the cpvm");
}
@Override
public String getName() {
return s_name;
}
public static String getResultObjectName() {
return "volume";
}
@Override
public long getAccountId() {
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
}
}

View File

@ -2448,25 +2448,27 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach
long proxyVmId = ((StartupProxyCommand)cmd).getProxyVmId();
ConsoleProxyVO consoleProxy = _consoleProxyDao.findById(proxyVmId);
//find corresponding host
HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy);
//now send a command to console proxy
UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr);
try {
Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd);
if(updateCertAns.getResult() == true)
{
//we have the cert copied over on cpvm
long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_REBOOT, "rebooting console proxy with Id: "+consoleProxy.getId());
rebootProxy(consoleProxy.getId(), eventId);
//when cp reboots, the context will be reinit with the new cert
s_logger.info("Successfully rebooted console proxy resource after custom certificate application");
}
} catch (AgentUnavailableException e) {
s_logger.warn("Unable to send update certificate command to the console proxy resource", e);
return false;
} catch (OperationTimedoutException e) {
s_logger.warn("Unable to send update certificate command to the console proxy resource", e);
return false;
if(consoleProxy!=null){
HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy);
//now send a command to console proxy
UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr);
try {
Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd);
if(updateCertAns.getResult() == true)
{
//we have the cert copied over on cpvm
long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_REBOOT, "rebooting console proxy with Id: "+consoleProxy.getId());
rebootProxy(consoleProxy.getId(), eventId);
//when cp reboots, the context will be reinit with the new cert
s_logger.info("Successfully rebooted console proxy resource after custom certificate application");
}
} catch (AgentUnavailableException e) {
s_logger.warn("Unable to send update certificate command to the console proxy resource", e);
return false;
} catch (OperationTimedoutException e) {
s_logger.warn("Unable to send update certificate command to the console proxy resource", e);
return false;
}
}
}else{
return false;//no cert

View File

@ -1219,5 +1219,5 @@ public interface ManagementServer {
*/
String[] getHypervisors(ListHypervisorsCmd cmd);
boolean uploadCertificate(UploadCustomCertificateCmd cmd);
boolean uploadCertificate(UploadCustomCertificateCmd cmd) throws ResourceAllocationException;
}

View File

@ -26,6 +26,7 @@ import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@ -148,6 +149,7 @@ import com.cloud.async.dao.AsyncJobDao;
import com.cloud.async.executor.ExtractJobResultObject;
import com.cloud.capacity.CapacityVO;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.certificate.CertificateVO;
import com.cloud.certificate.dao.CertificateDao;
import com.cloud.configuration.Config;
import com.cloud.configuration.ConfigurationManager;
@ -6946,7 +6948,12 @@ public class ManagementServerImpl implements ManagementServer {
}
@Override
public boolean uploadCertificate(UploadCustomCertificateCmd cmd) {
public boolean uploadCertificate(UploadCustomCertificateCmd cmd) throws ResourceAllocationException {
//limit no.of certs uploaded to 1
if(_certDao.listAll().size()>0){
throw new ResourceAllocationException("There is already a custom certificate in the db");
}
String certificatePath = cmd.getPath();
Long certVOId = _certDao.persistCustomCertToDb(certificatePath);//0 implies failure

View File

@ -118,7 +118,7 @@ CREATE TABLE `cloud`.`account_network_ref` (
CREATE TABLE `cloud`.`certificate` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`certificate` text NOT NULL UNIQUE COMMENT 'the actual custom certificate being stored in the db',
`certificate` text COMMENT 'the actual custom certificate being stored in the db',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;