private <T> Optional<T> post(Function<String, String> hostToUri, String type, Optional<?> body, Optional<Class<T>> clazz) { try { HttpResponse response = executeRequest(hostToUri, type, body, Method.POST, Optional.absent()); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } } catch (Exception e) { LOG.warn("Http post failed", e); } return Optional.absent(); }
/** * * @param requestId * @param runNowRequest * @param minimalReturn - if `true` will return a SingularityPendingRequestParent that is _not_ hydrated with extra task + deploy information * @return */ public SingularityPendingRequestParent runSingularityRequest(String requestId, Optional<SingularityRunNowRequest> runNowRequest, boolean minimalReturn) { final Function<String, String> requestUri = (host) -> String.format(REQUEST_RUN_FORMAT, getApiBase(host), requestId); final HttpResponse response = post(requestUri, String.format("run of request %s", requestId), runNowRequest, ImmutableMap.of("minimal", String.valueOf(minimalReturn))); return response.getAs(SingularityPendingRequestParent.class); }
private <T> Collection<T> getCollectionWithParams(Function<String, String> hostToUrl, String type, Optional<Map<String, Object>> queryParams, TypeReference<Collection<T>> typeReference) { final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUrl, Method.GET, Optional.absent(), queryParams.or(Collections.emptyMap())); if (response.getStatusCode() == 404) { return ImmutableList.of(); } checkResponse(type, response); LOG.info("Got {} in {}ms", type, System.currentTimeMillis() - start); return response.getAs(typeReference); }
public SingularityPendingRequestParent updateAuthorizedGroups(String requestId, SingularityUpdateGroupsRequest updateGroupsRequest) { final Function<String, String> requestUri = (host) -> String.format(REQUEST_GROUPS_UPDATE_FORMAT, getApiBase(host), requestId); final HttpResponse response = post(requestUri, String.format("update authorized groups of request %s", requestId), Optional.of(updateGroupsRequest)); return response.getAs(SingularityPendingRequestParent.class); }
private <T> T getFromMesos(String uri, Class<T> clazz, boolean useShortTimeout) { HttpResponse response = getFromMesos(uri, useShortTimeout); try { return response.getAs(clazz); } catch (Exception e) { throw new MesosClientException(String.format("Couldn't deserialize %s from %s", clazz.getSimpleName(), uri), e); } }
@Override public List<MesosTaskMonitorObject> getSlaveResourceUsage(String hostname, boolean useShortTimeout) { final String uri = String.format(MESOS_SLAVE_STATISTICS_URL, hostname); HttpResponse response = getFromMesos(uri, useShortTimeout); try { return response.getAs(TASK_MONITOR_TYPE_REFERENCE); } catch (Exception e) { throw new MesosClientException(String.format("Unable to deserialize task monitor object from %s", uri), e); } }
private <T> Optional<T> deleteWithParams(Function<String, String> hostToUrl, String type, String id, Optional<?> body, Optional<Map<String, Object>> queryParams, Optional<Class<T>> clazz) { LOG.info("Deleting {} {} from Singularity", type, id); final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUrl, Method.DELETE, body, queryParams.or(Collections.emptyMap())); if (response.getStatusCode() == 404) { LOG.info("{} ({}) was not found", type, id); return Optional.absent(); } checkResponse(type, response); LOG.info("Deleted {} ({}) from Singularity in %sms", type, id, System.currentTimeMillis() - start); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } return Optional.absent(); }
private <T> Optional<T> getSingleWithParams(Function<String, String> hostToUrl, String type, String id, Optional<Map<String, Object>> queryParams, TypeReference<T> typeReference) { final long start = System.currentTimeMillis(); HttpResponse response = executeGetSingleWithParams(hostToUrl, type, id, queryParams); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.info("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(typeReference)); }
public SingularityState getState(Optional<Boolean> skipCache, Optional<Boolean> includeRequestIds) { final Function<String, String> uri = (host) -> String.format(STATE_FORMAT, getApiBase(host)); LOG.info("Fetching state from {}", uri); final long start = System.currentTimeMillis(); Map<String, Boolean> queryParams = new HashMap<>(); if (skipCache.isPresent()) { queryParams.put("skipCache", skipCache.get()); } if (includeRequestIds.isPresent()) { queryParams.put("includeRequestIds", includeRequestIds.get()); } HttpResponse response = executeRequest(uri, Method.GET, Optional.absent(), queryParams); checkResponse("state", response); LOG.info("Got state in {}ms", System.currentTimeMillis() - start); return response.getAs(SingularityState.class); }
public Optional<SingularityTaskReconciliationStatistics> getTaskReconciliationStatistics() { final Function<String, String> uri = (host) -> String.format(TASK_RECONCILIATION_FORMAT, getApiBase(host)); LOG.info("Fetch task reconciliation statistics from {}", uri); final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(uri, Method.GET, Optional.absent(), Collections.emptyMap()); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse("task reconciliation statistics", response); LOG.info("Got task reconciliation statistics in {}ms", System.currentTimeMillis() - start); return Optional.of(response.getAs(SingularityTaskReconciliationStatistics.class)); }
private <T> Optional<T> getSingleWithParams(Function<String, String> hostToUrl, String type, String id, Optional<Map<String, Object>> queryParams, Class<T> clazz) { final long start = System.currentTimeMillis(); HttpResponse response = executeGetSingleWithParams(hostToUrl, type, id, queryParams); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.info("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(clazz)); }
public SingularityRequestParent createDeployForSingularityRequest(String requestId, SingularityDeploy pendingDeploy, Optional<Boolean> deployUnpause, Optional<String> message, Optional<SingularityRequest> updatedRequest) { final Function<String, String> requestUri = (String host) -> String.format(DEPLOYS_FORMAT, getApiBase(host)); HttpResponse response = post(requestUri, String.format("new deploy %s", new SingularityDeployKey(requestId, pendingDeploy.getId())), Optional.of(new SingularityDeployRequest(pendingDeploy, deployUnpause, message, updatedRequest))); return getAndLogRequestAndDeployStatus(response.getAs(SingularityRequestParent.class)); }
public SingularityRequestParent updateIncrementalDeployInstanceCount(SingularityUpdatePendingDeployRequest updateRequest) { final Function<String, String> requestUri = (host) -> String.format(UPDATE_DEPLOY_FORMAT, getApiBase(host)); HttpResponse response = post(requestUri, String.format("update deploy %s", new SingularityDeployKey(updateRequest.getRequestId(), updateRequest.getDeployId())), Optional.of(updateRequest)); return getAndLogRequestAndDeployStatus(response.getAs(SingularityRequestParent.class)); }
private <T> Optional<T> put(String uri, String type, Optional<Class<T>> clazz, Map<String, String> queryParams) { try { HttpResponse response = put(uri, type, queryParams); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } } catch (Exception e) { LOG.warn("Http post failed", e); } return Optional.absent(); }
private <T> Optional<T> post(String uri, String type, Optional<?> body, Optional<Class<T>> clazz, Map<String, String> queryParams) { try { HttpResponse response = post(uri, type, body, queryParams); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } } catch (Exception e) { LOG.warn("Http post failed", e); } return Optional.absent(); }
private <T> Optional<T> getSingle(String uri, String type, String id, Class<T> clazz, Map<String, String> queryParams) { checkNotNull(id, String.format("Provide a %s id", type)); LOG.debug("Getting {} {} from {}", type, id, uri); final long start = System.currentTimeMillis(); HttpResponse response = httpClient.execute(buildRequest(uri, queryParams).build()); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.debug("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(clazz)); }
static String formatResponse(HttpResponse response) { StringBuilder builder = new StringBuilder(); builder.append("------------------------------------------\n"); response.getHeaders().forEach(header -> { builder.append(header.getName()).append(" = ").append(header.getValue()).append("\n"); }); builder.append("------------------------------------------\n"); try { builder.append(ObjectMapperUtils.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(response.getAs(Object.class))); } catch (IOException ex) { throw new RuntimeException("Unable to process response", ex); } return builder.toString(); } }
private <T> Optional<T> delete(String uri, String type, String id, Map<String, String> queryParams, Optional<Class<T>> clazz) { LOG.debug("Deleting {} {} from {}", type, id, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, queryParams).setMethod(Method.DELETE); HttpResponse response = httpClient.execute(request.build()); if (response.getStatusCode() == 404) { LOG.debug("{} ({}) was not found", type, id); return Optional.absent(); } checkResponse(type, response); LOG.debug("Deleted {} ({}) from Baragon in %sms", type, id, System.currentTimeMillis() - start); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } return Optional.absent(); }
private <T> Collection<T> getCollection(String uri, String type, TypeReference<Collection<T>> typeReference) { LOG.debug("Getting all {} from {}", type, uri); final long start = System.currentTimeMillis(); HttpResponse response = httpClient.execute(buildRequest(uri).build()); if (response.getStatusCode() == 404) { throw new BaragonClientException(String.format("%s not found", type), 404); } checkResponse(type, response); LOG.debug("Got {} in {}ms", type, System.currentTimeMillis() - start); return response.getAs(typeReference); }