#![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 Tenants<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for Tenants<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Tenants")
.field("scope", &self.scope)
.finish()
}
}
impl<'c> Tenants<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
#[must_use]
pub fn in_org(mut self, org_id: Uuid) -> Self {
self.scope.org_id = Some(org_id);
self
}
pub async fn list(&self, page: PageRequest) -> Result<Page<models::Tenant>, AxiamError> {
let org_id = self.scope.org(self.client, "tenants.list")?;
let query = page.query();
let call = Call {
operation: "tenants.list",
verb: Verb::Get,
path_template: "/api/v1/organizations/{org_id}/tenants",
path: format!("/api/v1/organizations/{org_id}/tenants"),
query: &query,
};
self.client
.management_send::<_, Page<models::Tenant>>(call, None::<&()>)
.await
}
pub async fn list_all(&self, start: PageRequest) -> Result<Vec<models::Tenant>, AxiamError> {
collect_pages(start, |p| self.list(p)).await
}
pub async fn create(
&self,
body: &models::CreateTenantRequest,
) -> Result<models::Tenant, AxiamError> {
let org_id = self.scope.org(self.client, "tenants.create")?;
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "tenants.create",
verb: Verb::Post,
path_template: "/api/v1/organizations/{org_id}/tenants",
path: format!("/api/v1/organizations/{org_id}/tenants"),
query: &query,
};
self.client
.management_send::<_, models::Tenant>(call, Some(body))
.await
}
pub async fn get(&self, tenant_id: Uuid) -> Result<models::Tenant, AxiamError> {
let org_id = self.scope.org(self.client, "tenants.get")?;
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "tenants.get",
verb: Verb::Get,
path_template: "/api/v1/organizations/{org_id}/tenants/{tenant_id}",
path: format!("/api/v1/organizations/{org_id}/tenants/{tenant_id}"),
query: &query,
};
self.client
.management_send::<_, models::Tenant>(call, None::<&()>)
.await
}
pub async fn update(
&self,
tenant_id: Uuid,
body: &models::UpdateTenant,
) -> Result<models::Tenant, AxiamError> {
let org_id = self.scope.org(self.client, "tenants.update")?;
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "tenants.update",
verb: Verb::Put,
path_template: "/api/v1/organizations/{org_id}/tenants/{tenant_id}",
path: format!("/api/v1/organizations/{org_id}/tenants/{tenant_id}"),
query: &query,
};
self.client
.management_send::<_, models::Tenant>(call, Some(body))
.await
}
pub async fn delete(&self, tenant_id: Uuid) -> Result<(), AxiamError> {
let org_id = self.scope.org(self.client, "tenants.delete")?;
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "tenants.delete",
verb: Verb::Delete,
path_template: "/api/v1/organizations/{org_id}/tenants/{tenant_id}",
path: format!("/api/v1/organizations/{org_id}/tenants/{tenant_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn export_audit(&self, tenant_id: Uuid) -> Result<(), AxiamError> {
let org_id = self.scope.org(self.client, "tenants.export_audit")?;
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "tenants.export_audit",
verb: Verb::Post,
path_template: "/api/v1/organizations/{org_id}/tenants/{tenant_id}/audit-export",
path: format!("/api/v1/organizations/{org_id}/tenants/{tenant_id}/audit-export"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
}