add deleteCluster API command

This commit is contained in:
Kelven Yang 2010-12-29 11:04:36 -08:00
parent a70bbc557a
commit bce5dfb0d4
4 changed files with 93 additions and 2 deletions

View File

@ -0,0 +1,55 @@
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.response.SuccessResponse;
@Implementation(description="Deletes a cluster.", responseObject=SuccessResponse.class)
public class DeleteClusterCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(DeleteClusterCmd.class.getName());
private static final String s_name = "deleteclusterresponse";
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="the cluster ID")
private Long id;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getId() {
return id;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getCommandName() {
return s_name;
}
@Override
public void execute(){
boolean result = _resourceService.deleteCluster(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete cluster");
}
}
}

View File

@ -23,6 +23,7 @@ import com.cloud.api.commands.AddClusterCmd;
import com.cloud.api.commands.AddHostCmd;
import com.cloud.api.commands.AddSecondaryStorageCmd;
import com.cloud.api.commands.CancelMaintenanceCmd;
import com.cloud.api.commands.DeleteClusterCmd;
import com.cloud.api.commands.DeleteHostCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
import com.cloud.api.commands.ReconnectHostCmd;
@ -56,6 +57,7 @@ public interface ResourceService {
* @throws InvalidParameterValueException
*/
List<? extends Host> discoverCluster(AddClusterCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
boolean deleteCluster(DeleteClusterCmd cmd) throws InvalidParameterValueException;
List<? extends Host> discoverHosts(AddHostCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
List<? extends Host> discoverHosts(AddSecondaryStorageCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
@ -68,6 +70,4 @@ public interface ResourceService {
* @throws InvalidParameterValueException
*/
boolean deleteHost(DeleteHostCmd cmd) throws InvalidParameterValueException;
}

View File

@ -167,6 +167,7 @@ listCapacity=com.cloud.api.commands.ListCapacityCmd;1
#### host commands
addHost=com.cloud.api.commands.AddHostCmd;1
addCluster=com.cloud.api.commands.AddClusterCmd;1
deleteCluster=com.cloud.api.commands.DeleteClusterCmd;1
add=com.cloud.api.commands.AddHostCmd;1
reconnectHost=com.cloud.api.commands.ReconnectHostCmd;1
updateHost=com.cloud.api.commands.UpdateHostCmd;1

View File

@ -77,6 +77,7 @@ import com.cloud.api.commands.AddClusterCmd;
import com.cloud.api.commands.AddHostCmd;
import com.cloud.api.commands.AddSecondaryStorageCmd;
import com.cloud.api.commands.CancelMaintenanceCmd;
import com.cloud.api.commands.DeleteClusterCmd;
import com.cloud.api.commands.DeleteHostCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
import com.cloud.api.commands.ReconnectHostCmd;
@ -803,6 +804,40 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
throw new DiscoveryException("Unable to add the host");
}
@Override @DB
public boolean deleteCluster(DeleteClusterCmd cmd) throws InvalidParameterValueException {
Transaction txn = Transaction.currentTxn();
try {
txn.start();
ClusterVO cluster = _clusterDao.lockRow(cmd.getId(), true);
if(cluster == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Cluster: " + cmd.getId() + " does not even exist. Delete call is ignored.");
}
txn.rollback();
return true;
}
List<HostVO> hosts = _hostDao.listByCluster(cmd.getId());
if(hosts.size() > 0) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Cluster: " + cmd.getId() + " still has hosts");
}
txn.rollback();
return false;
}
_clusterDao.remove(cmd.getId());
txn.commit();
return true;
} catch (Throwable t) {
s_logger.error("Unable to delete cluster: " + cmd.getId(), t);
txn.rollback();
return false;
}
}
@Override
@DB
public boolean deleteHost(long hostId) {