use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.
the class TemplateManagerImpl method createPrivateTemplate.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command) throws CloudRuntimeException {
final long templateId = command.getEntityId();
Long volumeId = command.getVolumeId();
final Long snapshotId = command.getSnapshotId();
VMTemplateVO privateTemplate = null;
final Long accountId = CallContext.current().getCallingAccountId();
SnapshotVO snapshot = null;
VolumeVO volume = null;
OptimiseFor optimiseFor = command.getOptimiseFor();
String manufacturerString = command.getManufacturerString();
String cpuFlags = command.getCpuFlags();
Boolean macLearning = command.getMacLearning();
MaintenancePolicy maintenancePolicy = command.getMaintenancePolicy();
try {
final TemplateInfo tmplInfo = this._tmplFactory.getTemplate(templateId, DataStoreRole.Image);
long zoneId = 0;
if (snapshotId != null) {
snapshot = this._snapshotDao.findById(snapshotId);
zoneId = snapshot.getDataCenterId();
} else if (volumeId != null) {
volume = this._volumeDao.findById(volumeId);
zoneId = volume.getDataCenterId();
}
DataStore store = this._dataStoreMgr.getImageStore(zoneId);
if (store == null) {
throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
}
final AsyncCallFuture<TemplateApiResult> future;
if (snapshotId != null) {
final DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, this._snapshotStoreDao, this._dataStoreMgr);
SnapshotInfo snapInfo = this._snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (dataStoreRole == DataStoreRole.Image) {
if (snapInfo == null) {
snapInfo = this._snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
}
if (volumeId == null) {
volumeId = snapInfo.getVolumeId();
}
// We need to copy the snapshot onto secondary.
final SnapshotStrategy snapshotStrategy = this._storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
snapshotStrategy.backupSnapshot(snapInfo);
// Attempt to grab it again.
snapInfo = this._snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
}
}
final DataStore snapStore = snapInfo.getDataStore();
if (snapStore != null) {
// pick snapshot image store to create template
store = snapStore;
}
}
future = this._tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
} else if (volumeId != null) {
final VolumeInfo volInfo = this._volFactory.getVolume(volumeId);
future = this._tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
} else {
throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
}
if (volume != null) {
final VMInstanceVO vm = this._vmInstanceDao.findById(volume.getInstanceId());
if (vm != null) {
if (optimiseFor == null) {
optimiseFor = vm.getOptimiseFor();
}
if (manufacturerString == null) {
manufacturerString = vm.getManufacturerString();
}
if (cpuFlags == null) {
cpuFlags = vm.getCpuFlags();
}
if (macLearning == null) {
macLearning = vm.getMacLearning();
}
if (maintenancePolicy == null) {
maintenancePolicy = vm.getMaintenancePolicy();
}
}
}
final CommandResult result;
try {
result = future.get();
if (result.isFailed()) {
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 (this._dataStoreMgr.isRegionStore(store)) {
// template created on region store
this._tmpltSvr.associateTemplateToZone(templateId, null);
} else {
final VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
this._tmpltZoneDao.persist(templateZone);
}
if (optimiseFor == null) {
optimiseFor = OptimiseFor.Generic;
}
if (maintenancePolicy == null) {
maintenancePolicy = MaintenancePolicy.LiveMigrate;
}
privateTemplate = this._tmpltDao.findById(templateId);
privateTemplate.setCpuFlags(cpuFlags);
privateTemplate.setMacLearning(macLearning);
privateTemplate.setManufacturerString(manufacturerString);
privateTemplate.setOptimiseFor(optimiseFor);
privateTemplate.setMaintenancePolicy(maintenancePolicy);
this._tmpltDao.persist(privateTemplate);
} catch (final InterruptedException | 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(final 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.
TemplateManagerImpl.this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template_zone_ref record
TemplateManagerImpl.this._tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template record
TemplateManagerImpl.this._tmpltDao.expunge(templateId);
// decrement resource count
if (accountId != null) {
TemplateManagerImpl.this._resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
TemplateManagerImpl.this._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");
}
}
use of com.cloud.model.enumeration.DataStoreRole in project cosmic by MissionCriticalCloud.
the class ApiResponseHelper method createSnapshotResponse.
@Override
public SnapshotResponse createSnapshotResponse(final Snapshot snapshot) {
final SnapshotResponse snapshotResponse = new SnapshotResponse();
snapshotResponse.setId(snapshot.getUuid());
populateOwner(snapshotResponse, snapshot);
final VolumeVO volume = findVolumeById(snapshot.getVolumeId());
final String snapshotTypeStr = snapshot.getRecurringType().name();
snapshotResponse.setSnapshotType(snapshotTypeStr);
if (volume != null) {
snapshotResponse.setVolumeId(volume.getUuid());
snapshotResponse.setVolumeName(volume.getName());
snapshotResponse.setVolumeType(volume.getVolumeType().name());
final DataCenter zone = ApiDBUtils.findZoneById(volume.getDataCenterId());
if (zone != null) {
snapshotResponse.setZoneId(zone.getUuid());
}
}
snapshotResponse.setCreated(snapshot.getCreated());
snapshotResponse.setName(snapshot.getName());
snapshotResponse.setIntervalType(ApiDBUtils.getSnapshotIntervalTypes(snapshot.getId()));
snapshotResponse.setState(snapshot.getState());
final SnapshotInfo snapshotInfo;
if (snapshot instanceof SnapshotInfo) {
snapshotInfo = (SnapshotInfo) snapshot;
} else {
final DataStoreRole dataStoreRole = getDataStoreRole(snapshot, this._snapshotStoreDao, this._dataStoreMgr);
snapshotInfo = this.snapshotfactory.getSnapshot(snapshot.getId(), dataStoreRole);
}
if (snapshotInfo == null) {
s_logger.debug("Unable to find info for image store snapshot with uuid " + snapshot.getUuid());
snapshotResponse.setRevertable(false);
} else {
snapshotResponse.setRevertable(snapshotInfo.isRevertable());
snapshotResponse.setPhysicaSize(snapshotInfo.getPhysicalSize());
}
// set tag information
final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Snapshot, snapshot.getId());
final List<ResourceTagResponse> tagResponses = new ArrayList<>();
for (final ResourceTag tag : tags) {
final ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
if (tagResponse != null) {
tagResponses.add(tagResponse);
}
}
snapshotResponse.setTags(tagResponses);
snapshotResponse.setObjectName("snapshot");
return snapshotResponse;
}
Aggregations