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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ brainpod describe resource app --json
brainpod describe resource postgres --json
```

Resource schemas are fetched from `https://api.prod.brainpod.io/v1/openapi.json` on each request. If the production document cannot be reached or does not contain the expected resource schemas, the version embedded in the CLI is used instead. Set `BRAINPOD_OPENAPI_URL` to override the document URL, or pass `--endpoint` to derive the document URL from another API endpoint.
Resource schemas are fetched on each request from `<endpoint>/v1/openapi.json`, where the endpoint is resolved like every other command's: `--endpoint`, then `BRAINPOD_API_ENDPOINT`, then the configured endpoint, then `https://api.brainpod.io`. No API token is needed. If the document cannot be reached or does not contain the expected resource schemas, the version embedded in the CLI is used instead. Set `BRAINPOD_OPENAPI_URL` to override the document URL entirely.

Each schema carries the variables that kind exports, so the references an App resource can use in `spec.env` are discoverable before the resource exists. Names in this catalog are placeholders: `${<name>.uri}` becomes `${db.uri}` for a resource named `db`. Template-sourced variables also report the template, which contains references only and never values.

Expand Down
56 changes: 46 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ async fn run(opts: Opts) -> Result<output::CommandOutput> {

if let Command::Describe(args) = &opts.command {
if openapi::is_resource_path(&args.command) {
let resource_description =
openapi::describe(&args.command, opts.endpoint.as_deref()).await?;
let endpoint = api_endpoint(
opts.endpoint.clone(),
environment("BRAINPOD_API_ENDPOINT"),
|| Ok(Config::load(&Config::path()?)?.endpoint),
)?;
let resource_description = openapi::describe(&args.command, Some(&endpoint)).await?;
Comment on lines +108 to +113
if args.command.len() == 1 {
let mut description = describe::generate(Opts::command(), &args.command)?;
description
Expand Down Expand Up @@ -138,14 +142,9 @@ async fn run(opts: Opts) -> Result<output::CommandOutput> {
let config_path = Config::path()?;
let mut config = Config::load(&config_path)?;

let endpoint = match opts
.endpoint
.or_else(|| environment("BRAINPOD_API_ENDPOINT"))
.or_else(|| config.endpoint.clone())
{
Some(endpoint) => endpoint,
None => DEFAULT_ENDPOINT.to_owned(),
};
let endpoint = api_endpoint(opts.endpoint, environment("BRAINPOD_API_ENDPOINT"), || {
Ok(config.endpoint.clone())
})?;
let api_token = opts
.api_token
.or_else(|| environment("BRAINPOD_API_TOKEN"))
Expand Down Expand Up @@ -236,6 +235,19 @@ pub(crate) fn http_client_builder() -> Result<reqwest::ClientBuilder> {
Ok(reqwest::Client::builder())
}

/// Resolves the API endpoint: flag, then `BRAINPOD_API_ENDPOINT`, then the configured
/// endpoint (loaded only when needed), then the production default.
fn api_endpoint(
flag: Option<String>,
environment: Option<String>,
config: impl FnOnce() -> Result<Option<String>>,
) -> Result<String> {
if let Some(endpoint) = flag.or(environment) {
return Ok(endpoint);
}
Ok(config()?.unwrap_or_else(|| DEFAULT_ENDPOINT.to_owned()))
}

fn environment(name: &str) -> Option<String> {
std::env::var(name)
.ok()
Expand Down Expand Up @@ -430,6 +442,30 @@ mod tests {
assert!(result.is_err());
}

#[test]
fn resolves_api_endpoint_by_precedence() {
let config = || Ok(Some("https://config.example".to_owned()));
let flag = Some("https://flag.example".to_owned());
let env = Some("https://env.example".to_owned());

assert_eq!(
super::api_endpoint(flag, env.clone(), config).unwrap(),
"https://flag.example"
);
assert_eq!(
super::api_endpoint(None, env, || panic!("config must not be loaded")).unwrap(),
"https://env.example"
);
assert_eq!(
super::api_endpoint(None, None, config).unwrap(),
"https://config.example"
);
assert_eq!(
super::api_endpoint(None, None, || Ok(None)).unwrap(),
super::DEFAULT_ENDPOINT
);
}

#[test]
fn parses_cluster_list() {
let opts = Opts::try_parse_from(["brainpod", "cluster", "list"]).unwrap();
Expand Down
Loading