├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── test │ ├── resources │ │ ├── ssl │ │ │ ├── keyStore.jks │ │ │ ├── trustStore.jks │ │ │ ├── ca.cert │ │ │ ├── key.key │ │ │ └── key.crt │ │ └── generate-ssl-cert.sh │ └── java │ │ └── com │ │ └── ecwid │ │ └── consul │ │ ├── ConsulTestConstants.java │ │ ├── v1 │ │ ├── TagsParametersTest.java │ │ ├── NodeMetaParametersTest.java │ │ ├── event │ │ │ ├── model │ │ │ │ └── EventParamsTest.java │ │ │ └── EventListRequestTest.java │ │ ├── catalog │ │ │ ├── CatalogNodesRequestTest.java │ │ │ ├── CatalogServiceRequestTest.java │ │ │ ├── CatalogServicesRequestTest.java │ │ │ └── CatalogConsulClientTest.java │ │ ├── health │ │ │ ├── HealthServicesRequestTest.java │ │ │ └── HealthChecksForServiceRequestTest.java │ │ ├── kv │ │ │ ├── model │ │ │ │ └── PutParamsTest.java │ │ │ └── KeyValueConsulClientTest.java │ │ ├── acl │ │ │ └── AclConsulClientTest.java │ │ ├── QueryParamsTest.java │ │ └── ConsulClientTest.java │ │ ├── SingleUrlParametersTest.java │ │ ├── ConsulRawClientTest.java │ │ └── UtilsTest.java └── main │ └── java │ └── com │ └── ecwid │ └── consul │ ├── v1 │ ├── kv │ │ ├── model │ │ │ ├── DeleteRequest.java │ │ │ ├── PutParams.java │ │ │ ├── GetBinaryValue.java │ │ │ └── GetValue.java │ │ └── KeyValueClient.java │ ├── ConsistencyMode.java │ ├── acl │ │ ├── model │ │ │ ├── AclType.java │ │ │ ├── NewAcl.java │ │ │ ├── UpdateAcl.java │ │ │ └── Acl.java │ │ ├── AclClient.java │ │ └── AclConsulClient.java │ ├── query │ │ ├── QueryClient.java │ │ ├── model │ │ │ ├── QueryExecution.java │ │ │ ├── Check.java │ │ │ └── QueryNode.java │ │ └── QueryConsulClient.java │ ├── status │ │ ├── StatusClient.java │ │ └── StatusConsulClient.java │ ├── coordinate │ │ ├── CoordinateClient.java │ │ ├── model │ │ │ ├── Node.java │ │ │ ├── Datacenter.java │ │ │ └── Coord.java │ │ └── CoordinateConsulClient.java │ ├── catalog │ │ ├── model │ │ │ ├── WriteRequest.java │ │ │ ├── CatalogDeregistration.java │ │ │ ├── CatalogNode.java │ │ │ ├── Node.java │ │ │ └── CatalogService.java │ │ ├── CatalogNodesRequest.java │ │ ├── CatalogServicesRequest.java │ │ ├── CatalogClient.java │ │ └── CatalogServiceRequest.java │ ├── TagsParameters.java │ ├── event │ │ ├── EventClient.java │ │ ├── model │ │ │ ├── EventParams.java │ │ │ └── Event.java │ │ ├── EventConsulClient.java │ │ └── EventListRequest.java │ ├── NodeMetaParameters.java │ ├── Response.java │ ├── OperationException.java │ ├── session │ │ ├── SessionClient.java │ │ └── model │ │ │ ├── NewSession.java │ │ │ └── Session.java │ ├── agent │ │ ├── AgentClient.java │ │ └── model │ │ │ ├── Service.java │ │ │ ├── Member.java │ │ │ ├── Check.java │ │ │ └── NewCheck.java │ ├── Request.java │ ├── health │ │ ├── HealthClient.java │ │ ├── HealthChecksForServiceRequest.java │ │ ├── model │ │ │ └── Check.java │ │ ├── HealthServicesRequest.java │ │ └── HealthConsulClient.java │ └── QueryParams.java │ ├── ConsulRequest.java │ ├── UrlParameters.java │ ├── json │ ├── GsonFactory.java │ └── Base64TypeAdapter.java │ ├── transport │ ├── TransportException.java │ ├── HttpTransport.java │ ├── HttpResponse.java │ ├── TLSConfig.java │ ├── DefaultHttpTransport.java │ ├── HttpRequest.java │ ├── DefaultHttpsTransport.java │ └── AbstractHttpTransport.java │ ├── ConsulException.java │ ├── SingleUrlParameters.java │ └── Utils.java ├── .editorconfig ├── .travis.yml ├── .gitignore ├── gradlew.bat └── README.md /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ecwid/consul-api/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/test/resources/ssl/keyStore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ecwid/consul-api/HEAD/src/test/resources/ssl/keyStore.jks -------------------------------------------------------------------------------- /src/test/resources/ssl/trustStore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ecwid/consul-api/HEAD/src/test/resources/ssl/trustStore.jks -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Unix-style newlines with a newline ending every file 2 | [*] 3 | end_of_line = lf 4 | insert_final_newline = true 5 | charset = utf-8 6 | indent_style = tab -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/ConsulTestConstants.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | public class ConsulTestConstants { 4 | 5 | public static final String CONSUL_VERSION = "1.6.0"; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: xenial 3 | 4 | jdk: 5 | - openjdk8 6 | - openjdk10 7 | - openjdk11 8 | - openjdk12 9 | 10 | notifications: 11 | email: 12 | - vgv@ecwid.com 13 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/kv/model/DeleteRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv.model; 2 | 3 | /** 4 | * @author Vasily Vasilkov (vgv@ecwid.com) 5 | */ 6 | public class DeleteRequest { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/ConsulRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | import java.util.List; 4 | 5 | public interface ConsulRequest { 6 | 7 | public List asUrlParameters(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ IDEA 2 | .idea/ 3 | *.iml 4 | 5 | # Gradle 6 | .gradle/ 7 | build/ 8 | out/ 9 | 10 | # Special exception for my test main class used during development 11 | src/main/java/com/ecwid/consul/LocalTest.java 12 | 13 | .DS_Store 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/ConsistencyMode.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | /** 4 | * @author Vasily Vasilkov (vgv@ecwid.com) 5 | */ 6 | public enum ConsistencyMode { 7 | 8 | DEFAULT, 9 | 10 | STALE, 11 | 12 | CONSISTENT 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/UrlParameters.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public interface UrlParameters { 9 | 10 | public List toUrlParameters(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/acl/model/AclType.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public enum AclType { 9 | 10 | @SerializedName("client") 11 | CLIENT, 12 | 13 | @SerializedName("management") 14 | MANAGEMENT 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/query/QueryClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.query; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.query.model.QueryExecution; 6 | 7 | public interface QueryClient { 8 | 9 | public Response executePreparedQuery(String uuid, QueryParams queryParams); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/status/StatusClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.status; 2 | 3 | import com.ecwid.consul.v1.Response; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public interface StatusClient { 11 | 12 | public Response getStatusLeader(); 13 | 14 | public Response> getStatusPeers(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/json/GsonFactory.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.json; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | 6 | /** 7 | * @author Vasily Vasilkov (vgv@ecwid.com) 8 | */ 9 | public class GsonFactory { 10 | 11 | private static final Gson GSON = new Gson(); 12 | 13 | public static Gson getGson() { 14 | return GSON; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/TransportException.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | import com.ecwid.consul.ConsulException; 4 | 5 | import java.io.IOException; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class TransportException extends ConsulException { 11 | 12 | public TransportException(Throwable cause) { 13 | super(cause); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/TagsParametersTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class TagsParametersTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(TagsParameters.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/NodeMetaParametersTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class NodeMetaParametersTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(NodeMetaParameters.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/HttpTransport.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public interface HttpTransport { 9 | 10 | public HttpResponse makeGetRequest(HttpRequest request); 11 | 12 | public HttpResponse makePutRequest(HttpRequest request); 13 | 14 | public HttpResponse makeDeleteRequest(HttpRequest request); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/event/model/EventParamsTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event.model; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class EventParamsTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.simple().forClass(EventParams.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/catalog/CatalogNodesRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CatalogNodesRequestTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(CatalogNodesRequest.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/catalog/CatalogServiceRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CatalogServiceRequestTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(CatalogServiceRequest.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/health/HealthServicesRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class HealthServicesRequestTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(HealthServicesRequest.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/catalog/CatalogServicesRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CatalogServicesRequestTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(CatalogServicesRequest.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/ConsulException.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | /** 4 | * Base exception for any consul errors 5 | * 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class ConsulException extends RuntimeException { 9 | 10 | public ConsulException() { 11 | } 12 | 13 | public ConsulException(Throwable cause) { 14 | super(cause); 15 | } 16 | 17 | public ConsulException(String message) { 18 | super(message); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/health/HealthChecksForServiceRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health; 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier; 4 | import org.junit.jupiter.api.Nested; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class HealthChecksForServiceRequestTest { 8 | @Nested 9 | class EqualsAndHashCode { 10 | @Test 11 | void shouldVerify() { 12 | EqualsVerifier.forClass(HealthChecksForServiceRequest.class).verify(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/event/EventListRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event; 2 | 3 | import com.ecwid.consul.v1.NodeMetaParameters; 4 | import nl.jqno.equalsverifier.EqualsVerifier; 5 | import org.junit.jupiter.api.Nested; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class EventListRequestTest { 9 | @Nested 10 | class EqualsAndHashCode { 11 | @Test 12 | void shouldVerify() { 13 | EqualsVerifier.forClass(EventListRequest.class).verify(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/kv/model/PutParamsTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv.model; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import com.ecwid.consul.v1.QueryParams; 6 | import nl.jqno.equalsverifier.EqualsVerifier; 7 | import org.junit.jupiter.api.Nested; 8 | import org.junit.jupiter.api.Test; 9 | 10 | class PutParamsTest { 11 | @Nested 12 | class EqualsAndHashCode { 13 | @Test 14 | void shouldVerify() { 15 | EqualsVerifier.simple().forClass(PutParams.class).verify(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/coordinate/CoordinateClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.coordinate; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.coordinate.model.Datacenter; 6 | import com.ecwid.consul.v1.coordinate.model.Node; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public interface CoordinateClient { 14 | 15 | public Response> getDatacenters(); 16 | 17 | public Response> getNodes(QueryParams queryParams); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/model/WriteRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class WriteRequest { 9 | 10 | @SerializedName("Token") 11 | private String token; 12 | 13 | public String getToken() { 14 | return token; 15 | } 16 | 17 | public void setToken(String token) { 18 | this.token = token; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return "WriteRequest{" + 24 | "token='" + token + '\'' + 25 | '}'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/acl/AclClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl; 2 | 3 | import com.ecwid.consul.v1.Response; 4 | import com.ecwid.consul.v1.acl.model.Acl; 5 | import com.ecwid.consul.v1.acl.model.NewAcl; 6 | import com.ecwid.consul.v1.acl.model.UpdateAcl; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public interface AclClient { 14 | 15 | Response aclCreate(NewAcl newAcl, String token); 16 | 17 | Response aclUpdate(UpdateAcl updateAcl, String token); 18 | 19 | Response aclDestroy(String aclId, String token); 20 | 21 | Response getAcl(String id); 22 | 23 | Response aclClone(String aclId, String token); 24 | 25 | Response> getAclList(String token); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/coordinate/model/Node.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.coordinate.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class Node { 9 | 10 | @SerializedName("Node") 11 | private String node; 12 | 13 | @SerializedName("Coord") 14 | private Coord coord; 15 | 16 | public String getNode() { 17 | return node; 18 | } 19 | 20 | public void setNode(String node) { 21 | this.node = node; 22 | } 23 | 24 | public Coord getCoord() { 25 | return coord; 26 | } 27 | 28 | public void setCoord(Coord coord) { 29 | this.coord = coord; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "Node{" + 35 | "node='" + node + '\'' + 36 | ", coord=" + coord + 37 | '}'; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/json/Base64TypeAdapter.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.json; 2 | 3 | import com.google.gson.TypeAdapter; 4 | import com.google.gson.stream.JsonReader; 5 | import com.google.gson.stream.JsonToken; 6 | import com.google.gson.stream.JsonWriter; 7 | 8 | import java.io.IOException; 9 | import java.util.Base64; 10 | 11 | /** 12 | * @author Vasily Vasilkov (vgv@ecwid.com) 13 | */ 14 | public class Base64TypeAdapter extends TypeAdapter { 15 | 16 | @Override 17 | public void write(JsonWriter out, byte[] value) throws IOException { 18 | if (value == null) { 19 | out.nullValue(); 20 | } else { 21 | out.value(Base64.getEncoder().encodeToString(value)); 22 | } 23 | } 24 | 25 | @Override 26 | public byte[] read(JsonReader in) throws IOException { 27 | if (in.peek() == JsonToken.NULL) { 28 | in.nextNull(); 29 | return new byte[0]; 30 | } else { 31 | String data = in.nextString(); 32 | return Base64.getDecoder().decode(data); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/acl/model/NewAcl.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class NewAcl { 9 | 10 | @SerializedName("Name") 11 | private String name; 12 | 13 | @SerializedName("Type") 14 | private AclType type; 15 | 16 | @SerializedName("Rules") 17 | private String rules; 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public AclType getType() { 28 | return type; 29 | } 30 | 31 | public void setType(AclType type) { 32 | this.type = type; 33 | } 34 | 35 | public String getRules() { 36 | return rules; 37 | } 38 | 39 | public void setRules(String rules) { 40 | this.rules = rules; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "NewAcl{" + 46 | "name='" + name + '\'' + 47 | ", type=" + type + 48 | ", rules='" + rules + '\'' + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/TagsParameters.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.UrlParameters; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | public final class TagsParameters implements UrlParameters { 10 | 11 | private final String[] tags; 12 | 13 | public TagsParameters(String[] tags) { 14 | this.tags = tags; 15 | } 16 | 17 | @Override 18 | public List toUrlParameters() { 19 | List params = new ArrayList<>(); 20 | 21 | if (tags != null) { 22 | for (String tag : tags) { 23 | if (tag != null) { 24 | params.add("tag=" + tag); 25 | } 26 | } 27 | } 28 | 29 | return params; 30 | } 31 | 32 | @Override 33 | public boolean equals(Object o) { 34 | if (this == o) { 35 | return true; 36 | } 37 | if (!(o instanceof TagsParameters)) { 38 | return false; 39 | } 40 | TagsParameters that = (TagsParameters) o; 41 | return Arrays.equals(tags, that.tags); 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | return Arrays.hashCode(tags); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/SingleUrlParametersTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | 4 | import nl.jqno.equalsverifier.EqualsVerifier; 5 | import org.junit.jupiter.api.Nested; 6 | import org.junit.jupiter.api.Test; 7 | 8 | import java.util.Collections; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | 12 | 13 | public class SingleUrlParametersTest { 14 | 15 | @Test 16 | public void testToUrlParameters() throws Exception { 17 | UrlParameters parameters = new SingleUrlParameters("key"); 18 | assertEquals(Collections.singletonList("key"), parameters.toUrlParameters()); 19 | 20 | parameters = new SingleUrlParameters("key", "value"); 21 | assertEquals(Collections.singletonList("key=value"), parameters.toUrlParameters()); 22 | 23 | parameters = new SingleUrlParameters("key", "value value"); 24 | assertEquals(Collections.singletonList("key=value+value"), parameters.toUrlParameters()); 25 | } 26 | 27 | @Nested 28 | class EqualsAndHashCode { 29 | @Test 30 | void shouldVerify() { 31 | EqualsVerifier.forClass(SingleUrlParameters.class).verify(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/event/EventClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.event.model.Event; 6 | import com.ecwid.consul.v1.event.model.EventParams; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public interface EventClient { 14 | 15 | public Response eventFire(String event, String payload, EventParams eventParams, QueryParams queryParams); 16 | 17 | // ------------------------------------------------------------------------------- 18 | 19 | /** 20 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #eventList(EventListRequest eventListRequest)} 21 | */ 22 | @Deprecated 23 | public Response> eventList(QueryParams queryParams); 24 | 25 | /** 26 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #eventList(EventListRequest eventListRequest)} 27 | */ 28 | @Deprecated 29 | public Response> eventList(String event, QueryParams queryParams); 30 | 31 | public Response> eventList(EventListRequest eventListRequest); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/coordinate/model/Datacenter.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.coordinate.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class Datacenter { 11 | 12 | @SerializedName("Datacenter") 13 | private String datacenter; 14 | 15 | @SerializedName("AreaID") 16 | private String areaId; 17 | 18 | @SerializedName("Coordinates") 19 | private List coordinates; 20 | 21 | public String getDatacenter() { 22 | return datacenter; 23 | } 24 | 25 | public void setDatacenter(String datacenter) { 26 | this.datacenter = datacenter; 27 | } 28 | 29 | public String getAreaId() { 30 | return areaId; 31 | } 32 | 33 | public void setAreaId(String areaId) { 34 | this.areaId = areaId; 35 | } 36 | 37 | public List getCoordinates() { 38 | return coordinates; 39 | } 40 | 41 | public void setCoordinates(List coordinates) { 42 | this.coordinates = coordinates; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "Datacenter{" + 48 | "datacenter='" + datacenter + '\'' + 49 | ", areaId='" + areaId + '\'' + 50 | ", coordinates=" + coordinates + 51 | '}'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/acl/model/UpdateAcl.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class UpdateAcl { 9 | 10 | @SerializedName("ID") 11 | private String id; 12 | 13 | @SerializedName("Name") 14 | private String name; 15 | 16 | @SerializedName("Type") 17 | private AclType type; 18 | 19 | @SerializedName("Rules") 20 | private String rules; 21 | 22 | public String getId() { 23 | return id; 24 | } 25 | 26 | public void setId(String id) { 27 | this.id = id; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public AclType getType() { 39 | return type; 40 | } 41 | 42 | public void setType(AclType type) { 43 | this.type = type; 44 | } 45 | 46 | public String getRules() { 47 | return rules; 48 | } 49 | 50 | public void setRules(String rules) { 51 | this.rules = rules; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "UpdateAcl{" + 57 | "id='" + id + '\'' + 58 | ", name='" + name + '\'' + 59 | ", type=" + type + 60 | ", rules='" + rules + '\'' + 61 | '}'; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/SingleUrlParameters.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | import java.util.Collections; 4 | import java.util.List; 5 | import java.util.Objects; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public final class SingleUrlParameters implements UrlParameters { 11 | 12 | private final String key; 13 | private final String value; 14 | 15 | public SingleUrlParameters(String key) { 16 | this.key = key; 17 | this.value = null; 18 | } 19 | 20 | public SingleUrlParameters(String key, String value) { 21 | this.key = key; 22 | this.value = value; 23 | } 24 | 25 | @Override 26 | public List toUrlParameters() { 27 | if (value != null) { 28 | return Collections.singletonList(key + "=" + Utils.encodeValue(value)); 29 | } else { 30 | return Collections.singletonList(key); 31 | } 32 | } 33 | 34 | @Override 35 | public boolean equals(Object o) { 36 | if (this == o) { 37 | return true; 38 | } 39 | if (!(o instanceof SingleUrlParameters)) { 40 | return false; 41 | } 42 | SingleUrlParameters that = (SingleUrlParameters) o; 43 | return Objects.equals(key, that.key) && 44 | Objects.equals(value, that.value); 45 | } 46 | 47 | @Override 48 | public int hashCode() { 49 | return Objects.hash(key, value); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/HttpResponse.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | /** 4 | * @author Vasily Vasilkov (vgv@ecwid.com) 5 | */ 6 | public final class HttpResponse { 7 | 8 | private final int statusCode; 9 | private final String statusMessage; 10 | 11 | private final String content; 12 | 13 | private final Long consulIndex; 14 | private final Boolean consulKnownLeader; 15 | private final Long consulLastContact; 16 | 17 | public HttpResponse(int statusCode, String statusMessage, String content, Long consulIndex, Boolean consulKnownLeader, Long consulLastContact) { 18 | this.statusCode = statusCode; 19 | this.statusMessage = statusMessage; 20 | this.content = content; 21 | this.consulIndex = consulIndex; 22 | this.consulKnownLeader = consulKnownLeader; 23 | this.consulLastContact = consulLastContact; 24 | } 25 | 26 | public int getStatusCode() { 27 | return statusCode; 28 | } 29 | 30 | public String getStatusMessage() { 31 | return statusMessage; 32 | } 33 | 34 | public String getContent() { 35 | return content; 36 | } 37 | 38 | public Long getConsulIndex() { 39 | return consulIndex; 40 | } 41 | 42 | public Boolean isConsulKnownLeader() { 43 | return consulKnownLeader; 44 | } 45 | 46 | public Long getConsulLastContact() { 47 | return consulLastContact; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/TLSConfig.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | public final class TLSConfig { 4 | 5 | public enum KeyStoreInstanceType { 6 | JKS, JCEKS, PKCS12, PKCS11, DKS 7 | } 8 | 9 | private final KeyStoreInstanceType keyStoreInstanceType; 10 | private final String certificatePath; 11 | private final String certificatePassword; 12 | private final String keyStorePath; 13 | private final String keyStorePassword; 14 | 15 | public TLSConfig(KeyStoreInstanceType keyStoreInstanceType, String certificatePath, String certificatePassword, String keyStorePath, 16 | String keyStorePassword) { 17 | this.keyStoreInstanceType = keyStoreInstanceType; 18 | this.certificatePath = certificatePath; 19 | this.certificatePassword = certificatePassword; 20 | this.keyStorePath = keyStorePath; 21 | this.keyStorePassword = keyStorePassword; 22 | } 23 | 24 | public KeyStoreInstanceType getKeyStoreInstanceType() { 25 | return keyStoreInstanceType; 26 | } 27 | 28 | public String getCertificatePath() { 29 | return certificatePath; 30 | } 31 | 32 | public String getCertificatePassword() { 33 | return certificatePassword; 34 | } 35 | 36 | public String getKeyStorePath() { 37 | return keyStorePath; 38 | } 39 | 40 | public String getKeyStorePassword() { 41 | return keyStorePassword; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/coordinate/model/Coord.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.coordinate.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class Coord { 11 | 12 | @SerializedName("Error") 13 | private Double error; 14 | @SerializedName("Height") 15 | private Double height; 16 | @SerializedName("Adjustment") 17 | private Double adjustment; 18 | @SerializedName("Vec") 19 | private List vec; 20 | 21 | public Double getError() { 22 | return error; 23 | } 24 | 25 | public void setError(Double error) { 26 | this.error = error; 27 | } 28 | 29 | public Double getHeight() { 30 | return height; 31 | } 32 | 33 | public void setHeight(Double height) { 34 | this.height = height; 35 | } 36 | 37 | public Double getAdjustment() { 38 | return adjustment; 39 | } 40 | 41 | public void setAdjustment(Double adjustment) { 42 | this.adjustment = adjustment; 43 | } 44 | 45 | public List getVec() { 46 | return vec; 47 | } 48 | 49 | public void setVec(List vec) { 50 | this.vec = vec; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return "Coord{" + 56 | "error=" + error + 57 | ", height=" + height + 58 | ", adjustment=" + adjustment + 59 | ", vec=" + vec + 60 | '}'; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/NodeMetaParameters.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.UrlParameters; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Objects; 9 | 10 | public final class NodeMetaParameters implements UrlParameters { 11 | 12 | private final Map nodeMeta; 13 | 14 | public NodeMetaParameters(Map nodeMeta) { 15 | this.nodeMeta = nodeMeta; 16 | } 17 | 18 | @Override 19 | public List toUrlParameters() { 20 | List params = new ArrayList<>(); 21 | 22 | if (nodeMeta != null) { 23 | String key = "node-meta"; 24 | 25 | for (Map.Entry entry : nodeMeta.entrySet()) { 26 | String value = entry.getKey() + ":" + entry.getValue(); 27 | params.add(key + "=" + value); 28 | } 29 | } 30 | 31 | return params; 32 | } 33 | 34 | @Override 35 | public boolean equals(Object o) { 36 | if (this == o) { 37 | return true; 38 | } 39 | if (!(o instanceof NodeMetaParameters)) { 40 | return false; 41 | } 42 | NodeMetaParameters that = (NodeMetaParameters) o; 43 | return Objects.equals(nodeMeta, that.nodeMeta); 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return Objects.hash(nodeMeta); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/Response.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.transport.HttpResponse; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public final class Response { 9 | 10 | private final T value; 11 | 12 | private final Long consulIndex; 13 | private final Boolean consulKnownLeader; 14 | private final Long consulLastContact; 15 | 16 | public Response(T value, Long consulIndex, Boolean consulKnownLeader, Long consulLastContact) { 17 | this.value = value; 18 | this.consulIndex = consulIndex; 19 | this.consulKnownLeader = consulKnownLeader; 20 | this.consulLastContact = consulLastContact; 21 | } 22 | 23 | public Response(T value, HttpResponse httpResponse) { 24 | this(value, httpResponse.getConsulIndex(), httpResponse.isConsulKnownLeader(), httpResponse.getConsulLastContact()); 25 | } 26 | 27 | public T getValue() { 28 | return value; 29 | } 30 | 31 | public Long getConsulIndex() { 32 | return consulIndex; 33 | } 34 | 35 | public Boolean isConsulKnownLeader() { 36 | return consulKnownLeader; 37 | } 38 | 39 | public Long getConsulLastContact() { 40 | return consulLastContact; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "Response{" + 46 | "value=" + value + 47 | ", consulIndex=" + consulIndex + 48 | ", consulKnownLeader=" + consulKnownLeader + 49 | ", consulLastContact=" + consulLastContact + 50 | '}'; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/OperationException.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.ConsulException; 4 | import com.ecwid.consul.transport.HttpResponse; 5 | 6 | /** 7 | * @author Vasily Vasilkov (vgv@ecwid.com) 8 | */ 9 | public final class OperationException extends ConsulException { 10 | 11 | private final int statusCode; 12 | private final String statusMessage; 13 | private final String statusContent; 14 | 15 | public OperationException(int statusCode, String statusMessage, String statusContent) { 16 | super("OperationException(statusCode=" + statusCode + ", statusMessage='" + statusMessage + "', statusContent='" + statusContent + "')"); 17 | this.statusCode = statusCode; 18 | this.statusMessage = statusMessage; 19 | this.statusContent = statusContent; 20 | } 21 | 22 | public OperationException(HttpResponse httpResponse) { 23 | this(httpResponse.getStatusCode(), httpResponse.getStatusMessage(), httpResponse.getContent()); 24 | } 25 | 26 | public int getStatusCode() { 27 | return statusCode; 28 | } 29 | 30 | public String getStatusMessage() { 31 | return statusMessage; 32 | } 33 | 34 | public String getStatusContent() { 35 | return statusContent; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "OperationException{" + 41 | "statusCode=" + statusCode + 42 | ", statusMessage='" + statusMessage + '\'' + 43 | ", statusContent='" + statusContent + '\'' + 44 | '}'; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/session/SessionClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.session; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.session.model.NewSession; 6 | import com.ecwid.consul.v1.session.model.Session; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public interface SessionClient { 14 | 15 | public Response sessionCreate(NewSession newSession, QueryParams queryParams); 16 | 17 | public Response sessionCreate(NewSession newSession, QueryParams queryParams, String token); 18 | 19 | public Response sessionDestroy(String session, QueryParams queryParams); 20 | 21 | public Response sessionDestroy(String session, QueryParams queryParams, String token); 22 | 23 | Response getSessionInfo(String session, QueryParams queryParams); 24 | 25 | Response getSessionInfo(String session, QueryParams queryParams, String token); 26 | 27 | Response> getSessionNode(String node, QueryParams queryParams); 28 | 29 | Response> getSessionNode(String node, QueryParams queryParams, String token); 30 | 31 | Response> getSessionList(QueryParams queryParams); 32 | 33 | Response> getSessionList(QueryParams queryParams, String token); 34 | 35 | Response renewSession(String session, QueryParams queryParams); 36 | 37 | Response renewSession(String session, QueryParams queryParams, String token); 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/DefaultHttpTransport.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | import org.apache.http.client.HttpClient; 4 | import org.apache.http.client.config.RequestConfig; 5 | import org.apache.http.impl.client.HttpClientBuilder; 6 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 7 | 8 | /** 9 | * Default HTTP client This class is thread safe 10 | * 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public final class DefaultHttpTransport extends AbstractHttpTransport { 14 | 15 | private final HttpClient httpClient; 16 | 17 | public DefaultHttpTransport() { 18 | PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); 19 | connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS); 20 | connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE_CONNECTIONS); 21 | 22 | RequestConfig requestConfig = RequestConfig.custom(). 23 | setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT). 24 | setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT). 25 | setSocketTimeout(DEFAULT_READ_TIMEOUT). 26 | build(); 27 | 28 | HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(). 29 | setConnectionManager(connectionManager). 30 | setDefaultRequestConfig(requestConfig). 31 | useSystemProperties(); 32 | 33 | this.httpClient = httpClientBuilder.build(); 34 | } 35 | 36 | public DefaultHttpTransport(HttpClient httpClient) { 37 | this.httpClient = httpClient; 38 | } 39 | 40 | @Override 41 | protected HttpClient getHttpClient() { 42 | return httpClient; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/catalog/CatalogConsulClientTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import com.ecwid.consul.ConsulTestConstants; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.catalog.model.Node; 6 | import com.ecwid.consul.v1.kv.KeyValueConsulClient; 7 | import com.pszymczyk.consul.ConsulProcess; 8 | import com.pszymczyk.consul.ConsulStarterBuilder; 9 | import com.pszymczyk.consul.infrastructure.Ports; 10 | import org.junit.jupiter.api.AfterEach; 11 | import org.junit.jupiter.api.BeforeEach; 12 | import org.junit.jupiter.api.Test; 13 | 14 | import java.util.List; 15 | import java.util.Random; 16 | 17 | import static org.junit.jupiter.api.Assertions.*; 18 | 19 | class CatalogConsulClientTest { 20 | 21 | private static final Random rnd = new Random(); 22 | 23 | private ConsulProcess consul; 24 | private int port = Ports.nextAvailable(); 25 | 26 | private CatalogConsulClient consulClient = new CatalogConsulClient("localhost", port); 27 | 28 | @BeforeEach 29 | void setUp() { 30 | consul = ConsulStarterBuilder.consulStarter() 31 | .withConsulVersion(ConsulTestConstants.CONSUL_VERSION) 32 | .withHttpPort(port) 33 | .build() 34 | .start(); 35 | } 36 | 37 | @AfterEach 38 | void tearDown() { 39 | consul.close(); 40 | } 41 | 42 | @Test 43 | void testGetCatalogNodes() { 44 | CatalogNodesRequest request = CatalogNodesRequest.newBuilder().build(); 45 | Response> response = consulClient.getCatalogNodes(request); 46 | 47 | // We should find only one node – this 48 | assertEquals(1, response.getValue().size()); 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/session/model/NewSession.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.session.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class NewSession { 11 | 12 | @SerializedName("LockDelay") 13 | private long lockDelay; 14 | 15 | @SerializedName("Name") 16 | private String name; 17 | 18 | @SerializedName("Node") 19 | private String node; 20 | 21 | @SerializedName("Checks") 22 | private List checks; 23 | 24 | @SerializedName("Behavior") 25 | private Session.Behavior behavior; 26 | 27 | @SerializedName("TTL") 28 | private String ttl; 29 | 30 | public long getLockDelay() { 31 | return lockDelay; 32 | } 33 | 34 | public void setLockDelay(long lockDelayInSeconds) { 35 | this.lockDelay = lockDelayInSeconds; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public String getNode() { 47 | return node; 48 | } 49 | 50 | public void setNode(String node) { 51 | this.node = node; 52 | } 53 | 54 | public List getChecks() { 55 | return checks; 56 | } 57 | 58 | public void setChecks(List checks) { 59 | this.checks = checks; 60 | } 61 | 62 | public Session.Behavior getBehavior() { 63 | return behavior; 64 | } 65 | 66 | public void setBehavior(Session.Behavior behavior) { 67 | this.behavior = behavior; 68 | } 69 | 70 | public String getTtl() { 71 | return ttl; 72 | } 73 | 74 | public void setTtl(String ttl) { 75 | this.ttl = ttl; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/query/model/QueryExecution.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.query.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | public class QueryExecution { 8 | 9 | public static class DNS { 10 | @SerializedName("TTL") 11 | private String ttl; 12 | 13 | public String getTtl() { return ttl; } 14 | 15 | public void setTtl(String ttl) { this.ttl = ttl; } 16 | 17 | @Override 18 | public String toString() { 19 | return "DNS{" + 20 | "ttl=" + ttl + 21 | '}'; 22 | } 23 | } 24 | 25 | @SerializedName("Service") 26 | private String service; 27 | 28 | @SerializedName("Nodes") 29 | private List nodes; 30 | 31 | @SerializedName("DNS") 32 | private DNS dns; 33 | 34 | @SerializedName("Datacenter") 35 | private String datacenter; 36 | 37 | @SerializedName("Failovers") 38 | private Integer failovers; 39 | 40 | public String getService() { return service; } 41 | 42 | public void setService(String service) { this.service = service; } 43 | 44 | public List getNodes() { return nodes; } 45 | 46 | public void setNodes(List nodes) { this.nodes = nodes; } 47 | 48 | public String getDatacenter() { return datacenter; } 49 | 50 | public void setDatacenter(String datacenter) { this.datacenter = datacenter; } 51 | 52 | public Integer getFailovers() { return failovers; } 53 | 54 | public void setFailovers(Integer failovers) { this.failovers = failovers; } 55 | 56 | @Override 57 | public String toString() { 58 | return "CatalogNode{" + 59 | "service=" + service + 60 | ", nodes=" + nodes + 61 | ", dns=" + dns + 62 | ", datacenter=" + datacenter + 63 | ", failovers=" + failovers + 64 | '}'; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/model/CatalogDeregistration.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class CatalogDeregistration { 9 | 10 | @SerializedName("Datacenter") 11 | private String datacenter; 12 | 13 | @SerializedName("Node") 14 | private String node; 15 | 16 | @SerializedName("CheckID") 17 | private String checkId; 18 | 19 | @SerializedName("ServiceID") 20 | private String serviceId; 21 | 22 | @SerializedName("WriteRequest") 23 | private WriteRequest writeRequest; 24 | 25 | public String getDatacenter() { 26 | return datacenter; 27 | } 28 | 29 | public void setDatacenter(String datacenter) { 30 | this.datacenter = datacenter; 31 | } 32 | 33 | public String getNode() { 34 | return node; 35 | } 36 | 37 | public void setNode(String node) { 38 | this.node = node; 39 | } 40 | 41 | public String getCheckId() { 42 | return checkId; 43 | } 44 | 45 | public void setCheckId(String checkId) { 46 | this.checkId = checkId; 47 | } 48 | 49 | public String getServiceId() { 50 | return serviceId; 51 | } 52 | 53 | public void setServiceId(String serviceId) { 54 | this.serviceId = serviceId; 55 | } 56 | 57 | public WriteRequest getWriteRequest() { 58 | return writeRequest; 59 | } 60 | 61 | public void setWriteRequest(WriteRequest writeRequest) { 62 | this.writeRequest = writeRequest; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "CatalogDeregistration{" + 68 | "datacenter='" + datacenter + '\'' + 69 | ", node='" + node + '\'' + 70 | ", checkId='" + checkId + '\'' + 71 | ", serviceId='" + serviceId + '\'' + 72 | ", writeRequest=" + writeRequest + 73 | '}'; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/acl/model/Acl.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class Acl { 9 | 10 | @SerializedName("CreateIndex") 11 | private long createIndex; 12 | 13 | @SerializedName("ModifyIndex") 14 | private long modifyIndex; 15 | 16 | @SerializedName("ID") 17 | private String id; 18 | 19 | @SerializedName("Name") 20 | private String name; 21 | 22 | @SerializedName("Type") 23 | private AclType type; 24 | 25 | @SerializedName("Rules") 26 | private String rules; 27 | 28 | public long getCreateIndex() { 29 | return createIndex; 30 | } 31 | 32 | public void setCreateIndex(long createIndex) { 33 | this.createIndex = createIndex; 34 | } 35 | 36 | public long getModifyIndex() { 37 | return modifyIndex; 38 | } 39 | 40 | public void setModifyIndex(long modifyIndex) { 41 | this.modifyIndex = modifyIndex; 42 | } 43 | 44 | public String getId() { 45 | return id; 46 | } 47 | 48 | public void setId(String id) { 49 | this.id = id; 50 | } 51 | 52 | public String getName() { 53 | return name; 54 | } 55 | 56 | public void setName(String name) { 57 | this.name = name; 58 | } 59 | 60 | public AclType getType() { 61 | return type; 62 | } 63 | 64 | public void setType(AclType type) { 65 | this.type = type; 66 | } 67 | 68 | public String getRules() { 69 | return rules; 70 | } 71 | 72 | public void setRules(String rules) { 73 | this.rules = rules; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return "Acl{" + 79 | "createIndex=" + createIndex + 80 | ", modifyIndex=" + modifyIndex + 81 | ", id='" + id + '\'' + 82 | ", name='" + name + '\'' + 83 | ", type=" + type + 84 | ", rules='" + rules + '\'' + 85 | '}'; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/coordinate/CoordinateConsulClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.coordinate; 2 | 3 | import com.ecwid.consul.json.GsonFactory; 4 | import com.ecwid.consul.transport.HttpResponse; 5 | import com.ecwid.consul.v1.ConsulRawClient; 6 | import com.ecwid.consul.v1.OperationException; 7 | import com.ecwid.consul.v1.QueryParams; 8 | import com.ecwid.consul.v1.Response; 9 | import com.ecwid.consul.v1.coordinate.model.Datacenter; 10 | import com.ecwid.consul.v1.coordinate.model.Node; 11 | import com.google.gson.reflect.TypeToken; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * @author Vasily Vasilkov (vgv@ecwid.com) 17 | */ 18 | public class CoordinateConsulClient implements CoordinateClient { 19 | 20 | private final ConsulRawClient rawClient; 21 | 22 | public CoordinateConsulClient(ConsulRawClient rawClient) { 23 | this.rawClient = rawClient; 24 | } 25 | 26 | @Override 27 | public Response> getDatacenters() { 28 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/coordinate/datacenters"); 29 | 30 | if (httpResponse.getStatusCode() == 200) { 31 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 32 | }.getType()); 33 | return new Response>(value, httpResponse); 34 | } else { 35 | throw new OperationException(httpResponse); 36 | } 37 | } 38 | 39 | @Override 40 | public Response> getNodes(QueryParams queryParams) { 41 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/coordinate/nodes", queryParams); 42 | 43 | if (httpResponse.getStatusCode() == 200) { 44 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 45 | }.getType()); 46 | return new Response>(value, httpResponse); 47 | } else { 48 | throw new OperationException(httpResponse); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/query/QueryConsulClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.query; 2 | 3 | import com.ecwid.consul.json.GsonFactory; 4 | import com.ecwid.consul.transport.HttpResponse; 5 | import com.ecwid.consul.transport.TLSConfig; 6 | import com.ecwid.consul.v1.ConsulRawClient; 7 | import com.ecwid.consul.v1.OperationException; 8 | import com.ecwid.consul.v1.QueryParams; 9 | import com.ecwid.consul.v1.Response; 10 | import com.ecwid.consul.v1.query.model.QueryExecution; 11 | 12 | public final class QueryConsulClient implements QueryClient { 13 | 14 | private final ConsulRawClient rawClient; 15 | 16 | public QueryConsulClient(ConsulRawClient rawClient) { this.rawClient = rawClient; } 17 | 18 | public QueryConsulClient() { this(new ConsulRawClient()); } 19 | 20 | public QueryConsulClient(TLSConfig tlsConfig) { this(new ConsulRawClient(tlsConfig)); } 21 | 22 | public QueryConsulClient(String agentHost) { 23 | this(new ConsulRawClient(agentHost)); 24 | } 25 | 26 | public QueryConsulClient(String agentHost, TLSConfig tlsConfig) { 27 | this(new ConsulRawClient(agentHost, tlsConfig)); 28 | } 29 | 30 | public QueryConsulClient(String agentHost, int agentPort) { 31 | this(new ConsulRawClient(agentHost, agentPort)); 32 | } 33 | 34 | public QueryConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) { 35 | this(new ConsulRawClient(agentHost, agentPort, tlsConfig)); 36 | } 37 | 38 | @Override 39 | public Response executePreparedQuery(String uuid, QueryParams queryParams) { 40 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/query/" + uuid + "/execute", queryParams); 41 | 42 | if (httpResponse.getStatusCode() == 200) { 43 | QueryExecution queryExecution = GsonFactory.getGson().fromJson(httpResponse.getContent(), QueryExecution.class); 44 | return new Response(queryExecution, httpResponse); 45 | } else { 46 | throw new OperationException(httpResponse); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/resources/ssl/ca.cert: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIFNzCCAx+gAwIBAgIEPzow5DANBgkqhkiG9w0BAQ0FADBFMQswCQYDVQQGEwJV 3 | SzEPMA0GA1UEBxMGTG9uZG9uMRIwEAYDVQQKEwlUZXN0T3JnQ0ExETAPBgNVBAMT 4 | CHRlc3Qub3JnMB4XDTE3MDkwMTA0NTQzMFoXDTI3MDgzMDA0NTQzMFowRTELMAkG 5 | A1UEBhMCVUsxDzANBgNVBAcTBkxvbmRvbjESMBAGA1UEChMJVGVzdE9yZ0NBMREw 6 | DwYDVQQDEwh0ZXN0Lm9yZzCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIB 7 | AIEm7NA3+Eq6cRyBNH2ZIfXulCdfa+c/aH0u1mgpvd8al6B1BflltUqC01woLNT9 8 | 1sfAB0XvYXelpb6GuZJ+NJzwL2YUvBMmHaKmcWaHDq/fQp5mg5wvKTIUb8/qhbdt 9 | p7v2S3lyyH4aXNN0X8JGLPjrBKTPFI6yDpYfCnxXjL6v6VC2QkxXOeMxtLLt9Xbu 10 | +RN1n+Sc9uLwF/yCNfLigF6RTCRjhj20Hn4PPJRcPra/DzDJpjBnug/MesF29MmD 11 | T8vP3lgFDPftWOwp8f2xb8tWNLwa5yzQztHOro1+TPcnXqaTJ/wn9TF6YVYOFHDb 12 | TEthOK1SBCUkC5EtAYF5qvvAraOXZlifrH5ycFq4yYXMo16rKb90XyfdpITbx7vE 13 | wrmJlXoNGR1ptopTwD84oFVSDy2a4AUEr6lAeYsQYZ0XTandz7hTxI6eX5sCk6N6 14 | aSULSnCEGo5qkoy8QVOzLxRC4m9IP2/zPdys93xRMk+Q8C8hp7w6DUQOJiWdFSa1 15 | 49ZVECmhQ9gst+fqR+CDKq1gvN/GdivMHAwkt6ekEJnyNzJ59kLXGF1r/x1CO50I 16 | WsQ3gUDbOwLzwx/zWdsjq+AlaDGqAx3QyX6QoTss7Vv/5y+CMedDNpoQLiSEldQu 17 | GtxV6yWgiQtDd+W2UfCAywhT93Paobw7ZhTInavWfdWjAgMBAAGjLzAtMAwGA1Ud 18 | EwQFMAMBAf8wHQYDVR0OBBYEFOYrMdJOZ+3g9Cp/O5+ohS2ALuOeMA0GCSqGSIb3 19 | DQEBDQUAA4ICAQBNQhPjx9dGcVnLwBnLIwHyuz32c7snCNX9dUuVuk4lJBKKVJfB 20 | hiZ8NhN3wMldLXXhzowgSDXS1ZCsCjWyCsdIDV8rk9L8jSiief9is57fx+Ejk+gM 21 | KPdFCEcurBgf9iQKctE5LUeiK9R6twcmu3fj4HlDhtwrfHR/ARZIP5Hn8ta51RIn 22 | E723tZLk52IITQsuLsvWHh7eVJdn8aVyeVVse4/SlrN55d6ENPnSokPkfhS56R4f 23 | +KJK2dD1xhm24SuoynJ7SKWGiIj2VSu/i/Iq4aerOlqvFf5v4wTY017mZ1ngnlHn 24 | M8PI7wGs4h61evXdNt24JzL5FqVRAsyhhv+DLv/z6czuA6/sGhJpE3ZZ3RreGwMm 25 | i5vnoFayGPYbhAVsEXbpYSONQkzrCMi/jF2EEdqriKBrLE25IpMgRLbqYnb7h2mh 26 | ZBB6HtbKU7Iv4e3UNjSUszOhrmoGnQjTnWv6di7ONf92QdFbBuKPoPw99LfTv8/x 27 | ro+dOib4x6mrMlFlg7KxGJ+MzUm59cUxpeCDuYanFr3Ja+rTcQpDumkC2wB/ziJc 28 | ribv3X9ZXn7JxhoyWYnItvDOSBy7G9oxts4hYOzFV3Rj+fIzlECbUftDYjpRH3nF 29 | GRZBeLI14QIn2tcCi98yc8E/1okjqklynmYNSJIvUmzn4GRS+IS4ZhiW6A== 30 | -----END CERTIFICATE----- 31 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/event/model/EventParams.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Objects; 6 | 7 | import com.ecwid.consul.UrlParameters; 8 | import com.ecwid.consul.Utils; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public class EventParams implements UrlParameters { 14 | 15 | private String name; 16 | private String service; 17 | private String tag; 18 | private String node; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getService() { 29 | return service; 30 | } 31 | 32 | public void setService(String service) { 33 | this.service = service; 34 | } 35 | 36 | public String getTag() { 37 | return tag; 38 | } 39 | 40 | public void setTag(String tag) { 41 | this.tag = tag; 42 | } 43 | 44 | public String getNode() { 45 | return node; 46 | } 47 | 48 | public void setNode(String node) { 49 | this.node = node; 50 | } 51 | 52 | @Override 53 | public List toUrlParameters() { 54 | List result = new ArrayList(); 55 | 56 | if (name != null) { 57 | result.add("name=" + Utils.encodeValue(name)); 58 | } 59 | 60 | if (service != null) { 61 | result.add("service=" + Utils.encodeValue(service)); 62 | } 63 | 64 | if (tag != null) { 65 | result.add("tag=" + Utils.encodeValue(tag)); 66 | } 67 | 68 | if (node != null) { 69 | result.add("node=" + Utils.encodeValue(node)); 70 | } 71 | 72 | return result; 73 | } 74 | 75 | @Override 76 | public boolean equals(Object o) { 77 | if (this == o) { 78 | return true; 79 | } 80 | if (!(o instanceof EventParams)) { 81 | return false; 82 | } 83 | EventParams that = (EventParams) o; 84 | return Objects.equals(name, that.name) && 85 | Objects.equals(service, that.service) && 86 | Objects.equals(tag, that.tag) && 87 | Objects.equals(node, that.node); 88 | } 89 | 90 | @Override 91 | public int hashCode() { 92 | return Objects.hash(name, service, tag, node); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/HttpRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public final class HttpRequest { 7 | 8 | private final String url; 9 | private final Map headers; 10 | 11 | private final String content; 12 | private final byte[] binaryContent; 13 | 14 | private HttpRequest(String url, Map headers, String content, byte[] binaryContent) { 15 | if (content != null && binaryContent != null) { 16 | throw new IllegalArgumentException("You should set only content or binaryContent, not both."); 17 | } 18 | 19 | this.url = url; 20 | this.headers = headers; 21 | this.content = content; 22 | this.binaryContent = binaryContent; 23 | } 24 | 25 | public String getUrl() { 26 | return url; 27 | } 28 | 29 | public Map getHeaders() { 30 | return headers; 31 | } 32 | 33 | public String getContent() { 34 | return content; 35 | } 36 | 37 | public byte[] getBinaryContent() { 38 | return binaryContent; 39 | } 40 | 41 | // --------------------------------------- 42 | // Builder 43 | public static final class Builder { 44 | private String url; 45 | private Map headers = new HashMap<>(); 46 | private String content; 47 | private byte[] binaryContent; 48 | 49 | public static Builder newBuilder() { 50 | return new Builder(); 51 | } 52 | 53 | public Builder setUrl(String url) { 54 | this.url = url; 55 | return this; 56 | } 57 | 58 | public Builder addHeaders(Map headers) { 59 | this.headers.putAll(headers); 60 | return this; 61 | } 62 | 63 | public Builder addHeader(String name, String value) { 64 | this.headers.put(name, value); 65 | return this; 66 | } 67 | 68 | public Builder setContent(String content) { 69 | this.content = content; 70 | return this; 71 | } 72 | 73 | public Builder setBinaryContent(byte[] binaryContent) { 74 | this.binaryContent = binaryContent; 75 | return this; 76 | } 77 | 78 | public HttpRequest build() { 79 | return new HttpRequest(url, headers, content, binaryContent); 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/model/CatalogNode.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author Vasily Vasilkov (vgv@ecwid.com) 10 | */ 11 | public class CatalogNode { 12 | 13 | public static class Service { 14 | @SerializedName("ID") 15 | private String id; 16 | 17 | @SerializedName("Service") 18 | private String service; 19 | 20 | @SerializedName("Tags") 21 | private List tags; 22 | 23 | @SerializedName("Port") 24 | private Integer port; 25 | 26 | public String getId() { 27 | return id; 28 | } 29 | 30 | public void setId(String id) { 31 | this.id = id; 32 | } 33 | 34 | public String getService() { 35 | return service; 36 | } 37 | 38 | public void setService(String service) { 39 | this.service = service; 40 | } 41 | 42 | public List getTags() { 43 | return tags; 44 | } 45 | 46 | public void setTags(List tags) { 47 | this.tags = tags; 48 | } 49 | 50 | public Integer getPort() { 51 | return port; 52 | } 53 | 54 | public void setPort(Integer port) { 55 | this.port = port; 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return "Service{" + 61 | "id='" + id + '\'' + 62 | ", service='" + service + '\'' + 63 | ", tags=" + tags + 64 | ", port=" + port + 65 | '}'; 66 | } 67 | } 68 | 69 | @SerializedName("Node") 70 | private Node node; 71 | 72 | @SerializedName("Services") 73 | private Map services; 74 | 75 | public Node getNode() { 76 | return node; 77 | } 78 | 79 | public void setNode(Node node) { 80 | this.node = node; 81 | } 82 | 83 | public Map getServices() { 84 | return services; 85 | } 86 | 87 | public void setServices(Map services) { 88 | this.services = services; 89 | } 90 | 91 | @Override 92 | public String toString() { 93 | return "CatalogNode{" + 94 | "node=" + node + 95 | ", services=" + services + 96 | '}'; 97 | } 98 | } 99 | 100 | 101 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/kv/model/PutParams.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv.model; 2 | 3 | import com.ecwid.consul.UrlParameters; 4 | import com.ecwid.consul.Utils; 5 | 6 | import java.math.BigInteger; 7 | import java.util.ArrayList; 8 | import java.util.Iterator; 9 | import java.util.List; 10 | import java.util.Objects; 11 | 12 | /** 13 | * @author Vasily Vasilkov (vgv@ecwid.com) 14 | */ 15 | public class PutParams implements UrlParameters { 16 | 17 | private long flags; 18 | private Long cas; 19 | private String acquireSession; 20 | private String releaseSession; 21 | 22 | public long getFlags() { 23 | return flags; 24 | } 25 | 26 | public void setFlags(long flags) { 27 | this.flags = flags; 28 | } 29 | 30 | public Long getCas() { 31 | return cas; 32 | } 33 | 34 | public void setCas(Long cas) { 35 | this.cas = cas; 36 | } 37 | 38 | public String getAcquireSession() { 39 | return acquireSession; 40 | } 41 | 42 | public void setAcquireSession(String acquireSession) { 43 | this.acquireSession = acquireSession; 44 | } 45 | 46 | public String getReleaseSession() { 47 | return releaseSession; 48 | } 49 | 50 | public void setReleaseSession(String releaseSession) { 51 | this.releaseSession = releaseSession; 52 | } 53 | 54 | @Override 55 | public List toUrlParameters() { 56 | List params = new ArrayList(); 57 | 58 | if (flags != 0) { 59 | params.add("flags=" + flags); 60 | } 61 | if (cas != null) { 62 | params.add("cas=" + cas); 63 | } 64 | if (acquireSession != null) { 65 | params.add("acquire=" + Utils.encodeValue(acquireSession)); 66 | } 67 | if (releaseSession != null) { 68 | params.add("release=" + Utils.encodeValue(releaseSession)); 69 | } 70 | 71 | return params; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object o) { 76 | if (this == o) { 77 | return true; 78 | } 79 | if (!(o instanceof PutParams)) { 80 | return false; 81 | } 82 | PutParams putParams = (PutParams) o; 83 | return flags == putParams.flags && 84 | Objects.equals(cas, putParams.cas) && 85 | Objects.equals(acquireSession, putParams.acquireSession) && 86 | Objects.equals(releaseSession, putParams.releaseSession); 87 | } 88 | 89 | @Override 90 | public int hashCode() { 91 | return Objects.hash(flags, cas, acquireSession, releaseSession); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/status/StatusConsulClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.status; 2 | 3 | import com.ecwid.consul.json.GsonFactory; 4 | import com.ecwid.consul.transport.HttpResponse; 5 | import com.ecwid.consul.transport.TLSConfig; 6 | import com.ecwid.consul.v1.ConsulRawClient; 7 | import com.ecwid.consul.v1.OperationException; 8 | import com.ecwid.consul.v1.Response; 9 | import com.google.gson.reflect.TypeToken; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * @author Vasily Vasilkov (vgv@ecwid.com) 15 | */ 16 | public final class StatusConsulClient implements StatusClient { 17 | 18 | private final ConsulRawClient rawClient; 19 | 20 | public StatusConsulClient(ConsulRawClient rawClient) { 21 | this.rawClient = rawClient; 22 | } 23 | 24 | public StatusConsulClient() { 25 | this(new ConsulRawClient()); 26 | } 27 | 28 | public StatusConsulClient(TLSConfig tlsConfig) { 29 | this(new ConsulRawClient(tlsConfig)); 30 | } 31 | 32 | public StatusConsulClient(String agentHost) { 33 | this(new ConsulRawClient(agentHost)); 34 | } 35 | 36 | public StatusConsulClient(String agentHost, TLSConfig tlsConfig) { 37 | this(new ConsulRawClient(agentHost, tlsConfig)); 38 | } 39 | 40 | public StatusConsulClient(String agentHost, int agentPort) { 41 | this(new ConsulRawClient(agentHost, agentPort)); 42 | } 43 | 44 | public StatusConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) { 45 | this(new ConsulRawClient(agentHost, agentPort, tlsConfig)); 46 | } 47 | 48 | @Override 49 | public Response getStatusLeader() { 50 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/status/leader"); 51 | 52 | if (httpResponse.getStatusCode() == 200) { 53 | String value = GsonFactory.getGson().fromJson(httpResponse.getContent(), String.class); 54 | return new Response(value, httpResponse); 55 | } else { 56 | throw new OperationException(httpResponse); 57 | } 58 | } 59 | 60 | @Override 61 | public Response> getStatusPeers() { 62 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/status/peers"); 63 | 64 | if (httpResponse.getStatusCode() == 200) { 65 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 66 | }.getType()); 67 | return new Response>(value, httpResponse); 68 | } else { 69 | throw new OperationException(httpResponse); 70 | } 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/Utils.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.URI; 5 | import java.net.URL; 6 | import java.net.URLEncoder; 7 | import java.util.*; 8 | 9 | /** 10 | * @author Vasily Vasilkov (vgv@ecwid.com) 11 | */ 12 | public class Utils { 13 | 14 | public static String encodeValue(String value) { 15 | try { 16 | return URLEncoder.encode(value, "UTF-8"); 17 | } catch (UnsupportedEncodingException e) { 18 | throw new RuntimeException("So strange - every JVM has to support UTF-8 encoding."); 19 | } 20 | } 21 | 22 | public static String encodeUrl(String str) { 23 | try { 24 | URL url = new URL(str); 25 | URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 26 | return uri.toASCIIString(); 27 | } catch (Exception e) { 28 | throw new RuntimeException("Can't encode url", e); 29 | } 30 | } 31 | 32 | public static String generateUrl(String baseUrl, UrlParameters... params) { 33 | return generateUrl(baseUrl, Arrays.asList(params)); 34 | } 35 | 36 | public static String generateUrl(String baseUrl, List params) { 37 | if (params == null) { 38 | return baseUrl; 39 | } 40 | 41 | List allParams = new ArrayList(); 42 | for (UrlParameters item : params) { 43 | if (item != null) { 44 | allParams.addAll(item.toUrlParameters()); 45 | } 46 | } 47 | 48 | // construct the whole url 49 | StringBuilder result = new StringBuilder(baseUrl); 50 | 51 | Iterator paramsIterator = allParams.iterator(); 52 | if (paramsIterator.hasNext()) { 53 | result.append("?").append(paramsIterator.next()); 54 | while (paramsIterator.hasNext()) { 55 | result.append("&").append(paramsIterator.next()); 56 | } 57 | } 58 | return result.toString(); 59 | } 60 | 61 | public static Map createTokenMap(String token) { 62 | Map headers = new HashMap<>(); 63 | headers.put("X-Consul-Token", token); 64 | return headers; 65 | } 66 | 67 | public static String toSecondsString(long waitTime) { 68 | return String.valueOf(waitTime) + "s"; 69 | } 70 | 71 | public static String assembleAgentAddress(String host, int port, String path) { 72 | String agentPath = ""; 73 | if (path != null && !path.trim().isEmpty()) { 74 | agentPath = "/" + path; 75 | } 76 | 77 | return String.format("%s:%d%s", host, port, agentPath); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/kv/model/GetBinaryValue.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv.model; 2 | 3 | import com.ecwid.consul.json.Base64TypeAdapter; 4 | import com.google.gson.annotations.JsonAdapter; 5 | import com.google.gson.annotations.SerializedName; 6 | 7 | import java.util.Arrays; 8 | 9 | /** 10 | * @author Vasily Vasilkov (vgv@ecwid.com) 11 | */ 12 | public class GetBinaryValue { 13 | 14 | @SerializedName("CreateIndex") 15 | private long createIndex; 16 | 17 | @SerializedName("ModifyIndex") 18 | private long modifyIndex; 19 | 20 | @SerializedName("LockIndex") 21 | private Long lockIndex; 22 | 23 | @SerializedName("Flags") 24 | private long flags; 25 | 26 | @SerializedName("Session") 27 | private String session; 28 | 29 | @SerializedName("Key") 30 | private String key; 31 | 32 | @SerializedName("Value") 33 | @JsonAdapter(Base64TypeAdapter.class) 34 | private byte[] value; 35 | 36 | public long getCreateIndex() { 37 | return createIndex; 38 | } 39 | 40 | public void setCreateIndex(long createIndex) { 41 | this.createIndex = createIndex; 42 | } 43 | 44 | public long getModifyIndex() { 45 | return modifyIndex; 46 | } 47 | 48 | public void setModifyIndex(long modifyIndex) { 49 | this.modifyIndex = modifyIndex; 50 | } 51 | 52 | public Long getLockIndex() { 53 | return lockIndex; 54 | } 55 | 56 | public void setLockIndex(Long lockIndex) { 57 | this.lockIndex = lockIndex; 58 | } 59 | 60 | public long getFlags() { 61 | return flags; 62 | } 63 | 64 | public void setFlags(long flags) { 65 | this.flags = flags; 66 | } 67 | 68 | public String getSession() { 69 | return session; 70 | } 71 | 72 | public void setSession(String session) { 73 | this.session = session; 74 | } 75 | 76 | public String getKey() { 77 | return key; 78 | } 79 | 80 | public void setKey(String key) { 81 | this.key = key; 82 | } 83 | 84 | public byte[] getValue() { 85 | return value; 86 | } 87 | 88 | public void setValue(byte[] value) { 89 | this.value = value; 90 | } 91 | 92 | @Override 93 | public String toString() { 94 | return "GetBinaryValue{" + 95 | "createIndex=" + createIndex + 96 | ", modifyIndex=" + modifyIndex + 97 | ", lockIndex=" + lockIndex + 98 | ", flags=" + flags + 99 | ", session='" + session + '\'' + 100 | ", key='" + key + '\'' + 101 | ", value=" + Arrays.toString(value) + 102 | '}'; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/agent/AgentClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.agent; 2 | 3 | import com.ecwid.consul.v1.Response; 4 | import com.ecwid.consul.v1.agent.model.*; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author Vasily Vasilkov (vgv@ecwid.com) 11 | */ 12 | public interface AgentClient { 13 | 14 | public Response> getAgentChecks(); 15 | 16 | public Response> getAgentServices(); 17 | 18 | public Response> getAgentMembers(); 19 | 20 | public Response getAgentSelf(); 21 | 22 | public Response getAgentSelf(String token); 23 | 24 | public Response agentSetMaintenance(boolean maintenanceEnabled); 25 | 26 | public Response agentSetMaintenance(boolean maintenanceEnabled, String reason); 27 | 28 | public Response agentJoin(String address, boolean wan); 29 | 30 | public Response agentForceLeave(String node); 31 | 32 | public Response agentCheckRegister(NewCheck newCheck); 33 | 34 | public Response agentCheckRegister(NewCheck newCheck, String token); 35 | 36 | public Response agentCheckDeregister(String checkId); 37 | 38 | public Response agentCheckDeregister(String checkId, String token); 39 | 40 | public Response agentCheckPass(String checkId); 41 | 42 | public Response agentCheckPass(String checkId, String note); 43 | 44 | public Response agentCheckPass(String checkId, String note, String token); 45 | 46 | public Response agentCheckWarn(String checkId); 47 | 48 | public Response agentCheckWarn(String checkId, String note); 49 | 50 | public Response agentCheckWarn(String checkId, String note, String token); 51 | 52 | public Response agentCheckFail(String checkId); 53 | 54 | public Response agentCheckFail(String checkId, String note); 55 | 56 | public Response agentCheckFail(String checkId, String note, String token); 57 | 58 | public Response agentServiceRegister(NewService newService); 59 | 60 | public Response agentServiceRegister(NewService newService, String token); 61 | 62 | public Response agentServiceDeregister(String serviceId); 63 | 64 | public Response agentServiceDeregister(String serviceId, String token); 65 | 66 | public Response agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled); 67 | 68 | public Response agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled, String reason); 69 | 70 | public Response agentReload(); 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/acl/AclConsulClientTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl; 2 | 3 | import com.ecwid.consul.ConsulTestConstants; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.acl.model.Acl; 6 | import com.ecwid.consul.v1.acl.model.AclType; 7 | import com.ecwid.consul.v1.acl.model.NewAcl; 8 | import com.pszymczyk.consul.ConsulProcess; 9 | import com.pszymczyk.consul.ConsulStarterBuilder; 10 | import com.pszymczyk.consul.infrastructure.Ports; 11 | import org.junit.jupiter.api.AfterEach; 12 | import org.junit.jupiter.api.BeforeEach; 13 | import org.junit.jupiter.api.Test; 14 | 15 | import static org.hamcrest.MatcherAssert.assertThat; 16 | import static org.hamcrest.Matchers.equalTo; 17 | 18 | public class AclConsulClientTest { 19 | 20 | private static final String ACL_MASTER_TOKEN = "mastertoken"; 21 | 22 | private ConsulProcess consul; 23 | private int port = Ports.nextAvailable(); 24 | 25 | private AclClient aclClient = new AclConsulClient("localhost", port); 26 | 27 | @BeforeEach 28 | public void setup() { 29 | String customConfiguration = 30 | "{ \"acl_master_token\": \"" + ACL_MASTER_TOKEN + "\"" + 31 | ", \"acl_datacenter\": \"dc-test\"" + 32 | ", \"datacenter\": \"dc-test\" }"; 33 | 34 | consul = ConsulStarterBuilder.consulStarter() 35 | .withConsulVersion(ConsulTestConstants.CONSUL_VERSION) 36 | .withHttpPort(port) 37 | .withCustomConfig(customConfiguration) 38 | .build() 39 | .start(); 40 | } 41 | 42 | @AfterEach 43 | public void cleanup() throws Exception { 44 | consul.close(); 45 | } 46 | 47 | @Test 48 | public void should_create_client_acl_token() { 49 | should_create_acl_token(AclType.CLIENT); 50 | } 51 | 52 | @Test 53 | public void should_create_management_acl_token() { 54 | should_create_acl_token(AclType.MANAGEMENT); 55 | } 56 | 57 | private void should_create_acl_token(AclType aclType) { 58 | // given 59 | NewAcl newAcl = new NewAcl(); 60 | newAcl.setName("test-acl"); 61 | newAcl.setType(aclType); 62 | newAcl.setRules(""); 63 | 64 | // when 65 | Response response = aclClient.aclCreate(newAcl, ACL_MASTER_TOKEN); 66 | String aclId = response.getValue(); 67 | 68 | // then 69 | Acl createdAcl = aclClient.getAcl(aclId).getValue(); 70 | assertThat(createdAcl.getName(), equalTo(newAcl.getName())); 71 | assertThat(createdAcl.getType(), equalTo(newAcl.getType())); 72 | assertThat(createdAcl.getRules(), equalTo(newAcl.getRules())); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/model/Node.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class Node { 11 | 12 | @SerializedName("ID") 13 | private String id; 14 | 15 | @SerializedName("Node") 16 | private String node; 17 | 18 | @SerializedName("Address") 19 | private String address; 20 | 21 | @SerializedName("Datacenter") 22 | private String datacenter; 23 | 24 | @SerializedName("TaggedAddresses") 25 | private Map taggedAddresses; 26 | 27 | @SerializedName("Meta") 28 | private Map meta; 29 | 30 | @SerializedName("CreateIndex") 31 | private Long createIndex; 32 | 33 | @SerializedName("ModifyIndex") 34 | private Long modifyIndex; 35 | 36 | public String getId() { 37 | return id; 38 | } 39 | 40 | public void setId(String id) { 41 | this.id = id; 42 | } 43 | 44 | public String getNode() { 45 | return node; 46 | } 47 | 48 | public void setNode(String node) { 49 | this.node = node; 50 | } 51 | 52 | public String getAddress() { 53 | return address; 54 | } 55 | 56 | public void setAddress(String address) { 57 | this.address = address; 58 | } 59 | 60 | public String getDatacenter() { 61 | return datacenter; 62 | } 63 | 64 | public void setDatacenter(String datacenter) { 65 | this.datacenter = datacenter; 66 | } 67 | 68 | public Map getTaggedAddresses() { 69 | return taggedAddresses; 70 | } 71 | 72 | public void setTaggedAddresses(Map taggedAddresses) { 73 | this.taggedAddresses = taggedAddresses; 74 | } 75 | 76 | public Map getMeta() { 77 | return meta; 78 | } 79 | 80 | public void setMeta(Map meta) { 81 | this.meta = meta; 82 | } 83 | 84 | public Long getCreateIndex() { 85 | return createIndex; 86 | } 87 | 88 | public void setCreateIndex(Long createIndex) { 89 | this.createIndex = createIndex; 90 | } 91 | 92 | public Long getModifyIndex() { 93 | return modifyIndex; 94 | } 95 | 96 | public void setModifyIndex(Long modifyIndex) { 97 | this.modifyIndex = modifyIndex; 98 | } 99 | 100 | @Override 101 | public String toString() { 102 | return "Node{" + 103 | "id='" + id + '\'' + 104 | ", node='" + node + '\'' + 105 | ", address='" + address + '\'' + 106 | ", datacenter='" + datacenter + '\'' + 107 | ", taggedAddresses=" + taggedAddresses + 108 | ", meta=" + meta + 109 | ", createIndex=" + createIndex + 110 | ", modifyIndex=" + modifyIndex + 111 | '}'; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/Request.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.UrlParameters; 4 | import com.ecwid.consul.transport.HttpRequest; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | public final class Request { 10 | 11 | private final String endpoint; 12 | private final List urlParameters; 13 | 14 | private final String content; 15 | private final byte[] binaryContent; 16 | 17 | private final String token; 18 | 19 | private Request(String endpoint, List urlParameters, String content, byte[] binaryContent, String token) { 20 | if (content != null && binaryContent != null) { 21 | throw new IllegalArgumentException("You should set only content or binaryContent, not both."); 22 | } 23 | 24 | this.endpoint = endpoint; 25 | this.urlParameters = urlParameters; 26 | this.content = content; 27 | this.binaryContent = binaryContent; 28 | this.token = token; 29 | } 30 | 31 | public String getEndpoint() { 32 | return endpoint; 33 | } 34 | 35 | public List getUrlParameters() { 36 | return urlParameters; 37 | } 38 | 39 | public String getContent() { 40 | return content; 41 | } 42 | 43 | public byte[] getBinaryContent() { 44 | return binaryContent; 45 | } 46 | 47 | public String getToken() { 48 | return token; 49 | } 50 | 51 | // ------------------------------- 52 | // Builder 53 | public static class Builder { 54 | private String endpoint; 55 | private List urlParameters = new ArrayList<>(); 56 | 57 | private String content; 58 | private byte[] binaryContent; 59 | 60 | private String token; 61 | 62 | public static Builder newBuilder() { 63 | return new Builder(); 64 | } 65 | 66 | public Builder setEndpoint(String endpoint) { 67 | this.endpoint = endpoint; 68 | return this; 69 | } 70 | 71 | public Builder addUrlParameters(List urlParameters) { 72 | this.urlParameters.addAll(urlParameters); 73 | return this; 74 | } 75 | 76 | public Builder addUrlParameter(UrlParameters urlParameter) { 77 | this.urlParameters.add(urlParameter); 78 | return this; 79 | } 80 | 81 | public Builder setContent(String content) { 82 | this.content = content; 83 | return this; 84 | } 85 | 86 | public Builder setBinaryContent(byte[] binaryContent) { 87 | this.binaryContent = binaryContent; 88 | return this; 89 | } 90 | 91 | public Builder setToken(String token) { 92 | this.token = token; 93 | return this; 94 | } 95 | 96 | public Request build() { 97 | return new Request(endpoint, urlParameters, content, binaryContent, token); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/kv/model/GetValue.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.nio.charset.Charset; 6 | import java.util.Base64; 7 | 8 | /** 9 | * @author Vasily Vasilkov (vgv@ecwid.com) 10 | */ 11 | public class GetValue { 12 | 13 | private static final Charset UTF_8 = Charset.forName("UTF-8"); 14 | 15 | @SerializedName("CreateIndex") 16 | private long createIndex; 17 | 18 | @SerializedName("ModifyIndex") 19 | private long modifyIndex; 20 | 21 | @SerializedName("LockIndex") 22 | private Long lockIndex; 23 | 24 | @SerializedName("Flags") 25 | private long flags; 26 | 27 | @SerializedName("Session") 28 | private String session; 29 | 30 | @SerializedName("Key") 31 | private String key; 32 | 33 | @SerializedName("Value") 34 | private String value; 35 | 36 | public long getCreateIndex() { 37 | return createIndex; 38 | } 39 | 40 | public void setCreateIndex(long createIndex) { 41 | this.createIndex = createIndex; 42 | } 43 | 44 | public long getModifyIndex() { 45 | return modifyIndex; 46 | } 47 | 48 | public void setModifyIndex(long modifyIndex) { 49 | this.modifyIndex = modifyIndex; 50 | } 51 | 52 | public Long getLockIndex() { 53 | return lockIndex; 54 | } 55 | 56 | public void setLockIndex(Long lockIndex) { 57 | this.lockIndex = lockIndex; 58 | } 59 | 60 | public long getFlags() { 61 | return flags; 62 | } 63 | 64 | public void setFlags(long flags) { 65 | this.flags = flags; 66 | } 67 | 68 | public String getSession() { 69 | return session; 70 | } 71 | 72 | public void setSession(String session) { 73 | this.session = session; 74 | } 75 | 76 | public String getKey() { 77 | return key; 78 | } 79 | 80 | public void setKey(String key) { 81 | this.key = key; 82 | } 83 | 84 | public String getValue() { 85 | return value; 86 | } 87 | 88 | public void setValue(String value) { 89 | this.value = value; 90 | } 91 | 92 | public String getDecodedValue(Charset charset) { 93 | if (value == null) { 94 | return null; 95 | } 96 | if (charset == null) { 97 | charset = UTF_8; 98 | } 99 | return new String(Base64.getDecoder().decode(value), charset); 100 | } 101 | 102 | public String getDecodedValue() { 103 | return getDecodedValue(UTF_8); 104 | } 105 | 106 | @Override 107 | public String toString() { 108 | return "GetValue{" + 109 | "createIndex=" + createIndex + 110 | ", modifyIndex=" + modifyIndex + 111 | ", lockIndex=" + lockIndex + 112 | ", flags=" + flags + 113 | ", session='" + session + '\'' + 114 | ", key='" + key + '\'' + 115 | ", value='" + value + '\'' + 116 | '}'; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/ConsulRawClientTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | import com.ecwid.consul.v1.ConsulRawClient; 4 | import com.ecwid.consul.v1.QueryParams; 5 | import org.apache.http.client.HttpClient; 6 | import org.apache.http.client.ResponseHandler; 7 | import org.apache.http.client.methods.HttpUriRequest; 8 | import org.junit.jupiter.api.Test; 9 | import org.mockito.ArgumentCaptor; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | import static org.mockito.ArgumentMatchers.any; 13 | import static org.mockito.Mockito.mock; 14 | import static org.mockito.Mockito.verify; 15 | 16 | public class ConsulRawClientTest { 17 | 18 | private static final String ENDPOINT = "/any/endpoint"; 19 | private static final QueryParams EMPTY_QUERY_PARAMS = QueryParams.Builder.builder().build(); 20 | private static final String HOST = "host"; 21 | private static final int PORT = 8888; 22 | private static final String PATH = "path"; 23 | private static final String EXPECTED_AGENT_ADDRESS_NO_PATH = "http://" + HOST + ":"+ PORT + ENDPOINT; 24 | private static final String EXPECTED_AGENT_ADDRESS = "http://" + HOST + ":"+ PORT + "/" + PATH + ENDPOINT; 25 | 26 | @Test 27 | public void verifyDefaultUrl() throws Exception { 28 | // Given 29 | HttpClient httpClient = mock(HttpClient.class); 30 | ConsulRawClient client = ConsulRawClient.Builder.builder() 31 | .setHttpClient(httpClient) 32 | .setHost(HOST) 33 | .setPort(PORT) 34 | .build(); 35 | 36 | // When 37 | client.makeGetRequest(ENDPOINT, EMPTY_QUERY_PARAMS); 38 | 39 | // Then 40 | ArgumentCaptor calledUri = ArgumentCaptor.forClass(HttpUriRequest.class); 41 | verify(httpClient).execute(calledUri.capture(), any(ResponseHandler.class)); 42 | assertEquals(EXPECTED_AGENT_ADDRESS_NO_PATH, calledUri.getValue().getURI().toString()); 43 | } 44 | 45 | @Test 46 | public void verifyUrlWithPath() throws Exception { 47 | // Given 48 | HttpClient httpClient = mock(HttpClient.class); 49 | ConsulRawClient client = ConsulRawClient.Builder.builder() 50 | .setHttpClient(httpClient) 51 | .setHost(HOST) 52 | .setPort(PORT) 53 | .setPath(PATH) 54 | .build(); 55 | 56 | // When 57 | client.makeGetRequest(ENDPOINT, EMPTY_QUERY_PARAMS); 58 | 59 | // Then 60 | ArgumentCaptor calledUri = ArgumentCaptor.forClass(HttpUriRequest.class); 61 | verify(httpClient).execute(calledUri.capture(), any(ResponseHandler.class)); 62 | assertEquals(EXPECTED_AGENT_ADDRESS, calledUri.getValue().getURI().toString()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/agent/model/Service.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.agent.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author Vasily Vasilkov (vgv@ecwid.com) 10 | */ 11 | public class Service { 12 | 13 | @SerializedName("ID") 14 | private String id; 15 | 16 | @SerializedName("Service") 17 | private String service; 18 | 19 | @SerializedName("Tags") 20 | private List tags; 21 | 22 | @SerializedName("Address") 23 | private String address; 24 | 25 | @SerializedName("Meta") 26 | private Map meta; 27 | 28 | @SerializedName("Port") 29 | private Integer port; 30 | 31 | @SerializedName("EnableTagOverride") 32 | private Boolean enableTagOverride; 33 | 34 | @SerializedName("CreateIndex") 35 | private Long createIndex; 36 | 37 | @SerializedName("ModifyIndex") 38 | private Long modifyIndex; 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getService() { 49 | return service; 50 | } 51 | 52 | public void setService(String service) { 53 | this.service = service; 54 | } 55 | 56 | public List getTags() { 57 | return tags; 58 | } 59 | 60 | public void setTags(List tags) { 61 | this.tags = tags; 62 | } 63 | 64 | public String getAddress() { 65 | return address; 66 | } 67 | 68 | public void setAddress(String address) { 69 | this.address = address; 70 | } 71 | 72 | public Map getMeta() { 73 | return meta; 74 | } 75 | 76 | public void setMeta(Map meta) { 77 | this.meta = meta; 78 | } 79 | 80 | public Integer getPort() { 81 | return port; 82 | } 83 | 84 | public void setPort(Integer port) { 85 | this.port = port; 86 | } 87 | 88 | public Boolean getEnableTagOverride() { 89 | return enableTagOverride; 90 | } 91 | 92 | public void setEnableTagOverride(Boolean enableTagOverride) { 93 | this.enableTagOverride = enableTagOverride; 94 | } 95 | 96 | public Long getCreateIndex() { 97 | return createIndex; 98 | } 99 | 100 | public void setCreateIndex(Long createIndex) { 101 | this.createIndex = createIndex; 102 | } 103 | 104 | public Long getModifyIndex() { 105 | return modifyIndex; 106 | } 107 | 108 | public void setModifyIndex(Long modifyIndex) { 109 | this.modifyIndex = modifyIndex; 110 | } 111 | 112 | @Override 113 | public String toString() { 114 | return "Service{" + 115 | "id='" + id + '\'' + 116 | ", service='" + service + '\'' + 117 | ", tags=" + tags + 118 | ", address='" + address + '\'' + 119 | ", meta=" + meta + 120 | ", port=" + port + 121 | ", enableTagOverride=" + enableTagOverride + 122 | ", createIndex=" + createIndex + 123 | ", modifyIndex=" + modifyIndex + 124 | '}'; 125 | } 126 | } -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/session/model/Session.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.session.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class Session { 11 | 12 | public static enum Behavior { 13 | @SerializedName("release") 14 | RELEASE, 15 | 16 | @SerializedName("delete") 17 | DELETE 18 | } 19 | 20 | @SerializedName("LockDelay") 21 | private long lockDelay; 22 | 23 | @SerializedName("Checks") 24 | private List checks; 25 | 26 | @SerializedName("Node") 27 | private String node; 28 | 29 | @SerializedName("ID") 30 | private String id; 31 | 32 | @SerializedName("Name") 33 | private String name; 34 | 35 | @SerializedName("CreateIndex") 36 | private long createIndex; 37 | 38 | @SerializedName("ModifyIndex") 39 | private long modifyIndex; 40 | 41 | @SerializedName("TTL") 42 | private String ttl; 43 | 44 | @SerializedName("Behavior") 45 | private Behavior behavior; 46 | 47 | public long getLockDelay() { 48 | return lockDelay; 49 | } 50 | 51 | public void setLockDelay(long lockDelay) { 52 | this.lockDelay = lockDelay; 53 | } 54 | 55 | public List getChecks() { 56 | return checks; 57 | } 58 | 59 | public void setChecks(List checks) { 60 | this.checks = checks; 61 | } 62 | 63 | public String getNode() { 64 | return node; 65 | } 66 | 67 | public void setNode(String node) { 68 | this.node = node; 69 | } 70 | 71 | public String getId() { 72 | return id; 73 | } 74 | 75 | public void setId(String id) { 76 | this.id = id; 77 | } 78 | 79 | public String getName() { 80 | return name; 81 | } 82 | 83 | public void setName(String name) { 84 | this.name = name; 85 | } 86 | 87 | public long getCreateIndex() { 88 | return createIndex; 89 | } 90 | 91 | public void setCreateIndex(long createIndex) { 92 | this.createIndex = createIndex; 93 | } 94 | 95 | public long getModifyIndex() { 96 | return modifyIndex; 97 | } 98 | 99 | public void setModifyIndex(long modifyIndex) { 100 | this.modifyIndex = modifyIndex; 101 | } 102 | 103 | public String getTtl() { 104 | return ttl; 105 | } 106 | 107 | public void setTtl(String ttl) { 108 | this.ttl = ttl; 109 | } 110 | 111 | public Behavior getBehavior() { 112 | return behavior; 113 | } 114 | 115 | public void setBehavior(Behavior behavior) { 116 | this.behavior = behavior; 117 | } 118 | 119 | @Override 120 | public String toString() { 121 | return "Session{" + 122 | "lockDelay=" + lockDelay + 123 | ", checks=" + checks + 124 | ", node='" + node + '\'' + 125 | ", id='" + id + '\'' + 126 | ", name='" + name + '\'' + 127 | ", createIndex=" + createIndex + 128 | ", modifyIndex=" + modifyIndex + 129 | ", ttl='" + ttl + '\'' + 130 | ", behavior=" + behavior + 131 | '}'; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/UtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | public class UtilsTest { 8 | 9 | @Test 10 | public void testEncodeUrl() throws Exception { 11 | String uri = "http://example.com/path with spaces"; 12 | String expected = "http://example.com/path%20with%20spaces"; 13 | 14 | assertEquals(expected, Utils.encodeUrl(uri)); 15 | } 16 | 17 | @Test 18 | public void testGenerateUrl_Simple() throws Exception { 19 | assertEquals("/some-url", Utils.generateUrl("/some-url")); 20 | assertEquals("/some-url", Utils.generateUrl("/some-url", (UrlParameters) null)); 21 | assertEquals("/some-url", Utils.generateUrl("/some-url", null, null)); 22 | } 23 | 24 | @Test 25 | public void testGenerateUrl_Parametrized() throws Exception { 26 | UrlParameters first = new SingleUrlParameters("key", "value"); 27 | UrlParameters second = new SingleUrlParameters("key2"); 28 | assertEquals("/some-url?key=value&key2", Utils.generateUrl("/some-url", first, second)); 29 | } 30 | 31 | @Test 32 | public void testGenerateUrl_Encoded() throws Exception { 33 | UrlParameters first = new SingleUrlParameters("key", "value value"); 34 | UrlParameters second = new SingleUrlParameters("key2"); 35 | UrlParameters third = new SingleUrlParameters("key3", "value!value"); 36 | assertEquals("/some-url?key=value+value&key2&key3=value%21value", Utils.generateUrl("/some-url", first, second, third)); 37 | } 38 | 39 | @Test 40 | public void testToSecondsString() throws Exception { 41 | assertEquals("1000s", Utils.toSecondsString(1000L)); 42 | } 43 | 44 | @Test 45 | public void testAssembleAgentAddressWithPath() { 46 | // Given 47 | String expectedHost = "http://host"; 48 | int expectedPort = 8888; 49 | String expectedPath = "path"; 50 | 51 | // When 52 | String actualAddress = Utils.assembleAgentAddress(expectedHost, expectedPort, expectedPath); 53 | 54 | // Then 55 | assertEquals( 56 | String.format("%s:%d/%s", expectedHost, expectedPort, expectedPath), 57 | actualAddress 58 | ); 59 | } 60 | 61 | @Test 62 | public void testAssembleAgentAddressWithEmptyPath() { 63 | // Given 64 | String expectedHost = "http://host"; 65 | int expectedPort = 8888; 66 | String expectedPath = " "; 67 | 68 | // When 69 | String actualAddress = Utils.assembleAgentAddress(expectedHost, expectedPort, expectedPath); 70 | 71 | // Then 72 | assertEquals( 73 | String.format("%s:%d", expectedHost, expectedPort), 74 | actualAddress 75 | ); 76 | } 77 | 78 | @Test 79 | public void testAssembleAgentAddressWithoutPath() { 80 | // Given 81 | String expectedHost = "https://host"; 82 | int expectedPort = 8888; 83 | 84 | // When 85 | String actualAddress = Utils.assembleAgentAddress(expectedHost, expectedPort, null); 86 | 87 | // Then 88 | assertEquals( 89 | String.format("%s:%d", expectedHost, expectedPort), 90 | actualAddress 91 | ); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/event/EventConsulClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event; 2 | 3 | import com.ecwid.consul.json.GsonFactory; 4 | import com.ecwid.consul.transport.HttpResponse; 5 | import com.ecwid.consul.transport.TLSConfig; 6 | import com.ecwid.consul.v1.ConsulRawClient; 7 | import com.ecwid.consul.v1.OperationException; 8 | import com.ecwid.consul.v1.QueryParams; 9 | import com.ecwid.consul.v1.Response; 10 | import com.ecwid.consul.v1.event.model.Event; 11 | import com.ecwid.consul.v1.event.model.EventParams; 12 | import com.google.gson.reflect.TypeToken; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * @author Vasily Vasilkov (vgv@ecwid.com) 18 | */ 19 | public final class EventConsulClient implements EventClient { 20 | 21 | private final ConsulRawClient rawClient; 22 | 23 | public EventConsulClient(ConsulRawClient rawClient) { 24 | this.rawClient = rawClient; 25 | } 26 | 27 | public EventConsulClient() { 28 | this(new ConsulRawClient()); 29 | } 30 | 31 | public EventConsulClient(TLSConfig tlsConfig) { 32 | this(new ConsulRawClient(tlsConfig)); 33 | } 34 | 35 | public EventConsulClient(String agentHost) { 36 | this(new ConsulRawClient(agentHost)); 37 | } 38 | 39 | public EventConsulClient(String agentHost, TLSConfig tlsConfig) { 40 | this(new ConsulRawClient(agentHost, tlsConfig)); 41 | } 42 | 43 | public EventConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) { 44 | this(new ConsulRawClient(agentHost, agentPort, tlsConfig)); 45 | } 46 | 47 | @Override 48 | public Response eventFire(String event, String payload, EventParams eventParams, QueryParams queryParams) { 49 | HttpResponse httpResponse = rawClient.makePutRequest("/v1/event/fire/" + event, payload, eventParams, queryParams); 50 | 51 | if (httpResponse.getStatusCode() == 200) { 52 | Event value = GsonFactory.getGson().fromJson(httpResponse.getContent(), Event.class); 53 | return new Response(value, httpResponse); 54 | } else { 55 | throw new OperationException(httpResponse); 56 | } 57 | } 58 | 59 | @Override 60 | public Response> eventList(QueryParams queryParams) { 61 | return eventList(null, queryParams); 62 | } 63 | 64 | @Override 65 | public Response> eventList(String event, QueryParams queryParams) { 66 | EventListRequest request = EventListRequest.newBuilder() 67 | .setName(event) 68 | .setQueryParams(queryParams) 69 | .build(); 70 | 71 | return eventList(request); 72 | } 73 | 74 | @Override 75 | public Response> eventList(EventListRequest eventListRequest) { 76 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/event/list", eventListRequest.asUrlParameters()); 77 | 78 | if (httpResponse.getStatusCode() == 200) { 79 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 80 | }.getType()); 81 | return new Response>(value, httpResponse); 82 | } else { 83 | throw new OperationException(httpResponse); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/kv/KeyValueConsulClientTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv; 2 | 3 | import com.ecwid.consul.ConsulTestConstants; 4 | import com.pszymczyk.consul.ConsulProcess; 5 | import com.pszymczyk.consul.ConsulStarterBuilder; 6 | import com.pszymczyk.consul.infrastructure.Ports; 7 | import org.apache.commons.lang.math.RandomUtils; 8 | import org.junit.jupiter.api.AfterEach; 9 | import org.junit.jupiter.api.Assertions; 10 | import org.junit.jupiter.api.BeforeEach; 11 | import org.junit.jupiter.api.Test; 12 | 13 | import java.util.Random; 14 | 15 | class KeyValueConsulClientTest { 16 | 17 | private static final Random rnd = new Random(); 18 | 19 | private ConsulProcess consul; 20 | private int port = Ports.nextAvailable(); 21 | 22 | private KeyValueConsulClient consulClient = new KeyValueConsulClient("localhost", port); 23 | 24 | @BeforeEach 25 | void setUp() { 26 | consul = ConsulStarterBuilder.consulStarter() 27 | .withConsulVersion(ConsulTestConstants.CONSUL_VERSION) 28 | .withHttpPort(port) 29 | .build() 30 | .start(); 31 | } 32 | 33 | @AfterEach 34 | void tearDown() { 35 | consul.close(); 36 | } 37 | 38 | @Test 39 | void testSetKVBinaryValue() throws Exception { 40 | final String testKey = "test_key"; 41 | final byte[] testValue = new byte[100]; 42 | rnd.nextBytes(testValue); 43 | 44 | // Make sure there is no such key before test running 45 | Assertions.assertNull(consulClient.getKVValue(testKey).getValue()); 46 | // Set the key 47 | consulClient.setKVBinaryValue(testKey, testValue); 48 | // Make sure test key exists 49 | Assertions.assertArrayEquals(consulClient.getKVBinaryValue(testKey).getValue().getValue(), testValue); 50 | } 51 | 52 | @Test 53 | void testDeleteKvValue() throws Exception { 54 | final String testKey = "test_key"; 55 | final String testValue = "test_value"; 56 | 57 | // Make sure there is no such key before test running 58 | Assertions.assertNull(consulClient.getKVValue(testKey).getValue()); 59 | 60 | // Set the key 61 | consulClient.setKVValue(testKey, testValue); 62 | // Make sure test key exists 63 | Assertions.assertEquals(consulClient.getKVValue(testKey).getValue().getDecodedValue(), testValue); 64 | 65 | // Delete key 66 | consulClient.deleteKVValue(testKey); 67 | // Make sure there is no such key before test running 68 | Assertions.assertNull(consulClient.getKVValue(testKey).getValue()); 69 | } 70 | 71 | @Test 72 | void testDeleteKvValues() throws Exception { 73 | final String testKeyPrefix = "test_key"; 74 | final String testValue = "test_value"; 75 | 76 | // Prepare test keys under testKeyPrefix prefix 77 | for (int i = 0; i < 10; i++) { 78 | final String testKey = testKeyPrefix + "/" + i; 79 | // Make sure there is no such key before test running 80 | Assertions.assertNull(consulClient.getKVValue(testKey).getValue()); 81 | 82 | // Set the key 83 | consulClient.setKVValue(testKey, testValue); 84 | 85 | // Make sure test key exists 86 | Assertions.assertEquals(consulClient.getKVValue(testKey).getValue().getDecodedValue(), testValue); 87 | } 88 | 89 | // Delete all keys in single shot 90 | consulClient.deleteKVValues(testKeyPrefix); 91 | 92 | // Make sure all keys have been deleted 93 | Assertions.assertNull(consulClient.getKVKeysOnly(testKeyPrefix).getValue()); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/test/resources/generate-ssl-cert.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | source $1 4 | 5 | #CA Options 6 | DNAME_CA="CN=test.org,O=TestOrgCA,L=London,C=UK" 7 | CA_KEY_PASSWORD="change_me" 8 | CA_KEY_STORE_PASSWORD="change_me" 9 | CA_KEY_STORE="ca.keyStore.jks" 10 | CA_TRUST_STORE="ca.trustStore.jks" 11 | CA_TRUST_STORE_PASSWORD="change_me" 12 | CA_CERT="ca.cert" 13 | CA_KEY="custom-ca" 14 | 15 | # Client Options 16 | DNAME="CN=localBox,OU=OU,O=TestOrg,L=London,ST=Unknown,C=UK" 17 | SAN=dns:localhost,ip:127.0.0.1,ip:::1 18 | KEY_STORE="keyStore.jks" 19 | TRUST_STORE="trustStore.jks" 20 | KEY_ALIAS="key" 21 | KEY_PASSWORD="change_me" 22 | KEY_STORE_PASSWORD="change_me" 23 | TRUST_STORE_PASSWORD="change_me" 24 | CLIENT_CERT="client.p12" 25 | 26 | function generate_certificate { 27 | # Make Dir if not exist 28 | mkdir -p ssl 29 | 30 | cd ssl 31 | 32 | rm $KEY_STORE $CA_CERT $TRUST_STORE $CA_KEY_STORE $CA_TRUST_STORE $CLIENT_CERT 33 | 34 | # generate cert authority 35 | echo "Generating CA Cert" 36 | keytool -genkey -alias $CA_KEY -ext BC=ca:true -dname "$DNAME_CA" -keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass "$CA_KEY_PASSWORD" -validity 3650 -keystore "$CA_KEY_STORE" -storepass "$CA_KEY_STORE_PASSWORD" 37 | keytool -export -alias $CA_KEY -file "$CA_CERT" -dname "$DNAME_CA" -rfc -keystore "$CA_KEY_STORE" -storepass "$CA_KEY_STORE_PASSWORD" 38 | keytool -import -trustcacerts -noprompt -alias $CA_KEY -file "$CA_CERT" -keystore $CA_TRUST_STORE -storepass "$CA_TRUST_STORE_PASSWORD" 39 | 40 | # generate host keystore 41 | 42 | echo "Generate Client Key" 43 | keytool -genkey -alias "$KEY_ALIAS" -dname "$DNAME" -keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass "$KEY_PASSWORD" -validity 3650 -keystore "$KEY_STORE" -storepass "$KEY_STORE_PASSWORD" 44 | keytool -certreq -alias "$KEY_ALIAS" -ext BC=ca:true -ext "SAN=$SAN" -keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass "$KEY_PASSWORD" -validity 3650 -keystore "$KEY_STORE" -storepass "$KEY_STORE_PASSWORD" -file request.csr 45 | keytool -gencert -alias $CA_KEY -validity 3650 -sigalg SHA512withRSA -ext "SAN=$SAN" -infile request.csr -outfile response.crt -rfc -keypass "$CA_KEY_PASSWORD" -keystore "$CA_KEY_STORE" -storepass "$CA_KEY_STORE_PASSWORD" 46 | keytool -import -trustcacerts -noprompt -alias $CA_KEY -file "$CA_CERT" -keystore "$KEY_STORE" -storepass "$KEY_STORE_PASSWORD" 47 | keytool -import -trustcacerts -alias "$KEY_ALIAS" -file response.crt -keypass "$KEY_PASSWORD" -keystore "$KEY_STORE" -storepass "$KEY_STORE_PASSWORD" 48 | keytool -import -trustcacerts -noprompt -alias $CA_KEY -file "$CA_CERT" -keystore "$TRUST_STORE" -storepass "$TRUST_STORE_PASSWORD" 49 | rm request.csr response.crt 50 | 51 | echo "Generate Client Cert" 52 | keytool -importkeystore -srckeystore "$KEY_STORE" -srcalias "$KEY_ALIAS" -srcstorepass "$KEY_PASSWORD" -destkeystore "$CLIENT_CERT" -deststoretype PKCS12 -deststorepass "$KEY_PASSWORD" 53 | 54 | openssl pkcs12 -in "$CLIENT_CERT" -nokeys -out key.crt -passin pass:"$KEY_PASSWORD" 55 | openssl pkcs12 -in "$CLIENT_CERT" -nodes -nocerts -out key.key -passin pass:"$KEY_PASSWORD" 56 | } 57 | 58 | 59 | echo "Checking if certificate are already generated or not?" 60 | 61 | if [ ! -f ssl/keyStore.jks ]; then 62 | generate_certificate 63 | else 64 | echo "No need to create certificate" 65 | fi 66 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/event/model/Event.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * @author Vasily Vasilkov (vgv@ecwid.com) 7 | */ 8 | public class Event { 9 | 10 | @SerializedName("ID") 11 | private String id; 12 | 13 | @SerializedName("Name") 14 | private String name; 15 | 16 | @SerializedName("Payload") 17 | private String payload; 18 | 19 | @SerializedName("NodeFilter") 20 | private String nodeFilter; 21 | 22 | @SerializedName("ServiceFilter") 23 | private String serviceFilter; 24 | 25 | @SerializedName("TagFilter") 26 | private String tagFilter; 27 | 28 | @SerializedName("Version") 29 | private int version; 30 | 31 | @SerializedName("LTime") 32 | private int lTime; 33 | 34 | public String getId() { 35 | return id; 36 | } 37 | 38 | public void setId(String id) { 39 | this.id = id; 40 | } 41 | 42 | public String getName() { 43 | return name; 44 | } 45 | 46 | public void setName(String name) { 47 | this.name = name; 48 | } 49 | 50 | public String getPayload() { 51 | return payload; 52 | } 53 | 54 | public void setPayload(String payload) { 55 | this.payload = payload; 56 | } 57 | 58 | public String getNodeFilter() { 59 | return nodeFilter; 60 | } 61 | 62 | public void setNodeFilter(String nodeFilter) { 63 | this.nodeFilter = nodeFilter; 64 | } 65 | 66 | public String getServiceFilter() { 67 | return serviceFilter; 68 | } 69 | 70 | public void setServiceFilter(String serviceFilter) { 71 | this.serviceFilter = serviceFilter; 72 | } 73 | 74 | public String getTagFilter() { 75 | return tagFilter; 76 | } 77 | 78 | public void setTagFilter(String tagFilter) { 79 | this.tagFilter = tagFilter; 80 | } 81 | 82 | public int getVersion() { 83 | return version; 84 | } 85 | 86 | public void setVersion(int version) { 87 | this.version = version; 88 | } 89 | 90 | public int getlTime() { 91 | return lTime; 92 | } 93 | 94 | public void setlTime(int lTime) { 95 | this.lTime = lTime; 96 | } 97 | 98 | /** 99 | * Converted from https://github.com/hashicorp/consul/blob/master/api/event.go#L90-L104 100 | * This is a hack. It simulates the index generation to convert an event ID into a WaitIndex. 101 | * 102 | * @return a Wait Index value suitable for passing in to {@link com.ecwid.consul.v1.QueryParams} 103 | * for blocking eventList calls. 104 | */ 105 | public long getWaitIndex() { 106 | if (id == null || id.length() != 36) { 107 | return 0; 108 | } 109 | long lower = 0, upper = 0; 110 | for (int i = 0; i < 18; i++) { 111 | if (i != 8 && i != 13) { 112 | lower = lower * 16 + Character.digit(id.charAt(i), 16); 113 | } 114 | } 115 | for (int i = 19; i < 36; i++) { 116 | if (i != 23) { 117 | upper = upper * 16 + Character.digit(id.charAt(i), 16); 118 | } 119 | } 120 | return lower ^ upper; 121 | } 122 | 123 | @Override 124 | public String toString() { 125 | return "Event{" + 126 | "id='" + id + '\'' + 127 | ", name='" + name + '\'' + 128 | ", payload='" + payload + '\'' + 129 | ", nodeFilter='" + nodeFilter + '\'' + 130 | ", serviceFilter='" + serviceFilter + '\'' + 131 | ", tagFilter='" + tagFilter + '\'' + 132 | ", version=" + version + 133 | ", lTime=" + lTime + 134 | '}'; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/health/HealthClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.catalog.CatalogNodesRequest; 6 | import com.ecwid.consul.v1.health.model.Check; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public interface HealthClient { 14 | 15 | public Response> getHealthChecksForNode(String nodeName, QueryParams queryParams); 16 | 17 | // ------------------------------------------------------------------------------- 18 | 19 | /** 20 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest)} 21 | */ 22 | @Deprecated 23 | public Response> getHealthChecksForService(String serviceName, QueryParams queryParams); 24 | 25 | public Response> getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest); 26 | 27 | // ------------------------------------------------------------------------------- 28 | 29 | /** 30 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest)} 31 | */ 32 | @Deprecated 33 | public Response> getHealthServices(String serviceName, boolean onlyPassing, QueryParams queryParams); 34 | 35 | /** 36 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest)} 37 | */ 38 | @Deprecated 39 | public Response> getHealthServices(String serviceName, String tag, boolean onlyPassing, QueryParams queryParams); 40 | 41 | /** 42 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest)} 43 | */ 44 | @Deprecated 45 | public Response> getHealthServices(String serviceName, boolean onlyPassing, QueryParams queryParams, String token); 46 | 47 | /** 48 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest)} 49 | */ 50 | @Deprecated 51 | public Response> getHealthServices(String serviceName, String tag, boolean onlyPassing, QueryParams queryParams, String token); 52 | 53 | /** 54 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest)} 55 | */ 56 | @Deprecated 57 | public Response> getHealthServices(String serviceName, String[] tags, boolean onlyPassing, QueryParams queryParams, String token); 58 | 59 | public Response> getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest); 60 | 61 | // ------------------------------------------------------------------------------- 62 | 63 | public Response> getHealthChecksState(QueryParams queryParams); 64 | 65 | public Response> getHealthChecksState(Check.CheckStatus checkStatus, QueryParams queryParams); 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/agent/model/Member.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.agent.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author Vasily Vasilkov (vgv@ecwid.com) 10 | */ 11 | public class Member { 12 | 13 | @SerializedName("Name") 14 | private String name; 15 | 16 | @SerializedName("Addr") 17 | private String address; 18 | 19 | @SerializedName("Port") 20 | private Integer port; 21 | 22 | @SerializedName("Tags") 23 | private Map tags; 24 | 25 | @SerializedName("Status") 26 | private int status; 27 | 28 | @SerializedName("ProtocolMin") 29 | private int protocolMin; 30 | 31 | @SerializedName("ProtocolMax") 32 | private int protocolMax; 33 | 34 | @SerializedName("ProtocolCur") 35 | private int protocolCur; 36 | 37 | @SerializedName("DelegateMin") 38 | private int delegateMin; 39 | 40 | @SerializedName("DelegateMax") 41 | private int delegateMax; 42 | 43 | @SerializedName("DelegateCur") 44 | private int delegateCur; 45 | 46 | public String getName() { 47 | return name; 48 | } 49 | 50 | public void setName(String name) { 51 | this.name = name; 52 | } 53 | 54 | public String getAddress() { 55 | return address; 56 | } 57 | 58 | public void setAddress(String address) { 59 | this.address = address; 60 | } 61 | 62 | public Integer getPort() { 63 | return port; 64 | } 65 | 66 | public void setPort(Integer port) { 67 | this.port = port; 68 | } 69 | 70 | public Map getTags() { 71 | return tags; 72 | } 73 | 74 | public void setTags(Map tags) { 75 | this.tags = tags; 76 | } 77 | 78 | public int getStatus() { 79 | return status; 80 | } 81 | 82 | public void setStatus(int status) { 83 | this.status = status; 84 | } 85 | 86 | public int getProtocolMin() { 87 | return protocolMin; 88 | } 89 | 90 | public void setProtocolMin(int protocolMin) { 91 | this.protocolMin = protocolMin; 92 | } 93 | 94 | public int getProtocolMax() { 95 | return protocolMax; 96 | } 97 | 98 | public void setProtocolMax(int protocolMax) { 99 | this.protocolMax = protocolMax; 100 | } 101 | 102 | public int getProtocolCur() { 103 | return protocolCur; 104 | } 105 | 106 | public void setProtocolCur(int protocolCur) { 107 | this.protocolCur = protocolCur; 108 | } 109 | 110 | public int getDelegateMin() { 111 | return delegateMin; 112 | } 113 | 114 | public void setDelegateMin(int delegateMin) { 115 | this.delegateMin = delegateMin; 116 | } 117 | 118 | public int getDelegateMax() { 119 | return delegateMax; 120 | } 121 | 122 | public void setDelegateMax(int delegateMax) { 123 | this.delegateMax = delegateMax; 124 | } 125 | 126 | public int getDelegateCur() { 127 | return delegateCur; 128 | } 129 | 130 | public void setDelegateCur(int delegateCur) { 131 | this.delegateCur = delegateCur; 132 | } 133 | 134 | @Override 135 | public String toString() { 136 | return "Member{" + 137 | "name='" + name + '\'' + 138 | ", address='" + address + '\'' + 139 | ", port=" + port + 140 | ", tags=" + tags + 141 | ", status=" + status + 142 | ", protocolMin=" + protocolMin + 143 | ", protocolMax=" + protocolMax + 144 | ", protocolCur=" + protocolCur + 145 | ", delegateMin=" + delegateMin + 146 | ", delegateMax=" + delegateMax + 147 | ", delegateCur=" + delegateCur + 148 | '}'; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/test/resources/ssl/key.key: -------------------------------------------------------------------------------- 1 | Bag Attributes 2 | friendlyName: key 3 | localKeyID: 54 69 6D 65 20 31 35 30 34 32 34 31 36 37 37 34 34 34 4 | Key Attributes: 5 | -----BEGIN RSA PRIVATE KEY----- 6 | MIIJKQIBAAKCAgEA1FfeQBecQnmND29M+Fm1deTUZn/3bj4B3kRkOWJuKp4j3qOg 7 | pgFSqKwSC3Q75h3kGu0rrNb5tIFt8m4+hAARz3ykx9iUySu9lQYl08O4hZ7XOdAv 8 | Sa9ERKTuA8c9BMxyxBi1RZlHY4z3gdmC9E9xgiNSJcXww9acS6GGAET1lipfPojw 9 | I3DiYBZX3IfIx+YSsng2m8odnQp0h1/S1r7GL/dl6mVhZ6zkbw4u7zZFwEYgSLfs 10 | cjzVNMq9dn82FiSJaNd6+194bChPj49+imJEUSIrp82ZlL7RrclCL2jtZw1WrP1n 11 | AK7MV8YC2/WGLfLbURU8WZvFTOh2xTZ35vgKjP7XWflIC1SNXR71owvTsydedntn 12 | wbJbWr8QU41tDV/t9KAYmr6tyO+Lp9KUcwoBJ6pRJ4UH+KI1KU9Q3NFe/tvXFn6f 13 | ZjzFbiIEh0w5A6iqAckFknK5RY8DVIiwK2ruIOncRhEnEIjLqCkPAPGGuE28iiW3 14 | GsUIZBPJw3cwCcd/t88mpM5su1vyUdcv4YmH5kRQd1wuI+7zpTly5xlIFUxlWwTR 15 | n9DNbO7iM8THhkERz707q/bTn1qyeS158OES0+oSTzXjbu1OKhkesA9lceDKHS/8 16 | w/sPxlnZ+GCrYybxTTZaUQQJCudZY8/+0AzaxmZaBUcavl/kTCc1Bf5HqVsCAwEA 17 | AQKCAgEAq647UECGoyxZg2AusCD/2Mjhfdn6uFRD+Gv9SHQvtB9Vyv0qtu0c4qj0 18 | kfQxNbV3JtoY1M6e53U56qd9vgW8YLBbHgUbDqdhyWpGDTgSKV6qjuCmSwH8xS9A 19 | DSf6SrXuGGQNs67hDDj+KCsveM3+bQSXt1iZeA09Itz+VZtroDSz+wCGf3EWZi/K 20 | a9AQ4qY++bK+HClG1iCM5KLx+ZQW8tMowGGlZkhgDPY/tkKAFJveIE0sS5SpoNxG 21 | YNcraK2zt2N42GJxH6lb4itXWRVoPNzOxQYzJRM+vi6ZZ3GDeC1NpeHfdKrfJhw/ 22 | VY3mJaJT9fOgbqbF14+f74VBXimvhKSPhxgsdRBuNuexx0RsUFpH7eHPE02x4Hgb 23 | bZYLsiggztciPNh7sRBxx5dIdR7LyDFu9Gtwcez6EJddDSNP1espaW6XoSShH6rU 24 | 4xNt8miBSL9yPhgAfrH1yUPpUU1Vj0XOId9odFplJdCgC2U1b5Yz9TJECxM5AeEu 25 | 3Zc+Na6QFvgO0GNW2fIRYS8eWnCxVwRPsbH7hVuvhaUqBRfDesdEPcR56OgRr/zL 26 | 6sp/nbbZU4ktXTg3gogKwnIk7X1nQ2L4GnMydM26hno1g1l2SZ6PyOmcnSxfO194 27 | BJ8a0CTcERkIHliUpVFpYanamm1oZWYHUdXsHOvr3xwnyNADYCECggEBAPErGXOD 28 | ASmAo/OK4jqG9t5yUV9b1i0ngguc0a7bSahTdQ2sbqRW3fAtiftNFznDLlNnqTid 29 | Hl/F/tt+M19t0P4nZkVhiijhrLKR4Lq1g1sOwl3moJwyhEiT5Htp5ggQF/7wv6+9 30 | bKHr3QzXDYEuue9IShDpMbmet6jQGiclCO++AbkCBrmUCXOUACtDHlyNPrwaoz8P 31 | UF1bZUths9Mhhk57sWdkhoNRfUzbvsQfa8VxOt6RMI7VQae+JOYpNAPRIg2H5jEA 32 | AR02CosJjwdwDjDJ4us5YTm592TFNqv4ZAoYKU+bmcV7KcGPGnk76+0Tr/twxt+i 33 | rNm82vfo7yseGXECggEBAOFm89Xo69KmEcq7q1Ji7BffeywNmI/ZmbgiH9G4riVe 34 | Huzp7tteoE3IFnaPKlgCTT6zjASIM4+CWnH6ol7FdrpZF8FNb/NGz9QGU64JuilX 35 | +DmAsfxBrqrVcQARSgaoCVYStrh7aoBDV8RhSb7Ko2aY4AIhQnFqzHkeLs2RX7LB 36 | GZaCREZuAJn7j0iffxKK+8OQqccqFjO4wilAzXVtfD9R3Y7z80Zoetd+l1URheMO 37 | n7V+U4LBguDtqNjvnobH8zMqMjwVHCbvxAuVFJcl0ZZlwbMjZBptuFpQ2BHXVkC/ 38 | xpZ0x4rCb4cmDaB3qk6ZcRJLoCzure9M3aTl8IhI6YsCggEAS64wwHT96l46FPUi 39 | ZSdxVQEe1APnxCe/wZEmo+6gwLff5biUx/GSmApSYJAil7fOU9IV4nQ70eY6Qfrq 40 | eCnoCbmneGJRjt6y6R6qHS1U2UBackkrYZjgtj3i12+2BhW20gQOw0F4U5/GsH7T 41 | +BQHuTVAOOpU9mD+VXVon5wZn9JKjBo8rgPsq15oZysa3gRdCX56eBGAMKu7r9or 42 | Kjg9A2gBleaP56ms3m+e/8C0ezM7PBjn2grGHbOntKp9bi00uHZLIFlWACqzSEpp 43 | nfXmHh1cnmBVSF45amAQ9gpuqlRGsnqr2LL7uYgE0MKjGLSy5v7PCeLsxy9ir6Vj 44 | WG6LoQKCAQA0dNVCbxaSy3tQhyRz4/m2BJiRxAhBUg7oP2jQUf6VS7Y+xKKGAxuj 45 | fnFb3i2olcCMWxS26Uu2lkueQwoOrD3ZWGqi3faz00MCwQYwdqsQfByUpVLGtjKE 46 | J3BI570ml2y0z14eyPocJ5ABb/LNqDMm6WUYmczwwD4d4dxakv6Gh1IBKQfirC8P 47 | pu/NovDby+STutMIOs838kOdfitSrMxBoDfD0RpIxH/yLs/hSFa0ZO7eOiwNPiPL 48 | XQPymLF4BIig260dNnx6w0oIuAU2t+Jy8fOGUTI2xFonCrF+VXMJVphM45icvmte 49 | NIjvU2obKLKb42W/jzyDeIwMmEbGMNHJAoIBAQCYX2CR/GGI/mHNN+LtpBRlNGyr 50 | puqqc29Wi9AahEnPqZS2Z1hKn4nT4A/BZqcQfBKd8bq673Mt1nzyXzgUWLYO9+o4 51 | QAywKNb5EAVjMrA6sL8KJM18YvGrpw/PR9YMsz6mfze6BNvMF43IaH4CqcAYBGDW 52 | i5WPjxi4FbK+oU4kJsUnuZ4psA97nfeL/uAoIkN3GdiFC25ttBUPba6kIa00BqNK 53 | LHrJ9mqTadhEBOhAQLmSEpF+7tcsHo07BPr75zTnxZPe8CcQIb3zKDJ8VvsgwLsg 54 | 5tQghNPJ8Bgg10Ry2zFsvKgBsLXO39DqFmuToPrLhzPif1jNlJ9WXSuXegG/ 55 | -----END RSA PRIVATE KEY----- 56 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/QueryParamsTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.Utils; 4 | import com.ecwid.consul.v1.catalog.CatalogServiceRequest; 5 | import nl.jqno.equalsverifier.EqualsVerifier; 6 | import org.junit.jupiter.api.Nested; 7 | import org.junit.jupiter.api.Test; 8 | 9 | import java.util.List; 10 | 11 | import static com.ecwid.consul.v1.QueryParams.Builder; 12 | import static org.hamcrest.CoreMatchers.hasItem; 13 | import static org.hamcrest.MatcherAssert.assertThat; 14 | import static org.junit.jupiter.api.Assertions.assertEquals; 15 | import static org.junit.jupiter.api.Assertions.assertNull; 16 | 17 | public class QueryParamsTest { 18 | @Test 19 | public void queryParamsBuilder_ShouldReturnAllDefaults_WhenNoValuesAdded() { 20 | // Given 21 | final ConsistencyMode EXPECTED_MODE = ConsistencyMode.DEFAULT; 22 | final long EXPECTED_INDEX = -1; 23 | final long EXPECTED_WAIT_TIME = -1; 24 | final String EXPECTED_NEAR = null; 25 | 26 | // When 27 | QueryParams actual = Builder.builder().build(); 28 | 29 | // Then 30 | assertNull(actual.getDatacenter()); 31 | assertEquals(actual.getConsistencyMode(), EXPECTED_MODE); 32 | assertEquals(actual.getWaitTime(), EXPECTED_WAIT_TIME); 33 | assertEquals(actual.getIndex(), EXPECTED_INDEX); 34 | assertEquals(actual.getNear(), EXPECTED_NEAR); 35 | } 36 | 37 | @Test 38 | public void queryParamsBuilder_ShouldReturnQueryParams_WithCorrectValuesApplied() { 39 | // Given 40 | final String EXPECTED_DATACENTER = "testDC"; 41 | final ConsistencyMode EXPECTED_MODE = ConsistencyMode.CONSISTENT; 42 | final long EXPECTED_INDEX = 100; 43 | final long EXPECTED_WAIT_TIME = 10000; 44 | final String EXPECTED_NEAR = "_agent"; 45 | 46 | // When 47 | QueryParams actual = Builder.builder() 48 | .setDatacenter(EXPECTED_DATACENTER) 49 | .setConsistencyMode(EXPECTED_MODE) 50 | .setWaitTime(EXPECTED_WAIT_TIME) 51 | .setIndex(EXPECTED_INDEX) 52 | .setNear(EXPECTED_NEAR) 53 | .build(); 54 | 55 | // Then 56 | assertEquals(actual.getDatacenter(), EXPECTED_DATACENTER); 57 | assertEquals(actual.getConsistencyMode(), EXPECTED_MODE); 58 | assertEquals(actual.getIndex(), EXPECTED_INDEX); 59 | assertEquals(actual.getWaitTime(), EXPECTED_WAIT_TIME); 60 | assertEquals(actual.getNear(), EXPECTED_NEAR); 61 | } 62 | 63 | @Test 64 | public void queryParamsToUrlParameters_ShouldContainSetQueryParams_WithCorrectValuesApplied() { 65 | // Given 66 | final String EXPECTED_DATACENTER = "testDC"; 67 | final ConsistencyMode EXPECTED_MODE = ConsistencyMode.CONSISTENT; 68 | final long EXPECTED_WAIT = 1000L; 69 | final long EXPECTED_INDEX = 2000L; 70 | final String EXPECTED_NEAR = "_agent"; 71 | 72 | // When 73 | List urlParameters = Builder.builder() 74 | .setDatacenter(EXPECTED_DATACENTER) 75 | .setConsistencyMode(EXPECTED_MODE) 76 | .setWaitTime(EXPECTED_WAIT) 77 | .setIndex(EXPECTED_INDEX) 78 | .setNear(EXPECTED_NEAR) 79 | .build() 80 | .toUrlParameters(); 81 | 82 | // Then 83 | assertThat(urlParameters, hasItem("dc=" + EXPECTED_DATACENTER)); 84 | assertThat(urlParameters, hasItem(EXPECTED_MODE.name().toLowerCase())); 85 | assertThat(urlParameters, hasItem("wait=" + Utils.toSecondsString(EXPECTED_WAIT))); 86 | assertThat(urlParameters, hasItem("index=" + EXPECTED_INDEX)); 87 | assertThat(urlParameters, hasItem("near=" + EXPECTED_NEAR)); 88 | } 89 | 90 | @Nested 91 | class EqualsAndHashCode { 92 | @Test 93 | void shouldVerify() { 94 | EqualsVerifier.forClass(QueryParams.class).verify(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | consul-api 2 | ========== 3 | 4 | [![Build Status](https://api.travis-ci.org/Ecwid/consul-api.svg)](http://travis-ci.org/Ecwid/consul-api) 5 | 6 | Java client for Consul HTTP API (http://consul.io) 7 | 8 | Supports all API endpoints (http://www.consul.io/docs/agent/http.html), all consistency modes and parameters (tags, datacenters etc.) 9 | 10 | ## How to use 11 | ```java 12 | ConsulClient client = new ConsulClient("localhost"); 13 | 14 | // set KV 15 | byte[] binaryData = new byte[] {1,2,3,4,5,6,7}; 16 | client.setKVBinaryValue("someKey", binaryData); 17 | 18 | client.setKVValue("com.my.app.foo", "foo"); 19 | client.setKVValue("com.my.app.bar", "bar"); 20 | client.setKVValue("com.your.app.foo", "hello"); 21 | client.setKVValue("com.your.app.bar", "world"); 22 | 23 | // get single KV for key 24 | Response keyValueResponse = client.getKVValue("com.my.app.foo"); 25 | System.out.println(keyValueResponse.getValue().getKey() + ": " + keyValueResponse.getValue().getDecodedValue()); // prints "com.my.app.foo: foo" 26 | 27 | // get list of KVs for key prefix (recursive) 28 | Response> keyValuesResponse = client.getKVValues("com.my"); 29 | keyValuesResponse.getValue().forEach(value -> System.out.println(value.getKey() + ": " + value.getDecodedValue())); // prints "com.my.app.foo: foo" and "com.my.app.bar: bar" 30 | 31 | //list known datacenters 32 | Response> response = client.getCatalogDatacenters(); 33 | System.out.println("Datacenters: " + response.getValue()); 34 | 35 | // register new service 36 | NewService newService = new NewService(); 37 | newService.setId("myapp_01"); 38 | newService.setName("myapp"); 39 | newService.setTags(Arrays.asList("EU-West", "EU-East")); 40 | newService.setPort(8080); 41 | client.agentServiceRegister(newService); 42 | 43 | // register new service with associated health check 44 | NewService newService = new NewService(); 45 | newService.setId("myapp_02"); 46 | newService.setTags(Collections.singletonList("EU-East")); 47 | newService.setName("myapp"); 48 | newService.setPort(8080); 49 | 50 | NewService.Check serviceCheck = new NewService.Check(); 51 | serviceCheck.setScript("/usr/bin/some-check-script"); 52 | serviceCheck.setInterval("10s"); 53 | newService.setCheck(serviceCheck); 54 | 55 | client.agentServiceRegister(newService); 56 | 57 | // query for healthy services based on name (returns myapp_01 and myapp_02 if healthy) 58 | HealthServicesRequest request = HealthServicesRequest.newBuilder() 59 | .setPassing(true) 60 | .setQueryParams(QueryParams.DEFAULT) 61 | .build(); 62 | Response> healthyServices = client.getHealthServices("myapp", request); 63 | 64 | // query for healthy services based on name and tag (returns myapp_01 if healthy) 65 | HealthServicesRequest request = HealthServicesRequest.newBuilder() 66 | .setTag("EU-West") 67 | .setPassing(true) 68 | .setQueryParams(QueryParams.DEFAULT) 69 | .build(); 70 | Response> healthyServices = client.getHealthServices("myapp", request); 71 | ``` 72 | 73 | ## How to add consul-api into your project 74 | ### Gradle 75 | ``` 76 | compile "com.ecwid.consul:consul-api:1.4.5" 77 | ``` 78 | ### Maven 79 | ``` 80 | 81 | com.ecwid.consul 82 | consul-api 83 | 1.4.5 84 | 85 | ``` 86 | 87 | ## How to build from sources 88 | * Checkout the sources 89 | * ./gradlew build 90 | 91 | Gradle will compile sources, package classes (sources and javadocs too) into jars and run all tests. The build results will located in build/libs/ folder 92 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/CatalogNodesRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import com.ecwid.consul.ConsulRequest; 4 | import com.ecwid.consul.SingleUrlParameters; 5 | import com.ecwid.consul.UrlParameters; 6 | import com.ecwid.consul.v1.NodeMetaParameters; 7 | import com.ecwid.consul.v1.QueryParams; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Objects; 14 | 15 | public final class CatalogNodesRequest implements ConsulRequest { 16 | 17 | private final String datacenter; 18 | private final String near; 19 | private final Map nodeMeta; 20 | private final QueryParams queryParams; 21 | 22 | private CatalogNodesRequest(String datacenter, String near, Map nodeMeta, QueryParams queryParams) { 23 | this.datacenter = datacenter; 24 | this.near = near; 25 | this.nodeMeta = nodeMeta; 26 | this.queryParams = queryParams; 27 | } 28 | 29 | public String getDatacenter() { 30 | return datacenter; 31 | } 32 | 33 | public String getNear() { 34 | return near; 35 | } 36 | 37 | public Map getNodeMeta() { 38 | return nodeMeta; 39 | } 40 | 41 | public QueryParams getQueryParams() { 42 | return queryParams; 43 | } 44 | 45 | public static class Builder { 46 | private String datacenter; 47 | private String near; 48 | private Map nodeMeta; 49 | private QueryParams queryParams; 50 | 51 | private Builder() { 52 | } 53 | 54 | public Builder setDatacenter(String datacenter) { 55 | this.datacenter = datacenter; 56 | return this; 57 | } 58 | 59 | public Builder setNear(String near) { 60 | this.near = near; 61 | return this; 62 | } 63 | 64 | public Builder setNodeMeta(Map nodeMeta) { 65 | if (nodeMeta == null) { 66 | this.nodeMeta = null; 67 | } else { 68 | this.nodeMeta = Collections.unmodifiableMap(nodeMeta); 69 | } 70 | 71 | return this; 72 | } 73 | 74 | public Builder setQueryParams(QueryParams queryParams) { 75 | this.queryParams = queryParams; 76 | return this; 77 | } 78 | 79 | public CatalogNodesRequest build() { 80 | return new CatalogNodesRequest(datacenter, near, nodeMeta, queryParams); 81 | } 82 | } 83 | 84 | public static Builder newBuilder() { 85 | return new Builder(); 86 | } 87 | 88 | @Override 89 | public List asUrlParameters() { 90 | List params = new ArrayList<>(); 91 | 92 | if (datacenter != null) { 93 | params.add(new SingleUrlParameters("dc", datacenter)); 94 | } 95 | 96 | if (near != null) { 97 | params.add(new SingleUrlParameters("near", near)); 98 | } 99 | 100 | if (nodeMeta != null) { 101 | params.add(new NodeMetaParameters(nodeMeta)); 102 | } 103 | 104 | if (queryParams != null) { 105 | params.add(queryParams); 106 | } 107 | 108 | return params; 109 | } 110 | 111 | @Override 112 | public boolean equals(Object o) { 113 | if (this == o) { 114 | return true; 115 | } 116 | if (!(o instanceof CatalogNodesRequest)) { 117 | return false; 118 | } 119 | CatalogNodesRequest that = (CatalogNodesRequest) o; 120 | return Objects.equals(datacenter, that.datacenter) && 121 | Objects.equals(near, that.near) && 122 | Objects.equals(nodeMeta, that.nodeMeta) && 123 | Objects.equals(queryParams, that.queryParams); 124 | } 125 | 126 | @Override 127 | public int hashCode() { 128 | return Objects.hash(datacenter, near, nodeMeta, queryParams); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/health/HealthChecksForServiceRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health; 2 | 3 | import com.ecwid.consul.ConsulRequest; 4 | import com.ecwid.consul.SingleUrlParameters; 5 | import com.ecwid.consul.UrlParameters; 6 | import com.ecwid.consul.v1.NodeMetaParameters; 7 | import com.ecwid.consul.v1.QueryParams; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Objects; 14 | 15 | public final class HealthChecksForServiceRequest implements ConsulRequest { 16 | 17 | private final String datacenter; 18 | private final String near; 19 | private final Map nodeMeta; 20 | private final QueryParams queryParams; 21 | 22 | public HealthChecksForServiceRequest(String datacenter, String near, Map nodeMeta, QueryParams queryParams) { 23 | this.datacenter = datacenter; 24 | this.near = near; 25 | this.nodeMeta = nodeMeta; 26 | this.queryParams = queryParams; 27 | } 28 | 29 | public String getDatacenter() { 30 | return datacenter; 31 | } 32 | 33 | public String getNear() { 34 | return near; 35 | } 36 | 37 | public Map getNodeMeta() { 38 | return nodeMeta; 39 | } 40 | 41 | public QueryParams getQueryParams() { 42 | return queryParams; 43 | } 44 | 45 | public static class Builder { 46 | private String datacenter; 47 | private String near; 48 | private Map nodeMeta; 49 | private QueryParams queryParams; 50 | 51 | private Builder() { 52 | } 53 | 54 | public Builder setDatacenter(String datacenter) { 55 | this.datacenter = datacenter; 56 | return this; 57 | } 58 | 59 | public Builder setNear(String near) { 60 | this.near = near; 61 | return this; 62 | } 63 | 64 | public Builder setNodeMeta(Map nodeMeta) { 65 | this.nodeMeta = nodeMeta != null ? Collections.unmodifiableMap(nodeMeta) : null; 66 | return this; 67 | } 68 | 69 | public Builder setQueryParams(QueryParams queryParams) { 70 | this.queryParams = queryParams; 71 | return this; 72 | } 73 | 74 | public HealthChecksForServiceRequest build() { 75 | return new HealthChecksForServiceRequest(datacenter, near, nodeMeta, queryParams); 76 | } 77 | } 78 | 79 | public static Builder newBuilder() { 80 | return new Builder(); 81 | } 82 | 83 | @Override 84 | public List asUrlParameters() { 85 | List params = new ArrayList<>(); 86 | 87 | if (datacenter != null) { 88 | params.add(new SingleUrlParameters("dc", datacenter)); 89 | } 90 | 91 | if (near != null) { 92 | params.add(new SingleUrlParameters("near", near)); 93 | } 94 | 95 | if (nodeMeta != null) { 96 | params.add(new NodeMetaParameters(nodeMeta)); 97 | } 98 | 99 | if (queryParams != null) { 100 | params.add(queryParams); 101 | } 102 | 103 | return params; 104 | } 105 | 106 | @Override 107 | public boolean equals(Object o) { 108 | if (this == o) { 109 | return true; 110 | } 111 | if (!(o instanceof HealthChecksForServiceRequest)) { 112 | return false; 113 | } 114 | HealthChecksForServiceRequest that = (HealthChecksForServiceRequest) o; 115 | return Objects.equals(datacenter, that.datacenter) && 116 | Objects.equals(near, that.near) && 117 | Objects.equals(nodeMeta, that.nodeMeta) && 118 | Objects.equals(queryParams, that.queryParams); 119 | } 120 | 121 | @Override 122 | public int hashCode() { 123 | return Objects.hash(datacenter, near, nodeMeta, queryParams); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/CatalogServicesRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import com.ecwid.consul.ConsulRequest; 4 | import com.ecwid.consul.SingleUrlParameters; 5 | import com.ecwid.consul.UrlParameters; 6 | import com.ecwid.consul.v1.NodeMetaParameters; 7 | import com.ecwid.consul.v1.QueryParams; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Objects; 14 | 15 | public final class CatalogServicesRequest implements ConsulRequest { 16 | 17 | private final String datacenter; 18 | private final Map nodeMeta; 19 | private final QueryParams queryParams; 20 | private final String token; 21 | 22 | public CatalogServicesRequest(String datacenter, Map nodeMeta, QueryParams queryParams, String token) { 23 | this.datacenter = datacenter; 24 | this.nodeMeta = nodeMeta; 25 | this.queryParams = queryParams; 26 | this.token = token; 27 | } 28 | 29 | public String getDatacenter() { 30 | return datacenter; 31 | } 32 | 33 | public Map getNodeMeta() { 34 | return nodeMeta; 35 | } 36 | 37 | public QueryParams getQueryParams() { 38 | return queryParams; 39 | } 40 | 41 | public String getToken() { 42 | return token; 43 | } 44 | 45 | public static class Builder { 46 | private String datacenter; 47 | private Map nodeMeta; 48 | private QueryParams queryParams; 49 | private String token; 50 | 51 | private Builder() { 52 | } 53 | 54 | public Builder setDatacenter(String datacenter) { 55 | this.datacenter = datacenter; 56 | return this; 57 | } 58 | 59 | public Builder setNodeMeta(Map nodeMeta) { 60 | if (nodeMeta == null) { 61 | this.nodeMeta = null; 62 | } else { 63 | this.nodeMeta = Collections.unmodifiableMap(nodeMeta); 64 | } 65 | 66 | return this; 67 | } 68 | 69 | public Builder setQueryParams(QueryParams queryParams) { 70 | this.queryParams = queryParams; 71 | return this; 72 | } 73 | 74 | public Builder setToken(String token) { 75 | this.token = token; 76 | return this; 77 | } 78 | 79 | public CatalogServicesRequest build() { 80 | return new CatalogServicesRequest(datacenter, nodeMeta, queryParams, token); 81 | } 82 | } 83 | 84 | public static Builder newBuilder() { 85 | return new CatalogServicesRequest.Builder(); 86 | } 87 | 88 | @Override 89 | public List asUrlParameters() { 90 | List params = new ArrayList<>(); 91 | 92 | if (datacenter != null) { 93 | params.add(new SingleUrlParameters("dc", datacenter)); 94 | } 95 | 96 | if (nodeMeta != null) { 97 | params.add(new NodeMetaParameters(nodeMeta)); 98 | } 99 | 100 | if (queryParams != null) { 101 | params.add(queryParams); 102 | } 103 | 104 | if (token != null) { 105 | params.add(new SingleUrlParameters("token", token)); 106 | } 107 | 108 | return params; 109 | } 110 | 111 | @Override 112 | public boolean equals(Object o) { 113 | if (this == o) { 114 | return true; 115 | } 116 | if (!(o instanceof CatalogServicesRequest)) { 117 | return false; 118 | } 119 | CatalogServicesRequest that = (CatalogServicesRequest) o; 120 | return Objects.equals(datacenter, that.datacenter) && 121 | Objects.equals(nodeMeta, that.nodeMeta) && 122 | Objects.equals(queryParams, that.queryParams) && 123 | Objects.equals(token, that.token); 124 | } 125 | 126 | @Override 127 | public int hashCode() { 128 | return Objects.hash(datacenter, nodeMeta, queryParams, token); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/DefaultHttpsTransport.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.IOException; 5 | import java.security.*; 6 | 7 | import javax.net.ssl.KeyManager; 8 | import javax.net.ssl.KeyManagerFactory; 9 | import javax.net.ssl.SSLContext; 10 | import javax.net.ssl.TrustManager; 11 | import javax.net.ssl.TrustManagerFactory; 12 | 13 | import org.apache.http.client.HttpClient; 14 | import org.apache.http.client.config.RequestConfig; 15 | import org.apache.http.config.Registry; 16 | import org.apache.http.config.RegistryBuilder; 17 | import org.apache.http.conn.socket.ConnectionSocketFactory; 18 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 19 | 20 | import com.ecwid.consul.transport.TLSConfig.KeyStoreInstanceType; 21 | import org.apache.http.conn.ssl.TrustSelfSignedStrategy; 22 | import org.apache.http.impl.client.HttpClientBuilder; 23 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 24 | import org.apache.http.ssl.SSLContexts; 25 | 26 | /** 27 | * Default HTTPS client This class is thread safe 28 | * 29 | * @author Carlos Augusto Ribeiro Mantovani (gutomantovani@gmail.com) 30 | */ 31 | public final class DefaultHttpsTransport extends AbstractHttpTransport { 32 | 33 | private final HttpClient httpClient; 34 | 35 | public DefaultHttpsTransport(TLSConfig tlsConfig) { 36 | try { 37 | KeyStore clientStore = KeyStore.getInstance(tlsConfig.getKeyStoreInstanceType().name()); 38 | clientStore.load(new FileInputStream(tlsConfig.getCertificatePath()), tlsConfig.getCertificatePassword().toCharArray()); 39 | 40 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 41 | kmf.init(clientStore, tlsConfig.getCertificatePassword().toCharArray()); 42 | KeyManager[] kms = kmf.getKeyManagers(); 43 | 44 | KeyStore trustStore = KeyStore.getInstance(KeyStoreInstanceType.JKS.name()); 45 | trustStore.load(new FileInputStream(tlsConfig.getKeyStorePath()), tlsConfig.getKeyStorePassword().toCharArray()); 46 | 47 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 48 | tmf.init(trustStore); 49 | TrustManager[] tms = tmf.getTrustManagers(); 50 | 51 | SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustSelfSignedStrategy()).build(); 52 | sslContext.init(kms, tms, new SecureRandom()); 53 | SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext); 54 | 55 | Registry registry = RegistryBuilder.create() 56 | .register("https", factory).build(); 57 | 58 | PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); 59 | connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS); 60 | connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE_CONNECTIONS); 61 | 62 | RequestConfig requestConfig = RequestConfig.custom(). 63 | setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT). 64 | setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT). 65 | setSocketTimeout(DEFAULT_READ_TIMEOUT). 66 | build(); 67 | 68 | HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(). 69 | setConnectionManager(connectionManager). 70 | setDefaultRequestConfig(requestConfig); 71 | 72 | this.httpClient = httpClientBuilder.build(); 73 | } catch (GeneralSecurityException e) { 74 | throw new TransportException(e); 75 | } catch (IOException e) { 76 | throw new TransportException(e); 77 | } 78 | } 79 | 80 | public DefaultHttpsTransport(HttpClient httpClient) { 81 | this.httpClient = httpClient; 82 | } 83 | 84 | @Override 85 | protected HttpClient getHttpClient() { 86 | return httpClient; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/query/model/Check.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.query.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class Check { 11 | 12 | public static enum CheckStatus { 13 | @SerializedName("unknown") 14 | UNKNOWN, 15 | @SerializedName("passing") 16 | PASSING, 17 | @SerializedName("warning") 18 | WARNING, 19 | @SerializedName("critical") 20 | CRITICAL 21 | } 22 | 23 | @SerializedName("Node") 24 | private String node; 25 | 26 | @SerializedName("CheckID") 27 | private String checkId; 28 | 29 | @SerializedName("Name") 30 | private String name; 31 | 32 | @SerializedName("Status") 33 | private CheckStatus status; 34 | 35 | @SerializedName("Notes") 36 | private String notes; 37 | 38 | @SerializedName("Output") 39 | private String output; 40 | 41 | @SerializedName("ServiceID") 42 | private String serviceId; 43 | 44 | @SerializedName("ServiceName") 45 | private String serviceName; 46 | 47 | @SerializedName("ServiceTags") 48 | private List serviceTags; 49 | 50 | @SerializedName("CreateIndex") 51 | private Long createIndex; 52 | 53 | @SerializedName("ModifyIndex") 54 | private Long modifyIndex; 55 | 56 | public String getNode() { 57 | return node; 58 | } 59 | 60 | public void setNode(String node) { 61 | this.node = node; 62 | } 63 | 64 | public String getCheckId() { 65 | return checkId; 66 | } 67 | 68 | public void setCheckId(String checkId) { 69 | this.checkId = checkId; 70 | } 71 | 72 | public String getName() { 73 | return name; 74 | } 75 | 76 | public void setName(String name) { 77 | this.name = name; 78 | } 79 | 80 | public CheckStatus getStatus() { 81 | return status; 82 | } 83 | 84 | public void setStatus(CheckStatus status) { 85 | this.status = status; 86 | } 87 | 88 | public String getNotes() { 89 | return notes; 90 | } 91 | 92 | public void setNotes(String notes) { 93 | this.notes = notes; 94 | } 95 | 96 | public String getOutput() { 97 | return output; 98 | } 99 | 100 | public void setOutput(String output) { 101 | this.output = output; 102 | } 103 | 104 | public String getServiceId() { 105 | return serviceId; 106 | } 107 | 108 | public void setServiceId(String serviceId) { 109 | this.serviceId = serviceId; 110 | } 111 | 112 | public String getServiceName() { 113 | return serviceName; 114 | } 115 | 116 | public void setServiceName(String serviceName) { 117 | this.serviceName = serviceName; 118 | } 119 | 120 | public List getServiceTags() { 121 | return serviceTags; 122 | } 123 | 124 | public void setServiceTags(List serviceTags) { 125 | this.serviceTags = serviceTags; 126 | } 127 | 128 | public Long getCreateIndex() { 129 | return createIndex; 130 | } 131 | 132 | public void setCreateIndex(Long createIndex) { 133 | this.createIndex = createIndex; 134 | } 135 | 136 | public Long getModifyIndex() { 137 | return modifyIndex; 138 | } 139 | 140 | public void setModifyIndex(Long modifyIndex) { 141 | this.modifyIndex = modifyIndex; 142 | } 143 | 144 | @Override 145 | public String toString() { 146 | return "Check{" + 147 | "node='" + node + '\'' + 148 | ", checkId='" + checkId + '\'' + 149 | ", name='" + name + '\'' + 150 | ", status=" + status + 151 | ", notes='" + notes + '\'' + 152 | ", output='" + output + '\'' + 153 | ", serviceId='" + serviceId + '\'' + 154 | ", serviceName='" + serviceName + '\'' + 155 | ", serviceTags=" + serviceTags + 156 | ", createIndex=" + createIndex + 157 | ", modifyIndex=" + modifyIndex + 158 | '}'; 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /src/test/java/com/ecwid/consul/v1/ConsulClientTest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.ConsulTestConstants; 4 | import com.ecwid.consul.transport.TLSConfig; 5 | import com.ecwid.consul.v1.agent.model.NewService; 6 | import com.ecwid.consul.v1.agent.model.Service; 7 | import com.pszymczyk.consul.ConsulProcess; 8 | import com.pszymczyk.consul.ConsulStarterBuilder; 9 | import com.pszymczyk.consul.infrastructure.Ports; 10 | import org.hamcrest.collection.IsMapContaining; 11 | import org.junit.jupiter.api.AfterEach; 12 | import org.junit.jupiter.api.BeforeEach; 13 | import org.junit.jupiter.api.Test; 14 | 15 | import java.io.File; 16 | import java.util.Map; 17 | 18 | import static org.hamcrest.MatcherAssert.assertThat; 19 | 20 | 21 | public class ConsulClientTest { 22 | 23 | private ConsulProcess consul; 24 | private int randomHttpsPort = Ports.nextAvailable(); 25 | 26 | @BeforeEach 27 | public void setup() { 28 | String path = "src/test/resources/ssl"; 29 | String certRootPath = new File(path).getAbsolutePath(); 30 | //language=JSON 31 | String customConfiguration = 32 | "{\n" + 33 | " \"datacenter\": \"dc-test\",\n" + 34 | " \"log_level\": \"info\",\n" + 35 | " \"ports\": {\n" + 36 | " \"https\": "+ randomHttpsPort+ "\n" + 37 | " },\n" + 38 | " \"ca_file\": \"" + certRootPath + "/ca.cert\",\n" + 39 | " \"key_file\": \"" + certRootPath + "/key.key\",\n" + 40 | " \"cert_file\": \"" + certRootPath + "/key.crt\"\n" + 41 | "}\n"; 42 | 43 | consul = ConsulStarterBuilder.consulStarter() 44 | .withConsulVersion(ConsulTestConstants.CONSUL_VERSION) 45 | .withCustomConfig(customConfiguration) 46 | .build() 47 | .start(); 48 | } 49 | 50 | @AfterEach 51 | public void cleanup() throws Exception { 52 | consul.close(); 53 | } 54 | 55 | @Test 56 | public void agentHttpTest() throws Exception { 57 | String host = "http://localhost"; 58 | int port = consul.getHttpPort(); 59 | ConsulClient consulClient = new ConsulClient(host, port); 60 | serviceRegisterTest(consulClient); 61 | } 62 | 63 | @Test 64 | public void agentHttpsTest() throws Exception { 65 | 66 | String host = "https://localhost"; 67 | //TODO make https random port in consul 68 | int httpsPort = randomHttpsPort; 69 | 70 | String path = "src/test/resources/ssl"; 71 | String certRootPath = new File(path).getAbsolutePath(); 72 | String certificatePath = certRootPath + "/trustStore.jks"; 73 | String certificatePassword = "change_me"; 74 | String keyStorePath = certRootPath + "/keyStore.jks"; 75 | String keyStorePassword = "change_me"; 76 | 77 | TLSConfig tlsConfig = new TLSConfig(TLSConfig.KeyStoreInstanceType.JKS, 78 | certificatePath, certificatePassword, 79 | keyStorePath, keyStorePassword); 80 | ConsulClient consulClient = new ConsulClient(host, httpsPort, tlsConfig); 81 | serviceRegisterTest(consulClient); 82 | } 83 | 84 | private void serviceRegisterTest(ConsulClient consulClient) { 85 | NewService newService = new NewService(); 86 | String serviceName = "abc"; 87 | newService.setName(serviceName); 88 | consulClient.agentServiceRegister(newService); 89 | 90 | Response> agentServicesResponse = consulClient.getAgentServices(); 91 | Map services = agentServicesResponse.getValue(); 92 | assertThat(services, IsMapContaining.hasKey(serviceName)); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/health/model/Check.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Vasily Vasilkov (vgv@ecwid.com) 9 | */ 10 | public class Check { 11 | 12 | public static enum CheckStatus { 13 | @SerializedName("unknown") 14 | UNKNOWN, 15 | @SerializedName("passing") 16 | PASSING, 17 | @SerializedName("warning") 18 | WARNING, 19 | @SerializedName("critical") 20 | CRITICAL 21 | } 22 | 23 | @SerializedName("Node") 24 | private String node; 25 | 26 | @SerializedName("CheckID") 27 | private String checkId; 28 | 29 | @SerializedName("Name") 30 | private String name; 31 | 32 | @SerializedName("Status") 33 | private CheckStatus status; 34 | 35 | @SerializedName("Notes") 36 | private String notes; 37 | 38 | @SerializedName("Output") 39 | private String output; 40 | 41 | @SerializedName("ServiceID") 42 | private String serviceId; 43 | 44 | @SerializedName("ServiceName") 45 | private String serviceName; 46 | 47 | @SerializedName("ServiceTags") 48 | private List serviceTags; 49 | 50 | @SerializedName("CreateIndex") 51 | private Long createIndex; 52 | 53 | @SerializedName("ModifyIndex") 54 | private Long modifyIndex; 55 | 56 | public String getNode() { 57 | return node; 58 | } 59 | 60 | public void setNode(String node) { 61 | this.node = node; 62 | } 63 | 64 | public String getCheckId() { 65 | return checkId; 66 | } 67 | 68 | public void setCheckId(String checkId) { 69 | this.checkId = checkId; 70 | } 71 | 72 | public String getName() { 73 | return name; 74 | } 75 | 76 | public void setName(String name) { 77 | this.name = name; 78 | } 79 | 80 | public CheckStatus getStatus() { 81 | return status; 82 | } 83 | 84 | public void setStatus(CheckStatus status) { 85 | this.status = status; 86 | } 87 | 88 | public String getNotes() { 89 | return notes; 90 | } 91 | 92 | public void setNotes(String notes) { 93 | this.notes = notes; 94 | } 95 | 96 | public String getOutput() { 97 | return output; 98 | } 99 | 100 | public void setOutput(String output) { 101 | this.output = output; 102 | } 103 | 104 | public String getServiceId() { 105 | return serviceId; 106 | } 107 | 108 | public void setServiceId(String serviceId) { 109 | this.serviceId = serviceId; 110 | } 111 | 112 | public String getServiceName() { 113 | return serviceName; 114 | } 115 | 116 | public void setServiceName(String serviceName) { 117 | this.serviceName = serviceName; 118 | } 119 | 120 | public List getServiceTags() { 121 | return serviceTags; 122 | } 123 | 124 | public void setServiceTags(List serviceTags) { 125 | this.serviceTags = serviceTags; 126 | } 127 | 128 | public Long getCreateIndex() { 129 | return createIndex; 130 | } 131 | 132 | public void setCreateIndex(Long createIndex) { 133 | this.createIndex = createIndex; 134 | } 135 | 136 | public Long getModifyIndex() { 137 | return modifyIndex; 138 | } 139 | 140 | public void setModifyIndex(Long modifyIndex) { 141 | this.modifyIndex = modifyIndex; 142 | } 143 | 144 | @Override 145 | public String toString() { 146 | return "Check{" + 147 | "node='" + node + '\'' + 148 | ", checkId='" + checkId + '\'' + 149 | ", name='" + name + '\'' + 150 | ", status=" + status + 151 | ", notes='" + notes + '\'' + 152 | ", output='" + output + '\'' + 153 | ", serviceId='" + serviceId + '\'' + 154 | ", serviceName='" + serviceName + '\'' + 155 | ", serviceTags=" + serviceTags + 156 | ", createIndex=" + createIndex + 157 | ", modifyIndex=" + modifyIndex + 158 | '}'; 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/agent/model/Check.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.agent.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author Vasily Vasilkov (vgv@ecwid.com) 10 | */ 11 | public class Check { 12 | 13 | public static enum CheckStatus { 14 | @SerializedName("unknown") 15 | UNKNOWN, 16 | @SerializedName("passing") 17 | PASSING, 18 | @SerializedName("warning") 19 | WARNING, 20 | @SerializedName("critical") 21 | CRITICAL 22 | } 23 | 24 | @SerializedName("Node") 25 | private String node; 26 | 27 | @SerializedName("CheckID") 28 | private String checkId; 29 | 30 | @SerializedName("Name") 31 | private String name; 32 | 33 | @SerializedName("Status") 34 | private CheckStatus status; 35 | 36 | @SerializedName("Notes") 37 | private String notes; 38 | 39 | @SerializedName("Output") 40 | private String output; 41 | 42 | @SerializedName("ServiceID") 43 | private String serviceId; 44 | 45 | @SerializedName("ServiceName") 46 | private String serviceName; 47 | 48 | @SerializedName("ServiceTags") 49 | private List serviceTags; 50 | 51 | @SerializedName("CreateIndex") 52 | private Long createIndex; 53 | 54 | @SerializedName("ModifyIndex") 55 | private Long modifyIndex; 56 | 57 | public String getNode() { 58 | return node; 59 | } 60 | 61 | public void setNode(String node) { 62 | this.node = node; 63 | } 64 | 65 | public String getCheckId() { 66 | return checkId; 67 | } 68 | 69 | public void setCheckId(String checkId) { 70 | this.checkId = checkId; 71 | } 72 | 73 | public String getName() { 74 | return name; 75 | } 76 | 77 | public void setName(String name) { 78 | this.name = name; 79 | } 80 | 81 | public CheckStatus getStatus() { 82 | return status; 83 | } 84 | 85 | public void setStatus(CheckStatus status) { 86 | this.status = status; 87 | } 88 | 89 | public String getNotes() { 90 | return notes; 91 | } 92 | 93 | public void setNotes(String notes) { 94 | this.notes = notes; 95 | } 96 | 97 | public String getOutput() { 98 | return output; 99 | } 100 | 101 | public void setOutput(String output) { 102 | this.output = output; 103 | } 104 | 105 | public String getServiceId() { 106 | return serviceId; 107 | } 108 | 109 | public void setServiceId(String serviceId) { 110 | this.serviceId = serviceId; 111 | } 112 | 113 | public String getServiceName() { 114 | return serviceName; 115 | } 116 | 117 | public void setServiceName(String serviceName) { 118 | this.serviceName = serviceName; 119 | } 120 | 121 | public List getServiceTags() { 122 | return serviceTags; 123 | } 124 | 125 | public void setServiceTags(List serviceTags) { 126 | this.serviceTags = serviceTags; 127 | } 128 | 129 | public Long getCreateIndex() { 130 | return createIndex; 131 | } 132 | 133 | public void setCreateIndex(Long createIndex) { 134 | this.createIndex = createIndex; 135 | } 136 | 137 | public Long getModifyIndex() { 138 | return modifyIndex; 139 | } 140 | 141 | public void setModifyIndex(Long modifyIndex) { 142 | this.modifyIndex = modifyIndex; 143 | } 144 | 145 | @Override 146 | public String toString() { 147 | return "Check{" + 148 | "node='" + node + '\'' + 149 | ", checkId='" + checkId + '\'' + 150 | ", name='" + name + '\'' + 151 | ", status=" + status + 152 | ", notes='" + notes + '\'' + 153 | ", output='" + output + '\'' + 154 | ", serviceId='" + serviceId + '\'' + 155 | ", serviceName='" + serviceName + '\'' + 156 | ", serviceTags=" + serviceTags + 157 | ", createIndex=" + createIndex + 158 | ", modifyIndex=" + modifyIndex + 159 | '}'; 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/event/EventListRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.event; 2 | 3 | import com.ecwid.consul.ConsulRequest; 4 | import com.ecwid.consul.SingleUrlParameters; 5 | import com.ecwid.consul.UrlParameters; 6 | import com.ecwid.consul.v1.QueryParams; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Objects; 11 | 12 | public final class EventListRequest implements ConsulRequest { 13 | 14 | private final String name; 15 | private final String node; 16 | private final String service; 17 | private final String tag; 18 | private final QueryParams queryParams; 19 | private final String token; 20 | 21 | private EventListRequest(String name, String node, String service, String tag, QueryParams queryParams, String token) { 22 | this.name = name; 23 | this.node = node; 24 | this.service = service; 25 | this.tag = tag; 26 | this.queryParams = queryParams; 27 | this.token = token; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public String getNode() { 35 | return node; 36 | } 37 | 38 | public String getService() { 39 | return service; 40 | } 41 | 42 | public String getTag() { 43 | return tag; 44 | } 45 | 46 | public QueryParams getQueryParams() { 47 | return queryParams; 48 | } 49 | 50 | public String getToken() { 51 | return token; 52 | } 53 | 54 | public static class Builder { 55 | private String name; 56 | private String node; 57 | private String service; 58 | private String tag; 59 | private QueryParams queryParams; 60 | private String token; 61 | 62 | public Builder setName(String name) { 63 | this.name = name; 64 | return this; 65 | } 66 | 67 | public Builder setNode(String node) { 68 | this.node = node; 69 | return this; 70 | } 71 | 72 | public Builder setService(String service) { 73 | this.service = service; 74 | return this; 75 | } 76 | 77 | public Builder setTag(String tag) { 78 | this.tag = tag; 79 | return this; 80 | } 81 | 82 | public Builder setQueryParams(QueryParams queryParams) { 83 | this.queryParams = queryParams; 84 | return this; 85 | } 86 | 87 | public Builder setToken(String token) { 88 | this.token = token; 89 | return this; 90 | } 91 | 92 | public EventListRequest build() { 93 | return new EventListRequest(name, node, service, tag, queryParams, token); 94 | } 95 | } 96 | 97 | public static Builder newBuilder() { 98 | return new Builder(); 99 | } 100 | 101 | @Override 102 | public List asUrlParameters() { 103 | List params = new ArrayList<>(); 104 | 105 | if (name != null) { 106 | params.add(new SingleUrlParameters("name", name)); 107 | } 108 | 109 | if (node != null) { 110 | params.add(new SingleUrlParameters("node", node)); 111 | } 112 | 113 | if (service != null) { 114 | params.add(new SingleUrlParameters("service", service)); 115 | } 116 | 117 | if (tag != null) { 118 | params.add(new SingleUrlParameters("tag", tag)); 119 | } 120 | 121 | if (queryParams != null) { 122 | params.add(queryParams); 123 | } 124 | 125 | if (token != null) { 126 | params.add(new SingleUrlParameters("token", token)); 127 | } 128 | 129 | return params; 130 | } 131 | 132 | @Override 133 | public boolean equals(Object o) { 134 | if (this == o) { 135 | return true; 136 | } 137 | if (!(o instanceof EventListRequest)) { 138 | return false; 139 | } 140 | EventListRequest that = (EventListRequest) o; 141 | return Objects.equals(name, that.name) && 142 | Objects.equals(node, that.node) && 143 | Objects.equals(service, that.service) && 144 | Objects.equals(tag, that.tag) && 145 | Objects.equals(queryParams, that.queryParams) && 146 | Objects.equals(token, that.token); 147 | } 148 | 149 | @Override 150 | public int hashCode() { 151 | return Objects.hash(name, node, service, tag, queryParams, token); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/kv/KeyValueClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.kv; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.kv.model.GetBinaryValue; 6 | import com.ecwid.consul.v1.kv.model.GetValue; 7 | import com.ecwid.consul.v1.kv.model.PutParams; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author Vasily Vasilkov (vgv@ecwid.com) 13 | */ 14 | public interface KeyValueClient { 15 | 16 | public Response getKVValue(String key); 17 | 18 | public Response getKVValue(String key, String token); 19 | 20 | public Response getKVValue(String key, QueryParams queryParams); 21 | 22 | public Response getKVValue(String key, String token, QueryParams queryParams); 23 | 24 | 25 | public Response getKVBinaryValue(String key); 26 | 27 | public Response getKVBinaryValue(String key, String token); 28 | 29 | public Response getKVBinaryValue(String key, QueryParams queryParams); 30 | 31 | public Response getKVBinaryValue(String key, String token, QueryParams queryParams); 32 | 33 | 34 | public Response> getKVValues(String keyPrefix); 35 | 36 | public Response> getKVValues(String keyPrefix, String token); 37 | 38 | public Response> getKVValues(String keyPrefix, QueryParams queryParams); 39 | 40 | public Response> getKVValues(String keyPrefix, String token, QueryParams queryParams); 41 | 42 | 43 | public Response> getKVBinaryValues(String keyPrefix); 44 | 45 | public Response> getKVBinaryValues(String keyPrefix, String token); 46 | 47 | public Response> getKVBinaryValues(String keyPrefix, QueryParams queryParams); 48 | 49 | public Response> getKVBinaryValues(String keyPrefix, String token, QueryParams queryParams); 50 | 51 | 52 | public Response> getKVKeysOnly(String keyPrefix); 53 | 54 | public Response> getKVKeysOnly(String keyPrefix, String separator, String token); 55 | 56 | public Response> getKVKeysOnly(String keyPrefix, QueryParams queryParams); 57 | 58 | public Response> getKVKeysOnly(String keyPrefix, String separator, String token, QueryParams queryParams); 59 | 60 | 61 | public Response setKVValue(String key, String value); 62 | 63 | public Response setKVValue(String key, String value, PutParams putParams); 64 | 65 | public Response setKVValue(String key, String value, String token, PutParams putParams); 66 | 67 | public Response setKVValue(String key, String value, QueryParams queryParams); 68 | 69 | public Response setKVValue(String key, String value, PutParams putParams, QueryParams queryParams); 70 | 71 | public Response setKVValue(String key, String value, String token, PutParams putParams, QueryParams queryParams); 72 | 73 | 74 | public Response setKVBinaryValue(String key, byte[] value); 75 | 76 | public Response setKVBinaryValue(String key, byte[] value, PutParams putParams); 77 | 78 | public Response setKVBinaryValue(String key, byte[] value, String token, PutParams putParams); 79 | 80 | public Response setKVBinaryValue(String key, byte[] value, QueryParams queryParams); 81 | 82 | public Response setKVBinaryValue(String key, byte[] value, PutParams putParams, QueryParams queryParams); 83 | 84 | public Response setKVBinaryValue(String key, byte[] value, String token, PutParams putParams, QueryParams queryParams); 85 | 86 | 87 | public Response deleteKVValue(String key); 88 | 89 | public Response deleteKVValue(String key, String token); 90 | 91 | public Response deleteKVValue(String key, QueryParams queryParams); 92 | 93 | public Response deleteKVValue(String key, String token, QueryParams queryParams); 94 | 95 | 96 | public Response deleteKVValues(String key); 97 | 98 | public Response deleteKVValues(String key, String token); 99 | 100 | public Response deleteKVValues(String key, QueryParams queryParams); 101 | 102 | public Response deleteKVValues(String key, String token, QueryParams queryParams); 103 | 104 | } 105 | -------------------------------------------------------------------------------- /src/test/resources/ssl/key.crt: -------------------------------------------------------------------------------- 1 | Bag Attributes 2 | friendlyName: key 3 | localKeyID: 54 69 6D 65 20 31 35 30 34 32 34 31 36 37 37 34 34 34 4 | subject=/C=UK/ST=Unknown/L=London/O=TestOrg/OU=OU/CN=localBox 5 | issuer=/C=UK/L=London/O=TestOrgCA/CN=test.org 6 | -----BEGIN CERTIFICATE----- 7 | MIIFlTCCA32gAwIBAgIEAuGZ1zANBgkqhkiG9w0BAQ0FADBFMQswCQYDVQQGEwJV 8 | SzEPMA0GA1UEBxMGTG9uZG9uMRIwEAYDVQQKEwlUZXN0T3JnQ0ExETAPBgNVBAMT 9 | CHRlc3Qub3JnMB4XDTE3MDkwMTA0NTQzNVoXDTI3MDgzMDA0NTQzNVowYjELMAkG 10 | A1UEBhMCVUsxEDAOBgNVBAgTB1Vua25vd24xDzANBgNVBAcTBkxvbmRvbjEQMA4G 11 | A1UEChMHVGVzdE9yZzELMAkGA1UECxMCT1UxETAPBgNVBAMTCGxvY2FsQm94MIIC 12 | IjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA1FfeQBecQnmND29M+Fm1deTU 13 | Zn/3bj4B3kRkOWJuKp4j3qOgpgFSqKwSC3Q75h3kGu0rrNb5tIFt8m4+hAARz3yk 14 | x9iUySu9lQYl08O4hZ7XOdAvSa9ERKTuA8c9BMxyxBi1RZlHY4z3gdmC9E9xgiNS 15 | JcXww9acS6GGAET1lipfPojwI3DiYBZX3IfIx+YSsng2m8odnQp0h1/S1r7GL/dl 16 | 6mVhZ6zkbw4u7zZFwEYgSLfscjzVNMq9dn82FiSJaNd6+194bChPj49+imJEUSIr 17 | p82ZlL7RrclCL2jtZw1WrP1nAK7MV8YC2/WGLfLbURU8WZvFTOh2xTZ35vgKjP7X 18 | WflIC1SNXR71owvTsydedntnwbJbWr8QU41tDV/t9KAYmr6tyO+Lp9KUcwoBJ6pR 19 | J4UH+KI1KU9Q3NFe/tvXFn6fZjzFbiIEh0w5A6iqAckFknK5RY8DVIiwK2ruIOnc 20 | RhEnEIjLqCkPAPGGuE28iiW3GsUIZBPJw3cwCcd/t88mpM5su1vyUdcv4YmH5kRQ 21 | d1wuI+7zpTly5xlIFUxlWwTRn9DNbO7iM8THhkERz707q/bTn1qyeS158OES0+oS 22 | TzXjbu1OKhkesA9lceDKHS/8w/sPxlnZ+GCrYybxTTZaUQQJCudZY8/+0AzaxmZa 23 | BUcavl/kTCc1Bf5HqVsCAwEAAaNwMG4wHwYDVR0jBBgwFoAU5isx0k5n7eD0Kn87 24 | n6iFLYAu454wLAYDVR0RBCUwI4IJbG9jYWxob3N0hwR/AAABhxAAAAAAAAAAAAAA 25 | AAAAAAABMB0GA1UdDgQWBBSSvABn/uI0ZC4oPmHn6dofscIwMTANBgkqhkiG9w0B 26 | AQ0FAAOCAgEAE5jMRsL0TJsFsLvqVGP7+JLt3lK2f3FJNXKy9CgatTpraHVLxuY/ 27 | DQVSA24P/ZdzApVHpHHxUawjGmQtbPl6ndvrVhjVDaYeVYM+2StxqwxMc9ONplVC 28 | SqJGKlKB8PaG6YeM7q3uLi8TRcxMgx+x2UZXeJ+czrmRsY8v92/6SnD6n0AIKd+X 29 | xGg2NwhwkPlAmaNlSQLdI1PneD6pWN3q1Ss+4RjA6jrjRT5lfiF2o0rfGrlEuN8s 30 | TwsguVlBx2eUGPnOVobCGCYr/vemwO4N5ylSMmXbvqZYpiKD+mmtXoBar+9PQ52c 31 | ZDOkOACtr6W78v7ML0hxmPYNb9X9xgJa9e9DBKLojvECZ4V5eKoEUPzkUu1u1/7s 32 | 2CjceoJ5VRY2ewmpdG+BPP1ZKCbe69uL7fXFZF2hJ0x1HpGZcEXd74dzMPErpjns 33 | qVOp7CmR9Sx6yGHhVJFVqyqb89T3mnH7/0ANfR1Z0ovCwxkVVmNFSwvaNSTr7l+3 34 | Vr/otYDNEqpqfo4PCb3KfcZEHtg7fxiyCTLQV6u0ZFsr0OcqeQn+81ulZuKEa/Se 35 | mWt0cZWrfSg47TZHTCKk2IWmavuGl8uNNubgkahVtxWa1jFP9FBiHbKZl8sAzTlr 36 | d/d2SZHRy3suADja/t2t+Sf9zk6d6x/WrSbcXuu31AhW0T3J9crJKcw= 37 | -----END CERTIFICATE----- 38 | Bag Attributes 39 | friendlyName: CN=test.org,O=TestOrgCA,L=London,C=UK 40 | subject=/C=UK/L=London/O=TestOrgCA/CN=test.org 41 | issuer=/C=UK/L=London/O=TestOrgCA/CN=test.org 42 | -----BEGIN CERTIFICATE----- 43 | MIIFNzCCAx+gAwIBAgIEPzow5DANBgkqhkiG9w0BAQ0FADBFMQswCQYDVQQGEwJV 44 | SzEPMA0GA1UEBxMGTG9uZG9uMRIwEAYDVQQKEwlUZXN0T3JnQ0ExETAPBgNVBAMT 45 | CHRlc3Qub3JnMB4XDTE3MDkwMTA0NTQzMFoXDTI3MDgzMDA0NTQzMFowRTELMAkG 46 | A1UEBhMCVUsxDzANBgNVBAcTBkxvbmRvbjESMBAGA1UEChMJVGVzdE9yZ0NBMREw 47 | DwYDVQQDEwh0ZXN0Lm9yZzCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIB 48 | AIEm7NA3+Eq6cRyBNH2ZIfXulCdfa+c/aH0u1mgpvd8al6B1BflltUqC01woLNT9 49 | 1sfAB0XvYXelpb6GuZJ+NJzwL2YUvBMmHaKmcWaHDq/fQp5mg5wvKTIUb8/qhbdt 50 | p7v2S3lyyH4aXNN0X8JGLPjrBKTPFI6yDpYfCnxXjL6v6VC2QkxXOeMxtLLt9Xbu 51 | +RN1n+Sc9uLwF/yCNfLigF6RTCRjhj20Hn4PPJRcPra/DzDJpjBnug/MesF29MmD 52 | T8vP3lgFDPftWOwp8f2xb8tWNLwa5yzQztHOro1+TPcnXqaTJ/wn9TF6YVYOFHDb 53 | TEthOK1SBCUkC5EtAYF5qvvAraOXZlifrH5ycFq4yYXMo16rKb90XyfdpITbx7vE 54 | wrmJlXoNGR1ptopTwD84oFVSDy2a4AUEr6lAeYsQYZ0XTandz7hTxI6eX5sCk6N6 55 | aSULSnCEGo5qkoy8QVOzLxRC4m9IP2/zPdys93xRMk+Q8C8hp7w6DUQOJiWdFSa1 56 | 49ZVECmhQ9gst+fqR+CDKq1gvN/GdivMHAwkt6ekEJnyNzJ59kLXGF1r/x1CO50I 57 | WsQ3gUDbOwLzwx/zWdsjq+AlaDGqAx3QyX6QoTss7Vv/5y+CMedDNpoQLiSEldQu 58 | GtxV6yWgiQtDd+W2UfCAywhT93Paobw7ZhTInavWfdWjAgMBAAGjLzAtMAwGA1Ud 59 | EwQFMAMBAf8wHQYDVR0OBBYEFOYrMdJOZ+3g9Cp/O5+ohS2ALuOeMA0GCSqGSIb3 60 | DQEBDQUAA4ICAQBNQhPjx9dGcVnLwBnLIwHyuz32c7snCNX9dUuVuk4lJBKKVJfB 61 | hiZ8NhN3wMldLXXhzowgSDXS1ZCsCjWyCsdIDV8rk9L8jSiief9is57fx+Ejk+gM 62 | KPdFCEcurBgf9iQKctE5LUeiK9R6twcmu3fj4HlDhtwrfHR/ARZIP5Hn8ta51RIn 63 | E723tZLk52IITQsuLsvWHh7eVJdn8aVyeVVse4/SlrN55d6ENPnSokPkfhS56R4f 64 | +KJK2dD1xhm24SuoynJ7SKWGiIj2VSu/i/Iq4aerOlqvFf5v4wTY017mZ1ngnlHn 65 | M8PI7wGs4h61evXdNt24JzL5FqVRAsyhhv+DLv/z6czuA6/sGhJpE3ZZ3RreGwMm 66 | i5vnoFayGPYbhAVsEXbpYSONQkzrCMi/jF2EEdqriKBrLE25IpMgRLbqYnb7h2mh 67 | ZBB6HtbKU7Iv4e3UNjSUszOhrmoGnQjTnWv6di7ONf92QdFbBuKPoPw99LfTv8/x 68 | ro+dOib4x6mrMlFlg7KxGJ+MzUm59cUxpeCDuYanFr3Ja+rTcQpDumkC2wB/ziJc 69 | ribv3X9ZXn7JxhoyWYnItvDOSBy7G9oxts4hYOzFV3Rj+fIzlECbUftDYjpRH3nF 70 | GRZBeLI14QIn2tcCi98yc8E/1okjqklynmYNSJIvUmzn4GRS+IS4ZhiW6A== 71 | -----END CERTIFICATE----- 72 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/CatalogClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import com.ecwid.consul.v1.QueryParams; 4 | import com.ecwid.consul.v1.Response; 5 | import com.ecwid.consul.v1.catalog.model.CatalogDeregistration; 6 | import com.ecwid.consul.v1.catalog.model.CatalogNode; 7 | import com.ecwid.consul.v1.catalog.model.CatalogRegistration; 8 | import com.ecwid.consul.v1.catalog.model.Node; 9 | 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | * @author Vasily Vasilkov (vgv@ecwid.com) 15 | */ 16 | public interface CatalogClient { 17 | 18 | public Response catalogRegister(CatalogRegistration catalogRegistration); 19 | 20 | public Response catalogRegister(CatalogRegistration catalogRegistration, String token); 21 | 22 | // ------------------------------------------------------------------------------- 23 | 24 | public Response catalogDeregister(CatalogDeregistration catalogDeregistration); 25 | 26 | public Response catalogDeregister(CatalogDeregistration catalogDeregistration, String token); 27 | 28 | // ------------------------------------------------------------------------------- 29 | 30 | public Response> getCatalogDatacenters(); 31 | 32 | // ------------------------------------------------------------------------------- 33 | 34 | /** 35 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogNodes(CatalogNodesRequest catalogNodesRequest)} 36 | */ 37 | @Deprecated 38 | public Response> getCatalogNodes(QueryParams queryParams); 39 | 40 | public Response> getCatalogNodes(CatalogNodesRequest catalogNodesRequest); 41 | 42 | // ------------------------------------------------------------------------------- 43 | 44 | /** 45 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogServices(CatalogServicesRequest catalogServicesRequest)} 46 | */ 47 | @Deprecated 48 | public Response>> getCatalogServices(QueryParams queryParams); 49 | 50 | /** 51 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogServices(CatalogServicesRequest catalogServicesRequest)} 52 | */ 53 | @Deprecated 54 | public Response>> getCatalogServices(QueryParams queryParams, String token); 55 | 56 | public Response>> getCatalogServices(CatalogServicesRequest catalogServicesRequest); 57 | 58 | // ------------------------------------------------------------------------------- 59 | 60 | /** 61 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogService(String serviceName, CatalogServiceRequest catalogServiceRequest)} 62 | */ 63 | @Deprecated 64 | public Response> getCatalogService(String serviceName, QueryParams queryParams); 65 | 66 | /** 67 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogService(String serviceName, CatalogServiceRequest catalogServiceRequest)} 68 | */ 69 | @Deprecated 70 | public Response> getCatalogService(String serviceName, String tag, QueryParams queryParams); 71 | 72 | /** 73 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogService(String serviceName, CatalogServiceRequest catalogServiceRequest)} 74 | */ 75 | @Deprecated 76 | public Response> getCatalogService(String serviceName, QueryParams queryParams, String token); 77 | 78 | /** 79 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogService(String serviceName, CatalogServiceRequest catalogServiceRequest)} 80 | */ 81 | @Deprecated 82 | public Response> getCatalogService(String serviceName, String tag, QueryParams queryParams, String token); 83 | 84 | /** 85 | * @deprecated This method will be removed in consul-api 2.0. Use {@link #getCatalogService(String serviceName, CatalogServiceRequest catalogServiceRequest)} 86 | */ 87 | @Deprecated 88 | public Response> getCatalogService(String serviceName, String[] tags, QueryParams queryParams, String token); 89 | 90 | public Response> getCatalogService(String serviceName, CatalogServiceRequest catalogServiceRequest); 91 | 92 | // ------------------------------------------------------------------------------- 93 | 94 | public Response getCatalogNode(String nodeName, QueryParams queryParams); 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/QueryParams.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1; 2 | 3 | import com.ecwid.consul.UrlParameters; 4 | import com.ecwid.consul.Utils; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.Objects; 9 | 10 | /** 11 | * @author Vasily Vasilkov (vgv@ecwid.com) 12 | */ 13 | public final class QueryParams implements UrlParameters { 14 | public static final class Builder { 15 | public static Builder builder() { 16 | return new Builder(); 17 | } 18 | 19 | private String datacenter; 20 | private ConsistencyMode consistencyMode; 21 | private long waitTime; 22 | private long index; 23 | private String near; 24 | 25 | private Builder() { 26 | this.datacenter = null; 27 | this.consistencyMode = ConsistencyMode.DEFAULT; 28 | this.waitTime = -1; 29 | this.index = -1; 30 | this.near = null; 31 | } 32 | 33 | public Builder setConsistencyMode(ConsistencyMode consistencyMode) { 34 | this.consistencyMode = consistencyMode; 35 | return this; 36 | } 37 | 38 | public Builder setDatacenter(String datacenter) { 39 | this.datacenter = datacenter; 40 | return this; 41 | } 42 | 43 | public Builder setWaitTime(long waitTime) { 44 | this.waitTime = waitTime; 45 | return this; 46 | } 47 | 48 | public Builder setIndex(long index) { 49 | this.index = index; 50 | return this; 51 | } 52 | 53 | public Builder setNear(String near) { 54 | this.near = near; 55 | return this; 56 | } 57 | 58 | public QueryParams build() { 59 | return new QueryParams(datacenter, consistencyMode, waitTime, index, near); 60 | } 61 | } 62 | 63 | public static final QueryParams DEFAULT = new QueryParams(ConsistencyMode.DEFAULT); 64 | 65 | private final String datacenter; 66 | private final ConsistencyMode consistencyMode; 67 | private final long waitTime; 68 | private final long index; 69 | private final String near; 70 | 71 | private QueryParams(String datacenter, ConsistencyMode consistencyMode, long waitTime, long index, String near) { 72 | this.datacenter = datacenter; 73 | this.consistencyMode = consistencyMode; 74 | this.waitTime = waitTime; 75 | this.index = index; 76 | this.near = near; 77 | } 78 | 79 | private QueryParams(String datacenter, ConsistencyMode consistencyMode, long waitTime, long index) { 80 | this(datacenter, consistencyMode, waitTime, index, null); 81 | } 82 | 83 | public QueryParams(String datacenter) { 84 | this(datacenter, ConsistencyMode.DEFAULT, -1, -1); 85 | } 86 | 87 | public QueryParams(ConsistencyMode consistencyMode) { 88 | this(null, consistencyMode, -1, -1); 89 | } 90 | 91 | public QueryParams(String datacenter, ConsistencyMode consistencyMode) { 92 | this(datacenter, consistencyMode, -1, -1); 93 | } 94 | 95 | public QueryParams(long waitTime, long index) { 96 | this(null, ConsistencyMode.DEFAULT, waitTime, index); 97 | } 98 | 99 | public QueryParams(String datacenter, long waitTime, long index) { 100 | this(datacenter, ConsistencyMode.DEFAULT, waitTime, index, null); 101 | } 102 | 103 | public String getDatacenter() { 104 | return datacenter; 105 | } 106 | 107 | public ConsistencyMode getConsistencyMode() { 108 | return consistencyMode; 109 | } 110 | 111 | public long getWaitTime() { 112 | return waitTime; 113 | } 114 | 115 | public long getIndex() { 116 | return index; 117 | } 118 | 119 | public String getNear() { 120 | return near; 121 | } 122 | 123 | @Override 124 | public List toUrlParameters() { 125 | List params = new ArrayList(); 126 | 127 | // add basic params 128 | if (datacenter != null) { 129 | params.add("dc=" + Utils.encodeValue(datacenter)); 130 | } 131 | 132 | if (consistencyMode != ConsistencyMode.DEFAULT) { 133 | params.add(consistencyMode.name().toLowerCase()); 134 | } 135 | 136 | if (waitTime != -1) { 137 | params.add("wait=" + Utils.toSecondsString(waitTime)); 138 | } 139 | 140 | if (index != -1) { 141 | params.add("index=" + Long.toUnsignedString(index)); 142 | } 143 | 144 | if (near != null) { 145 | params.add("near=" + Utils.encodeValue(near)); 146 | } 147 | 148 | return params; 149 | } 150 | 151 | @Override 152 | public boolean equals(Object o) { 153 | if (this == o) { 154 | return true; 155 | } 156 | if (!(o instanceof QueryParams)) { 157 | return false; 158 | } 159 | QueryParams that = (QueryParams) o; 160 | return waitTime == that.waitTime && 161 | index == that.index && 162 | Objects.equals(datacenter, that.datacenter) && 163 | consistencyMode == that.consistencyMode && 164 | Objects.equals(near, that.near); 165 | } 166 | 167 | @Override 168 | public int hashCode() { 169 | return Objects.hash(datacenter, consistencyMode, waitTime, index, near); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/CatalogServiceRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog; 2 | 3 | import com.ecwid.consul.ConsulRequest; 4 | import com.ecwid.consul.SingleUrlParameters; 5 | import com.ecwid.consul.v1.TagsParameters; 6 | import com.ecwid.consul.UrlParameters; 7 | import com.ecwid.consul.v1.NodeMetaParameters; 8 | import com.ecwid.consul.v1.QueryParams; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.Collections; 13 | import java.util.List; 14 | import java.util.Map; 15 | import java.util.Objects; 16 | 17 | public final class CatalogServiceRequest implements ConsulRequest { 18 | 19 | private final String datacenter; 20 | private final String[] tags; 21 | private final String near; 22 | private final Map nodeMeta; 23 | private final QueryParams queryParams; 24 | private final String token; 25 | 26 | private CatalogServiceRequest(String datacenter, String[] tags, String near, Map nodeMeta, QueryParams queryParams, String token) { 27 | this.datacenter = datacenter; 28 | this.tags = tags; 29 | this.near = near; 30 | this.nodeMeta = nodeMeta; 31 | this.queryParams = queryParams; 32 | this.token = token; 33 | } 34 | 35 | public String getDatacenter() { 36 | return datacenter; 37 | } 38 | 39 | public String getTag() { 40 | return tags != null && tags.length > 0 ? tags[0] : null; 41 | } 42 | 43 | public String[] getTags() { 44 | return tags; 45 | } 46 | 47 | public String getNear() { 48 | return near; 49 | } 50 | 51 | public Map getNodeMeta() { 52 | return nodeMeta; 53 | } 54 | 55 | public QueryParams getQueryParams() { 56 | return queryParams; 57 | } 58 | 59 | public String getToken() { 60 | return token; 61 | } 62 | 63 | public static class Builder { 64 | private String datacenter; 65 | private String[] tags; 66 | private String near; 67 | private Map nodeMeta; 68 | private QueryParams queryParams; 69 | private String token; 70 | 71 | private Builder() { 72 | } 73 | 74 | public Builder setDatacenter(String datacenter) { 75 | this.datacenter = datacenter; 76 | return this; 77 | } 78 | 79 | public Builder setTag(String tag) { 80 | this.tags = new String[]{tag}; 81 | return this; 82 | } 83 | 84 | public Builder setTags(String[] tags) { 85 | this.tags = tags; 86 | return this; 87 | } 88 | 89 | public Builder setNear(String near) { 90 | this.near = near; 91 | return this; 92 | } 93 | 94 | public Builder setNodeMeta(Map nodeMeta) { 95 | if (nodeMeta == null) { 96 | this.nodeMeta = null; 97 | } else { 98 | this.nodeMeta = Collections.unmodifiableMap(nodeMeta); 99 | } 100 | 101 | return this; 102 | } 103 | 104 | public Builder setQueryParams(QueryParams queryParams) { 105 | this.queryParams = queryParams; 106 | return this; 107 | } 108 | 109 | public Builder setToken(String token) { 110 | this.token = token; 111 | return this; 112 | } 113 | 114 | public CatalogServiceRequest build() { 115 | return new CatalogServiceRequest(datacenter, tags, near, nodeMeta, queryParams, token); 116 | } 117 | } 118 | 119 | public static Builder newBuilder() { 120 | return new Builder(); 121 | } 122 | 123 | @Override 124 | public List asUrlParameters() { 125 | List params = new ArrayList<>(); 126 | 127 | if (datacenter != null) { 128 | params.add(new SingleUrlParameters("dc", datacenter)); 129 | } 130 | 131 | if (tags != null) { 132 | params.add(new TagsParameters(tags)); 133 | } 134 | 135 | if (near != null) { 136 | params.add(new SingleUrlParameters("near", near)); 137 | } 138 | 139 | if (nodeMeta != null) { 140 | params.add(new NodeMetaParameters(nodeMeta)); 141 | } 142 | 143 | if (queryParams != null) { 144 | params.add(queryParams); 145 | } 146 | 147 | if (token != null) { 148 | params.add(new SingleUrlParameters("token", token)); 149 | } 150 | 151 | return params; 152 | } 153 | 154 | @Override 155 | public boolean equals(Object o) { 156 | if (this == o) { 157 | return true; 158 | } 159 | if (!(o instanceof CatalogServiceRequest)) { 160 | return false; 161 | } 162 | CatalogServiceRequest that = (CatalogServiceRequest) o; 163 | return Objects.equals(datacenter, that.datacenter) && 164 | Arrays.equals(tags, that.tags) && 165 | Objects.equals(near, that.near) && 166 | Objects.equals(nodeMeta, that.nodeMeta) && 167 | Objects.equals(queryParams, that.queryParams) && 168 | Objects.equals(token, that.token); 169 | } 170 | 171 | @Override 172 | public int hashCode() { 173 | int result = Objects.hash(datacenter, near, nodeMeta, queryParams, token); 174 | result = 31 * result + Arrays.hashCode(tags); 175 | return result; 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/health/HealthServicesRequest.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health; 2 | 3 | import com.ecwid.consul.ConsulRequest; 4 | import com.ecwid.consul.SingleUrlParameters; 5 | import com.ecwid.consul.v1.TagsParameters; 6 | import com.ecwid.consul.UrlParameters; 7 | import com.ecwid.consul.v1.NodeMetaParameters; 8 | import com.ecwid.consul.v1.QueryParams; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.Collections; 13 | import java.util.List; 14 | import java.util.Map; 15 | import java.util.Objects; 16 | 17 | public final class HealthServicesRequest implements ConsulRequest { 18 | 19 | private final String datacenter; 20 | private final String near; 21 | private final String[] tags; 22 | private final Map nodeMeta; 23 | private final boolean passing; 24 | private final QueryParams queryParams; 25 | private final String token; 26 | 27 | private HealthServicesRequest(String datacenter, String near, String[] tags, Map nodeMeta, boolean passing, QueryParams queryParams, String token) { 28 | this.datacenter = datacenter; 29 | this.near = near; 30 | this.tags = tags; 31 | this.nodeMeta = nodeMeta; 32 | this.passing = passing; 33 | this.queryParams = queryParams; 34 | this.token = token; 35 | } 36 | 37 | public String getDatacenter() { 38 | return datacenter; 39 | } 40 | 41 | public String getNear() { 42 | return near; 43 | } 44 | 45 | public String getTag() { 46 | return tags != null && tags.length > 0 ? tags[0] : null; 47 | } 48 | 49 | public String[] getTags() { 50 | return tags; 51 | } 52 | 53 | public Map getNodeMeta() { 54 | return nodeMeta; 55 | } 56 | 57 | public boolean isPassing() { 58 | return passing; 59 | } 60 | 61 | public QueryParams getQueryParams() { 62 | return queryParams; 63 | } 64 | 65 | public String getToken() { 66 | return token; 67 | } 68 | 69 | public static class Builder { 70 | private String datacenter; 71 | private String near; 72 | private String[] tags; 73 | private Map nodeMeta; 74 | private boolean passing; 75 | private QueryParams queryParams; 76 | private String token; 77 | 78 | private Builder() { 79 | } 80 | 81 | public Builder setDatacenter(String datacenter) { 82 | this.datacenter = datacenter; 83 | return this; 84 | } 85 | 86 | public Builder setNear(String near) { 87 | this.near = near; 88 | return this; 89 | } 90 | 91 | public Builder setTag(String tag) { 92 | this.tags = new String[]{tag}; 93 | return this; 94 | } 95 | 96 | public Builder setTags(String[] tags) { 97 | this.tags = tags; 98 | return this; 99 | } 100 | 101 | public Builder setNodeMeta(Map nodeMeta) { 102 | this.nodeMeta = nodeMeta != null ? Collections.unmodifiableMap(nodeMeta) : null; 103 | return this; 104 | } 105 | 106 | public Builder setPassing(boolean passing) { 107 | this.passing = passing; 108 | return this; 109 | } 110 | 111 | public Builder setQueryParams(QueryParams queryParams) { 112 | this.queryParams = queryParams; 113 | return this; 114 | } 115 | 116 | public Builder setToken(String token) { 117 | this.token = token; 118 | return this; 119 | } 120 | 121 | public HealthServicesRequest build() { 122 | return new HealthServicesRequest(datacenter, near, tags, nodeMeta, passing, queryParams, token); 123 | } 124 | } 125 | 126 | public static Builder newBuilder() { 127 | return new Builder(); 128 | } 129 | 130 | @Override 131 | public List asUrlParameters() { 132 | List params = new ArrayList<>(); 133 | 134 | if (datacenter != null) { 135 | params.add(new SingleUrlParameters("dc", datacenter)); 136 | } 137 | 138 | if (near != null) { 139 | params.add(new SingleUrlParameters("near", near)); 140 | } 141 | 142 | if (tags != null) { 143 | params.add(new TagsParameters(tags)); 144 | } 145 | 146 | if (nodeMeta != null) { 147 | params.add(new NodeMetaParameters(nodeMeta)); 148 | } 149 | 150 | params.add(new SingleUrlParameters("passing", String.valueOf(passing))); 151 | 152 | if (queryParams != null) { 153 | params.add(queryParams); 154 | } 155 | 156 | if (token != null) { 157 | params.add(new SingleUrlParameters("token", token)); 158 | } 159 | 160 | return params; 161 | } 162 | 163 | @Override 164 | public boolean equals(Object o) { 165 | if (this == o) { 166 | return true; 167 | } 168 | if (!(o instanceof HealthServicesRequest)) { 169 | return false; 170 | } 171 | HealthServicesRequest that = (HealthServicesRequest) o; 172 | return passing == that.passing && 173 | Objects.equals(datacenter, that.datacenter) && 174 | Objects.equals(near, that.near) && 175 | Arrays.equals(tags, that.tags) && 176 | Objects.equals(nodeMeta, that.nodeMeta) && 177 | Objects.equals(queryParams, that.queryParams) && 178 | Objects.equals(token, that.token); 179 | } 180 | 181 | @Override 182 | public int hashCode() { 183 | int result = Objects.hash(datacenter, near, nodeMeta, passing, queryParams, token); 184 | result = 31 * result + Arrays.hashCode(tags); 185 | return result; 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/transport/AbstractHttpTransport.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.transport; 2 | 3 | import org.apache.http.Header; 4 | import org.apache.http.HeaderIterator; 5 | import org.apache.http.client.HttpClient; 6 | import org.apache.http.client.methods.*; 7 | import org.apache.http.entity.ByteArrayEntity; 8 | import org.apache.http.entity.StringEntity; 9 | import org.apache.http.util.EntityUtils; 10 | 11 | import java.io.IOException; 12 | import java.nio.charset.StandardCharsets; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | import java.util.Map; 16 | import java.util.logging.Logger; 17 | import java.util.stream.StreamSupport; 18 | 19 | public abstract class AbstractHttpTransport implements HttpTransport { 20 | 21 | private static final Logger log = Logger.getLogger(AbstractHttpTransport.class.getName()); 22 | 23 | static final int DEFAULT_MAX_CONNECTIONS = 1000; 24 | static final int DEFAULT_MAX_PER_ROUTE_CONNECTIONS = 500; 25 | static final int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000; // 10 sec 26 | 27 | // 10 minutes for read timeout due to blocking queries timeout 28 | // https://www.consul.io/api/index.html#blocking-queries 29 | static final int DEFAULT_READ_TIMEOUT = 1000 * 60 * 10; // 10 min 30 | 31 | @Override 32 | public HttpResponse makeGetRequest(HttpRequest request) { 33 | HttpGet httpGet = new HttpGet(request.getUrl()); 34 | addHeadersToRequest(httpGet, request.getHeaders()); 35 | 36 | return executeRequest(httpGet); 37 | } 38 | 39 | @Override 40 | public HttpResponse makePutRequest(HttpRequest request) { 41 | HttpPut httpPut = new HttpPut(request.getUrl()); 42 | addHeadersToRequest(httpPut, request.getHeaders()); 43 | if (request.getContent() != null) { 44 | httpPut.setEntity(new StringEntity(request.getContent(), StandardCharsets.UTF_8)); 45 | } else { 46 | httpPut.setEntity(new ByteArrayEntity(request.getBinaryContent())); 47 | } 48 | 49 | return executeRequest(httpPut); 50 | } 51 | 52 | @Override 53 | public HttpResponse makeDeleteRequest(HttpRequest request) { 54 | HttpDelete httpDelete = new HttpDelete(request.getUrl()); 55 | addHeadersToRequest(httpDelete, request.getHeaders()); 56 | return executeRequest(httpDelete); 57 | } 58 | 59 | /** 60 | * You should override this method to instantiate ready to use HttpClient 61 | * 62 | * @return HttpClient 63 | */ 64 | protected abstract HttpClient getHttpClient(); 65 | 66 | private HttpResponse executeRequest(HttpUriRequest httpRequest) { 67 | logRequest(httpRequest); 68 | 69 | try { 70 | return getHttpClient().execute(httpRequest, response -> { 71 | int statusCode = response.getStatusLine().getStatusCode(); 72 | String statusMessage = response.getStatusLine().getReasonPhrase(); 73 | 74 | String content = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); 75 | 76 | Long consulIndex = parseUnsignedLong(response.getFirstHeader("X-Consul-Index")); 77 | Boolean consulKnownLeader = parseBoolean(response.getFirstHeader("X-Consul-Knownleader")); 78 | Long consulLastContact = parseUnsignedLong(response.getFirstHeader("X-Consul-Lastcontact")); 79 | 80 | return new HttpResponse(statusCode, statusMessage, content, consulIndex, consulKnownLeader, consulLastContact); 81 | }); 82 | } catch (IOException e) { 83 | throw new TransportException(e); 84 | } 85 | } 86 | 87 | private Long parseUnsignedLong(Header header) { 88 | if (header == null) { 89 | return null; 90 | } 91 | 92 | String value = header.getValue(); 93 | if (value == null) { 94 | return null; 95 | } 96 | 97 | try { 98 | return Long.parseUnsignedLong(value); 99 | } catch (Exception e) { 100 | return null; 101 | } 102 | } 103 | 104 | private Boolean parseBoolean(Header header) { 105 | if (header == null) { 106 | return null; 107 | } 108 | 109 | if ("true".equals(header.getValue())) { 110 | return true; 111 | } 112 | 113 | if ("false".equals(header.getValue())) { 114 | return false; 115 | } 116 | 117 | return null; 118 | } 119 | 120 | private void addHeadersToRequest(HttpRequestBase request, Map headers) { 121 | if (headers == null) { 122 | return; 123 | } 124 | 125 | for (Map.Entry headerValue : headers.entrySet()) { 126 | String name = headerValue.getKey(); 127 | String value = headerValue.getValue(); 128 | 129 | request.addHeader(name, value); 130 | } 131 | } 132 | 133 | private void logRequest(HttpUriRequest httpRequest) { 134 | StringBuilder sb = new StringBuilder(); 135 | 136 | // method 137 | sb.append(httpRequest.getMethod()); 138 | sb.append(" "); 139 | 140 | // url 141 | sb.append(httpRequest.getURI()); 142 | sb.append(" "); 143 | 144 | // headers, if any 145 | HeaderIterator iterator = httpRequest.headerIterator(); 146 | if (iterator.hasNext()) { 147 | sb.append("Headers:["); 148 | 149 | Header header = iterator.nextHeader(); 150 | sb.append(header.getName()).append("=").append(header.getValue()); 151 | 152 | while (iterator.hasNext()) { 153 | header = iterator.nextHeader(); 154 | sb.append(header.getName()).append("=").append(header.getValue()); 155 | sb.append(";"); 156 | } 157 | 158 | sb.append("] "); 159 | } 160 | 161 | // 162 | log.finest(sb.toString()); 163 | } 164 | 165 | } 166 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/acl/AclConsulClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.acl; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import com.ecwid.consul.ConsulException; 7 | import com.ecwid.consul.SingleUrlParameters; 8 | import com.ecwid.consul.UrlParameters; 9 | import com.ecwid.consul.json.GsonFactory; 10 | import com.ecwid.consul.transport.HttpResponse; 11 | import com.ecwid.consul.transport.TLSConfig; 12 | import com.ecwid.consul.v1.ConsulRawClient; 13 | import com.ecwid.consul.v1.OperationException; 14 | import com.ecwid.consul.v1.Response; 15 | import com.ecwid.consul.v1.acl.model.Acl; 16 | import com.ecwid.consul.v1.acl.model.NewAcl; 17 | import com.ecwid.consul.v1.acl.model.UpdateAcl; 18 | import com.google.gson.reflect.TypeToken; 19 | 20 | /** 21 | * @author Vasily Vasilkov (vgv@ecwid.com) 22 | */ 23 | public final class AclConsulClient implements AclClient { 24 | 25 | private final ConsulRawClient rawClient; 26 | 27 | public AclConsulClient(ConsulRawClient rawClient) { 28 | this.rawClient = rawClient; 29 | } 30 | 31 | public AclConsulClient() { 32 | this(new ConsulRawClient()); 33 | } 34 | 35 | public AclConsulClient(TLSConfig tlsConfig) { 36 | this(new ConsulRawClient(tlsConfig)); 37 | } 38 | 39 | public AclConsulClient(String agentHost) { 40 | this(new ConsulRawClient(agentHost)); 41 | } 42 | 43 | public AclConsulClient(String agentHost, TLSConfig tlsConfig) { 44 | this(new ConsulRawClient(agentHost, tlsConfig)); 45 | } 46 | 47 | public AclConsulClient(String agentHost, int agentPort) { 48 | this(new ConsulRawClient(agentHost, agentPort)); 49 | } 50 | 51 | public AclConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) { 52 | this(new ConsulRawClient(agentHost, agentPort, tlsConfig)); 53 | } 54 | 55 | @Override 56 | public Response aclCreate(NewAcl newAcl, String token) { 57 | UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null; 58 | String json = GsonFactory.getGson().toJson(newAcl); 59 | HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/create", json, tokenParams); 60 | 61 | if (httpResponse.getStatusCode() == 200) { 62 | Map value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 63 | }.getType()); 64 | return new Response(value.get("ID"), httpResponse); 65 | } else { 66 | throw new OperationException(httpResponse); 67 | } 68 | } 69 | 70 | @Override 71 | public Response aclUpdate(UpdateAcl updateAcl, String token) { 72 | UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null; 73 | String json = GsonFactory.getGson().toJson(updateAcl); 74 | HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/update", json, tokenParams); 75 | 76 | if (httpResponse.getStatusCode() == 200) { 77 | return new Response(null, httpResponse); 78 | } else { 79 | throw new OperationException(httpResponse); 80 | } 81 | } 82 | 83 | @Override 84 | public Response aclDestroy(String aclId, String token) { 85 | UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null; 86 | HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/destroy/" + aclId, "", tokenParams); 87 | 88 | if (httpResponse.getStatusCode() == 200) { 89 | return new Response(null, httpResponse); 90 | } else { 91 | throw new OperationException(httpResponse); 92 | } 93 | } 94 | 95 | @Override 96 | public Response getAcl(String id) { 97 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/acl/info/" + id); 98 | 99 | if (httpResponse.getStatusCode() == 200) { 100 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 101 | }.getType()); 102 | 103 | if (value.isEmpty()) { 104 | return new Response(null, httpResponse); 105 | } else if (value.size() == 1) { 106 | return new Response(value.get(0), httpResponse); 107 | } else { 108 | throw new ConsulException("Strange response (list size=" + value.size() + ")"); 109 | } 110 | } else { 111 | throw new OperationException(httpResponse); 112 | } 113 | } 114 | 115 | @Override 116 | public Response aclClone(String aclId, String token) { 117 | UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null; 118 | HttpResponse httpResponse = rawClient.makePutRequest("/v1/acl/clone/" + aclId, "", tokenParams); 119 | 120 | if (httpResponse.getStatusCode() == 200) { 121 | Map value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 122 | }.getType()); 123 | return new Response(value.get("ID"), httpResponse); 124 | } else { 125 | throw new OperationException(httpResponse); 126 | } 127 | } 128 | 129 | @Override 130 | public Response> getAclList(String token) { 131 | UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null; 132 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/acl/list", tokenParams); 133 | 134 | if (httpResponse.getStatusCode() == 200) { 135 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 136 | }.getType()); 137 | return new Response>(value, httpResponse); 138 | } else { 139 | throw new OperationException(httpResponse); 140 | } 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/query/model/QueryNode.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.query.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class QueryNode { 9 | 10 | public static class Node { 11 | @SerializedName("ID") 12 | private String id; 13 | 14 | @SerializedName("Node") 15 | private String node; 16 | 17 | @SerializedName("Address") 18 | private String address; 19 | 20 | @SerializedName("Datacenter") 21 | private String datacenter; 22 | 23 | @SerializedName("TaggedAddresses") 24 | private Map taggedAddresses; 25 | 26 | @SerializedName("Meta") 27 | private Map meta; 28 | 29 | @SerializedName("CreateIndex") 30 | private Long createIndex; 31 | 32 | @SerializedName("ModifyIndex") 33 | private Long modifyIndex; 34 | 35 | public String getId() { 36 | return id; 37 | } 38 | 39 | public void setId(String id) { 40 | this.id = id; 41 | } 42 | 43 | public String getNode() { 44 | return node; 45 | } 46 | 47 | public void setNode(String node) { 48 | this.node = node; 49 | } 50 | 51 | public String getAddress() { 52 | return address; 53 | } 54 | 55 | public void setAddress(String address) { 56 | this.address = address; 57 | } 58 | 59 | public String getDatacenter() { 60 | return datacenter; 61 | } 62 | 63 | public void setDatacenter(String datacenter) { 64 | this.datacenter = datacenter; 65 | } 66 | 67 | public Map getTaggedAddresses() { 68 | return taggedAddresses; 69 | } 70 | 71 | public void setTaggedAddresses(Map taggedAddresses) { 72 | this.taggedAddresses = taggedAddresses; 73 | } 74 | 75 | public Map getMeta() { 76 | return meta; 77 | } 78 | 79 | public void setMeta(Map meta) { 80 | this.meta = meta; 81 | } 82 | 83 | public Long getCreateIndex() { 84 | return createIndex; 85 | } 86 | 87 | public void setCreateIndex(Long createIndex) { 88 | this.createIndex = createIndex; 89 | } 90 | 91 | public Long getModifyIndex() { 92 | return modifyIndex; 93 | } 94 | 95 | public void setModifyIndex(Long modifyIndex) { 96 | this.modifyIndex = modifyIndex; 97 | } 98 | 99 | @Override 100 | public String toString() { 101 | return "Node{" + 102 | "id='" + id + '\'' + 103 | ", node='" + node + '\'' + 104 | ", address='" + address + '\'' + 105 | ", datacenter='" + datacenter + '\'' + 106 | ", taggedAddresses=" + taggedAddresses + 107 | ", meta=" + meta + 108 | ", createIndex=" + createIndex + 109 | ", modifyIndex=" + modifyIndex + 110 | '}'; 111 | } 112 | } 113 | 114 | public static class Service { 115 | @SerializedName("ID") 116 | private String id; 117 | 118 | @SerializedName("Service") 119 | private String service; 120 | 121 | @SerializedName("Tags") 122 | private List tags; 123 | 124 | @SerializedName("Address") 125 | private String address; 126 | 127 | @SerializedName("Port") 128 | private Integer port; 129 | 130 | @SerializedName("EnableTagOverride") 131 | private Boolean enableTagOverride; 132 | 133 | @SerializedName("CreateIndex") 134 | private Long createIndex; 135 | 136 | @SerializedName("ModifyIndex") 137 | private Long modifyIndex; 138 | 139 | @SerializedName("Meta") 140 | private Map meta; 141 | 142 | public String getId() { 143 | return id; 144 | } 145 | 146 | public void setId(String id) { 147 | this.id = id; 148 | } 149 | 150 | public String getService() { 151 | return service; 152 | } 153 | 154 | public void setService(String service) { 155 | this.service = service; 156 | } 157 | 158 | public List getTags() { 159 | return tags; 160 | } 161 | 162 | public void setTags(List tags) { 163 | this.tags = tags; 164 | } 165 | 166 | public String getAddress() { 167 | return address; 168 | } 169 | 170 | public void setAddress(String address) { 171 | this.address = address; 172 | } 173 | 174 | public Integer getPort() { 175 | return port; 176 | } 177 | 178 | public void setPort(Integer port) { 179 | this.port = port; 180 | } 181 | 182 | public Boolean getEnableTagOverride() { 183 | return enableTagOverride; 184 | } 185 | 186 | public void setEnableTagOverride(Boolean enableTagOverride) { 187 | this.enableTagOverride = enableTagOverride; 188 | } 189 | 190 | public Map getMeta() { 191 | return meta; 192 | } 193 | 194 | public void setMeta(Map meta) { 195 | this.meta = meta; 196 | } 197 | 198 | public Long getCreateIndex() { 199 | return createIndex; 200 | } 201 | 202 | public void setCreateIndex(Long createIndex) { 203 | this.createIndex = createIndex; 204 | } 205 | 206 | public Long getModifyIndex() { 207 | return modifyIndex; 208 | } 209 | 210 | public void setModifyIndex(Long modifyIndex) { 211 | this.modifyIndex = modifyIndex; 212 | } 213 | 214 | @Override 215 | public String toString() { 216 | return "Service{" + 217 | "id='" + id + '\'' + 218 | ", service='" + service + '\'' + 219 | ", tags=" + tags + 220 | ", address='" + address + '\'' + 221 | ", port=" + port + 222 | ", enableTagOverride=" + enableTagOverride + 223 | ", meta=" + meta + 224 | ", createIndex=" + createIndex + 225 | ", modifyIndex=" + modifyIndex + 226 | '}'; 227 | } 228 | } 229 | 230 | @SerializedName("Node") 231 | private Node node; 232 | 233 | @SerializedName("Service") 234 | private Service service; 235 | 236 | @SerializedName("Checks") 237 | private List checks; 238 | 239 | public Node getNode() { return node; } 240 | 241 | public void setNode(Node node) { this.node = node; } 242 | 243 | public Service getService() { return service; } 244 | 245 | public void setService(Service service) { this.service = service; } 246 | 247 | public List getChecks() { return checks; } 248 | 249 | public void setChecks(List checks) { this.checks = checks; } 250 | 251 | @Override 252 | public String toString() { 253 | return "QueryNode{" + 254 | "node=" + node + 255 | ", service=" + service + 256 | ", checks=" + checks + 257 | '}'; 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/agent/model/NewCheck.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.agent.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author Vasily Vasilkov (vgv@ecwid.com) 10 | */ 11 | public class NewCheck { 12 | 13 | @SerializedName("ID") 14 | private String id; 15 | 16 | @SerializedName("Name") 17 | private String name; 18 | 19 | @SerializedName("ServiceID") 20 | private String serviceId; 21 | 22 | @SerializedName("Notes") 23 | private String notes; 24 | 25 | /** 26 | * @deprecated Please use Args parameter instead 27 | */ 28 | @SerializedName("Script") 29 | @Deprecated 30 | private String script; 31 | 32 | @SerializedName("Args") 33 | private List args; 34 | 35 | @SerializedName("HTTP") 36 | private String http; 37 | 38 | @SerializedName("Method") 39 | private String method; 40 | 41 | @SerializedName("Header") 42 | private Map> header; 43 | 44 | @SerializedName("TCP") 45 | private String tcp; 46 | 47 | @SerializedName("DockerContainerID") 48 | private String dockerContainerID; 49 | 50 | @SerializedName("Shell") 51 | private String shell; 52 | 53 | @SerializedName("Interval") 54 | private String interval; 55 | 56 | @SerializedName("Timeout") 57 | private String timeout; 58 | 59 | @SerializedName("TTL") 60 | private String ttl; 61 | 62 | @SerializedName("DeregisterCriticalServiceAfter") 63 | private String deregisterCriticalServiceAfter; 64 | 65 | @SerializedName("TLSSkipVerify") 66 | private Boolean tlsSkipVerify; 67 | 68 | @SerializedName("Status") 69 | private String status; 70 | 71 | @SerializedName("GRPC") 72 | private String grpc; 73 | 74 | @SerializedName("GRPCUseTLS") 75 | private Boolean grpcUseTLS; 76 | 77 | public String getId() { 78 | return id; 79 | } 80 | 81 | public void setId(String id) { 82 | this.id = id; 83 | } 84 | 85 | public String getName() { 86 | return name; 87 | } 88 | 89 | public void setName(String name) { 90 | this.name = name; 91 | } 92 | 93 | public String getServiceId() { 94 | return serviceId; 95 | } 96 | 97 | public void setServiceId(String serviceId) { 98 | this.serviceId = serviceId; 99 | } 100 | 101 | public String getNotes() { 102 | return notes; 103 | } 104 | 105 | public void setNotes(String notes) { 106 | this.notes = notes; 107 | } 108 | 109 | /** 110 | * @deprecated Please use Args parameter instead 111 | */ 112 | @Deprecated 113 | public String getScript() { 114 | return script; 115 | } 116 | 117 | /** 118 | * @deprecated Please use Args parameter instead 119 | */ 120 | @Deprecated 121 | public void setScript(String script) { 122 | this.script = script; 123 | } 124 | 125 | public List getArgs() { 126 | return args; 127 | } 128 | 129 | public void setArgs(List args) { 130 | this.args = args; 131 | } 132 | 133 | public String getHttp() { 134 | return http; 135 | } 136 | 137 | public void setHttp(String http) { 138 | this.http = http; 139 | } 140 | 141 | public String getMethod() { 142 | return method; 143 | } 144 | 145 | public void setMethod(String method) { 146 | this.method = method; 147 | } 148 | 149 | public Map> getHeader() { 150 | return header; 151 | } 152 | 153 | public void setHeader(Map> header) { 154 | this.header = header; 155 | } 156 | 157 | public String getTcp() { 158 | return tcp; 159 | } 160 | 161 | public void setTcp(String tcp) { 162 | this.tcp = tcp; 163 | } 164 | 165 | public String getDockerContainerID() { 166 | return dockerContainerID; 167 | } 168 | 169 | public void setDockerContainerID(String dockerContainerID) { 170 | this.dockerContainerID = dockerContainerID; 171 | } 172 | 173 | public String getShell() { 174 | return shell; 175 | } 176 | 177 | public void setShell(String shell) { 178 | this.shell = shell; 179 | } 180 | 181 | public String getInterval() { 182 | return interval; 183 | } 184 | 185 | public void setInterval(String interval) { 186 | this.interval = interval; 187 | } 188 | 189 | public String getTtl() { 190 | return ttl; 191 | } 192 | 193 | public void setTtl(String ttl) { 194 | this.ttl = ttl; 195 | } 196 | 197 | public String getTimeout() { 198 | return timeout; 199 | } 200 | 201 | public void setTimeout(String timeout) { 202 | this.timeout = timeout; 203 | } 204 | 205 | public String getDeregisterCriticalServiceAfter() { 206 | return deregisterCriticalServiceAfter; 207 | } 208 | 209 | public void setDeregisterCriticalServiceAfter(String deregisterCriticalServiceAfter) { 210 | this.deregisterCriticalServiceAfter = deregisterCriticalServiceAfter; 211 | } 212 | 213 | public Boolean getTlsSkipVerify() { 214 | return tlsSkipVerify; 215 | } 216 | 217 | public void setTlsSkipVerify(Boolean tlsSkipVerify) { 218 | this.tlsSkipVerify = tlsSkipVerify; 219 | } 220 | 221 | public String getStatus() { 222 | return status; 223 | } 224 | 225 | public void setStatus(String status) { 226 | this.status = status; 227 | } 228 | 229 | public String getGrpc() { 230 | return grpc; 231 | } 232 | 233 | public void setGrpc(String grpc) { 234 | this.grpc = grpc; 235 | } 236 | 237 | public Boolean getGrpcUseTLS() { 238 | return grpcUseTLS; 239 | } 240 | 241 | public void setGrpcUseTLS(Boolean grpcUseTLS) { 242 | this.grpcUseTLS = grpcUseTLS; 243 | } 244 | 245 | @Override 246 | public String toString() { 247 | return "NewCheck{" + 248 | "id='" + id + '\'' + 249 | ", name='" + name + '\'' + 250 | ", serviceId='" + serviceId + '\'' + 251 | ", notes='" + notes + '\'' + 252 | ", script='" + script + '\'' + 253 | ", args=" + args + 254 | ", http='" + http + '\'' + 255 | ", method='" + method + '\'' + 256 | ", header=" + header + 257 | ", tcp='" + tcp + '\'' + 258 | ", dockerContainerID='" + dockerContainerID + '\'' + 259 | ", shell='" + shell + '\'' + 260 | ", interval='" + interval + '\'' + 261 | ", timeout='" + timeout + '\'' + 262 | ", ttl='" + ttl + '\'' + 263 | ", deregisterCriticalServiceAfter='" + deregisterCriticalServiceAfter + '\'' + 264 | ", tlsSkipVerify=" + tlsSkipVerify + 265 | ", status='" + status + '\'' + 266 | ", grpc='" + grpc + '\'' + 267 | ", grpcUseTLS=" + grpcUseTLS + 268 | '}'; 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/health/HealthConsulClient.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.health; 2 | 3 | import java.util.List; 4 | 5 | import com.ecwid.consul.json.GsonFactory; 6 | import com.ecwid.consul.transport.HttpResponse; 7 | import com.ecwid.consul.transport.TLSConfig; 8 | import com.ecwid.consul.v1.ConsulRawClient; 9 | import com.ecwid.consul.v1.OperationException; 10 | import com.ecwid.consul.v1.QueryParams; 11 | import com.ecwid.consul.v1.Response; 12 | import com.ecwid.consul.v1.health.model.Check; 13 | import com.ecwid.consul.v1.health.model.HealthService; 14 | import com.google.gson.reflect.TypeToken; 15 | 16 | /** 17 | * @author Vasily Vasilkov (vgv@ecwid.com) 18 | */ 19 | public final class HealthConsulClient implements HealthClient { 20 | 21 | private final ConsulRawClient rawClient; 22 | 23 | public HealthConsulClient(ConsulRawClient rawClient) { 24 | this.rawClient = rawClient; 25 | } 26 | 27 | public HealthConsulClient() { 28 | this(new ConsulRawClient()); 29 | } 30 | 31 | public HealthConsulClient(TLSConfig tlsConfig) { 32 | this(new ConsulRawClient(tlsConfig)); 33 | } 34 | 35 | public HealthConsulClient(String agentHost) { 36 | this(new ConsulRawClient(agentHost)); 37 | } 38 | 39 | public HealthConsulClient(String agentHost, TLSConfig tlsConfig) { 40 | this(new ConsulRawClient(agentHost, tlsConfig)); 41 | } 42 | 43 | public HealthConsulClient(String agentHost, int agentPort) { 44 | this(new ConsulRawClient(agentHost, agentPort)); 45 | } 46 | 47 | public HealthConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) { 48 | this(new ConsulRawClient(agentHost, agentPort, tlsConfig)); 49 | } 50 | 51 | @Override 52 | public Response> getHealthChecksForNode(String nodeName, QueryParams queryParams) { 53 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/node/" + nodeName, queryParams); 54 | 55 | if (httpResponse.getStatusCode() == 200) { 56 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 57 | }.getType()); 58 | return new Response>(value, httpResponse); 59 | } else { 60 | throw new OperationException(httpResponse); 61 | } 62 | } 63 | 64 | @Override 65 | public Response> getHealthChecksForService(String serviceName, QueryParams queryParams) { 66 | HealthChecksForServiceRequest request = HealthChecksForServiceRequest.newBuilder() 67 | .setQueryParams(queryParams) 68 | .build(); 69 | 70 | return getHealthChecksForService(serviceName, request); 71 | } 72 | 73 | @Override 74 | public Response> getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest) { 75 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/checks/" + serviceName, healthChecksForServiceRequest.asUrlParameters()); 76 | 77 | if (httpResponse.getStatusCode() == 200) { 78 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 79 | }.getType()); 80 | return new Response>(value, httpResponse); 81 | } else { 82 | throw new OperationException(httpResponse); 83 | } 84 | } 85 | 86 | @Override 87 | public Response> getHealthServices(String serviceName, boolean onlyPassing, QueryParams queryParams) { 88 | return getHealthServices(serviceName, (String) null, onlyPassing, queryParams, null); 89 | } 90 | 91 | @Override 92 | public Response> getHealthServices(String serviceName, boolean onlyPassing, QueryParams queryParams, String token) { 93 | return getHealthServices(serviceName, (String) null, onlyPassing, queryParams, token); 94 | } 95 | 96 | @Override 97 | public Response> getHealthServices(String serviceName, String tag, boolean onlyPassing, QueryParams queryParams) { 98 | return getHealthServices(serviceName, tag, onlyPassing, queryParams, null); 99 | } 100 | 101 | @Override 102 | public Response> getHealthServices(String serviceName, String tag, boolean onlyPassing, QueryParams queryParams, String token) { 103 | return getHealthServices(serviceName, new String[]{tag}, onlyPassing, queryParams, token); 104 | } 105 | 106 | @Override 107 | public Response> getHealthServices(String serviceName, String[] tags, boolean onlyPassing, QueryParams queryParams, String token) { 108 | HealthServicesRequest request = HealthServicesRequest.newBuilder() 109 | .setTags(tags) 110 | .setPassing(onlyPassing) 111 | .setQueryParams(queryParams) 112 | .setToken(token) 113 | .build(); 114 | 115 | return getHealthServices(serviceName, request); 116 | } 117 | 118 | @Override 119 | public Response> getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest) { 120 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/service/" + serviceName, healthServicesRequest.asUrlParameters()); 121 | 122 | if (httpResponse.getStatusCode() == 200) { 123 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), 124 | new TypeToken>() { 125 | }.getType()); 126 | return new Response>(value, httpResponse); 127 | } else { 128 | throw new OperationException(httpResponse); 129 | } 130 | } 131 | 132 | @Override 133 | public Response> getHealthChecksState(QueryParams queryParams) { 134 | return getHealthChecksState(null, queryParams); 135 | } 136 | 137 | @Override 138 | public Response> getHealthChecksState(Check.CheckStatus checkStatus, QueryParams queryParams) { 139 | String status = checkStatus == null ? "any" : checkStatus.name().toLowerCase(); 140 | HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/state/" + status, queryParams); 141 | 142 | if (httpResponse.getStatusCode() == 200) { 143 | List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() { 144 | }.getType()); 145 | return new Response>(value, httpResponse); 146 | } else { 147 | throw new OperationException(httpResponse); 148 | } 149 | } 150 | 151 | } 152 | -------------------------------------------------------------------------------- /src/main/java/com/ecwid/consul/v1/catalog/model/CatalogService.java: -------------------------------------------------------------------------------- 1 | package com.ecwid.consul.v1.catalog.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Objects; 8 | 9 | /** 10 | * @author Vasily Vasilkov (vgv@ecwid.com) 11 | */ 12 | public class CatalogService { 13 | 14 | @SerializedName("ID") 15 | private String id; 16 | 17 | @SerializedName("Node") 18 | private String node; 19 | 20 | @SerializedName("Address") 21 | private String address; 22 | 23 | @SerializedName("Datacenter") 24 | private String datacenter; 25 | 26 | @SerializedName("TaggedAddresses") 27 | private Map taggedAddresses; 28 | 29 | @SerializedName("NodeMeta") 30 | private Map nodeMeta; 31 | 32 | @SerializedName("ServiceID") 33 | private String serviceId; 34 | 35 | @SerializedName("ServiceName") 36 | private String serviceName; 37 | 38 | @SerializedName("ServiceTags") 39 | private List serviceTags; 40 | 41 | @SerializedName("ServiceAddress") 42 | private String serviceAddress; 43 | 44 | @SerializedName("ServiceMeta") 45 | private Map serviceMeta; 46 | 47 | @SerializedName("ServicePort") 48 | private Integer servicePort; 49 | 50 | @SerializedName("ServiceEnableTagOverride") 51 | private Boolean serviceEnableTagOverride; 52 | 53 | @SerializedName("CreateIndex") 54 | private Long createIndex; 55 | 56 | @SerializedName("ModifyIndex") 57 | private Long modifyIndex; 58 | 59 | public String getId() { 60 | return id; 61 | } 62 | 63 | public void setId(String id) { 64 | this.id = id; 65 | } 66 | 67 | public String getNode() { 68 | return node; 69 | } 70 | 71 | public void setNode(String node) { 72 | this.node = node; 73 | } 74 | 75 | public String getAddress() { 76 | return address; 77 | } 78 | 79 | public void setAddress(String address) { 80 | this.address = address; 81 | } 82 | 83 | public String getDatacenter() { 84 | return datacenter; 85 | } 86 | 87 | public void setDatacenter(String datacenter) { 88 | this.datacenter = datacenter; 89 | } 90 | 91 | public Map getTaggedAddresses() { 92 | return taggedAddresses; 93 | } 94 | 95 | public void setTaggedAddresses(Map taggedAddresses) { 96 | this.taggedAddresses = taggedAddresses; 97 | } 98 | 99 | public Map getNodeMeta() { 100 | return nodeMeta; 101 | } 102 | 103 | public void setNodeMeta(Map nodeMeta) { 104 | this.nodeMeta = nodeMeta; 105 | } 106 | 107 | public String getServiceId() { 108 | return serviceId; 109 | } 110 | 111 | public void setServiceId(String serviceId) { 112 | this.serviceId = serviceId; 113 | } 114 | 115 | public String getServiceName() { 116 | return serviceName; 117 | } 118 | 119 | public void setServiceName(String serviceName) { 120 | this.serviceName = serviceName; 121 | } 122 | 123 | public List getServiceTags() { 124 | return serviceTags; 125 | } 126 | 127 | public void setServiceTags(List serviceTags) { 128 | this.serviceTags = serviceTags; 129 | } 130 | 131 | public String getServiceAddress() { 132 | return serviceAddress; 133 | } 134 | 135 | public void setServiceAddress(String serviceAddress) { 136 | this.serviceAddress = serviceAddress; 137 | } 138 | 139 | public Map getServiceMeta() { 140 | return serviceMeta; 141 | } 142 | 143 | public void setServiceMeta(Map serviceMeta) { 144 | this.serviceMeta = serviceMeta; 145 | } 146 | 147 | public Integer getServicePort() { 148 | return servicePort; 149 | } 150 | 151 | public void setServicePort(Integer servicePort) { 152 | this.servicePort = servicePort; 153 | } 154 | 155 | public Boolean getServiceEnableTagOverride() { 156 | return serviceEnableTagOverride; 157 | } 158 | 159 | public void setServiceEnableTagOverride(Boolean serviceEnableTagOverride) { 160 | this.serviceEnableTagOverride = serviceEnableTagOverride; 161 | } 162 | 163 | public Long getCreateIndex() { 164 | return createIndex; 165 | } 166 | 167 | public void setCreateIndex(Long createIndex) { 168 | this.createIndex = createIndex; 169 | } 170 | 171 | public Long getModifyIndex() { 172 | return modifyIndex; 173 | } 174 | 175 | public void setModifyIndex(Long modifyIndex) { 176 | this.modifyIndex = modifyIndex; 177 | } 178 | 179 | @Override 180 | public String toString() { 181 | return "CatalogService{" + 182 | "id='" + id + '\'' + 183 | ", node='" + node + '\'' + 184 | ", address='" + address + '\'' + 185 | ", datacenter='" + datacenter + '\'' + 186 | ", taggedAddresses=" + taggedAddresses + 187 | ", nodeMeta=" + nodeMeta + 188 | ", serviceId='" + serviceId + '\'' + 189 | ", serviceName='" + serviceName + '\'' + 190 | ", serviceTags=" + serviceTags + 191 | ", serviceAddress='" + serviceAddress + '\'' + 192 | ", serviceMeta=" + serviceMeta + 193 | ", servicePort=" + servicePort + 194 | ", serviceEnableTagOverride=" + serviceEnableTagOverride + 195 | ", createIndex=" + createIndex + 196 | ", modifyIndex=" + modifyIndex + 197 | '}'; 198 | } 199 | 200 | @Override 201 | public boolean equals(Object o) { 202 | if (this == o) return true; 203 | if (o == null || getClass() != o.getClass()) return false; 204 | CatalogService that = (CatalogService) o; 205 | return Objects.equals(id, that.id) && 206 | Objects.equals(node, that.node) && 207 | Objects.equals(address, that.address) && 208 | Objects.equals(datacenter, that.datacenter) && 209 | Objects.equals(taggedAddresses, that.taggedAddresses) && 210 | Objects.equals(nodeMeta, that.nodeMeta) && 211 | Objects.equals(serviceId, that.serviceId) && 212 | Objects.equals(serviceName, that.serviceName) && 213 | Objects.equals(serviceTags, that.serviceTags) && 214 | Objects.equals(serviceAddress, that.serviceAddress) && 215 | Objects.equals(serviceMeta, that.serviceMeta) && 216 | Objects.equals(servicePort, that.servicePort) && 217 | Objects.equals(serviceEnableTagOverride, that.serviceEnableTagOverride) && 218 | Objects.equals(createIndex, that.createIndex) && 219 | Objects.equals(modifyIndex, that.modifyIndex); 220 | } 221 | 222 | @Override 223 | public int hashCode() { 224 | return Objects.hash(id, node, address, datacenter, taggedAddresses, nodeMeta, serviceId, serviceName, serviceTags, serviceAddress, serviceMeta, servicePort, serviceEnableTagOverride, createIndex, modifyIndex); 225 | } 226 | } 227 | --------------------------------------------------------------------------------