mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat(reset): add floci reset to delete every resource in a running emulator
floci reset (and floci gcp/az/oci reset) calls the emulator's state reset endpoint, so a test run can start clean without restarting the container. It asks for confirmation on a terminal; --yes skips the prompt and is required when there is no terminal. A server without the endpoint (floci-az today) gets a 'not available' message. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
14 files changed
+288
-2
No files matched your search
@@ -72,7 +72,7 @@ Commands call `global.printer()` at the start of `call()`; never store a `Printe
|
||||
|
||||
**Docker:** `DockerClient` wraps `docker` CLI subprocesses, with no docker-java library. This is a deliberate native-image trade-off. `DockerClient` returns plain records (`ContainerInfo`, `ImageInfo`) and throws `DockerException` on non-zero exit codes.
|
||||
|
||||
**Floci server:** `FlociHttpClient` wraps `java.net.http.HttpClient` against the Floci REST API. All methods throw `FlociException`. The control-plane prefix is constructor-configurable: AWS and Azure use `/_floci`, GCP uses `/_floci-gcp`. Known live endpoints: `/_floci/health`, `/_floci/info`, `/_floci/init`, and on floci-az only `/_floci/tls-cert` (`tlsCert()`, the PEM CA; 404 when TLS is off). Snapshot endpoints (`/_floci/snapshots/*`) do not exist on the server yet.
|
||||
**Floci server:** `FlociHttpClient` wraps `java.net.http.HttpClient` against the Floci REST API. All methods throw `FlociException`. The control-plane prefix is constructor-configurable: AWS and Azure use `/_floci`, GCP uses `/_floci-gcp`. Known live endpoints: `/_floci/health`, `/_floci/info`, `/_floci/init`, `POST <prefix>/state/reset` (AWS, GCP and OCI; `floci reset`), and on floci-az only `/_floci/tls-cert` (`tlsCert()`, the PEM CA; 404 when TLS is off). Snapshot endpoints (`/_floci/snapshots/*`) do not exist on the server yet.
|
||||
|
||||
### Output pipeline
|
||||
|
||||
|
||||
@@ -241,6 +241,7 @@ Commands are organized into four product groups: `floci aws` (or bare `floci`),
|
||||
| `floci wait` | Poll until Floci is ready (CI-friendly) |
|
||||
| `floci version` | Show CLI and server versions |
|
||||
| `floci services` | List enabled AWS services |
|
||||
| `floci reset` | Delete every resource in the running emulator, keeping it running |
|
||||
| `floci doctor` | Run environment diagnostics |
|
||||
| `floci env` | Print AWS environment variables |
|
||||
| `floci snapshot save/load/list/delete` | Manage state snapshots |
|
||||
@@ -261,6 +262,7 @@ Bare `floci config ...` always means the AWS tree, whatever the default product;
|
||||
| `floci gcp wait` | Poll until Floci GCP is ready (CI-friendly) |
|
||||
| `floci gcp version` | Show CLI and server versions |
|
||||
| `floci gcp services` | List enabled GCP services |
|
||||
| `floci gcp reset` | Delete every resource in the running emulator, keeping it running |
|
||||
| `floci gcp doctor` | Run GCP environment diagnostics |
|
||||
| `floci gcp env` | Print GCP SDK emulator host variables |
|
||||
| `floci gcp snapshot` | Snapshot commands (coming soon) |
|
||||
@@ -278,6 +280,7 @@ Bare `floci config ...` always means the AWS tree, whatever the default product;
|
||||
| `floci az wait` | Poll until Floci Azure is ready (CI-friendly) |
|
||||
| `floci az version` | Show CLI and server versions |
|
||||
| `floci az services` | List enabled Azure services |
|
||||
| `floci az reset` | Delete every resource in the running emulator, keeping it running |
|
||||
| `floci az doctor` | Run Azure environment diagnostics |
|
||||
| `floci az env` | Print Azure connection string / SDK env vars |
|
||||
| `floci az setup` | Point the `az` CLI at the emulator: trust its certificate, register the cloud, log in |
|
||||
@@ -296,6 +299,7 @@ Bare `floci config ...` always means the AWS tree, whatever the default product;
|
||||
| `floci oci wait` | Poll until Floci OCI is ready (CI-friendly) |
|
||||
| `floci oci version` | Show CLI and server versions |
|
||||
| `floci oci services` | List enabled OCI services |
|
||||
| `floci oci reset` | Delete every resource in the running emulator, keeping it running |
|
||||
| `floci oci doctor` | Run OCI environment diagnostics |
|
||||
| `floci oci env` | Print OCI endpoint variables |
|
||||
| `floci oci setup` | Create a local OCI CLI profile (API key + `~/.oci/config`) |
|
||||
@@ -682,6 +686,18 @@ floci services # list all enabled services
|
||||
floci services -o json
|
||||
```
|
||||
|
||||
### `floci reset` / `floci gcp reset` / `floci az reset` / `floci oci reset`
|
||||
|
||||
```sh
|
||||
floci reset # asks first, then deletes every resource; the emulator keeps running
|
||||
floci reset --yes # no prompt (required when not on a terminal, e.g. in CI)
|
||||
floci gcp reset -y -o json
|
||||
```
|
||||
|
||||
It calls the emulator's `POST <prefix>/state/reset`. The AWS, GCP and OCI emulators serve it; on a
|
||||
server without it (currently floci-az) the command says reset is not available and exits 1. Without
|
||||
`--yes` and with no terminal to ask on, it exits 2.
|
||||
|
||||
### `floci config`
|
||||
|
||||
```sh
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.util.Arrays;
|
||||
WaitCommand.class,
|
||||
VersionCommand.class,
|
||||
ServicesCommand.class,
|
||||
ResetCommand.class,
|
||||
DoctorCommand.class,
|
||||
EnvCommand.class,
|
||||
ConfigCommand.class,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package io.floci.cli.commands;
|
||||
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.http.FlociException;
|
||||
import io.floci.cli.http.FlociHttpClient;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.io.Console;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci AWS instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class ResetCommand implements Callable<Integer> {
|
||||
|
||||
protected final ProductProfile profile;
|
||||
|
||||
@Mixin
|
||||
protected GlobalOptions global;
|
||||
|
||||
@Option(names = {"--yes", "-y"}, description = "Reset without asking for confirmation")
|
||||
boolean yes;
|
||||
|
||||
public ResetCommand() {
|
||||
this(ProductProfile.AWS);
|
||||
}
|
||||
|
||||
protected ResetCommand(ProductProfile profile) {
|
||||
this.profile = profile;
|
||||
this.global = new GlobalOptions(profile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||
|
||||
if (!yes) {
|
||||
Console console = System.console();
|
||||
if (console == null || !console.isTerminal()) {
|
||||
// Nobody to ask: a script must say so explicitly.
|
||||
printer.error("reset deletes every resource in " + profile.displayName() + " and needs confirmation.\n"
|
||||
+ "Pass --yes to reset without asking.");
|
||||
return 2;
|
||||
}
|
||||
String answer = console.readLine("Delete every resource in %s at %s? [y/N] ",
|
||||
profile.displayName(), effectiveEndpoint);
|
||||
if (answer == null || !(answer.trim().equalsIgnoreCase("y") || answer.trim().equalsIgnoreCase("yes"))) {
|
||||
printer.println("Reset cancelled.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
new FlociHttpClient(effectiveEndpoint, profile).resetState();
|
||||
} catch (FlociException e) {
|
||||
if (e.getMessage().contains("HTTP 404") || e.getMessage().contains("HTTP 501")) {
|
||||
printer.error("Resetting state is not available on this " + profile.displayName() + " server version.\n"
|
||||
+ "Restart it without --persist for a clean state, or track progress: " + profile.serverRepo() + "/issues");
|
||||
} else {
|
||||
String message = "Failed to reset " + profile.displayName() + ": " + e.getMessage();
|
||||
// Connection errors already say what to do; others get the status hint.
|
||||
printer.error(message.contains("\n") ? message
|
||||
: message + "\nRun '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator.");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (printer.format() != OutputFormat.text) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("reset", true);
|
||||
out.put("endpoint", effectiveEndpoint);
|
||||
printer.structured(out);
|
||||
return 0;
|
||||
}
|
||||
printer.println(Ansi.green("✓") + " " + profile.displayName() + " state reset" + Ansi.gray(" (" + effectiveEndpoint + ")"));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import picocli.CommandLine.*;
|
||||
WaitCommand.class,
|
||||
VersionCommand.class,
|
||||
ServicesCommand.class,
|
||||
ResetCommand.class,
|
||||
DoctorCommand.class,
|
||||
EnvCommand.class,
|
||||
ConfigCommand.class,
|
||||
|
||||
@@ -25,6 +25,7 @@ import picocli.CommandLine.*;
|
||||
AzWaitCommand.class,
|
||||
AzVersionCommand.class,
|
||||
AzServicesCommand.class,
|
||||
AzResetCommand.class,
|
||||
AzDoctorCommand.class,
|
||||
AzEnvCommand.class,
|
||||
AzSetupCommand.class,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.floci.cli.commands.az;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.ResetCommand;
|
||||
import picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci Azure instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class AzResetCommand extends ResetCommand {
|
||||
|
||||
public AzResetCommand() {
|
||||
super(ProductProfile.AZ);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import picocli.CommandLine.*;
|
||||
GcpWaitCommand.class,
|
||||
GcpVersionCommand.class,
|
||||
GcpServicesCommand.class,
|
||||
GcpResetCommand.class,
|
||||
GcpDoctorCommand.class,
|
||||
GcpEnvCommand.class,
|
||||
GcpConfigCommand.class,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.floci.cli.commands.gcp;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.ResetCommand;
|
||||
import picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci GCP instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class GcpResetCommand extends ResetCommand {
|
||||
|
||||
public GcpResetCommand() {
|
||||
super(ProductProfile.GCP);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import picocli.CommandLine.*;
|
||||
OciWaitCommand.class,
|
||||
OciVersionCommand.class,
|
||||
OciServicesCommand.class,
|
||||
OciResetCommand.class,
|
||||
OciDoctorCommand.class,
|
||||
OciEnvCommand.class,
|
||||
OciSetupCommand.class,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.floci.cli.commands.oci;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.ResetCommand;
|
||||
import picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci OCI instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class OciResetCommand extends ResetCommand {
|
||||
|
||||
public OciResetCommand() {
|
||||
super(ProductProfile.OCI);
|
||||
}
|
||||
}
|
||||
@@ -171,6 +171,11 @@ public class FlociHttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes every resource the emulator holds ({@code POST <prefix>/state/reset}). */
|
||||
public Map<String, Object> resetState() throws FlociException {
|
||||
return postJson(controlPrefix + "/state/reset", "{}");
|
||||
}
|
||||
|
||||
public Map<String, Object> postSnapshot(String name) throws FlociException {
|
||||
return postJson(controlPrefix + "/snapshots/" + name, "{}");
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class ProductTreesParsingTest {
|
||||
|
||||
private static final Set<String> LIFECYCLE_COMMANDS = Set.of(
|
||||
"start", "stop", "restart", "status", "logs", "wait",
|
||||
"version", "services", "doctor", "env", "config", "snapshot", "completion", "help");
|
||||
"version", "services", "reset", "doctor", "env", "config", "snapshot", "completion", "help");
|
||||
|
||||
private static CommandSpec leafSpec(String... args) {
|
||||
ParseResult r = new CommandLine(new FlociCli()).parseArgs(args);
|
||||
@@ -43,6 +43,7 @@ class ProductTreesParsingTest {
|
||||
void awsTreeDefaults() {
|
||||
assertDefaults("http://localhost:4566", "floci", "status");
|
||||
assertDefaults("http://localhost:4566", "floci", "aws", "status");
|
||||
assertDefaults("http://localhost:4566", "floci", "reset");
|
||||
assertStartDefaults("4566", "floci/floci:latest", "start");
|
||||
assertStartDefaults("4566", "floci/floci:latest", "aws", "start");
|
||||
}
|
||||
@@ -50,18 +51,21 @@ class ProductTreesParsingTest {
|
||||
@Test
|
||||
void gcpTreeDefaults() {
|
||||
assertDefaults("http://localhost:4588", "floci-gcp", "gcp", "status");
|
||||
assertDefaults("http://localhost:4588", "floci-gcp", "gcp", "reset");
|
||||
assertStartDefaults("4588", "floci/floci-gcp:latest", "gcp", "start");
|
||||
}
|
||||
|
||||
@Test
|
||||
void azTreeDefaults() {
|
||||
assertDefaults("http://localhost:4577", "floci-az", "az", "status");
|
||||
assertDefaults("http://localhost:4577", "floci-az", "az", "reset");
|
||||
assertStartDefaults("4577", "floci/floci-az:latest", "az", "start");
|
||||
}
|
||||
|
||||
@Test
|
||||
void ociTreeDefaults() {
|
||||
assertDefaults("http://localhost:4599", "floci-oci", "oci", "status");
|
||||
assertDefaults("http://localhost:4599", "floci-oci", "oci", "reset");
|
||||
assertStartDefaults("4599", "floci/floci-oci:latest", "oci", "start");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** BL-064: floci reset against a stub emulator. */
|
||||
class ResetCommandTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private HttpServer server;
|
||||
private final List<String> requests = new CopyOnWriteArrayList<>();
|
||||
private final AtomicInteger status = new AtomicInteger(200);
|
||||
|
||||
private record Run(int exit, String out, String err) {}
|
||||
|
||||
@BeforeEach
|
||||
void start() throws Exception {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/", exchange -> {
|
||||
requests.add(exchange.getRequestMethod() + " " + exchange.getRequestURI().getPath());
|
||||
byte[] body = "{\"status\":\"OK\"}".getBytes(StandardCharsets.UTF_8);
|
||||
if (status.get() == 200) {
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(body);
|
||||
}
|
||||
} else {
|
||||
exchange.sendResponseHeaders(status.get(), -1);
|
||||
}
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stop() {
|
||||
server.stop(0);
|
||||
}
|
||||
|
||||
private Run run(String... args) {
|
||||
List<String> all = new ArrayList<>(List.of(args));
|
||||
all.addAll(List.of("--endpoint", "http://127.0.0.1:" + server.getAddress().getPort(),
|
||||
"--container", "floci-reset-test-none"));
|
||||
PrintStream out = System.out;
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outBuf));
|
||||
System.setErr(new PrintStream(errBuf));
|
||||
try {
|
||||
int exit = FlociCli.buildCommandLine(new ProfileStore(tempDir)).execute(all.toArray(String[]::new));
|
||||
return new Run(exit, outBuf.toString(), errBuf.toString());
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void yesPostsToTheProductsResetEndpoint() {
|
||||
Run r = run("gcp", "reset", "--yes");
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
assertEquals(List.of("POST /_floci-gcp/state/reset"), requests);
|
||||
assertTrue(r.out().contains("state reset"), r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void structuredOutputReportsTheReset() {
|
||||
Run r = run("reset", "-y", "-o", "json");
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
assertEquals(List.of("POST /_floci/state/reset"), requests);
|
||||
assertTrue(r.out().contains("\"reset\" : true") || r.out().contains("\"reset\":true"), r.out());
|
||||
}
|
||||
|
||||
/** Nobody to ask: without --yes nothing is deleted and the exit code says it was a usage problem. */
|
||||
@Test
|
||||
void withoutATerminalItNeedsYes() {
|
||||
Run r = run("oci", "reset");
|
||||
|
||||
assertEquals(2, r.exit());
|
||||
assertTrue(r.err().contains("Pass --yes"), r.err());
|
||||
assertEquals(List.of(), requests, "nothing was reset");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServerWithoutTheEndpointSaysResetIsNotAvailable() {
|
||||
status.set(404);
|
||||
|
||||
Run r = run("az", "reset", "--yes");
|
||||
|
||||
assertEquals(1, r.exit());
|
||||
assertTrue(r.err().contains("not available"), r.err());
|
||||
assertTrue(r.err().contains("floci-az"), r.err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user