use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSComputeDiskDay2Service 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 {
validateRequest(request);
op.complete();
} catch (Exception e) {
op.fail(e);
return;
}
// initialize context with baseAdapterContext and request information
DeferredResult<DiskContext> drDiskContext = new DefaultAdapterContext(this, request).populateBaseContext(BaseAdapterStage.VMDESC).thenApply(c -> {
DiskContext context = new DiskContext();
context.baseAdapterContext = c;
context.request = request;
return context;
});
Function<DiskContext, DeferredResult<DiskContext>> fn;
// handle resource operation
if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
fn = this::performAttachOperation;
} else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
fn = this::performDetachOperation;
} else {
drDiskContext.thenApply(c -> {
Throwable err = new IllegalArgumentException(String.format("Unknown Operation %s for a disk", request.operation));
c.baseAdapterContext.taskManager.patchTaskToFailure(err);
return c;
});
return;
}
drDiskContext.thenCompose(this::setComputeState).thenCompose(this::setDiskState).thenCompose(this::setClient).thenCompose(fn).whenComplete((ctx, e) -> {
if (ctx.error == null) {
ctx.baseAdapterContext.taskManager.finishTask();
} else {
ctx.baseAdapterContext.taskManager.patchTaskToFailure(ctx.error);
}
});
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest 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;
}
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class BaseVSphereAdapterTest method getResourceOperationRequest.
private ResourceOperationRequest getResourceOperationRequest(String operation, String documentSelfLink, String taskLink) {
ResourceOperationRequest resourceOperationRequest = new ResourceOperationRequest();
resourceOperationRequest.operation = operation;
resourceOperationRequest.isMockRequest = isMock();
resourceOperationRequest.resourceReference = UriUtils.buildUri(this.host, documentSelfLink);
resourceOperationRequest.taskReference = UriUtils.buildUri(this.host, taskLink);
resourceOperationRequest.payload = new HashMap<>();
return resourceOperationRequest;
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class BaseVSphereAdapterTest method deleteSnapshotAndWait.
protected void deleteSnapshotAndWait(ComputeState computeState) throws Throwable {
// Get the snapshot associated with the compute
SnapshotState snapshotState = querySnapshotState(computeState.documentSelfLink, true);
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest snapshotRequest = getDeleteOrRevertSnapshotRequest(ResourceOperation.DELETE_SNAPSHOT.operation, "DELETE", computeState.documentSelfLink, snapshotState.documentSelfLink, taskLink);
Operation deleteSnapshotOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterSnapshotService.SELF_LINK)).setBody(snapshotRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> Assert.assertNull(e));
TestRequestSender sender = new TestRequestSender(this.host);
sender.sendRequest(deleteSnapshotOp);
this.host.log("Waiting for the snapshot to be deleted");
this.host.waitFor("Delete snapshot request failed", () -> {
SnapshotState finalSnapshotState = querySnapshotState(computeState.documentSelfLink, true);
ComputeState finalComputeState = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
String hasSnapshot = finalComputeState.customProperties.get(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS);
// snapshot. So hasSnapshot will still be true.
if (hasSnapshot != null && Boolean.parseBoolean(hasSnapshot) && finalSnapshotState == null) {
return true;
} else {
return false;
}
});
this.host.log("Delete snapshot operation completed successfully");
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class BaseVSphereAdapterTest method rebootVSphereVMAndWait.
protected void rebootVSphereVMAndWait(ComputeState computeState) {
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest rebootVMRequest = getResourceOperationRequest("Reboot", computeState.documentSelfLink, taskLink);
Operation rebootOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(rebootVMRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
Assert.assertNull(e);
});
TestRequestSender sender = new TestRequestSender(this.host);
sender.sendRequest(rebootOp);
this.host.log("Waiting for the machine to be rebooted");
ComputeState[] cState = new ComputeState[1];
this.host.waitFor("Reboot request failed", () -> {
cState[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
if (cState[0].powerState.equals(ComputeService.PowerState.ON)) {
return true;
} else {
return false;
}
});
assertEquals(ComputeService.PowerState.ON, cState[0].powerState);
assertNotNull(cState[0].address);
this.host.log("Reboot operation completed successfully");
}
Aggregations