public static void die(Object... args) { throw new ConsulException(sputs(args)); } }
/** * Pings the Consul Agent. */ public void pingAgent() { HTTP.Response response = HTTP.getResponse(createURI("/self").toString()); if (response.status() != 200) { die("Error pinging Consul", response.payloadAsString()); } }
/** * Register a service with Consul. * * @param registration The registration payload. */ public void register(final Registration registration) { final URI uri = createURI("/service/register"); HTTP.Response response = HTTP.jsonRestCallViaPUT(uri.toString(), toJson(registration)); if (response.status() != 200) { die("Error registering service with Consul", uri, registration, response.payloadAsString()); } }
public static void dieWithException(Exception ex, Object... args) { throw new ConsulException(sputs(args), ex); }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HTTP.Response response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
/** * Creates a new client given a host and a port. * * @param host The Consul API hostname or IP. * @param port The Consul port. * @return A new client. */ public static Consul consul(final String host, final int port) { try { return new Consul(new URL("http", host, port, "").toString()); } catch (MalformedURLException e) { throw new ConsulException("Bad Consul URL", e); } }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HTTP.Response response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
public static void die(Object... args) { throw new ConsulException(sputs(args)); } }
/** * /v1/session/info/session: * Queries a given session * * @param sessionId id of a session * @param requestOptions request options * @param datacenter datacenter * @return Session for this session id. */ public Optional<Session> info(final String sessionId, final String datacenter, final RequestOptions requestOptions) { final URI uri = createURI("/info/" + sessionId); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(datacenter, null, null, ""); HTTP.Response httpResponse = HTTP.getResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse == null || httpResponse.code() != 200) { die("Unable to get the sessions", uri, httpResponse); } final List<Session> sessions = fromJsonArray(httpResponse.body(), Session.class); if (sessions == null || sessions.size() == 0) { return Optional.empty(); } return Optional.of(sessions.get(0)); }
public static void dieWithException(Exception ex, Object... args) { throw new ConsulException(sputs(args), ex); }
/** * Remove registration of a particular service. * * @param serviceId the service id that you want to remove. */ public void deregister(final String serviceId) { final URI uri = createURI("/service/deregister/" + serviceId); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error removing registration of service with Consul", uri, serviceId, response.status(), response.payloadAsString()); } }
/** * Creates a new client given a host and a port. * * @param host The Consul API hostname or IP. * @param port The Consul port. * @return A new client. */ public static Consul consul(final String host, final int port) { try { return new Consul(new URL("http", host, port, "").toString()); } catch (MalformedURLException e) { throw new ConsulException("Bad Consul URL", e); } }
/** * Retrieves all services registered with the Agent. * <p> * GET /v1/agent/services * * @return Map of Service ID to Services. */ public Map<String, Service> getServices() { final URI uri = createURI("/services"); final HTTP.Response response = HTTP.getResponse(uri.toString()); final JsonParserAndMapper jsonParserAndMapper = new JsonParserFactory().create(); if (response.status() == 200) { final Map<String, Object> map = jsonParserAndMapper.parseMap(response.payloadAsString()); final Map<String, Service> returnMap = new HashMap<>(map.size()); map.entrySet().forEach(entry -> { @SuppressWarnings("unchecked") Service service = fromMap((Map<String, Object>) entry.getValue(), Service.class); returnMap.put(entry.getKey(), service); }); return returnMap; } die("Unable to get list of services", uri, response.status(), response.payloadAsString()); return null; }
/** * GET /v1/agent/force-leave/{node} * <p> * Instructs the agent to force a node into the "left" state. * * @param node node */ public void forceLeave(String node) { final URI uri = createURI("/force-leave/" + node); final HTTP.Response httpResponse = HTTP.getResponse(uri.toString()); if (httpResponse.code() != 200) { die("Unable to force leave", uri, httpResponse.code(), httpResponse.body()); } }
/** * Retrieves all checks registered with the Agent. * <p> * GET /v1/agent/checks * * @return Map of Check ID to Checks. */ public Map<String, HealthCheck> getChecks() { final URI uri = createURI("/checks"); final HTTP.Response response = HTTP.getResponse(uri.toString()); final JsonParserAndMapper jsonParserAndMapper = new JsonParserFactory().create(); if (response.status() == 200) { final Map<String, Object> map = jsonParserAndMapper.parseMap(response.payloadAsString()); final Map<String, HealthCheck> returnMap = new HashMap<>(map.size()); map.entrySet().forEach(entry -> { @SuppressWarnings("unchecked") HealthCheck healthCheck = fromMap((Map<String, Object>) entry.getValue(), HealthCheck.class); returnMap.put(entry.getKey(), healthCheck); }); return returnMap; } die("Unable to get health checks", uri, response.status(), response.statusMessageAsString(), response.payloadAsString()); return null; }
/** * Retrieves all members that the Agent can see in the gossip pool. * <p> * GET /v1/agent/members * * @return List of Members. */ public List<Member> getMembers() { final URI uri = createURI("/members"); final HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.code() == 200) { return fromJsonArray(response.body(), Member.class); } die("Unable to read members", uri, response.code(), response.body()); return Collections.emptyList(); }
/** * Retrieves all datacenters. * <p> * GET /v1/catalog/datacenters * * @return A list of datacenter names. */ public List<String> getDatacenters() { URI uri = createURI("/datacenters"); HTTP.Response httpResponse = HTTP.getResponse(uri.toString()); if (httpResponse.code() == 200) { return fromJsonArray(httpResponse.body(), String.class); } die("Unable to retrieve the datacenters", uri, httpResponse.code(), httpResponse.body()); return Collections.emptyList(); }
/** * De-registers a Health Check with the Agent * * @param checkId the id of the Check to deregister */ public void deregisterCheck(String checkId) { final URI uri = createURI("/check/deregister/" + checkId); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error removing registration of service with Consul", uri, checkId, response.status(), response.statusMessageAsString(), response.payloadAsString()); } }
/** * /v1/session/renew: Renews a TTL-based session * * @param sessionId session id * @param datacenter datacenter * @return session */ public Session renew(final String sessionId, final String datacenter) { final URI uri = createURI("/renew/" + sessionId); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(datacenter, null, null, ""); HTTP.Response httpResponse = HTTP.jsonRestCallViaPUT(uri.toString() + "?" + httpRequestBuilder.paramString(), ""); if (httpResponse == null || httpResponse.code() != 200) { die("Unable to renew the session", uri, httpResponse); } return fromJsonArray(httpResponse.body(), Session.class).get(0); }
/** * Deletes a specified key. * * @param key The key to delete. * @param params Map of parameters, e.g. recurse. */ private void delete(String key, Map<String, String> params) { final URI uri = createURI("/" + key); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(null, null, RequestOptions.BLANK, ""); final Set<Map.Entry<String, String>> entries = params.entrySet(); for (Map.Entry<String, String> entry : entries) { httpRequestBuilder.addParam(entry.getKey(), entry.getValue()); } httpRequestBuilder.setMethodDelete(); final HTTP.Response httpResponse = HTTP.deleteResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to delete key", uri, key, httpResponse.code(), httpResponse.body()); } } }