private void patchAdapter(NicSecurityGroupsTaskState taskState) { // use the first security group sendRequest(Operation.createGet( UriUtils.buildUri(this.getHost(), taskState.securityGroupLinks.get(0))) .setCompletion((o, e) -> { if (e != null) { sendSelfPatch(TaskState.TaskStage.FAILED, e); return; } SecurityGroupState securityGroupState = o.getBody(SecurityGroupState.class); NicSecurityGroupsRequest req = toReq(taskState, securityGroupState); sendRequest(Operation .createPatch( taskState.adapterReference) .setBody(req) .setCompletion( (oo, ee) -> { if (ee != null) { sendSelfPatch(TaskState.TaskStage.FAILED, ee); return; } })); })); }
private NicSecurityGroupsRequest toReq( NicSecurityGroupsTaskState taskState, SecurityGroupState securityGroupState) { NicSecurityGroupsRequest req = new NicSecurityGroupsRequest(); req.requestType = taskState.requestType; req.resourceReference = createInventoryUri(this.getHost(), taskState.networkInterfaceLink); req.securityGroupLinks = taskState.securityGroupLinks; req.authCredentialsLink = securityGroupState.authCredentialsLink; req.taskReference = this.getUri(); req.isMockRequest = taskState.isMockRequest; req.customProperties = taskState.customProperties; return req; }
private void sendSelfPatch(TaskState.TaskStage stage, Throwable e) { NicSecurityGroupsTaskState body = new NicSecurityGroupsTaskState(); body.taskInfo = new TaskState(); if (e == null) { body.taskInfo.stage = stage; } else { body.taskInfo.stage = TaskState.TaskStage.FAILED; body.taskInfo.failure = Utils.toServiceErrorResponse(e); logWarning(() -> String.format("Patching to failed: %s", Utils.toString(e))); } sendSelfPatch(body); } }
NicSecurityGroupsTaskState currState = getState(patch); NicSecurityGroupsTaskState patchState = patch .getBody(NicSecurityGroupsTaskState.class); currState.taskSubStage = nextStage(currState); handleSubStages(currState); logInfo(() -> String.format("%s security groups for %s started", currState.requestType.toString(), currState.networkInterfaceLink)); break; break; case FINISHED: SubStage nextStage = nextStage(currState); if (nextStage == SubStage.FINISHED) { currState.taskInfo.stage = TaskState.TaskStage.FINISHED; logInfo(() -> "Task is complete"); ServiceTaskCallback.sendResponse(currState.serviceTaskCallback, this, currState); } else { sendSelfPatch(TaskState.TaskStage.CREATED, null); logWarning(() -> String.format("Task failed with %s", Utils.toJsonHtml(currState.taskInfo.failure))); ServiceTaskCallback.sendResponse(currState.serviceTaskCallback, this, currState);
@Override public void handleStart(Operation start) { if (!start.hasBody()) { start.fail(new IllegalArgumentException("body is required")); return; } NicSecurityGroupsTaskState state = start .getBody(NicSecurityGroupsTaskState.class); try { Utils.validateState(getStateDescription(), state); } catch (Exception e) { start.fail(e); return; } validateSecurityGroupsEndpoint(state) .whenComplete((ignore, e) -> { if (e != null) { start.fail(e); return; } state.taskInfo = new TaskState(); state.taskInfo.stage = TaskState.TaskStage.CREATED; state.taskSubStage = SubStage.CREATED; start.complete(); // start the task sendSelfPatch(TaskState.TaskStage.CREATED, null); }); }
@Test public void testServiceOptions() { EnumSet<Service.ServiceOption> expected = EnumSet.of( Service.ServiceOption.REPLICATION, Service.ServiceOption.OWNER_SELECTION); assertThat(this.nicSecurityGroupsTaskService.getOptions(), is(expected)); assertThat(this.nicSecurityGroupsTaskService.getProcessingStage(), is(Service.ProcessingStage.CREATED)); } }
@Before public void setUpTest() { this.nicSecurityGroupsTaskService = new NicSecurityGroupsTaskService(); }