Skip to content

Commit 9b4d41a

Browse files
committed
feat(#529): add first basic cluster-endpoint
Added new create-endpoint for cluster with connection to the parser for the cluster-templates. Also added database-tables for cluster. Signed-off-by: Tobias Anker <tobias.anker@kitsunemimi.moe>
1 parent f00df77 commit 9b4d41a

12 files changed

Lines changed: 415 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hanami/Cargo.toml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ edition = "2024"
66
[dependencies]
77
# web-server
88
actix-web = "4"
9-
futures = "0.3"
10-
bytes = "1"
119
# openapi-generator
1210
schemars = { package = "apistos-schemars", version = "0.8", features = ["chrono", "uuid1", "url", "rust_decimal"] }
1311
apistos = "0.5"
@@ -23,16 +21,20 @@ once_cell = "1.18"
2321
# logger
2422
log = "0.4"
2523
env_logger = "0.10"
26-
# sqlite-database
24+
# database
2725
diesel = { version = "2.2", features = ["sqlite"] }
28-
lazy_static = "1.4"
29-
# timestamps
30-
chrono = "0.4.40"
31-
# randomizer
32-
rand = "0.9.0"
3326
# hash
3427
sha2 = "0.10.8"
3528
hex = "0.4"
3629
# parser-generator
3730
pest = "2"
3831
pest_derive = "2"
32+
# common stuff
33+
rand = "0.9.0"
34+
lazy_static = "1.4"
35+
chrono = "0.4.40"
36+
futures = "0.3"
37+
bytes = "1"
38+
uuid = { version = "1.16.0", features = ["v4"] }
39+
40+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2022 Tobias Anker <tobias.anker@kitsunemimim.moe>
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use apistos::ApiComponent;
16+
use serde::{Deserialize, Serialize};
17+
use schemars::JsonSchema;
18+
19+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, ApiComponent)]
20+
pub struct ClusterCreateReq {
21+
pub name: String,
22+
pub template: String,
23+
}
24+
25+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, ApiComponent)]
26+
pub struct ClusterResp {
27+
pub uuid: String,
28+
pub name: String,
29+
pub template: String,
30+
pub created_at: String,
31+
pub created_by: String,
32+
pub updated_at: String,
33+
pub updated_by: String,
34+
}
35+
36+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, ApiComponent)]
37+
pub struct ClusterBasicResp {
38+
pub uuid: String,
39+
pub name: String,
40+
}
41+
42+
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, ApiComponent)]
43+
pub struct ClusterListResp {
44+
pub clusters: Vec<ClusterBasicResp>
45+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2022 Tobias Anker <tobias.anker@kitsunemimim.moe>
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use apistos::actix::CreatedJson;
16+
use actix_web::web::Json;
17+
use apistos::api_operation;
18+
use log::error;
19+
use uuid::Uuid;
20+
21+
use crate::api::user_context::UserContext;
22+
use crate::api::errors::ErrorResponse;
23+
use crate::database::cluster_table;
24+
use crate::core::cluster::parser::cluster_parser;
25+
26+
use super::cluster_structs::{ClusterCreateReq, ClusterResp};
27+
28+
#[api_operation(
29+
tag = "cluster",
30+
summary = "Create new cluster",
31+
description = r###"Create new cluster based on a cluster-template."###,
32+
error_code = 401,
33+
error_code = 500
34+
)]
35+
pub async fn create_cluster(body: Json<ClusterCreateReq>, context: UserContext) -> Result<CreatedJson<ClusterResp>, ErrorResponse> {
36+
let cluster_uuid = Uuid::new_v4().to_string();
37+
38+
// parse cluster
39+
match cluster_parser::parse_cluster_template(&body.template) {
40+
Ok(parsed) => {
41+
// TODO
42+
}
43+
Err(e) => {
44+
let msg = format!("Failed to parse cluster-template with error: '{}'", e);
45+
return Err(ErrorResponse::InternalError(msg));
46+
}
47+
}
48+
49+
// add new cluster to datbase
50+
match cluster_table::add_new_cluster(
51+
&cluster_uuid,
52+
&body.name,
53+
&body.template,
54+
&context.user_id)
55+
{
56+
Ok(_) => {},
57+
Err(_) => {
58+
let msg = format!("Failed to add cluster with ID '{}' to database.", cluster_uuid);
59+
error!("{}", msg);
60+
return Err(ErrorResponse::InternalError("".to_string()));
61+
}
62+
};
63+
64+
// get new created cluster from database to get addtional information
65+
match cluster_table::get_cluster(&cluster_uuid) {
66+
Ok(cluster) => {
67+
let resp = ClusterResp {
68+
uuid: cluster.uuid.clone(),
69+
name: cluster.name.clone(),
70+
template: cluster.template.clone(),
71+
created_by: cluster.created_by.clone(),
72+
created_at: cluster.created_at.clone(),
73+
updated_by: cluster.updated_by.clone(),
74+
updated_at: cluster.updated_at.clone(),
75+
};
76+
77+
return Ok(CreatedJson(resp));
78+
},
79+
Err(_) =>
80+
{
81+
let msg = format!("Failed to get cluster with ID '{}' from database, even the cluster should exist.", cluster_uuid);
82+
error!("{}", msg);
83+
return Err(ErrorResponse::InternalError("".to_string()));
84+
}
85+
};
86+
}

src/hanami/src/api/http_endpoints/cluster/delete_cluster_v1_0.rs

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod create_cluster_v1_0;
2+
pub mod cluster_structs;

src/hanami/src/api/http_endpoints/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
// limitations under the License.
1414

1515
pub mod auth;
16-
pub mod user;
16+
pub mod user;
17+
pub mod cluster;

src/hanami/src/api/middleware.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub async fn authorization_middleware(
3636
debug!("Check token for request against {}", req.uri());
3737
// get token from header
3838
let token: &str;
39-
println!("middleware poi1");
4039
match req.headers().get("Authorization") {
4140
Some(value) => {
4241
match split_bearer_token(value.to_str().unwrap()) {
@@ -60,9 +59,9 @@ pub async fn authorization_middleware(
6059
}
6160
}
6261
}
63-
else {
64-
debug!("skip token-check");
65-
}
62+
//else {
63+
// debug!("skip token-check");
64+
//}
6665

6766
info!("Api-call against URI: {}", req.uri());
6867

src/hanami/src/api/user_context.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use actix_web::{FromRequest, HttpRequest};
1818
use apistos::ApiSecurity;
1919
use futures::future::{ready, Ready};
2020
use serde::{Deserialize, Serialize};
21-
use jsonwebtoken::{decode, DecodingKey, Validation, Algorithm};
2221
use log::debug;
2322

2423
use crate::common::functions::split_bearer_token;

0 commit comments

Comments
 (0)