#![allow(clippy::too_many_arguments)]
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 Audit<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for Audit<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Audit").field("scope", &self.scope).finish()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AuditListFilter {
pub actor_id: Option<String>,
pub action: Option<String>,
pub outcome: Option<String>,
pub resource_id: Option<String>,
pub from: Option<String>,
pub to: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AuditListSystemFilter {
pub actor_id: Option<String>,
pub action: Option<String>,
pub outcome: Option<String>,
pub resource_id: Option<String>,
pub from: Option<String>,
pub to: Option<String>,
}
impl<'c> Audit<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
pub async fn list(
&self,
filter: &AuditListFilter,
page: PageRequest,
) -> Result<Page<models::AuditLogEntry>, AxiamError> {
let mut query = page.query();
if let Some(v) = filter.actor_id.as_deref() {
query.push(("actor_id", v.to_string()));
}
if let Some(v) = filter.action.as_deref() {
query.push(("action", v.to_string()));
}
if let Some(v) = filter.outcome.as_deref() {
query.push(("outcome", v.to_string()));
}
if let Some(v) = filter.resource_id.as_deref() {
query.push(("resource_id", v.to_string()));
}
if let Some(v) = filter.from.as_deref() {
query.push(("from", v.to_string()));
}
if let Some(v) = filter.to.as_deref() {
query.push(("to", v.to_string()));
}
let call = Call {
operation: "audit.list",
verb: Verb::Get,
path_template: "/api/v1/audit-logs",
path: "/api/v1/audit-logs".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::AuditLogEntry>>(call, None::<&()>)
.await
}
pub async fn list_all(
&self,
filter: &AuditListFilter,
start: PageRequest,
) -> Result<Vec<models::AuditLogEntry>, AxiamError> {
collect_pages(start, |p| self.list(filter, p)).await
}
pub async fn list_system(
&self,
filter: &AuditListSystemFilter,
page: PageRequest,
) -> Result<Page<models::AuditLogEntry>, AxiamError> {
let mut query = page.query();
if let Some(v) = filter.actor_id.as_deref() {
query.push(("actor_id", v.to_string()));
}
if let Some(v) = filter.action.as_deref() {
query.push(("action", v.to_string()));
}
if let Some(v) = filter.outcome.as_deref() {
query.push(("outcome", v.to_string()));
}
if let Some(v) = filter.resource_id.as_deref() {
query.push(("resource_id", v.to_string()));
}
if let Some(v) = filter.from.as_deref() {
query.push(("from", v.to_string()));
}
if let Some(v) = filter.to.as_deref() {
query.push(("to", v.to_string()));
}
let call = Call {
operation: "audit.list_system",
verb: Verb::Get,
path_template: "/api/v1/audit-logs/system",
path: "/api/v1/audit-logs/system".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::AuditLogEntry>>(call, None::<&()>)
.await
}
pub async fn list_system_all(
&self,
filter: &AuditListSystemFilter,
start: PageRequest,
) -> Result<Vec<models::AuditLogEntry>, AxiamError> {
collect_pages(start, |p| self.list_system(filter, p)).await
}
}