use of com.emc.storageos.cinder.model.SnapshotCreateResponse 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.SnapshotCreateResponse in project coprhd-controller by CoprHD.
the class CinderApi method getTaskStatus.
/**
* Gets the current status { "creating", "attaching", "deleting", "Error", "Available" }
* of the component ( volume or snapshot ).
*
* @param volumeId
* @return
*/
public String getTaskStatus(String componentId, String componentType) throws Exception {
String status = "";
if (CinderConstants.ComponentType.volume.name().equals(componentType)) {
VolumeShowResponse volumeDetails = showVolume(componentId);
status = volumeDetails.volume.status;
} else if (CinderConstants.ComponentType.snapshot.name().equals(componentType)) {
SnapshotCreateResponse snapshotDetails = showSnapshot(componentId);
status = snapshotDetails.snapshot.status;
}
return status;
}
use of com.emc.storageos.cinder.model.SnapshotCreateResponse in project coprhd-controller by CoprHD.
the class CinderApi method showSnapshot.
/**
* Gets the Snapshot information.
* It is a synchronous operation.
*
* @param snapshotId
* @return
*/
public SnapshotCreateResponse showSnapshot(String snapshotId) throws Exception {
_log.info("CinderApi - start showSnapshot");
String showSnapshotUri = endPoint.getBaseUri() + String.format(CinderConstants.URI_DELETE_SNAPSHOT, new Object[] { endPoint.getCinderTenantId(), snapshotId });
ClientResponse js_response = getClient().get(URI.create(showSnapshotUri));
String jsonString = js_response.getEntity(String.class);
if (js_response.getStatus() == ClientResponse.Status.NOT_FOUND.getStatusCode()) {
throw CinderException.exceptions.snapshotNotFound(snapshotId);
}
SnapshotCreateResponse snapshotDetails = new Gson().fromJson(SecurityUtils.sanitizeJsonString(jsonString), SnapshotCreateResponse.class);
_log.info("CinderApi - end showSnapshot");
return snapshotDetails;
}
use of com.emc.storageos.cinder.model.SnapshotCreateResponse in project coprhd-controller by CoprHD.
the class CinderApi method createSnapshot.
/**
* Shows the specified volume attachment details.
*
* @param serverId the server id
* @param volumeAttachmentId the volume attachment id
* @return the volume attachment response
*/
/*
* public VolumeAttachResponse showVolumeAttachment(String serverId, String volumeAttachmentId)
* {
* _log.info("CinderApi - start showVolumeAttachment");
* String showVolumeAttachmentUri = endPoint.getBaseUri()
* + String.format(CinderConstants.URI_LIST_VOLUME_ATTACHMENT,
* new Object[] { endPoint.getCinderTenantId(), serverId, volumeAttachmentId });
* ClientResponse js_response = get_client().get(URI.create(showVolumeAttachmentUri));
* String jsonString = js_response.getEntity(String.class);
*
* VolumeAttachResponse volumeAttachmentDetails = new Gson().fromJson(jsonString, VolumeAttachResponse.class);
* _log.info("CinderApi - end showVolumeAttachment");
* return volumeAttachmentDetails;
* }
*/
/**
* Create Snapshot operation ( It is asynchronous operation )
*
* @param volumeName
* @param volumeTypeId
* @return
* @throws Exception
*/
public String createSnapshot(String volumeId, String snapshotName) throws Exception {
_log.info("CinderApi - start createSnapshot");
Gson gson = new Gson();
VolumeShowResponse volumeDetails = showVolume(volumeId);
String volumeName = volumeDetails.volume.name;
SnapshotCreateRequest request = new SnapshotCreateRequest();
request.snapshot.name = snapshotName;
request.snapshot.description = "Snapshot of volume " + volumeName;
request.snapshot.volume_id = volumeId;
request.snapshot.force = true;
String snapshotCreateUri = endPoint.getBaseUri() + String.format(CinderConstants.URI_CREATE_SNAPSHOT, endPoint.getCinderTenantId());
_log.debug("Creating snapshot with uri : {}", snapshotCreateUri);
String json = gson.toJson(request);
_log.debug("Creating snapshot with body : {}", json);
ClientResponse js_response = getClient().postWithHeader(URI.create(snapshotCreateUri), json);
String s = js_response.getEntity(String.class);
_log.debug("Got the response {}", s);
_log.debug("Response status {}", String.valueOf(js_response.getStatus()));
String snapshotId = "";
if (js_response.getStatus() == ClientResponse.Status.ACCEPTED.getStatusCode()) {
// This means snapshot creation request accepted
SnapshotCreateResponse response = gson.fromJson(SecurityUtils.sanitizeJsonString(s), SnapshotCreateResponse.class);
snapshotId = response.snapshot.id;
} else {
throw CinderException.exceptions.snapshotCreationFailed(s);
}
return snapshotId;
}
use of com.emc.storageos.cinder.model.SnapshotCreateResponse in project coprhd-controller by CoprHD.
the class CinderSnapshotCreateJob method updateStatus.
@Override
public void updateStatus(JobContext jobContext) throws Exception {
DbClient dbClient = jobContext.getDbClient();
try {
// Do nothing if the job is not completed yet
if (status == JobStatus.IN_PROGRESS) {
return;
}
String opId = getTaskCompleter().getOpId();
StringBuilder logMsgBuilder = new StringBuilder(String.format("Updating status of job %s to %s", opId, status.name()));
StorageSystem storageSystem = dbClient.queryObject(StorageSystem.class, getStorageSystemURI());
CinderApi cinderApi = jobContext.getCinderApiFactory().getApi(storageSystem.getActiveProviderURI(), getEndPointInfo());
URI snapshotId = getTaskCompleter().getId(0);
if (status == JobStatus.SUCCESS) {
SnapshotCreateResponse snapshotDetails = cinderApi.showSnapshot(getJobId());
BlockSnapshot snapshot = dbClient.queryObject(BlockSnapshot.class, snapshotId);
snapshot.setNativeId(snapshotDetails.snapshot.id);
snapshot.setNativeGuid(NativeGUIDGenerator.generateNativeGuid(storageSystem, snapshot));
snapshot.setInactive(false);
snapshot.setCreationTime(Calendar.getInstance());
dbClient.persistObject(snapshot);
if (logMsgBuilder.length() != 0) {
logMsgBuilder.append("\n");
}
logMsgBuilder.append(String.format("Created Snapshot successfully .. NativeId: %s, URI: %s", snapshot.getNativeId(), getTaskCompleter().getId()));
} else if (status == JobStatus.FAILED) {
logMsgBuilder.append("\n");
logMsgBuilder.append(String.format("Task %s failed to create volume: %s", opId, getTaskCompleter().getId().toString()));
Snapshot snapshot = dbClient.queryObject(Snapshot.class, snapshotId);
snapshot.setInactive(true);
dbClient.persistObject(snapshot);
}
_logger.info(logMsgBuilder.toString());
} catch (Exception e) {
_logger.error("Caught an exception while trying to updateStatus for CinderCreateSnapshotJob", e);
setErrorStatus("Encountered an internal error during snapshot create job status processing : " + e.getMessage());
} finally {
super.updateStatus(jobContext);
}
}
Aggregations