use crate::clients::user_mgmt_client::UserMgmtClient;
use crate::error;
use crate::management::users::user::{Group, RoleAndDescription, User, UserAndMetadata};
use crate::options::user_mgmt_options::{
ChangePasswordOptions, DropGroupOptions, DropUserOptions, GetAllGroupsOptions,
GetAllUsersOptions, GetGroupOptions, GetRolesOptions, GetUserOptions, UpsertGroupOptions,
UpsertUserOptions,
};
use crate::tracing::{Keyspace, SERVICE_VALUE_MANAGEMENT};
use couchbase_core::create_span;
use std::sync::Arc;
use tracing::Instrument;
#[derive(Clone)]
pub struct UserManager {
client: Arc<UserMgmtClient>,
}
impl UserManager {
pub(crate) fn new(client: Arc<UserMgmtClient>) -> Self {
Self { client }
}
pub async fn get_all_users(
&self,
opts: impl Into<Option<GetAllUsersOptions>>,
) -> error::Result<Vec<UserAndMetadata>> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_get_all_users"),
)
.await;
let result = self
.client
.get_all_users(opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn get_user(
&self,
username: impl Into<String>,
opts: impl Into<Option<GetUserOptions>>,
) -> error::Result<UserAndMetadata> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_get_user"),
)
.await;
let result = self
.client
.get_user(username.into(), opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn upsert_user(
&self,
settings: User,
opts: impl Into<Option<UpsertUserOptions>>,
) -> error::Result<()> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_upsert_user"),
)
.await;
let result = self
.client
.upsert_user(settings, opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn drop_user(
&self,
username: impl Into<String>,
opts: impl Into<Option<DropUserOptions>>,
) -> error::Result<()> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_drop_user"),
)
.await;
let result = self
.client
.drop_user(username.into(), opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn get_roles(
&self,
opts: impl Into<Option<GetRolesOptions>>,
) -> error::Result<Vec<RoleAndDescription>> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_get_roles"),
)
.await;
let result = self
.client
.get_roles(opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn get_group(
&self,
group_name: impl Into<String>,
opts: impl Into<Option<GetGroupOptions>>,
) -> error::Result<Group> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_get_group"),
)
.await;
let result = self
.client
.get_group(group_name.into(), opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn get_all_groups(
&self,
opts: impl Into<Option<GetAllGroupsOptions>>,
) -> error::Result<Vec<Group>> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_get_all_groups"),
)
.await;
let result = self
.client
.get_all_groups(opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn upsert_group(
&self,
group: Group,
opts: impl Into<Option<UpsertGroupOptions>>,
) -> error::Result<()> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_upsert_group"),
)
.await;
let result = self
.client
.upsert_group(group, opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn drop_group(
&self,
group_name: impl Into<String>,
opts: impl Into<Option<DropGroupOptions>>,
) -> error::Result<()> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_drop_group"),
)
.await;
let result = self
.client
.drop_group(group_name.into(), opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
pub async fn change_password(
&self,
password: impl Into<String>,
opts: impl Into<Option<ChangePasswordOptions>>,
) -> error::Result<()> {
let ctx = self
.client
.tracing_client()
.begin_operation(
Some(SERVICE_VALUE_MANAGEMENT),
Keyspace::Cluster,
create_span!("manager_users_change_password"),
)
.await;
let result = self
.client
.change_password(password.into(), opts.into().unwrap_or_default())
.instrument(ctx.span().clone())
.await;
ctx.end_operation(result.as_ref().err());
result
}
}