Don't allow to remove a network with deleteNetwork api when the network has active nics assigned

This commit is contained in:
alena 2010-12-20 20:19:47 -08:00
parent 21bb9df292
commit 724b8d80bd
3 changed files with 13 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.response.SuccessResponse;
import com.cloud.exception.InvalidParameterValueException;
@Implementation(description="Deletes a network", responseObject=SuccessResponse.class)
public class DeleteNetworkCmd extends BaseCmd{
@ -60,6 +61,11 @@ public class DeleteNetworkCmd extends BaseCmd{
@Override
public void execute(){
//Don't allow to delete network via api call when it has vms assigned to it
int nicCount = _networkService.getActiveNicsInNetwork(id);
if (nicCount > 0) {
throw new InvalidParameterValueException("Unable to remove the network id=" + id + " as it has active Nics.");
}
boolean result = _networkService.deleteNetwork(id);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());

View File

@ -88,4 +88,6 @@ public interface NetworkService {
boolean deleteNetwork(long networkId) throws InvalidParameterValueException, PermissionDeniedException;
boolean restartNetwork(RestartNetworkCmd cmd) throws ConcurrentOperationException;
int getActiveNicsInNetwork(long networkId);
}

View File

@ -2116,4 +2116,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
}
return success;
}
@Override
public int getActiveNicsInNetwork(long networkId) {
return _networksDao.getActiveNicsIn(networkId);
}
}