use parking_lot::RwLock;
use std::collections::{BTreeSet, HashMap};
#[derive(Default)]
pub struct Tenants {
owned: RwLock<HashMap<String, BTreeSet<String>>>,
}
impl Tenants {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&self, key: impl Into<String>, targets: impl IntoIterator<Item = String>) {
self.owned
.write()
.insert(key.into(), targets.into_iter().collect());
}
pub fn is_empty(&self) -> bool {
self.owned.read().is_empty()
}
pub fn owned(&self, key: &str) -> Option<BTreeSet<String>> {
self.owned.read().get(key).cloned()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::*;
#[test]
fn empty_until_seeded_then_resolves_owned() {
let t = Tenants::new();
assert!(t.is_empty());
t.insert("tenant-a", ["github".to_string(), "eks-prod".to_string()]);
assert!(!t.is_empty());
let owned = t.owned("tenant-a").unwrap();
assert!(owned.contains("github"));
assert!(!owned.contains("aws-acct-b"));
assert!(t.owned("nobody").is_none());
}
}