Skip to content

Commit

Permalink
vertx meta data changes final (#114)
Browse files Browse the repository at this point in the history
* vertx meta data changes

* vertx meta data changes
  • Loading branch information
paras-amdocs-openet authored Oct 23, 2024
1 parent f79249c commit 3d5c926
Show file tree
Hide file tree
Showing 13 changed files with 707 additions and 24 deletions.
15 changes: 0 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen-json</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-docgen-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
Expand Down
65 changes: 64 additions & 1 deletion src/main/java/io/vertx/ext/consul/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author <a href="mailto:[email protected]">Ruslan Sennov</a>
*/
@DataObject
public class Service {
public class Service implements TxnResult {

private static final String NODE = "Node";
private static final String ADDRESS = "Address";
Expand All @@ -44,6 +44,8 @@ public class Service {
private static final String SERVICE_ADDRESS = "ServiceAddress";
private static final String SERVICE_META = "ServiceMeta";
private static final String SERVICE_PORT = "ServicePort";
private static final String CREATE_INDEX = "CreateIndex";
private static final String MODIFY_INDEX = "ModifyIndex";

private String node;
private String nodeAddress;
Expand All @@ -53,6 +55,8 @@ public class Service {
private String address;
private Map<String, String> meta;
private int port;
private long createIndex;
private long modifyIndex;

/**
* Default constructor
Expand All @@ -74,6 +78,8 @@ public Service(Service other) {
this.address = other.address;
this.meta = other.meta;
this.port = other.port;
this.createIndex = other.createIndex;
this.modifyIndex = other.modifyIndex;
}

/**
Expand All @@ -90,6 +96,8 @@ public Service(JsonObject service) {
this.address = service.getString(SERVICE_ADDRESS);
this.meta = mapStringString(service.getJsonObject(SERVICE_META));
this.port = service.getInteger(SERVICE_PORT, 0);
this.createIndex = service.getLong(CREATE_INDEX, 0l);
this.modifyIndex = service.getLong(MODIFY_INDEX, 0l);
}

/**
Expand Down Expand Up @@ -123,6 +131,12 @@ public JsonObject toJson() {
if (port != 0) {
jsonObject.put(SERVICE_PORT, port);
}
if (createIndex != 0l) {
jsonObject.put(CREATE_INDEX, createIndex);
}
if (modifyIndex != 0l) {
jsonObject.put(MODIFY_INDEX, modifyIndex);
}
return jsonObject;
}

Expand Down Expand Up @@ -285,13 +299,60 @@ public Service setPort(int port) {
return this;
}

/**
* Get the internal index value that represents when the entry was created.
*
* @return the internal index value that represents when the entry was created.
*/
public long getCreateIndex() {
return createIndex;
}

/**
* Set the internal index value that represents when the entry was created.
*
* @param createIndex the internal index value that represents when the entry was created.
* @return reference to this, for fluency
*/
public Service setCreateIndex(long createIndex) {
this.createIndex = createIndex;
return this;
}

/**
* Get the last index that modified this key.
*
* @return the last index that modified this key.
*/
public long getModifyIndex() {
return modifyIndex;
}

/**
* Set the last index that modified this key.
*
* @param modifyIndex the last index that modified this key.
* @return reference to this, for fluency
*/
public Service setModifyIndex(long modifyIndex) {
this.modifyIndex = modifyIndex;
return this;
}

@Override
public TxnOperationType getOperationType() {
return TxnOperationType.SERVICE;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Service service = (Service) o;

if (createIndex != service.createIndex) return false;
if (modifyIndex != service.modifyIndex) return false;
if (port != service.port) return false;
if (node != null ? !node.equals(service.node) : service.node != null) return false;
if (nodeAddress != null ? !nodeAddress.equals(service.nodeAddress) : service.nodeAddress != null) return false;
Expand All @@ -312,6 +373,8 @@ public int hashCode() {
result = 31 * result + (address != null ? address.hashCode() : 0);
result = 31 * result + (meta != null ? meta.hashCode() : 0);
result = 31 * result + port;
result = 31 * result + (int) (createIndex ^ (createIndex >>> 32));
result = 31 * result + (int) (modifyIndex ^ (modifyIndex >>> 32));
return result;
}

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/io/vertx/ext/consul/ServiceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class ServiceOptions {
private int port;
private CheckOptions checkOptions;
private List<CheckOptions> checkListOptions;
private long createIndex;
private long modifyIndex;

/**
* Default constructor
Expand All @@ -61,6 +63,8 @@ public ServiceOptions(ServiceOptions options) {
this.port = options.port;
this.checkOptions = options.checkOptions;
this.checkListOptions = options.checkListOptions;
this.createIndex = options.createIndex;
this.modifyIndex = options.modifyIndex;
}

/**
Expand Down Expand Up @@ -242,4 +246,45 @@ public ServiceOptions setCheckListOptions(List<CheckOptions> checkListOptions) {
this.checkListOptions = checkListOptions;
return this;
}

/**
* Get the internal index value that represents when the entry was created.
*
* @return the internal index value that represents when the entry was created.
*/
public long getCreateIndex() {
return createIndex;
}

/**
* Set the internal index value that represents when the entry was created.
*
* @param createIndex the internal index value that represents when the entry was created.
* @return reference to this, for fluency
*/
public ServiceOptions setCreateIndex(long createIndex) {
this.createIndex = createIndex;
return this;
}

/**
* Get the last index that modified this key.
*
* @return the last index that modified this key.
*/
public long getModifyIndex() {
return modifyIndex;
}

/**
* Set the last index that modified this key.
*
* @param modifyIndex the last index that modified this key.
* @return reference to this, for fluency
*/
public ServiceOptions setModifyIndex(long modifyIndex) {
this.modifyIndex = modifyIndex;
return this;
}

}
3 changes: 1 addition & 2 deletions src/main/java/io/vertx/ext/consul/TxnOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
package io.vertx.ext.consul;

/**
* Represents operation in transaction. Key/Value is the only available operation type,
* though other types of operations may be added in future versions of Consul to be mixed with key/value operations
* Represents operation in transaction. The available operation types are KV and Service
*
* @author <a href="mailto:[email protected]">Ruslan Sennov</a>
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/vertx/ext/consul/TxnOperationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import io.vertx.codegen.annotations.VertxGen;

/**
* Represents the type of operation in a transaction. KV is the only available operation type,
* though other types of operations may be added in future versions of Consul to be mixed with key/value operations
* Represents the type of operation in a transaction. The available operation types are KV and Service
*
* @author <a href="mailto:[email protected]">Ruslan Sennov</a>
* @see <a href="https://www.consul.io/docs/agent/http/kv.html#txn">/v1/txn</a> endpoint
*/
@VertxGen
public enum TxnOperationType {
KV
KV,
SERVICE
}
18 changes: 18 additions & 0 deletions src/main/java/io/vertx/ext/consul/TxnRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public TxnRequest(JsonObject json) {
.setIndex(txn.getLong("Index"))
.setSession(txn.getString("Session"))
.setType(TxnKVVerb.ofVerb(txn.getString("Verb"))));
} else if (obj.containsKey("Service")) {
JsonObject txn = obj.getJsonObject("Service");
ServiceOptions serviceOptions = new ServiceOptions(txn.getJsonObject("Service"));
serviceOptions.setName(txn.getJsonObject("Service").getString("Service"));
operations.add(new TxnServiceOperation()
.setServiceOptions(serviceOptions)
.setNode(txn.getString("Node"))
.setType(TxnServiceVerb.ofVerb(txn.getString("Verb"))));
}
});
}
Expand All @@ -82,6 +90,16 @@ public JsonObject toJson() {
.put("Index", kvOp.getIndex())
.put("Session", kvOp.getSession());
arr.add(new JsonObject().put("KV", obj));
} else if (op instanceof TxnServiceOperation) {
TxnServiceOperation serviceOp = (TxnServiceOperation) op;
JsonObject serviceObj = serviceOp.getServiceOptions().toJson();
serviceObj.put("Service", serviceObj.getValue("name"));
serviceObj.remove("name");
JsonObject obj = new JsonObject()
.put("Verb", serviceOp.getType().getVerb())
.put("Service", serviceObj)
.put("Node", serviceOp.getNode());
arr.add(new JsonObject().put("Service", obj));
}
});
return new JsonObject().put("operations", arr);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/vertx/ext/consul/TxnResponse.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* Copyright (c) 2016 The original author or authors
*
Expand Down Expand Up @@ -50,6 +51,10 @@ public TxnResponse(JsonObject json) {
JsonObject obj = (JsonObject) entry;
if (obj.containsKey("KV")) {
results.add(new KeyValue(obj.getJsonObject("KV")));
} else if (obj.containsKey("Service")) {
Service service = new Service(obj.getJsonObject("Service"));
service.setName(obj.getJsonObject("Service").getString("Service"));
results.add(service);
}
});
}
Expand All @@ -68,6 +73,11 @@ public JsonObject toJson() {
results.forEach(op -> {
if (op instanceof KeyValue) {
jsonResults.add(new JsonObject().put("KV", ((KeyValue) op).toJson()));
} else if (op instanceof Service) {
JsonObject jsonObject = ((Service) op).toJson();
jsonObject.put("Service", jsonObject.getString("ServiceName"));
jsonObject.remove("ServiceName");
jsonResults.add(new JsonObject().put("Service", jsonObject));
}
});
JsonArray jsonErrors = new JsonArray();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/vertx/ext/consul/TxnResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
package io.vertx.ext.consul;

/**
* Represents result of operation. Key/Value is the only available result type,
* though other types of results may be added in future versions of Consul to be mixed with key/value operations
* Represents result of operation. The available operation types are KV and Service
*
* @author <a href="mailto:[email protected]">Ruslan Sennov</a>
*/
Expand Down
Loading

0 comments on commit 3d5c926

Please sign in to comment.