Skip to content

Commit 2c44673

Browse files
authored
Merge pull request #9 from Embucket/CP-5
Add schemas
2 parents d40f4c9 + 74deda1 commit 2c44673

File tree

16 files changed

+336
-312
lines changed

16 files changed

+336
-312
lines changed

crates/.DS_Store

6 KB
Binary file not shown.

crates/nexus/.DS_Store

6 KB
Binary file not shown.

crates/nexus/src/.DS_Store

8 KB
Binary file not shown.

crates/nexus/src/http/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ pub mod control {
77
pub mod ui {
88
pub mod handlers;
99
pub mod models;
10-
pub mod models;
1110
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::http::ui::models::database::Database;
2+
use crate::http::ui::models::errors::AppError;
3+
use crate::http::ui::models::storage_profile::StorageProfile;
4+
use crate::http::ui::models::warehouse::Warehouse;
5+
use crate::state::AppState;
6+
use catalog::models::{DatabaseIdent, Table};
7+
use control_plane::models::Warehouse as WarehouseModel;
8+
use uuid::Uuid;
9+
10+
impl AppState {
11+
pub async fn get_warehouse_model(
12+
&self,
13+
warehouse_id: Uuid,
14+
) -> Result<WarehouseModel, AppError> {
15+
self.control_svc
16+
.get_warehouse(warehouse_id)
17+
.await
18+
.map_err(|e| {
19+
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
20+
AppError::new(e, fmt.as_str())
21+
})
22+
}
23+
pub async fn get_warehouse_by_id(&self, warehouse_id: Uuid) -> Result<Warehouse, AppError> {
24+
self.get_warehouse_model(warehouse_id).await.map(|warehouse| warehouse.into())
25+
}
26+
27+
pub async fn get_profile_by_id(
28+
&self,
29+
storage_profile_id: Uuid,
30+
) -> Result<StorageProfile, AppError> {
31+
self.control_svc
32+
.get_profile(storage_profile_id)
33+
.await
34+
.map_err(|e| {
35+
let fmt = format!("{}: failed to get profile by id {}", e, storage_profile_id);
36+
AppError::new(e, fmt.as_str())
37+
}).map(|profile| profile.into())
38+
}
39+
40+
pub async fn get_database_by_ident(&self, ident: &DatabaseIdent) -> Result<Database, AppError> {
41+
self.catalog_svc
42+
.get_namespace(ident)
43+
.await
44+
.map_err(|e| {
45+
let fmt = format!("{}: failed to get database with db ident {}", e, &ident);
46+
AppError::new(e, fmt.as_str())
47+
}).map(|database| database.into())
48+
}
49+
50+
pub async fn list_tables(&self, ident: &DatabaseIdent) -> Result<Vec<Table>, AppError> {
51+
self.catalog_svc.list_tables(ident).await.map_err(|e| {
52+
let fmt = format!(
53+
"{}: failed to get database tables with db ident {}",
54+
e, &ident
55+
);
56+
AppError::new(e, fmt.as_str())
57+
})
58+
}
59+
}

crates/nexus/src/http/ui/handlers/databases.rs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,7 @@ pub async fn create_database(
5555
Path(warehouse_id): Path<Uuid>,
5656
Json(payload): Json<database::CreateDatabasePayload>,
5757
) -> Result<Json<database::Database>, AppError> {
58-
let warehouse = state
59-
.control_svc
60-
.get_warehouse(warehouse_id)
61-
.await
62-
.map_err(|e| {
63-
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
64-
AppError::new(e, fmt.as_str())
65-
})?;
58+
let warehouse = state.get_warehouse_by_id(warehouse_id).await?;
6659
let ident = DatabaseIdent {
6760
warehouse: WarehouseIdent::new(warehouse.id),
6861
namespace: NamespaceIdent::new(payload.name),
@@ -130,48 +123,18 @@ pub async fn get_database(
130123
State(state): State<AppState>,
131124
Path((warehouse_id, database_name)): Path<(Uuid, String)>,
132125
) -> Result<Json<database::DatabaseDashboard>, AppError> {
133-
let warehouse = state
134-
.control_svc
135-
.get_warehouse(warehouse_id)
136-
.await
137-
.map_err(|e| {
138-
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
139-
AppError::new(e, fmt.as_str())
140-
})?;
141-
let profile = state
142-
.control_svc
143-
.get_profile(warehouse.storage_profile_id)
144-
.await
145-
.map_err(|e| {
146-
let fmt = format!(
147-
"{}: failed to get profile by id {}",
148-
e, warehouse.storage_profile_id
149-
);
150-
AppError::new(e, fmt.as_str())
151-
})?;
126+
let warehouse = state.get_warehouse_by_id(warehouse_id).await?;
127+
let profile = state.get_profile_by_id(warehouse.storage_profile_id).await?;
152128
let ident = DatabaseIdent {
153129
warehouse: WarehouseIdent::new(warehouse.id),
154130
namespace: NamespaceIdent::new(database_name),
155131
};
156-
let database = state.catalog_svc.get_namespace(&ident).await.map_err(|e| {
157-
let fmt = format!(
158-
"{}: failed to get database with db ident {}",
159-
e, &ident
160-
);
161-
AppError::new(e, fmt.as_str())
162-
})?;
163-
let tables = state.catalog_svc.list_tables(&ident).await
164-
.map_err(|e| {
165-
let fmt = format!(
166-
"{}: failed to get database tables with db ident {}",
167-
e, &ident
168-
);
169-
AppError::new(e, fmt.as_str())
170-
})?;
132+
let database = state.get_database_by_ident(&ident).await?;
133+
let tables = state.catalog_svc.list_tables(&ident).await?;
171134
Ok(Json(DatabaseDashboard {
172-
name: database.ident.to_string(),
135+
name: ident.namespace.first().unwrap().to_string(),
173136
properties: Option::from(database.properties),
174-
id: get_database_id(database.ident),
137+
id: get_database_id(ident),
175138
warehouse_id,
176139
warehouse: WarehouseEntity::new(warehouse.into(), profile.into()),
177140
tables: tables

crates/nexus/src/http/ui/handlers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ pub mod databases;
22
pub mod profiles;
33
pub mod tables;
44
pub mod warehouses;
5+
6+
pub mod common;

crates/nexus/src/http/ui/handlers/profiles.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,7 @@ pub async fn get_storage_profile(
7777
State(state): State<AppState>,
7878
Path(storage_profile_id): Path<Uuid>,
7979
) -> Result<Json<storage_profile::StorageProfile>, AppError> {
80-
let profile: StorageProfile = state
81-
.control_svc
82-
.get_profile(storage_profile_id)
83-
.await
84-
.map_err(|e| {
85-
let fmt = format!(
86-
"{}: failed to get storage profile with id {}",
87-
e, storage_profile_id
88-
);
89-
AppError::new(e, fmt.as_str())
90-
})?;
80+
let profile = state.get_profile_by_id(storage_profile_id).await?;
9181
Ok(Json(profile.into()))
9282
}
9383

crates/nexus/src/http/ui/handlers/tables.rs

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::http::ui::models::errors::AppError;
22
use crate::http::ui::models::table;
33
use crate::http::ui::models::table::TableQueryRequest;
4-
use crate::http::ui::models::table::{Table, TableCreateRequest, TableExtended};
4+
use crate::http::ui::models::table::{
5+
Table, TableCreateRequest, TableExtended, TableQueryResponse,
6+
};
57
use crate::state::AppState;
68
use axum::{extract::Path, extract::State, Json};
79
use catalog::models::{DatabaseIdent, TableIdent, WarehouseIdent};
@@ -19,7 +21,11 @@ use uuid::Uuid;
1921
),
2022
components(
2123
schemas(
22-
table::TableQueryResponse,
24+
TableQueryResponse,
25+
TableQueryRequest,
26+
TableExtended,
27+
TableCreateRequest,
28+
Table,
2329
AppError,
2430
)
2531
),
@@ -49,34 +55,15 @@ pub async fn get_table(
4955
State(state): State<AppState>,
5056
Path((warehouse_id, database_name, table_name)): Path<(Uuid, String, String)>,
5157
) -> Result<Json<TableExtended>, AppError> {
52-
let warehouse = state
53-
.control_svc
54-
.get_warehouse(warehouse_id)
55-
.await
56-
.map_err(|e| {
57-
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
58-
AppError::new(e, fmt.as_str())
59-
})?;
58+
let warehouse = state.get_warehouse_by_id(warehouse_id).await?;
6059
let profile = state
61-
.control_svc
62-
.get_profile(warehouse.storage_profile_id)
63-
.await
64-
.map_err(|e| {
65-
let fmt = format!(
66-
"{}: failed to get profile by id {}",
67-
e, warehouse.storage_profile_id
68-
);
69-
AppError::new(e, fmt.as_str())
70-
})?;
60+
.get_profile_by_id(warehouse.storage_profile_id)
61+
.await?;
7162
let ident = DatabaseIdent {
7263
warehouse: WarehouseIdent::new(warehouse.id),
7364
namespace: NamespaceIdent::new(database_name),
7465
};
75-
let database = state.catalog_svc.get_namespace(&ident).await.map_err(|e| {
76-
let fmt = format!("{}: failed to get database with db ident {}", e, &ident);
77-
AppError::new(e, fmt.as_str())
78-
})?;
79-
66+
let database = state.get_database_by_ident(&ident).await?;
8067
let table_ident = TableIdent {
8168
database: ident,
8269
table: table_name,
@@ -116,14 +103,7 @@ pub async fn create_table(
116103
Path((warehouse_id, database_name)): Path<(Uuid, String)>,
117104
Json(payload): Json<TableCreateRequest>,
118105
) -> Result<Json<Table>, AppError> {
119-
let warehouse = state
120-
.control_svc
121-
.get_warehouse(warehouse_id)
122-
.await
123-
.map_err(|e| {
124-
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
125-
AppError::new(e, fmt.as_str())
126-
})?;
106+
let warehouse = state.get_warehouse_model(warehouse_id).await?;
127107
let db_ident = DatabaseIdent {
128108
warehouse: WarehouseIdent::new(warehouse.id),
129109
namespace: NamespaceIdent::new(database_name),
@@ -158,14 +138,7 @@ pub async fn delete_table(
158138
State(state): State<AppState>,
159139
Path((warehouse_id, database_name, table_name)): Path<(Uuid, String, String)>,
160140
) -> Result<(), AppError> {
161-
let warehouse = state
162-
.control_svc
163-
.get_warehouse(warehouse_id)
164-
.await
165-
.map_err(|e| {
166-
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
167-
AppError::new(e, fmt.as_str())
168-
})?;
141+
let warehouse = state.get_warehouse_by_id(warehouse_id).await?;
169142
let table_ident = TableIdent {
170143
database: DatabaseIdent {
171144
warehouse: WarehouseIdent::new(warehouse.id),
@@ -187,15 +160,15 @@ pub async fn delete_table(
187160
#[utoipa::path(
188161
post,
189162
path = "/ui/warehouses/{warehouseId}/databases/{databaseName}/tables/{tableName}/query",
190-
request_body = table::TableQueryRequest,
163+
request_body = TableQueryRequest,
191164
operation_id = "webTableQuery",
192165
params(
193166
("warehouseId" = Uuid, Path, description = "Warehouse ID"),
194167
("databaseName" = Uuid, Path, description = "Database Name"),
195168
("tableName" = Uuid, Path, description = "Table name")
196169
),
197170
responses(
198-
(status = 200, description = "Returns result of the query", body = Vec<table::TableQueryResponse>),
171+
(status = 200, description = "Returns result of the query", body = TableQueryResponse),
199172
(status = 500, description = "Internal server error")
200173
)
201174
)]

crates/nexus/src/http/ui/handlers/warehouses.rs

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,7 @@ pub async fn navigation(
6767
let mut databases_short = Vec::new();
6868

6969
for database in databases {
70-
let tables = state
71-
.catalog_svc
72-
.list_tables(&database.ident)
73-
.await
74-
.map_err(|e| {
75-
let fmt = format!(
76-
"{}: failed to get database tables with db ident {}",
77-
e, &database.ident
78-
);
79-
AppError::new(e, fmt.as_str())
80-
})?;
70+
let tables = state.catalog_svc.list_tables(&database.ident).await?;
8171
let ident = database.ident.clone();
8272
databases_short.push(DatabaseShort {
8373
id: get_database_id(database.ident),
@@ -110,7 +100,7 @@ pub async fn navigation(
110100
operation_id = "webWarehousesDashboard",
111101
responses(
112102
(status = 200, description = "List all warehouses", body = warehouse::WarehousesDashboard),
113-
(status = 500, description = "List all warehouses", body = AppError),
103+
(status = 500, description = "List all warehouses error", body = AppError),
114104
115105
)
116106
)]
@@ -161,25 +151,10 @@ pub async fn get_warehouse(
161151
State(state): State<AppState>,
162152
Path(warehouse_id): Path<Uuid>,
163153
) -> Result<Json<warehouse::WarehouseDashboard>, AppError> {
164-
let warehouse = state
165-
.control_svc
166-
.get_warehouse(warehouse_id)
167-
.await
168-
.map_err(|e| {
169-
let fmt = format!("{}: failed to get warehouse by id {}", e, warehouse_id);
170-
AppError::new(e, fmt.as_str())
171-
})?;
154+
let warehouse = state.get_warehouse_by_id(warehouse_id).await?;
172155
let profile = state
173-
.control_svc
174-
.get_profile(warehouse.storage_profile_id)
175-
.await
176-
.map_err(|e| {
177-
let fmt = format!(
178-
"{}: failed to get profile by id {}",
179-
e, warehouse.storage_profile_id
180-
);
181-
AppError::new(e, fmt.as_str())
182-
})?;
156+
.get_profile_by_id(warehouse.storage_profile_id)
157+
.await?;
183158
let databases = state
184159
.catalog_svc
185160
.list_namespaces(&WarehouseIdent::new(warehouse.id), None)
@@ -215,26 +190,16 @@ pub async fn get_warehouse(
215190
operation_id = "webCreateWarehouse",
216191
responses(
217192
(status = 201, description = "Warehouse created", body = warehouse::Warehouse),
218-
(status = 422, description = "Unprocessable Entity"),
219-
(status = 500, description = "Internal server error")
193+
(status = 422, description = "Unprocessable Entity", body = AppError),
194+
(status = 500, description = "Internal server error", body = AppError)
220195
)
221196
)]
222197
pub async fn create_warehouse(
223198
State(state): State<AppState>,
224199
Json(payload): Json<warehouse::CreateWarehousePayload>,
225200
) -> Result<Json<warehouse::Warehouse>, AppError> {
226201
let request: WarehouseCreateRequest = payload.into();
227-
state
228-
.control_svc
229-
.get_profile(request.storage_profile_id)
230-
.await
231-
.map_err(|e| {
232-
let fmt = format!(
233-
"{}: failed to get profile with id {}",
234-
e, request.storage_profile_id
235-
);
236-
AppError::new(e, fmt.as_str())
237-
})?;
202+
state.get_profile_by_id(request.storage_profile_id).await?;
238203
let warehouse: WarehouseModel =
239204
state
240205
.control_svc
@@ -253,13 +218,13 @@ pub async fn create_warehouse(
253218
#[utoipa::path(
254219
delete,
255220
path = "/ui/warehouses/{warehouseId}",
256-
operation_id = "webCreteWarehouse",
221+
operation_id = "webCreateWarehouse",
257222
params(
258223
("warehouseId" = Uuid, Path, description = "Warehouse ID")
259224
),
260225
responses(
261226
(status = 204, description = "Warehouse deleted"),
262-
(status = 404, description = "Warehouse not found")
227+
(status = 404, description = "Warehouse not found", body = AppError)
263228
)
264229
)]
265230
pub async fn delete_warehouse(

0 commit comments

Comments
 (0)