zilliz 1.4.2

TUI and CLI tool for managing Zilliz Cloud clusters and Milvus operations
Documentation
use crate::config::manager::ConfigManager;
use crate::model::types::{CliModel, DEFAULT_CONTROL_PLANE_ENDPOINT};

/// Resolve the control-plane base URL in precedence order:
///   1. `explicit` (e.g., a `--endpoint` flag value passed by the user)
///   2. the per-user persisted endpoint in `~/.zilliz/config [default].endpoint`
///      (written by `login --cn`, cleared on `logout` or non-CN `login`)
///   3. the `endpoint` field in the bundled control-plane JSON model
///   4. `DEFAULT_CONTROL_PLANE_ENDPOINT`
pub fn resolve_control_plane_url(
    config_mgr: &ConfigManager,
    model: &CliModel,
    explicit: Option<&str>,
) -> String {
    if let Some(url) = explicit {
        if !url.is_empty() {
            return url.to_string();
        }
    }
    if let Some(url) = config_mgr.get_control_plane_endpoint() {
        if !url.is_empty() {
            return url;
        }
    }
    if let Some(url) = model.endpoint.as_ref() {
        if !url.is_empty() {
            return url.clone();
        }
    }
    DEFAULT_CONTROL_PLANE_ENDPOINT.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::types::{AuthConfig, CliModel};
    use indexmap::IndexMap;

    fn model_with_endpoint(endpoint: Option<&str>) -> CliModel {
        CliModel {
            version: String::new(),
            min_cli_version: String::new(),
            endpoint: endpoint.map(|s| s.to_string()),
            auth: Some(AuthConfig {
                auth0_domain: String::new(),
                client_id: String::new(),
                login_api: String::new(),
            }),
            resources: IndexMap::new(),
        }
    }

    fn temp_mgr() -> (tempfile::TempDir, ConfigManager) {
        let dir = tempfile::tempdir().unwrap();
        let mgr = ConfigManager::new(Some(dir.path().to_path_buf())).unwrap();
        (dir, mgr)
    }

    #[test]
    fn explicit_wins_over_everything() {
        let (_d, mgr) = temp_mgr();
        mgr.set_control_plane_endpoint("https://persisted.example.com")
            .unwrap();
        let m = model_with_endpoint(Some("https://model.example.com"));
        assert_eq!(
            resolve_control_plane_url(&mgr, &m, Some("https://explicit.example.com")),
            "https://explicit.example.com"
        );
    }

    #[test]
    fn persisted_wins_over_model_and_default() {
        let (_d, mgr) = temp_mgr();
        mgr.set_control_plane_endpoint("https://persisted.example.com")
            .unwrap();
        let m = model_with_endpoint(Some("https://model.example.com"));
        assert_eq!(
            resolve_control_plane_url(&mgr, &m, None),
            "https://persisted.example.com"
        );
    }

    #[test]
    fn model_wins_over_default() {
        let (_d, mgr) = temp_mgr();
        let m = model_with_endpoint(Some("https://model.example.com"));
        assert_eq!(
            resolve_control_plane_url(&mgr, &m, None),
            "https://model.example.com"
        );
    }

    #[test]
    fn default_applies_when_nothing_is_set() {
        let (_d, mgr) = temp_mgr();
        let m = model_with_endpoint(None);
        assert_eq!(
            resolve_control_plane_url(&mgr, &m, None),
            DEFAULT_CONTROL_PLANE_ENDPOINT
        );
    }

    #[test]
    fn empty_explicit_is_ignored() {
        let (_d, mgr) = temp_mgr();
        let m = model_with_endpoint(Some("https://model.example.com"));
        assert_eq!(
            resolve_control_plane_url(&mgr, &m, Some("")),
            "https://model.example.com"
        );
    }
}