diff --git a/server/src/com/cloud/api/commands/DeleteVolumeCmd.java b/server/src/com/cloud/api/commands/DeleteVolumeCmd.java index ad9b87c54fd..570b1d88aa5 100644 --- a/server/src/com/cloud/api/commands/DeleteVolumeCmd.java +++ b/server/src/com/cloud/api/commands/DeleteVolumeCmd.java @@ -18,18 +18,10 @@ package com.cloud.api.commands; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.storage.VolumeVO; -import com.cloud.user.Account; -import com.cloud.utils.Pair; public class DeleteVolumeCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(DeleteVolumeCmd.class.getName()); @@ -64,45 +56,45 @@ public class DeleteVolumeCmd extends BaseCmd { return "volume"; } - @Override - public List> execute(Map params) { - Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Long volumeId = (Long) params.get(BaseCmd.Properties.ID.getName()); - - boolean isAdmin; - if (account == null) { - // Admin API call - isAdmin = true; - } else { - // User API call - isAdmin = isAdmin(account.getType()); - } - - // Check that the volume ID is valid - VolumeVO volume = getManagementServer().findVolumeById(volumeId); - if (volume == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId); - } - - // If the account is not an admin, check that the volume is owned by the account that was passed in - if (!isAdmin) { - if (account.getId() != volume.getAccountId()) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId + " for account: " + account.getAccountName()); - } - } else if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), volume.getDomainId())) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to delete volume with id " + volumeId + ", permission denied."); - } - - try { - getManagementServer().destroyVolume(volumeId); - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), "true")); - - return returnValues; - } catch (Exception ex) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete volume: " + ex.getMessage()); - } - } +// @Override +// public List> execute(Map params) { +// Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// Long volumeId = (Long) params.get(BaseCmd.Properties.ID.getName()); +// +// boolean isAdmin; +// if (account == null) { +// // Admin API call +// isAdmin = true; +// } else { +// // User API call +// isAdmin = isAdmin(account.getType()); +// } +// +// // Check that the volume ID is valid +// VolumeVO volume = getManagementServer().findVolumeById(volumeId); +// if (volume == null) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId); +// } +// +// // If the account is not an admin, check that the volume is owned by the account that was passed in +// if (!isAdmin) { +// if (account.getId() != volume.getAccountId()) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId + " for account: " + account.getAccountName()); +// } +// } else if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), volume.getDomainId())) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to delete volume with id " + volumeId + ", permission denied."); +// } +// +// try { +// getManagementServer().destroyVolume(volumeId); +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), "true")); +// +// return returnValues; +// } catch (Exception ex) { +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete volume: " + ex.getMessage()); +// } +// } @Override diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 967ba6a7a7a..3aac64179a0 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -472,7 +472,7 @@ public interface ManagementServer { * Marks a data volume as destroyed * @param volumeId */ - void destroyVolume(long volumeId) throws InvalidParameterValueException; +// void destroyVolume(long volumeId) throws InvalidParameterValueException; /** * Return a list of IP addresses diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index b948ee2a831..b23e66f29a9 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -1824,32 +1824,32 @@ public class ManagementServerImpl implements ManagementServer { return null; } - @Override - public void destroyVolume(long volumeId) throws InvalidParameterValueException { - // Check that the volume is valid - VolumeVO volume = _volumeDao.findById(volumeId); - if (volume == null) { - throw new InvalidParameterValueException("Please specify a valid volume ID."); - } - - // Check that the volume is stored on shared storage - if (!_storageMgr.volumeOnSharedStoragePool(volume)) { - throw new InvalidParameterValueException("Please specify a volume that has been created on a shared storage pool."); - } - - // Check that the volume is not currently attached to any VM - if (volume.getInstanceId() != null) { - throw new InvalidParameterValueException("Please specify a volume that is not attached to any VM."); - } - - // Check that the volume is not already destroyed - if (volume.getDestroyed()) { - throw new InvalidParameterValueException("Please specify a volume that is not already destroyed."); - } - - // Destroy the volume - _storageMgr.destroyVolume(volume); - } +// @Override +// public void destroyVolume(long volumeId) throws InvalidParameterValueException { +// // Check that the volume is valid +// VolumeVO volume = _volumeDao.findById(volumeId); +// if (volume == null) { +// throw new InvalidParameterValueException("Please specify a valid volume ID."); +// } +// +// // Check that the volume is stored on shared storage +// if (!_storageMgr.volumeOnSharedStoragePool(volume)) { +// throw new InvalidParameterValueException("Please specify a volume that has been created on a shared storage pool."); +// } +// +// // Check that the volume is not currently attached to any VM +// if (volume.getInstanceId() != null) { +// throw new InvalidParameterValueException("Please specify a volume that is not attached to any VM."); +// } +// +// // Check that the volume is not already destroyed +// if (volume.getDestroyed()) { +// throw new InvalidParameterValueException("Please specify a volume that is not already destroyed."); +// } +// +// // Destroy the volume +// _storageMgr.destroyVolume(volume); +// } @Override public List listPublicIpAddressesBy(Long accountId, boolean allocatedOnly, Long zoneId, Long vlanDbId) { diff --git a/server/src/com/cloud/storage/StorageManager.java b/server/src/com/cloud/storage/StorageManager.java index 56ea892b0ba..d78993fbb21 100644 --- a/server/src/com/cloud/storage/StorageManager.java +++ b/server/src/com/cloud/storage/StorageManager.java @@ -207,7 +207,7 @@ public interface StorageManager extends Manager { * @param volume */ void destroyVolume(VolumeVO volume); - boolean deleteVolume(DeleteVolumeCmd cmd); + boolean deleteVolume(DeleteVolumeCmd cmd) throws InvalidParameterValueException; /** Create capacity entries in the op capacity table * @param storagePool diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index 2544c967f7d..b9903e7af90 100644 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -40,6 +40,7 @@ import java.util.concurrent.TimeUnit; import javax.ejb.Local; import javax.naming.ConfigurationException; +import org.GNOME.Accessibility._DocumentStub; import org.apache.log4j.Logger; import com.cloud.agent.api.Answer; @@ -83,6 +84,7 @@ import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; import com.cloud.dc.dao.DataCenterDao; import com.cloud.dc.dao.HostPodDao; +import com.cloud.domain.dao.DomainDao; import com.cloud.event.EventState; import com.cloud.event.EventTypes; import com.cloud.event.EventVO; @@ -130,6 +132,7 @@ import com.cloud.template.TemplateManager; import com.cloud.user.Account; import com.cloud.user.AccountManager; import com.cloud.user.User; +import com.cloud.user.UserContext; import com.cloud.user.dao.AccountDao; import com.cloud.uservm.UserVm; import com.cloud.utils.NumbersUtil; @@ -195,6 +198,7 @@ public class StorageManagerImpl implements StorageManager { @Inject protected VMTemplateDao _templateDao; @Inject protected VMTemplateHostDao _templateHostDao; @Inject protected ServiceOfferingDao _offeringDao; + @Inject protected DomainDao _domainDao; protected SearchBuilder HostTemplateStatesSearch; protected SearchBuilder PoolsUsedByVmSearch; @@ -2253,9 +2257,67 @@ public class StorageManagerImpl implements StorageManager { } } + private boolean isAdmin(short accountType) { + return ((accountType == Account.ACCOUNT_TYPE_ADMIN) || + (accountType == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || + (accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)); + } + @Override - public boolean deleteVolume(DeleteVolumeCmd cmd) { + public boolean deleteVolume(DeleteVolumeCmd cmd) throws InvalidParameterValueException { - return false; + Account account = (Account) UserContext.current().getAccountObject(); + Long volumeId = (Long) cmd.getId(); + + boolean isAdmin; + if (account == null) { + // Admin API call + isAdmin = true; + } else { + // User API call + isAdmin = isAdmin(account.getType()); + } + + // Check that the volume ID is valid + VolumeVO volume = _volsDao.findById(volumeId); + if (volume == null) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId); + } + + // If the account is not an admin, check that the volume is owned by the account that was passed in + if (!isAdmin) { + if (account.getId() != volume.getAccountId()) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId + " for account: " + account.getAccountName()); + } + } else if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), volume.getDomainId())) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to delete volume with id " + volumeId + ", permission denied."); + } + + + // Check that the volume is stored on shared storage + if (!volumeOnSharedStoragePool(volume)) { + throw new InvalidParameterValueException("Please specify a volume that has been created on a shared storage pool."); + } + + // Check that the volume is not currently attached to any VM + if (volume.getInstanceId() != null) { + throw new InvalidParameterValueException("Please specify a volume that is not attached to any VM."); + } + + // Check that the volume is not already destroyed + if (volume.getDestroyed()) { + throw new InvalidParameterValueException("Please specify a volume that is not already destroyed."); + } + + try { + // Destroy the volume + destroyVolume(volume); + } catch (Exception e) { + s_logger.warn("Error destroying volume:"+e); + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Error destroying volume:"+e); + } + + return true; + } }