/** * 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()); } }
/** * Checks in with Consul. * * @param checkId The Check ID to check in. * @param status The current state of the Check. * @param note Any note to associate with the Check. */ public void check(String checkId, Status status, String note) { final URI uri = createURI("/check/" + status.getUri() + "/" + checkId); final HTTP.Response httpResponse = Str.isEmpty(note) ? HTTP.getResponse(uri.toString()) : HTTP.getResponse(uri.toString() + "?note=" + note); if (httpResponse.code() != 200) { notRegistered("Unable to perform check", uri, httpResponse.code(), httpResponse.statusMessageAsString(), httpResponse.body()); } }
/** * 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()); } }
/** * 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 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(); }
/** * 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(); }
/** * 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()); } }
/** * @param datacenter datacenter * @param requestOptions request options for long poll and consistency. * @return list of sessions */ public List<Session> getSessions(final String datacenter, final RequestOptions requestOptions) { final URI uri = createURI("/list"); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(datacenter, null, requestOptions, ""); HTTP.Response httpResponse = HTTP.getResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse == null || httpResponse.code() != 200) { die("Unable to get the sessions", uri, httpResponse); } return fromJsonArray(httpResponse.body(), Session.class); }
@Test public void testPing2() { HTTP.Response response = HTTP.getResponse(buildURL("ping2")); assertEquals(777, response.status()); assertEquals("hello mom", response.body()); }
/** * Retrieves the host/port of the Consul leader. * <p> * GET /v1/status/leader * * @return The host/port of the leader. */ public String getLeader() { final URI uri = createURI("/leader"); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(null, null, RequestOptions.BLANK, ""); final HTTP.Response httpResponse = HTTP.getResponse(uri + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to retrieve the leader", uri, httpResponse.code(), httpResponse.body()); } return fromJson(httpResponse.body(), String.class).replace("\"", "").trim(); }
/** * /v1/session/node/node: * Lists sessions belonging to a node * * @param node node * @param requestOptions request options for consistency and long poll * @param datacenter data center param * @return list of sessions for this node */ public List<Session> getSessionsForNode(final String node, final String datacenter, final RequestOptions requestOptions) { final URI uri = createURI("/node/" + node); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(datacenter, null, requestOptions, ""); HTTP.Response httpResponse = HTTP.getResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse == null || httpResponse.code() != 200) { die("Unable to get the sessions", uri, httpResponse); } return fromJsonArray(httpResponse.body(), Session.class); }
@Test public void testPing() { HTTP.Response response = HTTP.getResponse(buildURL("ping")); assertEquals(200, response.status()); final List<String> controls = response.headers().get(HttpHeaders.CACHE_CONTROL); Assert.assertEquals("no-cache, no-store", controls.get(0)); Assert.assertEquals("max-age=0", controls.get(1)); assertEquals("\"love rocket\"", response.body()); }
/** * <p> * Retrieves the Agent's configuration and member information. * </p> * GET /v1/agent/self * * @return The Agent information. */ public AgentInfo getAgentInfo() { final URI uri = createURI("/self"); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error getting info about this agent", uri, response.status(), response.statusMessageAsString(), response.payloadAsString()); } return fromJson(response.payloadAsString(), AgentInfo.class); }
/** * Retrieves a list of matching keys for the given key. * <p> * GET /v1/keyValueStore/{key}?keys * * @param key The key to retrieve. * @return A list of zero to many keys. */ public List<String> getKeys(String key) { final URI uri = createURI("/" + key); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(null, null, RequestOptions.BLANK, ""); httpRequestBuilder.addParam("keys", "true"); final HTTP.Response httpResponse = HTTP.getResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() == 200) { return fromJsonArray(httpResponse.body(), String.class); } else { die("Unable to get nested keys", uri, key, httpResponse.code(), httpResponse.body()); return Collections.emptyList(); } }
/** * Retrieves all services for a given datacenter with {@link io.advantageous.consul.domain.option.RequestOptions}. * <p> * GET /v1/catalog/services?dc={datacenter} * * @param datacenter datacenter * @param tag tag * @param requestOptions The Query Options to use. * @return A {@link io.advantageous.consul.domain.ConsulResponse} containing a map of service name to list of tags. */ public ConsulResponse<Map<String, List<String>>> getServices( @SuppressWarnings("SameParameterValue") final String datacenter, @SuppressWarnings("SameParameterValue") final String tag, final RequestOptions requestOptions) { final URI uri = createURI("/services"); final HttpRequestBuilder httpRequestBuilder = RequestUtils.getHttpRequestBuilder(datacenter, tag, requestOptions, "/"); HTTP.Response httpResponse = HTTP.getResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to retrieve the datacenters", uri, httpResponse.code(), httpResponse.body()); } //noinspection unchecked return (ConsulResponse<Map<String, List<String>>>) (Object) RequestUtils.consulResponse(Map.class, httpResponse); }
public ConsulResponse<List<Node>> getNodes(final String datacenter, final String tag, final RequestOptions requestOptions) { final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(datacenter, tag, requestOptions, ""); final URI uri = createURI("/nodes"); final HTTP.Response httpResponse = HTTP.getResponse(uri + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to retrieve the nodes", uri, httpResponse.code(), httpResponse.body()); } return RequestUtils.consulResponseList(Node.class, httpResponse); }
/** * Retrieves a list of {@link io.advantageous.consul.domain.KeyValue} objects for a specific key * from the key/value store. * <p> * GET /v1/keyValueStore/{key}?recurse * * @param key The key to retrieve. * @return A list of zero to many {@link io.advantageous.consul.domain.KeyValue} objects. */ public List<KeyValue> getValues(String key) { final URI uri = createURI("/" + key); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(null, null, RequestOptions.BLANK, ""); httpRequestBuilder.addParam("recurse", "true"); final HTTP.Response httpResponse = HTTP.getResponse(uri.toString() + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to retrieve the service", uri, httpResponse.code(), httpResponse.body()); } return fromJsonArray(httpResponse.body(), KeyValue.class); }
/** * Retrieves a single node for a given datacenter with {@link io.advantageous.consul.domain.option.RequestOptions}. * <p> * GET /v1/catalog/node/{node}?dc={datacenter} * * @param node node * @param datacenter dc * @param tag tag * @param requestOptions The Query Options to use. * @return A list of matching {@link io.advantageous.consul.domain.CatalogService} objects. */ public ConsulResponse<CatalogNode> getNode(final String node, final String datacenter, final String tag, final RequestOptions requestOptions) { final URI uri = createURI("/node/" + node); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(datacenter, tag, requestOptions, ""); final HTTP.Response httpResponse = HTTP.getResponse(uri + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to retrieve the node", uri, httpResponse.code(), httpResponse.body()); } return RequestUtils.consulResponse(CatalogNode.class, httpResponse); }
/** * Retrieves a list of host/ports for raft peers. * <p> * GET /v1/status/peers * * @return List of host/ports for raft peers. */ public List<String> getPeers() { final URI uri = createURI("/peers"); final HttpRequestBuilder httpRequestBuilder = RequestUtils .getHttpRequestBuilder(null, null, RequestOptions.BLANK, ""); final HTTP.Response httpResponse = HTTP.getResponse(uri + "?" + httpRequestBuilder.paramString()); if (httpResponse.code() != 200) { die("Unable to get the peers", uri, httpResponse.code(), httpResponse.body()); } return fromJsonArray(httpResponse.body(), String.class); }