use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AzureLifecycleOperationServiceTest method triggerSuspend.
private void triggerSuspend() {
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest request = new ResourceOperationRequest();
request.isMockRequest = this.isMock;
request.operation = ResourceOperation.SUSPEND.operation;
request.resourceReference = UriUtils.buildUri(this.host, this.vmState.documentSelfLink);
request.taskReference = UriUtils.buildUri(this.host, taskLink);
TestContext ctx = this.host.testCreate(2);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else if (TaskState.isFinished(response.taskInfo)) {
ctx.completeIteration();
}
return true;
});
Operation restartOp = Operation.createPatch(UriUtils.buildUri(this.host, AzureLifecycleOperationService.SELF_LINK)).setBody(request).setReferer("/tests2").setCompletion((o, e) -> {
if (e != null) {
ctx.failIteration(e);
return;
}
ctx.completeIteration();
});
this.host.send(restartOp);
ctx.await();
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class VSphereAdapterD2PowerOpsService 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();
logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
VSphereVMContext vmContext = new VSphereVMContext(this, request, op);
vmContext.pool = VSphereIOThreadPoolAllocator.getPool(this);
VSphereVMContext.populateVMContextThen(this, vmContext, ctx -> {
if (request.isMockRequest) {
patchComputeAndCompleteRequest(ctx, request.operation);
return;
}
handleResourceOperationRequest(ctx, request.operation);
});
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest 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.registry.operations.ResourceOperationRequest 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.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AzureComputeDiskDay2Service 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 ex) {
op.fail(ex);
return;
}
op.complete();
// initialize context object
AzureComputeDiskDay2Context context = new AzureComputeDiskDay2Context(this, request);
DeferredResult<AzureComputeDiskDay2Context> execution = DeferredResult.completed(context);
// common steps for both attach / detach ops
execution = execution.thenCompose(this::getComputeState).thenCompose(this::getDiskState).thenCompose(this::configureAzureSDKClient).thenCompose(this::getAzureVirtualMachine);
if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
execution = execution.thenCompose(this::getAzureDisk).thenCompose(this::performDiskAttachment).thenCompose(this::updateComputeStateAndDiskState);
} else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
execution = execution.thenCompose(this::performDiskDetach).thenCompose(this::updateComputeStateAndDiskState);
}
execution.whenComplete((o, ex) -> context.finish(ex));
}
Aggregations