Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check-and-set to deleteKV without changing return type #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/ecwid/consul/v1/ConsulClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.ecwid.consul.v1.health.model.HealthService;
import com.ecwid.consul.v1.kv.KeyValueClient;
import com.ecwid.consul.v1.kv.KeyValueConsulClient;
import com.ecwid.consul.v1.kv.model.DeleteParams;
import com.ecwid.consul.v1.kv.model.GetBinaryValue;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.ecwid.consul.v1.kv.model.PutParams;
Expand Down Expand Up @@ -759,11 +760,26 @@ public Response<Void> deleteKVValue(String key, QueryParams queryParams) {
return keyValueClient.deleteKVValue(key, queryParams);
}

@Override
public Response<Void> deleteKVValue(String key, DeleteParams deleteParams) {
return keyValueClient.deleteKVValue(key, deleteParams);
}

@Override
public Response<Void> deleteKVValue(String key, String token, QueryParams queryParams) {
return keyValueClient.deleteKVValue(key, token, queryParams);
}

@Override
public Response<Void> deleteKVValue(String key, String token, DeleteParams deleteParams) {
return keyValueClient.deleteKVValue(key, token, deleteParams);
}

@Override
public Response<Void> deleteKVValue(String key, String token, DeleteParams deleteParams, QueryParams queryParams) {
return keyValueClient.deleteKVValue(key, token, deleteParams, queryParams);
}

@Override
public Response<Void> deleteKVValues(String key) {
return keyValueClient.deleteKVValues(key);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/ecwid/consul/v1/kv/KeyValueClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.DeleteParams;
import com.ecwid.consul.v1.kv.model.GetBinaryValue;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.ecwid.consul.v1.kv.model.PutParams;
Expand Down Expand Up @@ -90,8 +91,14 @@ public interface KeyValueClient {

public Response<Void> deleteKVValue(String key, QueryParams queryParams);

public Response<Void> deleteKVValue(String key, DeleteParams deleteParams);

public Response<Void> deleteKVValue(String key, String token, QueryParams queryParams);

public Response<Void> deleteKVValue(String key, String token, DeleteParams deleteParams);

public Response<Void> deleteKVValue(String key, String token, DeleteParams deleteParams, QueryParams queryParams);


public Response<Void> deleteKVValues(String key);

Expand Down
31 changes: 26 additions & 5 deletions src/main/java/com/ecwid/consul/v1/kv/KeyValueConsulClient.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.ecwid.consul.v1.kv;

import java.util.List;

import com.ecwid.consul.ConsulException;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.*;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.OperationException;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Request;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.DeleteParams;
import com.ecwid.consul.v1.kv.model.GetBinaryValue;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.ecwid.consul.v1.kv.model.PutParams;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
* @author Vasily Vasilkov ([email protected])
*/
Expand Down Expand Up @@ -312,20 +317,36 @@ public Response<Void> deleteKVValue(String key) {

@Override
public Response<Void> deleteKVValue(String key, String token) {
return deleteKVValue(key, token, QueryParams.DEFAULT);
return deleteKVValue(key, token, null, QueryParams.DEFAULT);
}

@Override
public Response<Void> deleteKVValue(String key, QueryParams queryParams) {
return deleteKVValue(key, null, queryParams);
return deleteKVValue(key, null, null, queryParams);
}

@Override
public Response<Void> deleteKVValue(String key, DeleteParams deleteParams) {
return deleteKVValue(key, null, deleteParams, QueryParams.DEFAULT);
}

@Override
public Response<Void> deleteKVValue(String key, String token, QueryParams queryParams) {
return deleteKVValue(key, null, null, queryParams);
}

@Override
public Response<Void> deleteKVValue(String key, String token, DeleteParams deleteParams) {
return deleteKVValue(key, token, deleteParams, QueryParams.DEFAULT);
}

@Override
public Response<Void> deleteKVValue(String key, String token, DeleteParams deleteParams, QueryParams queryParams) {
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/kv/" + key)
.setToken(token)
.addUrlParameter(queryParams)
.addUrlParameter(deleteParams)
.build();

HttpResponse httpResponse = rawClient.makeDeleteRequest(request);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/ecwid/consul/v1/kv/model/DeleteParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.ecwid.consul.v1.kv.model;

import com.ecwid.consul.UrlParameters;

import java.util.ArrayList;
import java.util.List;

/**
* @author Kevin Binswanger ([email protected])
*/
public class DeleteParams implements UrlParameters {

private Long cas;

public Long getCas() {
return cas;
}

public void setCas(Long cas) {
this.cas = cas;
}

@Override
public List<String> toUrlParameters() {
List<String> params = new ArrayList<String>();

if (cas != null) {
params.add("cas=" + cas);
}

return params;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.ecwid.consul.v1.kv;

import com.ecwid.consul.ConsulTestConstants;
import com.ecwid.consul.v1.kv.model.DeleteParams;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.pszymczyk.consul.ConsulProcess;
import com.pszymczyk.consul.ConsulStarterBuilder;
import com.pszymczyk.consul.infrastructure.Ports;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -68,6 +69,44 @@ void testDeleteKvValue() throws Exception {
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
}

@Test
void testDeleteKvValueCas() {
final String testKey = "test_key";
final String testValue = "test_value";
final String testModifiedValue = "test_value2";

// Make sure there is no such key before test running
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());

// Set the key
consulClient.setKVValue(testKey, testValue);
// Make sure test key exists
GetValue getValue = consulClient.getKVValue(testKey).getValue();
Assertions.assertEquals(testValue, getValue.getDecodedValue());

// Modify the value
consulClient.setKVValue(testKey, testModifiedValue);
// Make sure it returns modified value
GetValue modifiedGetValue = consulClient.getKVValue(testKey).getValue();
Assertions.assertEquals(testModifiedValue, modifiedGetValue.getDecodedValue());
// Verify that consul changed the modified index
Assertions.assertNotEquals(getValue.getModifyIndex(), modifiedGetValue.getModifyIndex());

// Delete key with the old modify index
DeleteParams deleteOldIndex = new DeleteParams();
deleteOldIndex.setCas(getValue.getModifyIndex());
consulClient.deleteKVValue(testKey, deleteOldIndex);
// Make sure the key is still there
Assertions.assertEquals(testModifiedValue, consulClient.getKVValue(testKey).getValue().getDecodedValue());

// Delete the key with the new modify index
DeleteParams deleteNewIndex = new DeleteParams();
deleteNewIndex.setCas(modifiedGetValue.getModifyIndex());
consulClient.deleteKVValue(testKey, deleteNewIndex);
// Make sure the key is gone
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
}

@Test
void testDeleteKvValues() throws Exception {
final String testKeyPrefix = "test_key";
Expand Down