use crate::error::Result;
use dashmap::DashMap;
use std::collections::HashSet;
use std::sync::Arc;
pub struct IsolationManager {
tenant_resources: Arc<DashMap<String, HashSet<String>>>,
}
impl IsolationManager {
pub fn new() -> Self {
Self {
tenant_resources: Arc::new(DashMap::new()),
}
}
pub fn assign_resource(&self, tenant_id: String, resource_id: String) -> Result<()> {
self.tenant_resources
.entry(tenant_id)
.or_default()
.insert(resource_id);
Ok(())
}
pub fn owns_resource(&self, tenant_id: &str, resource_id: &str) -> bool {
self.tenant_resources
.get(tenant_id)
.is_some_and(|resources| resources.contains(resource_id))
}
pub fn get_resources(&self, tenant_id: &str) -> Vec<String> {
self.tenant_resources
.get(tenant_id)
.map(|resources| resources.iter().cloned().collect())
.unwrap_or_default()
}
}
impl Default for IsolationManager {
fn default() -> Self {
Self::new()
}
}