athena_rs 3.22.1

Hyper performant polyglot Database driver
Documentation
//! Managed container and client-alias resolution helpers for provisioning flows.
//!
//! This module isolates container lookup and client-name inference policy from
//! endpoint orchestration in `provision.rs`.

use actix_web::HttpResponse;

use crate::AppState;
use crate::provisioning::{DockerManagedContainer, list_managed_postgres_containers};

/// Resolves a status-query client alias from a requested client/container name.
///
/// Runtime-registered client names are returned as-is. Otherwise, this helper
/// attempts to map a managed container name to its `athena.client` label.
pub(super) async fn resolve_status_client_name_alias(state: &AppState, requested: &str) -> String {
    let candidate = requested.trim();
    if candidate.is_empty() {
        return String::new();
    }

    if state.pg_registry.registered_client(candidate).is_some() {
        return candidate.to_string();
    }

    let containers: Vec<DockerManagedContainer> = match list_managed_postgres_containers().await {
        Ok(value) => value,
        Err(_) => return candidate.to_string(),
    };

    if let Some(container) = containers
        .iter()
        .find(|container| container.container_name.eq_ignore_ascii_case(candidate))
        && let Some(linked_client) = container_client_label(container)
    {
        return linked_client;
    }

    candidate.to_string()
}

/// Finds one managed Postgres container by container name (case-insensitive).
pub(super) async fn find_managed_container(
    container_name: &str,
) -> Result<Option<DockerManagedContainer>, HttpResponse> {
    let containers: Vec<DockerManagedContainer> = list_managed_postgres_containers()
        .await
        .map_err(|err| super::map_provisioning_error("Failed to list Postgres instances", err))?;

    Ok(containers.into_iter().find(|container| {
        container
            .container_name
            .eq_ignore_ascii_case(container_name)
    }))
}

/// Resolves effective client name for a managed instance operation.
///
/// An explicit non-empty client name wins. Otherwise, this helper attempts
/// to derive it from managed container label `athena.client`.
pub(super) async fn resolve_instance_client_name(
    container_name: &str,
    explicit_client_name: Option<&str>,
) -> Result<Option<String>, HttpResponse> {
    if let Some(client_name) = explicit_client_name
        .map(str::trim)
        .filter(|value| !value.is_empty())
    {
        return Ok(Some(client_name.to_string()));
    }

    let managed: Option<DockerManagedContainer> = find_managed_container(container_name).await?;
    Ok(managed.as_ref().and_then(container_client_label))
}

/// Extracts the `athena.client` label from a managed container when present.
fn container_client_label(container: &DockerManagedContainer) -> Option<String> {
    container
        .labels
        .get("athena.client")
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}