Skip to main content

docbox_management_interface/
lib.rs

1pub mod error;
2pub mod remote;
3pub mod types;
4
5pub use async_trait::async_trait;
6pub use error::{DocboxServiceError, ManagementError};
7pub use remote::{RemoteDocboxManagementInterface, RemoteDocboxManagementTransport};
8use serde::{Deserialize, Serialize};
9pub use types::*;
10
11#[derive(Debug, Serialize, Deserialize)]
12#[serde(tag = "command", content = "payload")]
13pub enum DocboxManagementCommand {
14    CreateRoot,
15    CheckRoot,
16    CreateTenant(CreateTenantInput),
17    GetTenant(GetTenantInput),
18    DeleteTenant(DeleteTenantInput),
19    GetTenants(GetTenantsInput),
20    SetTenantAllowedCorsOrigins(SetTenantAllowedCorsOriginsInput),
21    MigrateRoot,
22    MigrateTenant(MigrateTenantInput),
23    MigrateIAM(MigrateTenantIAMInput),
24    GetPendingRootMigrations,
25    GetTenantPendingMigrations(GetTenantPendingMigrationsInput),
26    FlushTenantCache,
27}
28
29fn serialize_value<T: Serialize>(value: T) -> Result<serde_json::Value, ManagementError> {
30    serde_json::to_value(value).map_err(ManagementError::SerializeResponse)
31}
32
33impl DocboxManagementCommand {
34    pub async fn execute(
35        self,
36        interface: &dyn DocboxManagementInterface,
37    ) -> Result<serde_json::Value, ManagementError> {
38        match self {
39            DocboxManagementCommand::CreateRoot => serialize_value(interface.create_root().await?),
40            DocboxManagementCommand::CheckRoot => serialize_value(interface.check_root().await?),
41            DocboxManagementCommand::CreateTenant(input) => {
42                serialize_value(interface.create_tenant(input).await?)
43            }
44            DocboxManagementCommand::GetTenant(input) => {
45                serialize_value(interface.get_tenant(input).await?)
46            }
47            DocboxManagementCommand::DeleteTenant(input) => {
48                serialize_value(interface.delete_tenant(input).await?)
49            }
50            DocboxManagementCommand::GetTenants(input) => {
51                serialize_value(interface.get_tenants(input).await?)
52            }
53            DocboxManagementCommand::SetTenantAllowedCorsOrigins(input) => {
54                serialize_value(interface.set_tenant_allowed_cors_origins(input).await?)
55            }
56            DocboxManagementCommand::MigrateRoot => {
57                serialize_value(interface.migrate_root().await?)
58            }
59            DocboxManagementCommand::MigrateTenant(input) => {
60                serialize_value(interface.migrate_tenant(input).await?)
61            }
62            DocboxManagementCommand::MigrateIAM(input) => {
63                serialize_value(interface.migrate_tenant_iam(input).await?)
64            }
65            DocboxManagementCommand::GetPendingRootMigrations => {
66                serialize_value(interface.get_pending_root_migrations().await?)
67            }
68            DocboxManagementCommand::GetTenantPendingMigrations(input) => {
69                serialize_value(interface.get_tenant_pending_migrations(input).await?)
70            }
71            DocboxManagementCommand::FlushTenantCache => {
72                serialize_value(interface.flush_tenant_cache().await?)
73            }
74        }
75    }
76}
77
78/// Management interface providing the management functionality with an abstracted backend
79/// to allow the various points of management (CLI, Management Lambda, ..etc)
80#[async_trait::async_trait]
81pub trait DocboxManagementInterface {
82    /// Checks if the docbox root database has been initialized
83    async fn check_root(&self) -> Result<CheckRootOutput, ManagementError>;
84
85    /// Create the root docbox database
86    async fn create_root(&self) -> Result<(), ManagementError>;
87
88    /// Create a new tenant
89    async fn create_tenant(
90        &self,
91        input: CreateTenantInput,
92    ) -> Result<CreateTenantOutput, ManagementError>;
93
94    /// Get a specific tenant
95    async fn get_tenant(&self, input: GetTenantInput) -> Result<GetTenantOutput, ManagementError>;
96
97    /// Delete a specific tenant
98    async fn delete_tenant(
99        &self,
100        input: DeleteTenantInput,
101    ) -> Result<DeleteTenantOutput, ManagementError>;
102
103    /// Get a collection of tenants
104    async fn get_tenants(
105        &self,
106        input: GetTenantsInput,
107    ) -> Result<GetTenantsOutput, ManagementError>;
108
109    /// Set the allowed CORS origins for a tenants storage bucket
110    async fn set_tenant_allowed_cors_origins(
111        &self,
112        input: SetTenantAllowedCorsOriginsInput,
113    ) -> Result<(), ManagementError>;
114
115    /// Apply root database migrations
116    async fn migrate_root(&self) -> Result<(), ManagementError>;
117
118    /// Apply migrations for tenant(s)
119    async fn migrate_tenant(
120        &self,
121        input: MigrateTenantInput,
122    ) -> Result<MigrateTenantOutput, ManagementError>;
123
124    /// Migrate a tenant from secrets based authentication to IAM based
125    /// database authentication
126    async fn migrate_tenant_iam(
127        &self,
128        input: MigrateTenantIAMInput,
129    ) -> Result<MigrateTenantIAMOutput, ManagementError>;
130
131    /// Get migrations that are waiting to be applied to the root
132    async fn get_pending_root_migrations(&self) -> Result<Vec<String>, ManagementError>;
133
134    /// Get pending database migrations for a specific tenant
135    async fn get_tenant_pending_migrations(
136        &self,
137        input: GetTenantPendingMigrationsInput,
138    ) -> Result<GetTenantPendingMigrationsOutput, ManagementError>;
139
140    /// Flush the tenant database cache for persisted servers
141    async fn flush_tenant_cache(&self) -> Result<(), ManagementError>;
142}