use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class AzureComputeStatsGatherer method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
op.complete();
ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
TaskManager taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
AzureStatsDataHolder statsData = new AzureStatsDataHolder();
statsData.statsRequest = statsRequest;
statsData.taskManager = taskManager;
// TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-1336
getVMDescription(statsData);
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class AzureCostStatsService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required."));
return;
}
op.complete();
ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
TaskManager taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
if (statsRequest.isMockRequest) {
// patch status to parent task
taskManager.finishTask();
return;
}
Context context = new Context(statsRequest);
context.taskManager = taskManager;
handleRequest(context);
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class AWSMockStatsService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
op.complete();
ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
AWSMockStatsDataHolder statsData = new AWSMockStatsDataHolder();
statsData.statsRequest = statsRequest;
statsData.taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
if (statsRequest.isMockRequest) {
// patch status to parent task
statsData.taskManager.finishTask();
return;
}
getVMDescription(statsData);
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class GCPStatsService method handlePatch.
/**
* The REST PATCH request handler. This is the entry of starting stats collection.
* @param patch Operation which should contain request body.
*/
@Override
public void handlePatch(Operation patch) {
setOperationHandlerInvokeTimeStat(patch);
if (!patch.hasBody()) {
patch.fail(new IllegalArgumentException("body is required"));
return;
}
patch.complete();
ComputeStatsRequest statsRequest = patch.getBody(ComputeStatsRequest.class);
GCPStatsDataHolder statsData = new GCPStatsDataHolder(patch);
statsData.statsRequest = statsRequest;
statsData.taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
// If mock mode is enabled, patch back to the parent.
if (statsData.statsRequest.isMockRequest) {
statsData.stage = StatsCollectionStage.FINISHED;
handleStatsRequest(statsData);
} else {
statsData.stage = StatsCollectionStage.VM_DESC;
handleStatsRequest(statsData);
}
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class VSphereAdapterSnapshotService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
if (request.isMockRequest) {
mgr.patchTask(TaskStage.FINISHED);
return;
}
if (MapUtils.isEmpty(request.payload)) {
mgr.patchTaskToFailure(new IllegalArgumentException("Payload is missing for snapshot operation"));
return;
}
SnapshotRequestType requestType = SnapshotRequestType.fromString(request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_REQUEST_TYPE));
if (requestType == null) {
mgr.patchTaskToFailure(new IllegalArgumentException("No Request Type is specified in the payload for snapshot operation"));
return;
}
String computeLink = request.resourceLink();
String snapshotLink = request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_DOCUMENT_LINK);
if ((SnapshotRequestType.DELETE.equals(requestType) || SnapshotRequestType.REVERT.equals(requestType)) && (StringUtils.isBlank(snapshotLink))) {
mgr.patchTaskToFailure(new IllegalArgumentException("No Snapshot Link is specified in the payload for snapshot operation"));
return;
}
switch(requestType) {
case CREATE:
SnapshotContext createSnapshotContext = new SnapshotContext(populateAndGetSnapshotState(request), mgr, requestType, op);
createSnapshotContext.snapshotMemory = Boolean.valueOf(request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_MEMORY));
DeferredResult.completed(createSnapshotContext).thenCompose(this::thenSetComputeDescription).thenCompose(this::thenSetParentComputeDescription).thenCompose(this::querySnapshotStates).thenCompose(this::performSnapshotOperation).whenComplete((context, err) -> {
if (err != null) {
mgr.patchTaskToFailure(err);
return;
}
mgr.finishTask();
});
break;
case DELETE:
Operation.createGet(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshotLink)).setCompletion((o, e) -> {
if (e != null) {
mgr.patchTaskToFailure(e);
return;
}
SnapshotContext deleteSnapshotContext = new SnapshotContext(o.getBody(SnapshotState.class), mgr, requestType, op);
if (!computeLink.equals(deleteSnapshotContext.snapshotState.computeLink)) {
mgr.patchTaskToFailure(new IllegalArgumentException("Snapshot does not belong to the specified compute."));
return;
}
DeferredResult.completed(deleteSnapshotContext).thenCompose(this::thenSetComputeDescription).thenCompose(this::thenSetParentComputeDescription).thenCompose(this::performSnapshotOperation).whenComplete((context, err) -> {
if (err != null) {
mgr.patchTaskToFailure(err);
return;
}
mgr.finishTask();
});
}).sendWith(this);
break;
case REVERT:
Operation.createGet(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshotLink)).setCompletion((o, e) -> {
if (e != null) {
mgr.patchTaskToFailure(e);
return;
}
SnapshotContext revertSnapshotContext = new SnapshotContext(o.getBody(SnapshotState.class), mgr, requestType, op);
if (!computeLink.equals(revertSnapshotContext.snapshotState.computeLink)) {
mgr.patchTaskToFailure(new IllegalArgumentException("Snapshot does not belong to the specified compute."));
return;
}
DeferredResult.completed(revertSnapshotContext).thenCompose(this::thenSetComputeDescription).thenCompose(this::thenSetParentComputeDescription).thenCompose(this::querySnapshotStates).thenCompose(this::performSnapshotOperation).whenComplete((context, err) -> {
if (err != null) {
mgr.patchTaskToFailure(err);
return;
}
mgr.finishTask();
});
}).sendWith(this);
break;
default:
mgr.patchTaskToFailure(new IllegalStateException(String.format("Unknown Operation %s for a Snapshot", requestType)));
break;
}
}
Aggregations