diff --git a/AGENTS.md b/AGENTS.md index eafc09d..16cfa6b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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 /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 diff --git a/README.md b/README.md index bd89781..d8a3d9e 100644 --- a/README.md +++ b/README.md @@ -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 /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 diff --git a/src/main/java/io/floci/cli/FlociCli.java b/src/main/java/io/floci/cli/FlociCli.java index 146d811..39e98b2 100644 --- a/src/main/java/io/floci/cli/FlociCli.java +++ b/src/main/java/io/floci/cli/FlociCli.java @@ -42,6 +42,7 @@ import java.util.Arrays; WaitCommand.class, VersionCommand.class, ServicesCommand.class, + ResetCommand.class, DoctorCommand.class, EnvCommand.class, ConfigCommand.class, diff --git a/src/main/java/io/floci/cli/commands/ResetCommand.java b/src/main/java/io/floci/cli/commands/ResetCommand.java new file mode 100644 index 0000000..3aabb2c --- /dev/null +++ b/src/main/java/io/floci/cli/commands/ResetCommand.java @@ -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 { + + 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 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; + } +} diff --git a/src/main/java/io/floci/cli/commands/aws/AwsCommand.java b/src/main/java/io/floci/cli/commands/aws/AwsCommand.java index 5eb8174..61675eb 100644 --- a/src/main/java/io/floci/cli/commands/aws/AwsCommand.java +++ b/src/main/java/io/floci/cli/commands/aws/AwsCommand.java @@ -24,6 +24,7 @@ import picocli.CommandLine.*; WaitCommand.class, VersionCommand.class, ServicesCommand.class, + ResetCommand.class, DoctorCommand.class, EnvCommand.class, ConfigCommand.class, diff --git a/src/main/java/io/floci/cli/commands/az/AzCommand.java b/src/main/java/io/floci/cli/commands/az/AzCommand.java index d847d57..87ff985 100644 --- a/src/main/java/io/floci/cli/commands/az/AzCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzCommand.java @@ -25,6 +25,7 @@ import picocli.CommandLine.*; AzWaitCommand.class, AzVersionCommand.class, AzServicesCommand.class, + AzResetCommand.class, AzDoctorCommand.class, AzEnvCommand.class, AzSetupCommand.class, diff --git a/src/main/java/io/floci/cli/commands/az/AzResetCommand.java b/src/main/java/io/floci/cli/commands/az/AzResetCommand.java new file mode 100644 index 0000000..79f0250 --- /dev/null +++ b/src/main/java/io/floci/cli/commands/az/AzResetCommand.java @@ -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); + } +} diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpCommand.java index 5f8eb78..5e9868d 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpCommand.java @@ -24,6 +24,7 @@ import picocli.CommandLine.*; GcpWaitCommand.class, GcpVersionCommand.class, GcpServicesCommand.class, + GcpResetCommand.class, GcpDoctorCommand.class, GcpEnvCommand.class, GcpConfigCommand.class, diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpResetCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpResetCommand.java new file mode 100644 index 0000000..9425712 --- /dev/null +++ b/src/main/java/io/floci/cli/commands/gcp/GcpResetCommand.java @@ -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); + } +} diff --git a/src/main/java/io/floci/cli/commands/oci/OciCommand.java b/src/main/java/io/floci/cli/commands/oci/OciCommand.java index c704940..899c0c1 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciCommand.java @@ -25,6 +25,7 @@ import picocli.CommandLine.*; OciWaitCommand.class, OciVersionCommand.class, OciServicesCommand.class, + OciResetCommand.class, OciDoctorCommand.class, OciEnvCommand.class, OciSetupCommand.class, diff --git a/src/main/java/io/floci/cli/commands/oci/OciResetCommand.java b/src/main/java/io/floci/cli/commands/oci/OciResetCommand.java new file mode 100644 index 0000000..bebe489 --- /dev/null +++ b/src/main/java/io/floci/cli/commands/oci/OciResetCommand.java @@ -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); + } +} diff --git a/src/main/java/io/floci/cli/http/FlociHttpClient.java b/src/main/java/io/floci/cli/http/FlociHttpClient.java index 73ea7f1..7e4091d 100644 --- a/src/main/java/io/floci/cli/http/FlociHttpClient.java +++ b/src/main/java/io/floci/cli/http/FlociHttpClient.java @@ -171,6 +171,11 @@ public class FlociHttpClient { } } + /** Deletes every resource the emulator holds ({@code POST /state/reset}). */ + public Map resetState() throws FlociException { + return postJson(controlPrefix + "/state/reset", "{}"); + } + public Map postSnapshot(String name) throws FlociException { return postJson(controlPrefix + "/snapshots/" + name, "{}"); } diff --git a/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java b/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java index e7c4929..2acb7b7 100644 --- a/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java +++ b/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java @@ -19,7 +19,7 @@ class ProductTreesParsingTest { private static final Set 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"); } diff --git a/src/test/java/io/floci/cli/unit/ResetCommandTest.java b/src/test/java/io/floci/cli/unit/ResetCommandTest.java new file mode 100644 index 0000000..f2b91bb --- /dev/null +++ b/src/test/java/io/floci/cli/unit/ResetCommandTest.java @@ -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 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 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()); + } +}