use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class VSphereAdapterResizeComputeService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
op.complete();
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
if (request.payload != null && !request.payload.isEmpty()) {
logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
final String newCpu = request.payload.get(COMPUTE_CPU_COUNT);
final String newMemory = request.payload.get(COMPUTE_MEMORY_IN_MBYTES);
final String rebootVMFlag = request.payload.get(REBOOT_VM_FLAG);
if ((newCpu == null || newCpu.isEmpty()) && (newMemory == null || newMemory.isEmpty())) {
// payload doesn't contain anything so patch task to FINISHED and return
mgr.patchTask(TaskState.TaskStage.FINISHED);
return;
}
VSphereVMContext vmContext = new VSphereVMContext(this, request, op);
vmContext.pool = VSphereIOThreadPoolAllocator.getPool(this);
VSphereVMContext.populateVMContextThen(this, vmContext, ctx -> {
int newCpuCount;
long newMemoryInMBytes;
boolean rebootVM;
try {
if (newCpu != null && !newCpu.isEmpty()) {
newCpuCount = Integer.parseInt(newCpu);
} else {
newCpuCount = toIntExact(ctx.child.description.cpuCount);
}
if (newMemory != null && !newMemory.isEmpty()) {
newMemoryInMBytes = Long.parseLong(newMemory);
} else {
newMemoryInMBytes = ctx.child.description.totalMemoryBytes / MEGA_BYTES_TO_BYTES_CONSTANT;
}
} catch (NumberFormatException numberEx) {
// NumberFormatException for all the parseXXX
logWarning(String.format("Request payload has values for CPU: %s or memory: %s in incorrect types " + numberEx.getMessage(), newCpu, newMemory));
ctx.failWithMessage(String.format("Request payload has values for CPU: %s or memory: %s in incorrect types ", newCpu, newMemory), numberEx);
return;
}
if (rebootVMFlag != null && !rebootVMFlag.isEmpty()) {
rebootVM = Boolean.valueOf(rebootVMFlag);
} else {
rebootVM = true;
}
if (request.isMockRequest) {
patchComputeAndCompleteRequest(ctx, newCpuCount, newMemoryInMBytes);
return;
}
handleResourceOperationRequest(ctx, newCpuCount, newMemoryInMBytes, rebootVM);
});
} else {
mgr.patchTaskToFailure(new IllegalArgumentException("Request payload is required for resize compute operation"));
}
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class VSphereComputeDiskManagementService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
try {
validateInputRequest(request);
} catch (Exception e) {
op.fail(e);
return;
}
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
TaskManager taskManager = new TaskManager(this, request.taskReference, request.resourceLink());
logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
VSphereVMDiskContext.populateVMDiskContextThen(this, createInitialContext(taskManager, request, op), ctx -> {
if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
handleAttachDisk(ctx);
} else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
handleDetachDisk(ctx);
} else {
IllegalArgumentException exception = new IllegalArgumentException(String.format("Unknown Operation %s for a disk", request.operation));
ctx.fail(exception);
}
});
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method handleStart.
@Override
public void handleStart(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
VSphereIncrementalEnumerationRequest enumerationRequest = op.getBody(VSphereIncrementalEnumerationRequest.class);
ComputeEnumerateResourceRequest request = enumerationRequest.request;
validate(request);
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
Operation.createGet(parentUri).setCompletion(o -> {
thenWithParentState(request, o.getBody(ComputeService.ComputeStateWithDescription.class), mgr);
}, mgr).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class VSphereAdapterImageEnumerationService 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();
ImageEnumerateRequest request = op.getBody(ImageEnumerateRequest.class);
validate(request);
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
if (request.isMockRequest) {
// just finish the mock request
mgr.patchTask(TaskStage.FINISHED);
return;
}
Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference)).setCompletion(o -> thenWithEndpointState(request, o.getBody(EndpointState.class), mgr), mgr).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.
the class VSphereAdapterResourceEnumerationService method handlePatch.
/**
* On every enumeration request, we check if there's an existing stateless service and delete it.
* This is needed to ensure no two enumeration processes are happening for same endpoint.
* After deleting the service, we create a new stateless service by issuing POST on the factory of
* stateless service and passing the last path segment of endpoint document selflink.
*
* @param op the enumeration operation request
*/
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ComputeEnumerateResourceRequest request = op.getBody(ComputeEnumerateResourceRequest.class);
validate(request);
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
if (request.isMockRequest) {
// just finish the mock request
mgr.patchTask(TaskState.TaskStage.FINISHED);
return;
}
String eventBasedEnumerationServiceURI = UriUtils.buildUriPath(VSphereIncrementalEnumerationService.FACTORY_LINK, UriUtils.getLastPathSegment(request.endpointLink));
URI uri = buildUri(this.getHost(), eventBasedEnumerationServiceURI);
VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest enumerationRequest = new VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest();
enumerationRequest.documentSelfLink = UriUtils.getLastPathSegment(eventBasedEnumerationServiceURI);
enumerationRequest.request = request;
// if yes, patch it. If no, create new one via post to factory.
if (EnumerationAction.START.equals(request.enumerationAction)) {
Operation.createGet(uri).setCompletion((o, e) -> {
if (null != e || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) {
// service not found, create a new one
createNewEnumerationService(enumerationRequest, mgr);
} else {
logInfo("Patching incremental service for incremental enumeration for %s", enumerationRequest.documentSelfLink);
Operation patchRequest = Operation.createPatch(uri).setBody(enumerationRequest).setCompletion((operation, err) -> {
if (err != null) {
logSevere("Unable to send enumeration request to enumeration" + " service for endpoint %s", enumerationRequest.documentSelfLink);
mgr.patchTaskToFailure(err);
}
});
patchRequest.sendWith(this);
}
}).sendWith(this);
} else {
// Stop any existing enumeration process when REFRESH is received.
logFine("Deleting the incremental enumeration service.");
Operation deleteRequest = Operation.createDelete(uri).setCompletion((o, e) -> {
if (null != e) {
logWarning("Delete of enumeration service failed for endpoint " + enumerationRequest.documentSelfLink + " Message: %s", e.getMessage());
}
// The delete service call will take care of stopping any running enumeration.
if (request.enumerationAction == EnumerationAction.STOP) {
logInfo("Successfully stopped enumeration for endpoint " + request.endpointLink);
mgr.patchTask(TaskState.TaskStage.FINISHED);
} else {
logInfo("Creating the enumeration service for endpoint %s", request.endpointLink);
createNewEnumerationService(enumerationRequest, mgr);
}
});
deleteRequest.sendWith(this);
}
}
Aggregations