use of com.emc.storageos.cinder.model.CinderSnapshot in project coprhd-controller by CoprHD.
the class SnapshotService method getSnapShot.
/**
* Get the details of a specific snapshot
*
* @prereq none
*
* @param tenant_id
* the URN of the tenant
* @param snapshot_id
* the URN of the snapshot
*
* @brief Show snapshot
* @return snapshot details
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/{snapshot_id}")
@CheckPermission(roles = { Role.SYSTEM_MONITOR, Role.TENANT_ADMIN }, acls = { ACL.ANY })
public Response getSnapShot(@PathParam("tenant_id") String openstack_tenant_id, @PathParam("snapshot_id") String snapshot_id, @HeaderParam("X-Cinder-V1-Call") String isV1Call, @Context HttpHeaders header) {
CinderSnapshot response = new CinderSnapshot();
_log.info("START get snapshot with id {}", snapshot_id);
BlockSnapshot snapshot = findSnapshot(snapshot_id, openstack_tenant_id);
if (snapshot == null) {
_log.error("Invalid snapshot ID ={} ", snapshot_id);
return CinderApiUtils.createErrorResponse(400, "Bad Request : Invalid snapshot " + snapshot_id);
}
response = getSnapshotDetail(snapshot, isV1Call, openstack_tenant_id);
return CinderApiUtils.getCinderResponse(response, header, true, CinderConstants.STATUS_OK);
}
use of com.emc.storageos.cinder.model.CinderSnapshot in project coprhd-controller by CoprHD.
the class SnapshotService method createSnapshot.
/**
* The snapshot of a volume in Block Store is a point in time copy of the
* volume. This API allows the user to create snapshot of a volume
* NOTE: This is an asynchronous operation.
*
* @prereq none
*
* @param param
* POST data containing the snapshot creation information.
*
* @brief Create snapshot
* @return Details of the newly created snapshot
* @throws InternalException
*/
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response createSnapshot(@PathParam("tenant_id") String openstack_tenant_id, SnapshotCreateRequestGen param, @Context HttpHeaders header, @HeaderParam("X-Cinder-V1-Call") String isV1Call) throws InternalException {
// Step 1: Parameter validation
String snapshotName = null;
String snapshotDescription = null;
if (isV1Call != null) {
snapshotName = param.snapshot.display_name;
snapshotDescription = param.snapshot.display_description;
} else {
snapshotName = param.snapshot.name;
snapshotDescription = param.snapshot.description;
}
// if snapshot name is empty create random name
if (snapshotName == null) {
snapshotName = "snapshot-" + RandomStringUtils.random(10);
}
if (snapshotName == null || (snapshotName.length() <= 2)) {
throw APIException.badRequests.parameterIsNotValid(param.snapshot.name);
}
URI volumeUri = null;
Volume volume = null;
volumeUri = URI.create(param.snapshot.volume_id);
volume = queryVolumeResource(volumeUri, openstack_tenant_id);
if (volume == null) {
_log.error("Invalid source volume id to create snapshot ={} ", param.snapshot.volume_id);
return CinderApiUtils.createErrorResponse(404, "Not Found : Invalid source volume id " + param.snapshot.volume_id);
}
VirtualPool pool = _dbClient.queryObject(VirtualPool.class, volume.getVirtualPool());
if (pool == null) {
_log.info("Virtual Pool corresponding to the volume does not exist.");
throw APIException.badRequests.parameterIsNotValid(volume.getVirtualPool().toString());
}
if (!validateSnapshotCreate(openstack_tenant_id, pool, volume.getProvisionedCapacity())) {
_log.info("The volume can not be created because of insufficient quota for virtual pool.");
throw APIException.badRequests.insufficientQuotaForVirtualPool(pool.getLabel(), "virtual pool");
}
if (!validateSnapshotCreate(openstack_tenant_id, null, volume.getProvisionedCapacity())) {
_log.info("The volume can not be created because of insufficient quota for Project.");
throw APIException.badRequests.insufficientQuotaForProject(pool.getLabel(), "project");
}
BlockFullCopyManager fcManager = new BlockFullCopyManager(_dbClient, _permissionsHelper, _auditMgr, _coordinator, _placementManager, sc, uriInfo, _request, _tenantsService);
VolumeIngestionUtil.checkOperationSupportedOnIngestedVolume(volume, ResourceOperationTypeEnum.CREATE_VOLUME_SNAPSHOT, _dbClient);
// Don't operate on VPLEX backend volumes or RP journal volumes.
BlockServiceUtils.validateNotAnInternalBlockObject(volume, false);
validateSourceVolumeHasExported(volume);
String snapshotType = TechnologyType.NATIVE.toString();
Boolean createInactive = Boolean.FALSE;
Boolean readOnly = Boolean.FALSE;
BlockServiceApi api = getBlockServiceImpl(pool, _dbClient);
List<Volume> volumesToSnap = new ArrayList<Volume>();
volumesToSnap.addAll(api.getVolumesToSnap(volume, snapshotType));
api.validateCreateSnapshot(volume, volumesToSnap, snapshotType, snapshotName, readOnly, fcManager);
String taskId = UUID.randomUUID().toString();
List<URI> snapshotURIs = new ArrayList<URI>();
List<BlockSnapshot> snapshots = api.prepareSnapshots(volumesToSnap, snapshotType, snapshotName, snapshotURIs, taskId);
TaskList response = new TaskList();
for (BlockSnapshot snapshot : snapshots) {
response.getTaskList().add(toTask(snapshot, taskId));
}
// Update the task status for the volumes task.
_dbClient.createTaskOpStatus(Volume.class, volume.getId(), taskId, ResourceOperationTypeEnum.CREATE_VOLUME_SNAPSHOT);
// Invoke the block service API implementation to create the snapshot
api.createSnapshot(volume, snapshotURIs, snapshotType, createInactive, readOnly, taskId);
SnapshotCreateResponse snapCreateResp = new SnapshotCreateResponse();
for (TaskResourceRep rep : response.getTaskList()) {
URI snapshotUri = rep.getResource().getId();
BlockSnapshot snap = _dbClient.queryObject(BlockSnapshot.class, snapshotUri);
if (snap != null) {
StringMap extensions = snap.getExtensions();
if (extensions == null)
extensions = new StringMap();
extensions.put("display_description", (snapshotDescription == null) ? "" : snapshotDescription);
extensions.put("taskid", rep.getId().toString());
_log.debug("Create snapshot : stored description");
snap.setExtensions(extensions);
ScopedLabelSet tagSet = new ScopedLabelSet();
snap.setTag(tagSet);
String[] splits = snapshotUri.toString().split(":");
String tagName = splits[3];
// this check will verify whether retrieved data is not corrupted
if (tagName == null || tagName.isEmpty() || tagName.length() < 2) {
throw APIException.badRequests.parameterTooShortOrEmpty("Tag", 2);
}
Volume parentVol = _permissionsHelper.getObjectById(snap.getParent(), Volume.class);
URI tenantOwner = parentVol.getTenant().getURI();
ScopedLabel tagLabel = new ScopedLabel(tenantOwner.toString(), tagName);
tagSet.add(tagLabel);
_dbClient.updateObject(snap);
if (isV1Call != null) {
_log.debug("Inside V1 call");
return CinderApiUtils.getCinderResponse(getSnapshotDetail(snap, isV1Call, openstack_tenant_id), header, true, CinderConstants.STATUS_OK);
} else {
return CinderApiUtils.getCinderResponse(getSnapshotDetail(snap, isV1Call, openstack_tenant_id), header, true, CinderConstants.STATUS_ACCEPT);
}
}
}
return CinderApiUtils.getCinderResponse(new CinderSnapshot(), header, true, CinderConstants.STATUS_ACCEPT);
}
use of com.emc.storageos.cinder.model.CinderSnapshot in project coprhd-controller by CoprHD.
the class SnapshotService method getSnapshotDetail.
// INTERNAL FUNCTIONS
protected CinderSnapshot getSnapshotDetail(BlockSnapshot snapshot, String isV1Call, String openstack_tenant_id) {
CinderSnapshot detail = new CinderSnapshot();
detail.id = getCinderHelper().trimId(snapshot.getId().toString());
detail.volume_id = getCinderHelper().trimId(snapshot.getParent().getURI().toString());
detail.created_at = date(snapshot.getCreationTime().getTimeInMillis());
detail.project_id = openstack_tenant_id;
detail.size = (int) ((snapshot.getProvisionedCapacity() + HALF_GB) / GB);
StringMap extensions = snapshot.getExtensions();
String description = null;
Map<String, String> metaMap = new HashMap<String, String>();
if (extensions != null) {
description = extensions.get("display_description");
_log.debug("Retreiving the tasks for snapshot id {}", snapshot.getId());
List<Task> taskLst = TaskUtils.findResourceTasks(_dbClient, snapshot.getId());
_log.debug("Retreived the tasks for snapshot id {}", snapshot.getId());
String taskInProgressId = null;
if (snapshot.getExtensions().containsKey("taskid")) {
taskInProgressId = snapshot.getExtensions().get("taskid");
// Task acttask = TaskUtils.findTaskForRequestId(_dbClient, snapshot.getId(), taskInProgressId);
for (Task tsk : taskLst) {
if (tsk.getId().toString().equals(taskInProgressId)) {
if (tsk.getStatus().equals("ready")) {
detail.status = CinderConstants.ComponentStatus.AVAILABLE.getStatus().toLowerCase();
snapshot.getExtensions().put("status", CinderConstants.ComponentStatus.AVAILABLE.getStatus().toLowerCase());
snapshot.getExtensions().remove("taskid");
_dbClient.updateObject(snapshot);
} else if (tsk.getStatus().equals("pending")) {
if (tsk.getDescription().equals(ResourceOperationTypeEnum.CREATE_VOLUME_SNAPSHOT.getDescription())) {
detail.status = CinderConstants.ComponentStatus.CREATING.getStatus().toLowerCase();
} else if (tsk.getDescription().equals(ResourceOperationTypeEnum.DELETE_VOLUME_SNAPSHOT.getDescription())) {
detail.status = CinderConstants.ComponentStatus.DELETING.getStatus().toLowerCase();
}
} else if (tsk.getStatus().equals("error")) {
detail.status = CinderConstants.ComponentStatus.ERROR.getStatus().toLowerCase();
snapshot.getExtensions().put("status", CinderConstants.ComponentStatus.ERROR.getStatus().toLowerCase());
snapshot.getExtensions().remove("taskid");
_dbClient.updateObject(snapshot);
}
break;
}
}
} else if (snapshot.getExtensions().containsKey("status") && !snapshot.getExtensions().get("status").toString().toLowerCase().equals("")) {
detail.status = snapshot.getExtensions().get("status").toString().toLowerCase();
} else {
// "available";
detail.status = CinderConstants.ComponentStatus.AVAILABLE.getStatus().toLowerCase();
}
for (String mapEntry : extensions.keySet()) {
if (mapEntry.startsWith("METADATA_")) {
String value = extensions.get(mapEntry);
metaMap.put(mapEntry.substring("METADATA_".length()), value);
}
}
}
if (isV1Call != null) {
detail.display_name = snapshot.getLabel();
detail.display_description = (description == null) ? "" : description;
} else {
detail.name = snapshot.getLabel();
detail.description = (description == null) ? "" : description;
}
// default
detail.progress = ZERO_PERCENT_COMPLETION;
if ((detail.status == CinderConstants.ComponentStatus.CREATING.getStatus().toLowerCase()) || (detail.status == CinderConstants.ComponentStatus.DELETING.getStatus().toLowerCase()) || (detail.status == CinderConstants.ComponentStatus.ERROR.getStatus().toLowerCase()) || (detail.status == CinderConstants.ComponentStatus.ERROR_DELETING.getStatus().toLowerCase())) {
detail.progress = ZERO_PERCENT_COMPLETION;
} else if (detail.status == CinderConstants.ComponentStatus.AVAILABLE.getStatus().toLowerCase()) {
detail.progress = HUNDRED_PERCENT_COMPLETION;
}
detail.metadata = metaMap;
return detail;
}
use of com.emc.storageos.cinder.model.CinderSnapshot in project coprhd-controller by CoprHD.
the class SnapshotService method getSnapshotList.
/**
* Get the summary list of all snapshots for the given tenant
*
* @prereq none
*
* @param tenant_id
* the URN of the tenant
*
* @brief List snapshots
* @return Snapshot list
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/detail")
@CheckPermission(roles = { Role.SYSTEM_MONITOR, Role.TENANT_ADMIN }, acls = { ACL.ANY })
public Response getSnapshotList(@PathParam("tenant_id") String openstack_tenant_id, @HeaderParam("X-Cinder-V1-Call") String isV1Call, @Context HttpHeaders header) {
CinderSnapshotListRestResp snapshots = new CinderSnapshotListRestResp();
URIQueryResultList uris = getSnapshotUris(openstack_tenant_id);
if (uris != null) {
while (uris.iterator().hasNext()) {
URI snapshotUri = uris.iterator().next();
BlockSnapshot snap = _dbClient.queryObject(BlockSnapshot.class, snapshotUri);
if (snap != null && !snap.getInactive()) {
CinderSnapshot cinder_snapshot = getSnapshotDetail(snap, isV1Call, openstack_tenant_id);
snapshots.getSnapshots().add(cinder_snapshot);
}
}
}
return CinderApiUtils.getCinderResponse(snapshots, header, false, CinderConstants.STATUS_OK);
}
Aggregations