use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ProvisionDiskTaskService method validateDiskAndStart.
private void validateDiskAndStart(ProvisionDiskTaskState state, Operation startPost) {
URI diskUri = UriUtils.buildUri(getHost(), state.diskLink);
sendRequest(Operation.createGet(createInventoryUri(this.getHost(), diskUri)).setCompletion((o, e) -> {
if (e != null) {
logWarning(() -> String.format("Failure retrieving disk state (%s): %s", diskUri, e.toString()));
o.complete();
failTask(e);
return;
}
DiskState disk = o.getBody(DiskState.class);
state.diskAdapterReference = disk.diskAdapterReference;
startPost.complete();
if (disk.capacityMBytes < 0) {
failTask(new IllegalArgumentException("disk capacity is mandatory for a disk"));
return;
}
if (state.taskSubStage == ProvisionDiskTaskState.SubStage.CREATING_DISK && state.diskAdapterReference == null) {
failTask(new IllegalArgumentException("diskState does not have create service specified"));
return;
}
sendSelfPatch(TaskStage.STARTED, state.taskSubStage, null);
}));
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ProvisionDiskTaskService method sendSelfPatch.
private void sendSelfPatch(TaskStage newStage, ProvisionDiskTaskState.SubStage newSubStage, Throwable ex) {
ProvisionDiskTaskState patchBody = new ProvisionDiskTaskState();
patchBody.taskInfo = new TaskState();
patchBody.taskInfo.stage = newStage;
patchBody.taskSubStage = newSubStage;
if (ex != null) {
patchBody.taskInfo.failure = Utils.toServiceErrorResponse(ex);
}
Operation patch = Operation.createPatch(createInventoryUri(this.getHost(), getUri())).setBody(patchBody).setCompletion((o, e) -> {
if (e != null) {
logWarning(() -> String.format("Self patch failed: %s", com.vmware.xenon.common.Utils.toString(e)));
}
});
sendRequest(patch);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ProvisionDiskTaskService method createSubTask.
public void createSubTask(CompletionHandler c, ProvisionDiskTaskState.SubStage nextStage, ProvisionDiskTaskState currentState) {
ServiceTaskCallback<SubStage> callback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), getSelfLink()));
callback.onSuccessTo(nextStage);
SubTaskService.SubTaskState<SubStage> subTaskInitState = new SubTaskService.SubTaskState<>();
subTaskInitState.errorThreshold = 0;
subTaskInitState.serviceTaskCallback = callback;
subTaskInitState.tenantLinks = currentState.tenantLinks;
subTaskInitState.documentExpirationTimeMicros = currentState.documentExpirationTimeMicros;
Operation startPost = Operation.createPost(createInventoryUri(this.getHost(), SubTaskService.FACTORY_LINK)).setBody(subTaskInitState).setCompletion(c);
sendRequest(startPost);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ProvisionSecurityGroupTaskService method createSubTask.
private void createSubTask(ProvisionSecurityGroupTaskState taskState, Consumer<String> subTaskLinkConsumer) {
ServiceTaskCallback<SubStage> callback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), getSelfLink()));
callback.onSuccessFinishTask();
SubTaskService.SubTaskState<SubStage> subTaskInitState = new SubTaskService.SubTaskState<>();
// tell the sub task with what to patch us, on completion
subTaskInitState.serviceTaskCallback = callback;
subTaskInitState.completionsRemaining = taskState.securityGroupDescriptionLinks.size();
subTaskInitState.tenantLinks = taskState.tenantLinks;
subTaskInitState.documentExpirationTimeMicros = taskState.documentExpirationTimeMicros;
Operation startPost = Operation.createPost(this, SubTaskService.FACTORY_LINK).setBody(subTaskInitState).setCompletion((o, e) -> {
if (e != null) {
logWarning(() -> String.format("Failure creating sub task: %s", Utils.toString(e)));
sendSelfPatch(TaskState.TaskStage.FAILED, e);
return;
}
SubTaskService.SubTaskState<?> body = o.getBody(SubTaskService.SubTaskState.class);
subTaskLinkConsumer.accept(body.documentSelfLink);
});
sendRequest(startPost);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ResourceAllocationTaskService method createDiskResources.
/**
* Create disks to attach to the compute resource. Use the disk description links to figure out
* what type of disks to create.
*
* @param currentState
* @param parentLink
* @param computeResourceId
*/
private void createDiskResources(ResourceAllocationTaskState currentState, ComputeDescription cd, String parentLink, String computeResourceId, String name, List<String> networkLinks) {
if (cd.diskDescLinks == null || cd.diskDescLinks.isEmpty()) {
createComputeResource(currentState, cd, parentLink, computeResourceId, name, new ArrayList<>(), networkLinks);
return;
}
DeferredResult<List<String>> result = DeferredResult.allOf(cd.diskDescLinks.stream().map(link -> {
Operation op = Operation.createGet(this, link);
return this.sendWithDeferredResult(op, DiskState.class);
}).map(dr -> dr.thenCompose(d -> {
String link = d.documentSelfLink;
// create a new disk based off the template but use a
// unique ID
d.id = UUID.randomUUID().toString();
d.documentSelfLink = null;
d.tenantLinks = currentState.tenantLinks;
if (d.customProperties == null) {
d.customProperties = new HashMap<>();
}
d.descriptionLink = link;
return this.sendWithDeferredResult(Operation.createPost(this, DiskService.FACTORY_LINK).setBody(d), DiskState.class);
})).map(dsr -> dsr.thenApply(ds -> ds.documentSelfLink)).collect(Collectors.toList()));
result.whenComplete((diskLinks, e) -> {
if (e != null) {
logWarning(() -> String.format("Failure creating disk: %s", e.toString()));
this.sendFailureSelfPatch(e);
return;
}
// we have created all the disks. Now create the compute host
// resource
createComputeResource(currentState, cd, parentLink, computeResourceId, name, diskLinks, networkLinks);
});
}
Aggregations