#![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 Roles<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for Roles<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Roles").field("scope", &self.scope).finish()
}
}
impl<'c> Roles<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
pub async fn list(&self, page: PageRequest) -> Result<Page<models::Role>, AxiamError> {
let query = page.query();
let call = Call {
operation: "roles.list",
verb: Verb::Get,
path_template: "/api/v1/roles",
path: "/api/v1/roles".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::Role>>(call, None::<&()>)
.await
}
pub async fn list_all(&self, start: PageRequest) -> Result<Vec<models::Role>, AxiamError> {
collect_pages(start, |p| self.list(p)).await
}
pub async fn create(
&self,
body: &models::CreateRoleRequest,
) -> Result<models::Role, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.create",
verb: Verb::Post,
path_template: "/api/v1/roles",
path: "/api/v1/roles".to_string(),
query: &query,
};
self.client
.management_send::<_, models::Role>(call, Some(body))
.await
}
pub async fn get(&self, role_id: Uuid) -> Result<models::Role, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.get",
verb: Verb::Get,
path_template: "/api/v1/roles/{role_id}",
path: format!("/api/v1/roles/{role_id}"),
query: &query,
};
self.client
.management_send::<_, models::Role>(call, None::<&()>)
.await
}
pub async fn update(
&self,
role_id: Uuid,
body: &models::UpdateRole,
) -> Result<models::Role, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.update",
verb: Verb::Put,
path_template: "/api/v1/roles/{role_id}",
path: format!("/api/v1/roles/{role_id}"),
query: &query,
};
self.client
.management_send::<_, models::Role>(call, Some(body))
.await
}
pub async fn delete(&self, role_id: Uuid) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.delete",
verb: Verb::Delete,
path_template: "/api/v1/roles/{role_id}",
path: format!("/api/v1/roles/{role_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_users(
&self,
role_id: Uuid,
) -> Result<Vec<models::RoleUserAssignment>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.list_users",
verb: Verb::Get,
path_template: "/api/v1/roles/{role_id}/users",
path: format!("/api/v1/roles/{role_id}/users"),
query: &query,
};
self.client
.management_send::<_, Vec<models::RoleUserAssignment>>(call, None::<&()>)
.await
}
pub async fn assign_to_user(
&self,
role_id: Uuid,
body: &models::AssignRoleToUserRequest,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.assign_to_user",
verb: Verb::Post,
path_template: "/api/v1/roles/{role_id}/users",
path: format!("/api/v1/roles/{role_id}/users"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn unassign_from_user(
&self,
role_id: Uuid,
user_id: Uuid,
resource_id: Option<&str>,
) -> Result<(), AxiamError> {
let mut query: Vec<(&'static str, String)> = Vec::new();
if let Some(v) = resource_id {
query.push(("resource_id", v.to_string()));
}
let call = Call {
operation: "roles.unassign_from_user",
verb: Verb::Delete,
path_template: "/api/v1/roles/{role_id}/users/{user_id}",
path: format!("/api/v1/roles/{role_id}/users/{user_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_groups(
&self,
role_id: Uuid,
) -> Result<Vec<models::RoleGroupAssignment>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.list_groups",
verb: Verb::Get,
path_template: "/api/v1/roles/{role_id}/groups",
path: format!("/api/v1/roles/{role_id}/groups"),
query: &query,
};
self.client
.management_send::<_, Vec<models::RoleGroupAssignment>>(call, None::<&()>)
.await
}
pub async fn assign_to_group(
&self,
role_id: Uuid,
body: &models::AssignRoleToGroupRequest,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.assign_to_group",
verb: Verb::Post,
path_template: "/api/v1/roles/{role_id}/groups",
path: format!("/api/v1/roles/{role_id}/groups"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn unassign_from_group(
&self,
role_id: Uuid,
group_id: Uuid,
resource_id: Option<&str>,
) -> Result<(), AxiamError> {
let mut query: Vec<(&'static str, String)> = Vec::new();
if let Some(v) = resource_id {
query.push(("resource_id", v.to_string()));
}
let call = Call {
operation: "roles.unassign_from_group",
verb: Verb::Delete,
path_template: "/api/v1/roles/{role_id}/groups/{group_id}",
path: format!("/api/v1/roles/{role_id}/groups/{group_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_permissions(
&self,
role_id: Uuid,
) -> Result<Vec<models::ResolvedPermissionGrant>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.list_permissions",
verb: Verb::Get,
path_template: "/api/v1/roles/{role_id}/permissions",
path: format!("/api/v1/roles/{role_id}/permissions"),
query: &query,
};
self.client
.management_send::<_, Vec<models::ResolvedPermissionGrant>>(call, None::<&()>)
.await
}
pub async fn grant_permission(
&self,
role_id: Uuid,
body: &models::GrantPermissionRequest,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.grant_permission",
verb: Verb::Post,
path_template: "/api/v1/roles/{role_id}/permissions",
path: format!("/api/v1/roles/{role_id}/permissions"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn revoke_permission(
&self,
role_id: Uuid,
permission_id: Uuid,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.revoke_permission",
verb: Verb::Delete,
path_template: "/api/v1/roles/{role_id}/permissions/{permission_id}",
path: format!("/api/v1/roles/{role_id}/permissions/{permission_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_service_accounts(
&self,
role_id: Uuid,
) -> Result<Vec<models::RoleServiceAccountAssignment>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.list_service_accounts",
verb: Verb::Get,
path_template: "/api/v1/roles/{role_id}/service-accounts",
path: format!("/api/v1/roles/{role_id}/service-accounts"),
query: &query,
};
self.client
.management_send::<_, Vec<models::RoleServiceAccountAssignment>>(call, None::<&()>)
.await
}
pub async fn assign_to_service_account(
&self,
role_id: Uuid,
body: &models::AssignRoleToServiceAccountRequest,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "roles.assign_to_service_account",
verb: Verb::Post,
path_template: "/api/v1/roles/{role_id}/service-accounts",
path: format!("/api/v1/roles/{role_id}/service-accounts"),
query: &query,
};
self.client
.management_send_no_content(call, Some(body))
.await
}
pub async fn unassign_from_service_account(
&self,
role_id: Uuid,
service_account_id: Uuid,
resource_id: Option<&str>,
) -> Result<(), AxiamError> {
let mut query: Vec<(&'static str, String)> = Vec::new();
if let Some(v) = resource_id {
query.push(("resource_id", v.to_string()));
}
let call = Call {
operation: "roles.unassign_from_service_account",
verb: Verb::Delete,
path_template: "/api/v1/roles/{role_id}/service-accounts/{service_account_id}",
path: format!("/api/v1/roles/{role_id}/service-accounts/{service_account_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
}