use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.
the class LinstorPrimaryDataStoreDriverImpl method deleteAsync.
@Override
public void deleteAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
s_logger.debug("deleteAsync: " + dataObject.getType() + ";" + dataObject.getUuid());
String errMsg = null;
final long storagePoolId = dataStore.getId();
final StoragePoolVO storagePool = _storagePoolDao.findById(storagePoolId);
switch(dataObject.getType()) {
case VOLUME:
{
final VolumeInfo volumeInfo = (VolumeInfo) dataObject;
final String rscName = LinstorUtil.RSC_PREFIX + volumeInfo.getPath();
deleteResourceDefinition(storagePool, rscName);
long usedBytes = storagePool.getUsedBytes();
long capacityIops = storagePool.getCapacityIops();
usedBytes -= volumeInfo.getSize();
if (volumeInfo.getMaxIops() != null)
capacityIops += volumeInfo.getMaxIops();
storagePool.setUsedBytes(Math.max(0, usedBytes));
storagePool.setCapacityIops(Math.max(0, capacityIops));
_storagePoolDao.update(storagePoolId, storagePool);
}
break;
case SNAPSHOT:
final SnapshotInfo snapshotInfo = (SnapshotInfo) dataObject;
final String rscName = LinstorUtil.RSC_PREFIX + snapshotInfo.getBaseVolume().getPath();
deleteSnapshot(dataStore, rscName, getSnapshotName(snapshotInfo.getUuid()));
long usedBytes = storagePool.getUsedBytes() - snapshotInfo.getSize();
storagePool.setUsedBytes(Math.max(0, usedBytes));
_storagePoolDao.update(storagePoolId, storagePool);
break;
default:
errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
s_logger.error(errMsg);
}
if (callback != null) {
CommandResult result = new CommandResult();
result.setResult(errMsg);
callback.complete(result);
}
}
use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.
the class DateraPrimaryDataStoreDriver method deleteAsync.
/**
* Entrypoint for delete operations.
*
* @param dataStore Primary storage
* @param dataObject object to delete
* @param callback used for async, complete the callback after the operation
* is done.
*/
@Override
public void deleteAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
String errMsg = null;
try {
if (dataObject.getType() == DataObjectType.VOLUME) {
s_logger.debug("deleteAsync - deleting volume");
deleteVolume((VolumeInfo) dataObject, dataStore.getId());
} else if (dataObject.getType() == DataObjectType.SNAPSHOT) {
s_logger.debug("deleteAsync - deleting snapshot");
deleteSnapshot((SnapshotInfo) dataObject, dataStore.getId());
} else if (dataObject.getType() == DataObjectType.TEMPLATE) {
s_logger.debug("deleteAsync - deleting template");
deleteTemplate((TemplateInfo) dataObject, dataStore.getId());
} else {
errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
}
} catch (Exception ex) {
errMsg = ex.getMessage();
s_logger.error(errMsg);
}
CommandResult result = new CommandResult();
result.setResult(errMsg);
callback.complete(result);
}
use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.
the class LinstorPrimaryDataStoreDriverImpl method revertSnapshot.
@Override
public void revertSnapshot(SnapshotInfo snapshot, SnapshotInfo snapshotOnPrimaryStore, AsyncCompletionCallback<CommandResult> callback) {
s_logger.debug("Linstor: revertSnapshot");
final VolumeInfo volumeInfo = snapshot.getBaseVolume();
VolumeVO volumeVO = _volumeDao.findById(volumeInfo.getId());
if (volumeVO == null || volumeVO.getRemoved() != null) {
CommandResult commandResult = new CommandResult();
commandResult.setResult("The volume that the snapshot belongs to no longer exists.");
callback.complete(commandResult);
return;
}
String resultMsg;
try {
final StoragePool pool = (StoragePool) snapshot.getDataStore();
final String rscName = LinstorUtil.RSC_PREFIX + volumeInfo.getUuid();
final String snapName = LinstorUtil.RSC_PREFIX + snapshot.getUuid();
final DevelopersApi linstorApi = LinstorUtil.getLinstorAPI(pool.getHostAddress());
ApiCallRcList answers = linstorApi.resourceSnapshotRollback(rscName, snapName);
resultMsg = checkLinstorAnswers(answers);
} catch (ApiException apiEx) {
s_logger.error("Linstor: ApiEx - " + apiEx.getMessage());
resultMsg = apiEx.getBestMessage();
}
if (callback != null) {
CommandResult result = new CommandResult();
result.setResult(resultMsg);
callback.complete(result);
}
}
use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.
the class ElastistorPrimaryDataStoreDriver method deleteAsync.
@Override
public void deleteAsync(DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
String errMsg = null;
StoragePoolVO storagePool = _storagePoolDao.findById(dataStore.getId());
if (!(storagePool.isManaged())) {
super.deleteAsync(dataStore, dataObject, callback);
return;
}
if (dataObject.getType() == DataObjectType.VOLUME) {
VolumeInfo volumeInfo = (VolumeInfo) dataObject;
long storagePoolId = dataStore.getId();
boolean result = false;
try {
result = ElastistorUtil.deleteElastistorVolume(volumeInfo.getUuid());
} catch (Throwable e) {
e.printStackTrace();
CommandResult result2 = new CommandResult();
result2.setResult(e.toString());
callback.complete(result2);
}
if (result) {
long usedBytes = storagePool.getUsedBytes();
long capacityIops = storagePool.getCapacityIops();
usedBytes -= volumeInfo.getSize();
capacityIops += volumeInfo.getMaxIops();
storagePool.setUsedBytes(usedBytes < 0 ? 0 : usedBytes);
storagePool.setCapacityIops(capacityIops < 0 ? 0 : capacityIops);
_storagePoolDao.update(storagePoolId, storagePool);
} else {
errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
}
} else {
errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
}
CommandResult result = new CommandResult();
result.setResult(errMsg);
callback.complete(result);
}
use of org.apache.cloudstack.storage.command.CommandResult in project cloudstack by apache.
the class TemplateManagerImpl method createPrivateTemplate.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
final long templateId = command.getEntityId();
Long volumeId = command.getVolumeId();
Long snapshotId = command.getSnapshotId();
VMTemplateVO privateTemplate = null;
final Long accountId = CallContext.current().getCallingAccountId();
SnapshotVO snapshot = null;
VolumeVO volume = null;
Account caller = CallContext.current().getCallingAccount();
try {
TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
long zoneId = 0;
if (snapshotId != null) {
snapshot = _snapshotDao.findById(snapshotId);
zoneId = snapshot.getDataCenterId();
} else if (volumeId != null) {
volume = _volumeDao.findById(volumeId);
zoneId = volume.getDataCenterId();
}
DataStore store = _dataStoreMgr.getImageStoreWithFreeCapacity(zoneId);
if (store == null) {
throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
}
AsyncCallFuture<TemplateApiResult> future = null;
if (snapshotId != null) {
DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (dataStoreRole == DataStoreRole.Image) {
if (snapInfo == null) {
snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
}
// We need to copy the snapshot onto secondary.
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
snapshotStrategy.backupSnapshot(snapInfo);
// Attempt to grab it again.
snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
}
}
_accountMgr.checkAccess(caller, null, true, snapInfo);
DataStore snapStore = snapInfo.getDataStore();
if (snapStore != null) {
// pick snapshot image store to create template
store = snapStore;
}
}
future = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
} else if (volumeId != null) {
VolumeInfo volInfo = _volFactory.getVolume(volumeId);
if (volInfo == null) {
throw new InvalidParameterValueException("No such volume exist");
}
_accountMgr.checkAccess(caller, null, true, volInfo);
future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
} else {
throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
}
CommandResult result = null;
try {
result = future.get();
if (result.isFailed()) {
privateTemplate = null;
s_logger.debug("Failed to create template" + result.getResult());
throw new CloudRuntimeException("Failed to create template" + result.getResult());
}
// create entries in template_zone_ref table
if (_dataStoreMgr.isRegionStore(store)) {
// template created on region store
_tmpltSvr.associateTemplateToZone(templateId, null);
} else {
VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
_tmpltZoneDao.persist(templateZone);
}
privateTemplate = _tmpltDao.findById(templateId);
TemplateDataStoreVO srcTmpltStore = _tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), zoneId, privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), srcTmpltStore.getPhysicalSize(), privateTemplate.getSize());
_usageEventDao.persist(usageEvent);
} catch (InterruptedException e) {
s_logger.debug("Failed to create template", e);
throw new CloudRuntimeException("Failed to create template", e);
} catch (ExecutionException e) {
s_logger.debug("Failed to create template", e);
throw new CloudRuntimeException("Failed to create template", e);
}
} finally {
if (privateTemplate == null) {
final VolumeVO volumeFinal = volume;
final SnapshotVO snapshotFinal = snapshot;
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
// template_store_ref entries should have been removed using our
// DataObject.processEvent command in case of failure, but clean
// it up here to avoid
// some leftovers which will cause removing template from
// vm_template table fail.
_tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template_zone_ref record
_tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template record
_tmpltDao.expunge(templateId);
// decrement resource count
if (accountId != null) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
}
}
});
}
}
if (privateTemplate != null) {
return privateTemplate;
} else {
throw new CloudRuntimeException("Failed to create a template");
}
}
Aggregations