Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ Event watches flush text or JSON output as messages arrive and reconnect after e

`brainpod cluster list` lists active clusters and their supported architectures.

`brainpod storage providers` lists the installation's object storage providers (`KEY NAME REGIONS`; `--json` returns the array). A Bucket resource's `spec.provider` is a provider key and `spec.region` one of that provider's regions; `brainpod describe resource bucket` repeats this in its `notes`.

`brainpod agent` maintains a session console: a page an agent puts in front of the user so a deploy is something they watch rather than sit through. `agent start` writes `console.html` and `session.json` into a directory of their own under `.brainpod/` at the repository root, adds `.brainpod/` to `.gitignore` unless `--no-ignore` is passed, and prints the page to open. The page reads `session.json` and `session.log` from its own directory and needs no server. Where `agent serve` is running it advertises an event stream in the session, and the page upgrades from polling to push; if that stream drops it falls back to polling, so the two paths run the same code. Every write replaces the file through a temporary rename, so the page never reads a partial write.

Which of the two ways to open the page works depends on the browser. An agent's embedded browser will only execute a local page from inside the project, so it opens `console.html` directly. A browser outside the agent will display that page but never populate it, because reading a file from the same directory is blocked on `file://`. For those, `agent serve` publishes the console over loopback instead, which puts the page and its session on one origin. It announces the URL on stdout and then blocks, so run it in the background and read the first line; the URL carries a random path because loopback is reachable by anything else on the machine.
Expand Down
24 changes: 24 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum Command {
Whoami,
/// List available clusters
Cluster(ClusterArgs),
/// List object storage providers and regions for Bucket resources
Storage(StorageArgs),
/// Inspect pods
Pod(PodArgs),
/// Browse and install blueprints
Expand Down Expand Up @@ -117,6 +119,18 @@ enum ClusterCommand {
List,
}

#[derive(Debug, Args)]
pub struct StorageArgs {
#[command(subcommand)]
command: StorageCommand,
}

#[derive(Debug, Subcommand)]
enum StorageCommand {
/// List storage providers and their regions; a Bucket's spec.provider is a KEY and spec.region one of its REGIONS
Providers,
}

#[derive(Debug, Args)]
pub struct PodArgs {
#[command(subcommand)]
Expand Down Expand Up @@ -581,6 +595,7 @@ pub async fn handle(command: Command, context: CommandContext<'_>) -> Result<Com
View::Whoami,
)),
Command::Cluster(args) => handle_cluster(client_required(client)?, args).await,
Command::Storage(args) => handle_storage(client_required(client)?, args).await,
Command::Pod(args) => handle_pod(client_required(client)?, args).await,
Command::Tunnel(args) => {
crate::tunnel::handle(
Expand Down Expand Up @@ -670,6 +685,15 @@ async fn handle_cluster(client: &Client, args: ClusterArgs) -> Result<CommandOut
}
}

async fn handle_storage(client: &Client, args: StorageArgs) -> Result<CommandOutput> {
match args.command {
StorageCommand::Providers => Ok(CommandOutput::new(
client.get(&["v1", "storage", "providers"], &[]).await?,
View::StorageProviderList,
)),
}
}

fn handle_config(args: ConfigArgs, config: &mut Config, path: &Path) -> Result<CommandOutput> {
match args.command {
ConfigCommand::Show => Ok(CommandOutput::new(
Expand Down
13 changes: 10 additions & 3 deletions src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,14 @@ fn requirements(path: &[&str]) -> (Option<bool>, Option<bool>) {
}
["blueprint"] => (Some(true), None),
["blueprint", "install"] => (Some(true), Some(true)),
["blueprint", _] | ["whoami"] | ["cluster"] | ["cluster", _] | ["pod"] | ["pod", _] => {
(Some(true), Some(false))
}
["blueprint", _]
| ["whoami"]
| ["cluster"]
| ["cluster", _]
| ["storage"]
| ["storage", _]
| ["pod"]
| ["pod", _] => (Some(true), Some(false)),
["s3"] | ["s3", _] => (Some(true), Some(true)),
["image"]
| ["image", _]
Expand Down Expand Up @@ -288,6 +293,7 @@ fn effect(path: &[&str]) -> (&'static str, &'static str) {
),
["whoami"]
| ["cluster", "list"]
| ["storage", "providers"]
| ["pod", "list"]
| ["pod", "get"]
| ["blueprint", "list"]
Expand Down Expand Up @@ -362,6 +368,7 @@ fn next_steps(path: &[&str]) -> Vec<&'static str> {
"Inspect the updated draft with `brainpod resource list`.",
"Read the references other resources can use with `brainpod resource variables`.",
"Discover the variables a kind exports before creating it with `brainpod describe resource <kind>`; it needs no pod and no API token.",
"Choose a Bucket's spec.provider and spec.region from `brainpod storage providers`.",
"Deploy it with `brainpod deploy` when ready.",
],
["resource", "list"] | ["resource", "get"] => vec![
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ mod tests {
);
}

#[test]
fn parses_storage_providers() {
let opts = Opts::try_parse_from(["brainpod", "storage", "providers"]).unwrap();

assert!(matches!(opts.command, Command::Storage(_)));
assert!(super::cmd::needs_client(&opts.command));
}

#[test]
fn parses_cluster_list() {
let opts = Opts::try_parse_from(["brainpod", "cluster", "list"]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/openapi.json

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub async fn describe(path: &[String], endpoint: Option<&str>) -> Result<Value>
"sourceUrl": url,
"schema": resource.schema,
"variables": resource.variables,
"notes": resource_notes(&resource.kind),
}));
}

Expand All @@ -59,6 +60,7 @@ pub async fn describe(path: &[String], endpoint: Option<&str>) -> Result<Value>
"kind": resource.kind,
"schema": resource.schema,
"variables": resource.variables,
"notes": resource_notes(&resource.kind),
})).collect::<Vec<_>>(),
}))
}
Expand Down Expand Up @@ -127,6 +129,17 @@ struct ResourceCatalog {
variables: Value,
}

/// Guidance for fields whose valid values are installation data rather than schema enums.
fn resource_notes(kind: &str) -> Vec<&'static str> {
if kind.eq_ignore_ascii_case("Bucket") {
vec![
"spec.provider must be a provider `key` and spec.region one of that provider's `regions` from `brainpod storage providers`.",
]
} else {
Vec::new()
}
}

fn resource_schemas(spec: &Value) -> Result<Vec<ResourceCatalog>> {
let branches = spec
.pointer("/components/schemas/ResourceInput/oneOf")
Expand Down Expand Up @@ -167,7 +180,25 @@ fn resource_schemas(spec: &Value) -> Result<Vec<ResourceCatalog>> {
mod tests {
use serde_json::{Value, json};

use super::{is_resource_path, resource_schemas};
use super::{EMBEDDED_OPENAPI, is_resource_path, resource_notes, resource_schemas};

#[test]
fn embedded_fallback_describes_bucket() {
let spec: Value = serde_json::from_str(EMBEDDED_OPENAPI).unwrap();
let resources = resource_schemas(&spec).unwrap();

assert!(resources.iter().any(|resource| resource.kind == "Bucket"));
}

#[test]
fn points_bucket_provider_and_region_at_storage_providers() {
assert!(
resource_notes("Bucket")
.iter()
.any(|note| note.contains("brainpod storage providers"))
);
assert!(resource_notes("App").is_empty());
}

#[test]
fn recognizes_resource_schema_paths_without_shadowing_commands() {
Expand Down
52 changes: 50 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum View {
ConfigChange,
Whoami,
ClusterList,
StorageProviderList,
PodList,
PodCreated,
PodGet,
Expand Down Expand Up @@ -179,6 +180,7 @@ fn render(value: &Value, view: View, color: bool) -> Vec<String> {
View::ConfigChange => render_config_change(value),
View::Whoami => render_whoami(value),
View::ClusterList => render_cluster_list(value),
View::StorageProviderList => render_storage_provider_list(value),
View::PodList => render_pod_list(value),
View::PodCreated => {
let mut lines = vec!["Pod created".to_owned(), String::new()];
Expand Down Expand Up @@ -341,6 +343,15 @@ fn render_resource_schema(value: &Value) -> Vec<String> {
render_schema_properties(value.get("schema"), 2, 0, &mut lines);
lines.push(String::new());
lines.extend(render_variable_catalog(value.get("variables")));
if let Some(notes) = value
.get("notes")
.and_then(Value::as_array)
.filter(|notes| !notes.is_empty())
{
lines.push(String::new());
lines.push("Notes".to_owned());
lines.extend(notes.iter().map(|note| format!(" {}", scalar(note))));
}
lines
}

Expand Down Expand Up @@ -680,6 +691,27 @@ fn render_cluster_list(value: &Value) -> Vec<String> {
table(&["ID", "PROVIDER", "REGION", "ARCHITECTURES"], rows)
}

fn render_storage_provider_list(value: &Value) -> Vec<String> {
let Some(providers) = value.as_array() else {
return vec!["No storage providers returned.".to_owned()];
};
if providers.is_empty() {
return vec!["No storage providers.".to_owned()];
}

let rows = providers
.iter()
.map(|provider| {
vec![
field(provider, "key"),
field(provider, "name"),
string_list(provider.get("regions")),
]
})
.collect();
table(&["KEY", "NAME", "REGIONS"], rows)
}

fn render_pod_list(value: &Value) -> Vec<String> {
let Some(pods) = value.as_array() else {
return vec!["No pods returned.".to_owned()];
Expand Down Expand Up @@ -1430,8 +1462,8 @@ mod tests {

use super::{
render_cluster_list, render_event, render_image_inspect, render_image_list,
render_resource_schema, render_resource_variables, render_whoami, stream_error,
write_login_json, write_stream_json,
render_resource_schema, render_resource_variables, render_storage_provider_list,
render_whoami, stream_error, write_login_json, write_stream_json,
};

#[test]
Expand Down Expand Up @@ -1497,6 +1529,22 @@ mod tests {
assert!(lines.iter().any(|line| line.contains("amd64, arm64")));
}

#[test]
fn renders_storage_provider_list() {
let lines = render_storage_provider_list(&json!([
{"key": "exoscale", "name": "Exoscale", "regions": ["ch-gva-2", "de-fra-1"]}
]));

assert!(lines[0].contains("KEY") && lines[0].contains("REGIONS"));
assert!(lines.iter().any(|line: &String| line.contains("exoscale")
&& line.contains("Exoscale")
&& line.contains("ch-gva-2, de-fra-1")));
assert_eq!(
render_storage_provider_list(&json!([])),
vec!["No storage providers.".to_owned()]
);
}

#[test]
fn renders_platform_event() {
let event = json!({
Expand Down
Loading