#![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 ServiceAccounts<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for ServiceAccounts<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ServiceAccounts")
.field("scope", &self.scope)
.finish()
}
}
impl<'c> ServiceAccounts<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
pub async fn list(
&self,
page: PageRequest,
) -> Result<Page<models::ServiceAccountResponse>, AxiamError> {
let query = page.query();
let call = Call {
operation: "service_accounts.list",
verb: Verb::Get,
path_template: "/api/v1/service-accounts",
path: "/api/v1/service-accounts".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::ServiceAccountResponse>>(call, None::<&()>)
.await
}
pub async fn list_all(
&self,
start: PageRequest,
) -> Result<Vec<models::ServiceAccountResponse>, AxiamError> {
collect_pages(start, |p| self.list(p)).await
}
pub async fn create(
&self,
body: &models::CreateServiceAccountRequest,
) -> Result<models::ServiceAccountCreatedResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.create",
verb: Verb::Post,
path_template: "/api/v1/service-accounts",
path: "/api/v1/service-accounts".to_string(),
query: &query,
};
let wire: models::ServiceAccountCreatedResponseWire =
self.client.management_send(call, Some(body)).await?;
Ok(models::ServiceAccountCreatedResponse::from(wire))
}
pub async fn get(&self, sa_id: Uuid) -> Result<models::ServiceAccountResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.get",
verb: Verb::Get,
path_template: "/api/v1/service-accounts/{sa_id}",
path: format!("/api/v1/service-accounts/{sa_id}"),
query: &query,
};
self.client
.management_send::<_, models::ServiceAccountResponse>(call, None::<&()>)
.await
}
pub async fn update(
&self,
sa_id: Uuid,
body: &models::UpdateServiceAccount,
) -> Result<models::ServiceAccountResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.update",
verb: Verb::Put,
path_template: "/api/v1/service-accounts/{sa_id}",
path: format!("/api/v1/service-accounts/{sa_id}"),
query: &query,
};
self.client
.management_send::<_, models::ServiceAccountResponse>(call, Some(body))
.await
}
pub async fn delete(&self, sa_id: Uuid) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.delete",
verb: Verb::Delete,
path_template: "/api/v1/service-accounts/{sa_id}",
path: format!("/api/v1/service-accounts/{sa_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn rotate_secret(
&self,
sa_id: Uuid,
) -> Result<models::RotateSecretResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.rotate_secret",
verb: Verb::Post,
path_template: "/api/v1/service-accounts/{sa_id}/rotate-secret",
path: format!("/api/v1/service-accounts/{sa_id}/rotate-secret"),
query: &query,
};
let wire: models::RotateSecretResponseWire =
self.client.management_send(call, None::<&()>).await?;
Ok(models::RotateSecretResponse::from(wire))
}
pub async fn bind_certificate(
&self,
sa_id: Uuid,
body: &models::BindCertificate,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.bind_certificate",
verb: Verb::Post,
path_template: "/api/v1/service-accounts/{sa_id}/bind-certificate",
path: format!("/api/v1/service-accounts/{sa_id}/bind-certificate"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn list_roles(
&self,
service_account_id: Uuid,
) -> Result<Vec<models::RoleAssignment>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.list_roles",
verb: Verb::Get,
path_template: "/api/v1/service-accounts/{service_account_id}/roles",
path: format!("/api/v1/service-accounts/{service_account_id}/roles"),
query: &query,
};
self.client
.management_send::<_, Vec<models::RoleAssignment>>(call, None::<&()>)
.await
}
pub async fn list_groups(
&self,
service_account_id: Uuid,
) -> Result<Vec<models::Group>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "service_accounts.list_groups",
verb: Verb::Get,
path_template: "/api/v1/service-accounts/{service_account_id}/groups",
path: format!("/api/v1/service-accounts/{service_account_id}/groups"),
query: &query,
};
self.client
.management_send::<_, Vec<models::Group>>(call, None::<&()>)
.await
}
}