use std::sync::Arc;
use crate::error::{map_manager_err, ManagerError};
use crate::gen::manager::apis::configuration::Configuration;
use crate::gen::manager::apis::manager_api;
use crate::gen::manager::apis::user_management_api as api;
use crate::gen::manager::models;
use crate::retry::{with_retry, RetryPolicy};
pub struct UsersResource {
pub(crate) cfg: Arc<Configuration>,
pub(crate) retry: RetryPolicy,
}
impl UsersResource {
#[allow(deprecated)]
pub async fn me(&self) -> Result<models::GenericItemResponse, ManagerError> {
with_retry(&self.retry, true, || manager_api::get_me(self.cfg.as_ref()))
.await
.map_err(map_manager_err)
}
pub async fn get_by_email(
&self,
email: &str,
) -> Result<models::ManagedUserItemResponse, ManagerError> {
with_retry(&self.retry, true, || {
manager_api::get_user_by_email(self.cfg.as_ref(), email)
})
.await
.map_err(map_manager_err)
}
pub async fn list_all(&self) -> Result<Vec<models::ManagedUser>, ManagerError> {
let resp = with_retry(&self.retry, true, || {
api::list_users(self.cfg.as_ref(), None)
})
.await
.map_err(map_manager_err)?;
Ok(resp.items)
}
pub async fn create(
&self,
user: models::CreateManagedUserRequest,
) -> Result<models::ManagedUserItemResponse, ManagerError> {
with_retry(&self.retry, false, || {
api::create_user(self.cfg.as_ref(), Some(user.clone()))
})
.await
.map_err(map_manager_err)
}
pub async fn enable(&self, emails: Vec<String>) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::enable_users(self.cfg.as_ref(), Some(email_list(&emails)))
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn disable(&self, emails: Vec<String>) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::disable_users(self.cfg.as_ref(), Some(email_list(&emails)))
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn delete(&self, emails: Vec<String>) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::delete_users(self.cfg.as_ref(), Some(email_list(&emails)))
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn reset_passwords(&self, emails: Vec<String>) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::reset_passwords(self.cfg.as_ref(), Some(email_list(&emails)))
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn list_roles(&self) -> Result<Vec<models::AccountRole>, ManagerError> {
let resp = with_retry(&self.retry, true, || {
api::list_available_roles(self.cfg.as_ref())
})
.await
.map_err(map_manager_err)?;
Ok(resp.items)
}
pub async fn add_roles(
&self,
emails: Vec<String>,
roles: Vec<models::AccountRole>,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::add_roles(self.cfg.as_ref(), Some(role_binding(&emails, &roles)))
})
.await
.map_err(map_manager_err)?;
Ok(())
}
pub async fn remove_roles(
&self,
emails: Vec<String>,
roles: Vec<models::AccountRole>,
) -> Result<(), ManagerError> {
with_retry(&self.retry, false, || {
api::remove_roles(self.cfg.as_ref(), Some(role_binding(&emails, &roles)))
})
.await
.map_err(map_manager_err)?;
Ok(())
}
}
fn email_list(emails: &[String]) -> models::EmailListRequest {
models::EmailListRequest {
emails: emails.to_vec(),
}
}
fn role_binding(emails: &[String], roles: &[models::AccountRole]) -> models::EmailRoleBinding {
models::EmailRoleBinding {
emails: Some(emails.to_vec()),
roles: Some(roles.to_vec()),
}
}