@Override public void handleStart(Operation post) { if (!post.hasBody()) { post.fail(new IllegalArgumentException("body is required")); return; } TenantState newState = post.getBody(TenantState.class); if (newState.id == null || newState.id.isEmpty()) { newState.id = UUID.randomUUID().toString(); } post.setBody(newState).complete(); }
private CheckpointState validateState(Operation op) { if (!op.hasBody()) { op.fail(new IllegalArgumentException("initial state is required")); return null; } CheckpointState body = op.getBody(CheckpointState.class); if (body.timestamp == null) { op.fail(new IllegalArgumentException("timestamp is required")); return null; } return body; }
private void getClusterDto(String clusterLink, Consumer<ClusterService.ClusterDto> consumer) { Operation.createGet(this, clusterLink) .setCompletion((o, e) -> { if (e != null) { failTask("Failed to get cluster " + clusterLink, e); return; } consumer.accept(o.getBody(ClusterService.ClusterDto.class)); }) .sendWith(this); }
private void patchKubernetesEntity(RequestContext context, Operation inspectResponse, CompletionHandler handler) { String jsonResponse = inspectResponse.getBody(String.class); BaseKubernetesState newState = null; try { newState = context.kubernetesState.getClass().newInstance(); newState.setKubernetesEntityFromJson(jsonResponse); } catch (Throwable ex) { fail(context.request, ex); } sendRequest(Operation.createPatch(this, context.kubernetesState.documentSelfLink) .setBody(newState) .setCompletion(handler)); } }
@Override public void handlePatch(Operation patch) { LocalPrincipalState currentState = getState(patch); LocalPrincipalState patchState = patch.getBody(LocalPrincipalState.class); try { validatePrincipalPatch(patchState, currentState); PropertyUtils.mergeServiceDocuments(currentState, patchState); patch.complete(); } catch (Exception ex) { patch.fail(ex); } }
private void getConfigProperty(String propName, Consumer<ConfigurationState> callback) { sendRequest(Operation .createGet(getHost(), UriUtils.buildUriPath(CONFIG_PROPS, propName)) .setCompletion((res, ex) -> { if (ex == null && res.hasBody()) { ConfigurationState body = res.getBody(ConfigurationState.class); callback.accept(body); } })); }
private void testOperation(final Function<URI, Operation> createOp, String inBody) throws Throwable { URI pingUri = UriUtils.buildUri(host, MockPingService.SELF_LINK); URI rpPingPath = UriUtilsExtended.getReverseProxyUri(pingUri); URI rpPingUri = UriUtils.buildUri(host, rpPingPath.toString()); Operation op = createOp.apply(rpPingUri); if (inBody != null) { op.setBody(inBody); } op.setCompletion((o, e) -> { if (e != null) { Operation.failActionNotSupported(o); return; } String outBody = o.getBody(String.class); assertEquals(MockPingService.BODY_PONG, outBody); }); verifyOperation(op); }
public <T extends ServiceDocument> T verifyPost(Class<T> documentType, String factoryLink, T state, int expectedStatusCode) { URI uri = UriUtils.buildUri(this, factoryLink); Operation op = Operation.createPost(uri).setBody(state); Operation response = this.sender.sendAndWait(op); String message = String.format("Status code expected: %s, actual: %s", expectedStatusCode, response.getStatusCode()); assertEquals(message, expectedStatusCode, response.getStatusCode()); return response.hasBody() ? response.getBody(documentType) : null; }
private void validateState(Operation start) { if (!start.hasBody()) { throw new IllegalArgumentException( "attempt to initialize service with an empty state"); } BankAccountServiceState state = start.getBody(BankAccountServiceState.class); if (state.balance < 0) { throw new IllegalArgumentException("balance cannot be negative"); } }
@Override public void handleStart(Operation start) { if (!start.hasBody()) { start.fail(new IllegalStateException("no body given")); return; } LocalFileServiceState state = start.getBody(LocalFileServiceState.class); if (state.localFileUri == null) { start.fail(new IllegalStateException("file scheme uri is required")); return; } start.complete(); }
private void getClosureDescription(String closureLink, Consumer<Closure> callbackFunction) { sendRequest(Operation.createGet(this, closureLink) .setCompletion( (o, e) -> { if (e != null) { failTask("Failure retrieving closure description.", e); return; } Closure desc = o.getBody(Closure.class); callbackFunction.accept(desc); })); }
private void createProject(ProjectContentBody projectContent, Runnable callback) { ProjectState state = new ProjectState(); state.name = projectContent.name; state.description = projectContent.description; state.isPublic = projectContent.isPublic; sendRequest(Operation.createPost(this, ProjectFactoryService.SELF_LINK) .setBody(state) .setReferer(getHost().getUri()) .setCompletion((o, ex) -> { if (ex != null) { logWarning("Failed to create project: %s", Utils.toString(ex)); callback.run(); } else { ProjectState created = o.getBody(ProjectState.class); updateProjectWithMembers(created, projectContent, callback); } })); }
@Override public void handleStart(Operation op) { if (op.hasBody()) { LoaderServiceState s = op.getBody(LoaderServiceState.class); if (s.loaderType == null) { s.loaderType = LoaderType.FILESYSTEM; } logFine("Initial path is %s", s.path); } super.setMaintenanceIntervalMicros(MAINTENANCE_INTERVAL_MICROS); op.complete(); }
private void getConfigProperty(String propName, BiConsumer<ConfigurationState, Throwable> callback) { sendRequest(Operation .createGet(getHost(), UriUtils.buildUriPath(CONFIG_PROPS, propName)) .setCompletion((res, ex) -> { ConfigurationState body = null; if (ex == null && res.hasBody()) { body = res.getBody(ConfigurationState.class); } callback.accept(body, ex); })); }
public <T extends ServiceDocument> T verifyPost(Class<T> documentType, String factoryLink, T state, int expectedStatusCode) { URI uri = UriUtils.buildUri(this, factoryLink); Operation op = Operation.createPost(uri).setBody(state); Operation response = this.sender.sendAndWait(op); String message = String.format("Status code expected: %s, actual: %s", expectedStatusCode, response.getStatusCode()); assertEquals(message, expectedStatusCode, response.getStatusCode()); return response.hasBody() ? response.getBody(documentType) : null; }
@Override public void handlePatch(Operation op) { if (!op.hasBody()) { op.fail(new IllegalArgumentException("body is required")); return; } RequestContext context = new RequestContext(); context.operation = op; context.request = op.getBody(ImageRequest.class); context.request.validate();// validate the request logInfo("Processing operation request for resource %s", context.request.resourceReference); processRequest(context); }