diff --git a/api/src/com/cloud/api/commands/DeleteClusterCmd.java b/api/src/com/cloud/api/commands/DeleteClusterCmd.java new file mode 100644 index 00000000000..56216ed5c17 --- /dev/null +++ b/api/src/com/cloud/api/commands/DeleteClusterCmd.java @@ -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"); + } + } +} diff --git a/api/src/com/cloud/resource/ResourceService.java b/api/src/com/cloud/resource/ResourceService.java index 5d9b3ca9a1c..3014027c8d1 100644 --- a/api/src/com/cloud/resource/ResourceService.java +++ b/api/src/com/cloud/resource/ResourceService.java @@ -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 discoverCluster(AddClusterCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException; + boolean deleteCluster(DeleteClusterCmd cmd) throws InvalidParameterValueException; List discoverHosts(AddHostCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException; List discoverHosts(AddSecondaryStorageCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException; @@ -68,6 +70,4 @@ public interface ResourceService { * @throws InvalidParameterValueException */ boolean deleteHost(DeleteHostCmd cmd) throws InvalidParameterValueException; - - } diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index 8f090dbe753..a0f2bb7b083 100755 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -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 diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index cb579947bd6..5d0ff918307 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -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 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) {