Skip to content

Commit

Permalink
feat(kv-store): introduce CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mulier-p committed Jul 9, 2024
1 parent 38b72a2 commit 1b6e853
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.kestra.cli.commands.namespaces.kv;

import io.kestra.cli.AbstractCommand;
import io.kestra.cli.App;
import io.micronaut.configuration.picocli.PicocliRunner;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

@CommandLine.Command(
name = "kv",
description = "handle KV Store",
mixinStandardHelpOptions = true,
subcommands = {
KvUpdateCommand.class,
}
)
@Slf4j
public class KvCommand extends AbstractCommand {
@SneakyThrows
@Override
public Integer call() throws Exception {
super.call();

PicocliRunner.call(App.class, "namespace", "kv", "--help");

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.kestra.cli.commands.namespaces.kv;

import io.kestra.cli.AbstractApiCommand;
import io.kestra.cli.AbstractValidateCommand;
import io.kestra.core.services.KVStoreService;
import io.kestra.core.storages.kv.KVMetadata;
import io.kestra.core.storages.kv.KVStoreValueWrapper;
import io.kestra.core.utils.KestraIgnore;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.client.multipart.MultipartBody;
import io.micronaut.http.client.netty.DefaultHttpClient;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.regex.Pattern;

@CommandLine.Command(
name = "update",
description = "update value for a KV Store key",
mixinStandardHelpOptions = true
)
@Slf4j
public class KvUpdateCommand extends AbstractApiCommand {
private static final Pattern STRING_PATTERN = Pattern.compile("^(?![\\d{\\[\"]+)(?!false)(?!true)(?!P(?=[^T]|T.)(?:\\d*D)?(?:T(?=.)(?:\\d*H)?(?:\\d*M)?(?:\\d*S)?)?).*$");

@CommandLine.Parameters(index = "0", description = "the namespace to update")
public String namespace;

@CommandLine.Parameters(index = "1", description = "the key to update")
public String key;

@CommandLine.Parameters(index = "2", description = "the value to assign to the key. If the value is an object, it must be in JSON format. If the value must be read from file, use -f parameter.")
public String value;

@CommandLine.Option(names = {"-e", "--expiration"}, description = "the duration after which the key should expire.")
public String expiration;

@CommandLine.Option(names = {"-t", "--type"}, description = "the type of the value. Optional and useful to override the deduced type (eg. numbers, booleans or JSON as full string). Must be one of STRING, NUMBER, BOOLEAN, DATETIME, DATE, DURATION, JSON.")
public String type;

@CommandLine.Option(names = {"-f", "--file-value"}, description = "the file from which to read the value to set. If this is provided, it will take precedence over any specified value.")
public Path fileValue;

@Override
public Integer call() throws Exception {
super.call();

if (fileValue != null) {
value = Files.readString(Path.of(fileValue.toString().trim()));
}

String formattedValue = value;
if (type != null) {
if (type.trim().equals("STRING") && !value.startsWith("\"")) {
formattedValue = "\"" + value.replace("\"", "\\\"") + "\"";
}
} else if (STRING_PATTERN.matcher(value).matches()) {
formattedValue = "\"" + value + "\"";
}

Duration ttl = expiration == null ? null : Duration.parse(expiration);
String trimmedValue = formattedValue.trim();
boolean isJson = trimmedValue.startsWith("{") && trimmedValue.endsWith("}") || trimmedValue.startsWith("[") && trimmedValue.endsWith("]");
MutableHttpRequest<String> request = HttpRequest.PUT(apiUri("/namespaces/") + namespace + "/kv/" + key, formattedValue)
.contentType(isJson ? MediaType.APPLICATION_JSON_TYPE : MediaType.TEXT_PLAIN_TYPE);

if (ttl != null) {
request.header("ttl", ttl.toString());
}

try (DefaultHttpClient client = client()) {
client.toBlocking().exchange(this.requestOptions(request));
}

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.kestra.cli.commands.namespaces.kv;

import io.kestra.cli.commands.namespaces.files.NamespaceFilesCommand;
import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.Is.is;

class KvCommandTest {
@Test
void runWithNoParam() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));

try (ApplicationContext ctx = ApplicationContext.builder().deduceEnvironment(false).start()) {
String[] args = {};
Integer call = PicocliRunner.call(NamespaceFilesCommand.class, ctx, args);

assertThat(call, is(0));
assertThat(out.toString(), containsString("Usage: kestra namespace kv"));

Check failure on line 26 in cli/src/test/java/io/kestra/cli/commands/namespaces/kv/KvCommandTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test Report

KvCommandTest.runWithNoParam()

java.lang.AssertionError: Expected: a string containing "Usage: kestra namespace kv" but: was "Usage: kestra namespace files [-hVv] [--internal-log] [-c=<config>] [-l=<logLevel>] [-p=<pluginsPath>] [COMMAND] handle namespace files -c, --config=<config> Path to a configuration file Default: /home/runner/.kestra/config.yml -h, --help Show this help message and exit. --internal-log Change also log level for internal log -l, --log-level=<logLevel> Change log level (values: TRACE, DEBUG, INFO, WARN, ERROR) Default: INFO -p, --plugins=<pluginsPath> Path to plugins directory -v, --verbose Change log level. Multiple -v options increase the verbosity. -V, --version Print version information and exit. Commands: update update namespace files "
Raw output
java.lang.AssertionError: 
Expected: a string containing "Usage: kestra namespace kv"
     but: was "Usage: kestra namespace files [-hVv] [--internal-log] [-c=<config>]
                              [-l=<logLevel>] [-p=<pluginsPath>] [COMMAND]
handle namespace files
  -c, --config=<config>   Path to a configuration file
                            Default: /home/runner/.kestra/config.yml
  -h, --help              Show this help message and exit.
      --internal-log      Change also log level for internal log
  -l, --log-level=<logLevel>
                          Change log level (values: TRACE, DEBUG, INFO, WARN,
                            ERROR)
                            Default: INFO
  -p, --plugins=<pluginsPath>
                          Path to plugins directory
  -v, --verbose           Change log level. Multiple -v options increase the
                            verbosity.
  -V, --version           Print version information and exit.
Commands:
  update  update namespace files
"
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:6)
	at io.kestra.cli.commands.namespaces.kv.KvCommandTest.runWithNoParam(KvCommandTest.java:26)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package io.kestra.cli.commands.namespaces.kv;

import io.kestra.core.exceptions.ResourceExpiredException;
import io.kestra.core.services.KVStoreService;
import io.kestra.core.storages.kv.KVStore;
import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class KvUpdateCommandTest {
@Test
void string() throws IOException, ResourceExpiredException {
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {

EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class);
embeddedServer.start();

String[] args = {
"--server",
embeddedServer.getURL().toString(),
"--user",
"myuser:pass:word",
"io.kestra.cli",
"string",
"stringValue"
};
PicocliRunner.call(KvUpdateCommand.class, ctx, args);

KVStoreService kvStoreService = ctx.getBean(KVStoreService.class);
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null);

assertThat(kvStore.get("string").get(), is("stringValue"));
assertThat(kvStore.getRaw("string").get(), is("\"stringValue\""));
}
}

@Test
void integer() throws IOException, ResourceExpiredException {
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {

EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class);
embeddedServer.start();

String[] args = {
"--server",
embeddedServer.getURL().toString(),
"--user",
"myuser:pass:word",
"io.kestra.cli",
"int",
"1"
};
PicocliRunner.call(KvUpdateCommand.class, ctx, args);

KVStoreService kvStoreService = ctx.getBean(KVStoreService.class);
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null);

assertThat(kvStore.get("int").get(), is(1));
assertThat(kvStore.getRaw("int").get(), is("1"));
}
}

@Test
void integerStr() throws IOException, ResourceExpiredException {
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {

EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class);
embeddedServer.start();

String[] args = {
"--server",
embeddedServer.getURL().toString(),
"--user",
"myuser:pass:word",
"io.kestra.cli",
"intStr",
"1",
"-t STRING"
};
PicocliRunner.call(KvUpdateCommand.class, ctx, args);

KVStoreService kvStoreService = ctx.getBean(KVStoreService.class);
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null);

assertThat(kvStore.get("intStr").get(), is("1"));
assertThat(kvStore.getRaw("intStr").get(), is("\"1\""));
}
}

@Test
void object() throws IOException, ResourceExpiredException {
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {

EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class);
embeddedServer.start();

String[] args = {
"--server",
embeddedServer.getURL().toString(),
"--user",
"myuser:pass:word",
"io.kestra.cli",
"object",
"{\"some\":\"json\"}",
};
PicocliRunner.call(KvUpdateCommand.class, ctx, args);

KVStoreService kvStoreService = ctx.getBean(KVStoreService.class);
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null);

assertThat(kvStore.get("object").get(), is(Map.of("some", "json")));
assertThat(kvStore.getRaw("object").get(), is("{some:\"json\"}"));
}
}

@Test
void objectStr() throws IOException, ResourceExpiredException {
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {

EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class);
embeddedServer.start();

String[] args = {
"--server",
embeddedServer.getURL().toString(),
"--user",
"myuser:pass:word",
"io.kestra.cli",
"objectStr",
"{\"some\":\"json\"}",
"-t STRING"
};
PicocliRunner.call(KvUpdateCommand.class, ctx, args);

KVStoreService kvStoreService = ctx.getBean(KVStoreService.class);
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null);

assertThat(kvStore.get("objectStr").get(), is("{\"some\":\"json\"}"));
assertThat(kvStore.getRaw("objectStr").get(), is("\"{\\\"some\\\":\\\"json\\\"}\""));
}
}

@Test
void fromFile() throws IOException, ResourceExpiredException {
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {

EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class);
embeddedServer.start();

File file = File.createTempFile("objectFromFile", ".json");
file.createNewFile();
file.deleteOnExit();
Files.write(file.toPath(), "{\"some\":\"json\",\"from\":\"file\"}".getBytes());

String[] args = {
"--server",
embeddedServer.getURL().toString(),
"--user",
"myuser:pass:word",
"io.kestra.cli",
"objectFromFile",
"valueThatWillGetOverriden",
"-f " + file.getAbsolutePath()
};
PicocliRunner.call(KvUpdateCommand.class, ctx, args);

KVStoreService kvStoreService = ctx.getBean(KVStoreService.class);
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null);

assertThat(kvStore.get("objectFromFile").get(), is(Map.of("some", "json", "from", "file")));
assertThat(kvStore.getRaw("objectFromFile").get(), is("{some:\"json\",from:\"file\"}"));
}
}
}

0 comments on commit 1b6e853

Please sign in to comment.