This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement operations to set and delete credentials in etcd (#279)
Signed-off-by: Eamonn Mansour <[email protected]>
- Loading branch information
Showing
11 changed files
with
672 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ons-parent/dev.galasa.cps.etcd/src/main/java/dev/galasa/cps/etcd/internal/Etcd3Store.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright contributors to the Galasa project | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package dev.galasa.cps.etcd.internal; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
import io.etcd.jetcd.ByteSequence; | ||
import io.etcd.jetcd.Client; | ||
import io.etcd.jetcd.KV; | ||
import io.etcd.jetcd.KeyValue; | ||
import io.etcd.jetcd.kv.GetResponse; | ||
import io.etcd.jetcd.options.DeleteOption; | ||
|
||
/** | ||
* Abstract class containing common methods used to interact with etcd, like getting, setting, | ||
* and deleting properties. | ||
*/ | ||
public abstract class Etcd3Store { | ||
|
||
protected final Client client; | ||
protected final KV kvClient; | ||
|
||
public Etcd3Store(Client client) { | ||
this.client = client; | ||
this.kvClient = client.getKVClient(); | ||
} | ||
|
||
public Etcd3Store(URI etcdUri) { | ||
this(Client.builder().endpoints(etcdUri).build()); | ||
} | ||
|
||
protected String get(String key) throws InterruptedException, ExecutionException { | ||
ByteSequence bsKey = ByteSequence.from(key, UTF_8); | ||
CompletableFuture<GetResponse> getFuture = kvClient.get(bsKey); | ||
GetResponse response = getFuture.get(); | ||
List<KeyValue> kvs = response.getKvs(); | ||
|
||
String retrievedKey = null; | ||
if (!kvs.isEmpty()) { | ||
retrievedKey = kvs.get(0).getValue().toString(UTF_8); | ||
} | ||
return retrievedKey; | ||
} | ||
|
||
protected void put(String key, String value) throws InterruptedException, ExecutionException { | ||
ByteSequence bytesKey = ByteSequence.from(key, UTF_8); | ||
ByteSequence bytesValue = ByteSequence.from(value, UTF_8); | ||
kvClient.put(bytesKey, bytesValue).get(); | ||
} | ||
|
||
protected void delete(@NotNull String key) throws InterruptedException, ExecutionException { | ||
ByteSequence bytesKey = ByteSequence.from(key, StandardCharsets.UTF_8); | ||
kvClient.delete(bytesKey).get(); | ||
} | ||
|
||
protected void deletePrefix(@NotNull String keyPrefix) throws InterruptedException, ExecutionException { | ||
ByteSequence bsKey = ByteSequence.from(keyPrefix, UTF_8); | ||
DeleteOption options = DeleteOption.newBuilder().isPrefix(true).build(); | ||
kvClient.delete(bsKey, options).get(); | ||
} | ||
|
||
protected void shutdownStore() { | ||
kvClient.close(); | ||
client.close(); | ||
} | ||
} |
Oops, something went wrong.