#![allow(clippy::too_many_arguments)]
use uuid::Uuid;
use crate::AxiamError;
use crate::client::AxiamClient;
use crate::management::models;
use crate::management::page::{Page, PageRequest, collect_pages};
use crate::management::request::{Call, Verb};
use crate::management::scope::Scope;
#[derive(Clone, Copy)]
pub struct Groups<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for Groups<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Groups")
.field("scope", &self.scope)
.finish()
}
}
impl<'c> Groups<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
pub async fn list(&self, page: PageRequest) -> Result<Page<models::Group>, AxiamError> {
let query = page.query();
let call = Call {
operation: "groups.list",
verb: Verb::Get,
path_template: "/api/v1/groups",
path: "/api/v1/groups".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::Group>>(call, None::<&()>)
.await
}
pub async fn list_all(&self, start: PageRequest) -> Result<Vec<models::Group>, AxiamError> {
collect_pages(start, |p| self.list(p)).await
}
pub async fn create(
&self,
body: &models::CreateGroupRequest,
) -> Result<models::Group, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.create",
verb: Verb::Post,
path_template: "/api/v1/groups",
path: "/api/v1/groups".to_string(),
query: &query,
};
self.client
.management_send::<_, models::Group>(call, Some(body))
.await
}
pub async fn get(&self, group_id: Uuid) -> Result<models::Group, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.get",
verb: Verb::Get,
path_template: "/api/v1/groups/{group_id}",
path: format!("/api/v1/groups/{group_id}"),
query: &query,
};
self.client
.management_send::<_, models::Group>(call, None::<&()>)
.await
}
pub async fn update(
&self,
group_id: Uuid,
body: &models::UpdateGroup,
) -> Result<models::Group, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.update",
verb: Verb::Put,
path_template: "/api/v1/groups/{group_id}",
path: format!("/api/v1/groups/{group_id}"),
query: &query,
};
self.client
.management_send::<_, models::Group>(call, Some(body))
.await
}
pub async fn delete(&self, group_id: Uuid) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.delete",
verb: Verb::Delete,
path_template: "/api/v1/groups/{group_id}",
path: format!("/api/v1/groups/{group_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_members(
&self,
group_id: Uuid,
page: PageRequest,
) -> Result<Page<models::UserResponse>, AxiamError> {
let query = page.query();
let call = Call {
operation: "groups.list_members",
verb: Verb::Get,
path_template: "/api/v1/groups/{group_id}/members",
path: format!("/api/v1/groups/{group_id}/members"),
query: &query,
};
self.client
.management_send::<_, Page<models::UserResponse>>(call, None::<&()>)
.await
}
pub async fn list_members_all(
&self,
group_id: Uuid,
start: PageRequest,
) -> Result<Vec<models::UserResponse>, AxiamError> {
collect_pages(start, |p| self.list_members(group_id, p)).await
}
pub async fn add_member(
&self,
group_id: Uuid,
body: &models::AddMemberRequest,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.add_member",
verb: Verb::Post,
path_template: "/api/v1/groups/{group_id}/members",
path: format!("/api/v1/groups/{group_id}/members"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn remove_member(&self, group_id: Uuid, user_id: Uuid) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.remove_member",
verb: Verb::Delete,
path_template: "/api/v1/groups/{group_id}/members/{user_id}",
path: format!("/api/v1/groups/{group_id}/members/{user_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_roles(
&self,
group_id: Uuid,
) -> Result<Vec<models::RoleAssignment>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.list_roles",
verb: Verb::Get,
path_template: "/api/v1/groups/{group_id}/roles",
path: format!("/api/v1/groups/{group_id}/roles"),
query: &query,
};
self.client
.management_send::<_, Vec<models::RoleAssignment>>(call, None::<&()>)
.await
}
pub async fn list_service_accounts(
&self,
group_id: Uuid,
page: PageRequest,
) -> Result<Page<models::ServiceAccountResponse>, AxiamError> {
let query = page.query();
let call = Call {
operation: "groups.list_service_accounts",
verb: Verb::Get,
path_template: "/api/v1/groups/{group_id}/service-accounts",
path: format!("/api/v1/groups/{group_id}/service-accounts"),
query: &query,
};
self.client
.management_send::<_, Page<models::ServiceAccountResponse>>(call, None::<&()>)
.await
}
pub async fn list_service_accounts_all(
&self,
group_id: Uuid,
start: PageRequest,
) -> Result<Vec<models::ServiceAccountResponse>, AxiamError> {
collect_pages(start, |p| self.list_service_accounts(group_id, p)).await
}
pub async fn add_service_account(
&self,
group_id: Uuid,
body: &models::AddServiceAccountMemberRequest,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.add_service_account",
verb: Verb::Post,
path_template: "/api/v1/groups/{group_id}/service-accounts",
path: format!("/api/v1/groups/{group_id}/service-accounts"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn remove_service_account(
&self,
group_id: Uuid,
service_account_id: Uuid,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "groups.remove_service_account",
verb: Verb::Delete,
path_template: "/api/v1/groups/{group_id}/service-accounts/{service_account_id}",
path: format!("/api/v1/groups/{group_id}/service-accounts/{service_account_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
}