Skip to content

Commit e0f4363

Browse files
committed
fix: refine codes
1 parent 2c088a9 commit e0f4363

File tree

17 files changed

+117
-94
lines changed

17 files changed

+117
-94
lines changed

core/src/ten_manager/src/designer/graphs/nodes/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct AddGraphNodeRequestPayload {
3333

3434
pub name: String,
3535
pub addon: String,
36-
pub extension_group: Option<String>,
36+
pub extension_group: String,
3737
pub app: Option<String>,
3838

3939
pub property: Option<serde_json::Value>,

core/src/ten_manager/src/designer/graphs/nodes/delete.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct DeleteGraphNodeRequestPayload {
3333

3434
pub name: String,
3535
pub addon: String,
36-
pub extension_group: Option<String>,
36+
pub extension_group: String,
3737
pub app: Option<String>,
3838
}
3939

@@ -47,7 +47,7 @@ pub fn graph_delete_extension_node(
4747
pkg_name: String,
4848
addon: String,
4949
app: Option<String>,
50-
extension_group: Option<String>,
50+
extension_group: &str,
5151
) -> Result<()> {
5252
// Find and remove the matching node.
5353
let original_nodes_len = graph.nodes.len();
@@ -56,7 +56,7 @@ pub fn graph_delete_extension_node(
5656
&& node.type_and_name.name == pkg_name
5757
&& node.addon == addon
5858
&& node.app == app
59-
&& node.extension_group == extension_group)
59+
&& node.extension_group == Some(extension_group.to_string()))
6060
});
6161

6262
// If no node was removed, return early.
@@ -167,7 +167,7 @@ pub async fn delete_graph_node_endpoint(
167167
request_payload.name.clone(),
168168
request_payload.addon.clone(),
169169
request_payload.app.clone(),
170-
request_payload.extension_group.clone(),
170+
&request_payload.extension_group,
171171
) {
172172
let error_response = ErrorResponse {
173173
status: Status::Fail,
@@ -183,7 +183,7 @@ pub async fn delete_graph_node_endpoint(
183183
graph_info,
184184
&request_payload.name,
185185
&request_payload.addon,
186-
request_payload.extension_group.as_deref(),
186+
&request_payload.extension_group,
187187
request_payload.app.as_deref(),
188188
);
189189

@@ -202,7 +202,7 @@ fn update_property_json_if_needed(
202202
graph_info: &mut GraphInfo,
203203
node_name: &str,
204204
addon: &str,
205-
extension_group: Option<&str>,
205+
extension_group: &str,
206206
app: Option<&str>,
207207
) {
208208
// Try to find the belonging package info
@@ -222,7 +222,7 @@ fn update_property_json_if_needed(
222222
name: node_name.to_string(),
223223
},
224224
addon: addon.to_string(),
225-
extension_group: extension_group.map(String::from),
225+
extension_group: Some(extension_group.to_string()),
226226
app: app.map(String::from),
227227
property: None,
228228
};

core/src/ten_manager/src/designer/graphs/nodes/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn update_graph_node_in_property_all_fields(
178178
graph_info: &mut GraphInfo,
179179
node_name: &str,
180180
addon_name: &str,
181-
extension_group_name: &Option<String>,
181+
extension_group_name: &str,
182182
app_uri: &Option<String>,
183183
property: &Option<serde_json::Value>,
184184
action: GraphNodeUpdateAction,
@@ -193,7 +193,7 @@ pub fn update_graph_node_in_property_all_fields(
193193
name: node_name.to_string(),
194194
},
195195
addon: addon_name.to_string(),
196-
extension_group: extension_group_name.clone(),
196+
extension_group: Some(extension_group_name.to_string()),
197197
app: app_uri.clone(),
198198
property: property.clone(),
199199
};

core/src/ten_manager/src/designer/graphs/nodes/property/update.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct UpdateGraphNodePropertyRequestPayload {
3333

3434
pub name: String,
3535
pub addon: String,
36-
pub extension_group: Option<String>,
36+
pub extension_group: String,
3737
pub app: Option<String>,
3838

3939
pub property: Option<serde_json::Value>,
@@ -52,7 +52,8 @@ fn update_node_property_in_graph(
5252
let graph_node = graph_info.graph.nodes.iter_mut().find(|node| {
5353
node.type_and_name.name == request_payload.name
5454
&& node.addon == request_payload.addon
55-
&& node.extension_group == request_payload.extension_group
55+
&& node.extension_group
56+
== Some(request_payload.extension_group.clone())
5657
&& node.app == request_payload.app
5758
});
5859

core/src/ten_manager/src/designer/graphs/nodes/replace.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,21 @@ pub async fn replace_graph_node_endpoint(
9999

100100
// Replace the addon and property of the graph node.
101101
let graph_node = graph_node.unwrap();
102-
let extension_group = graph_node.extension_group.clone();
102+
103+
let extension_group =
104+
if let Some(extension_group) = &graph_node.extension_group {
105+
extension_group.to_string()
106+
} else {
107+
let error_response = ErrorResponse {
108+
status: Status::Fail,
109+
message: "The replacing extension node does not belong to any \
110+
extension group"
111+
.to_string(),
112+
error: None,
113+
};
114+
return Ok(HttpResponse::BadRequest().json(error_response));
115+
};
116+
103117
graph_node.addon = request_payload.addon.clone();
104118
graph_node.property = request_payload.property.clone();
105119

core/src/ten_manager/src/designer/template_pkgs/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ pub struct GetTemplateRequestPayload {
4444
pub language: TemplateLanguage,
4545
}
4646

47+
#[derive(Serialize, Deserialize, Debug)]
48+
pub struct GetTemplateResponsePayload {
49+
pub pkg_name: String,
50+
pub pkg_version: String,
51+
}
52+
4753
#[derive(Serialize, Deserialize, Debug)]
4854
pub struct GetTemplateResponseData {
49-
pub template_name: Vec<String>,
55+
pub templates: Vec<GetTemplateResponsePayload>,
5056
}
5157

5258
pub async fn get_template_endpoint(
@@ -85,13 +91,16 @@ pub async fn get_template_endpoint(
8591
match result {
8692
Ok(packages) => {
8793
// Extract the package names from the PkgRegistryInfo structs.
88-
let template_names: Vec<String> = packages
94+
let templates: Vec<GetTemplateResponsePayload> = packages
8995
.iter()
90-
.map(|pkg| pkg.basic_info.type_and_name.name.clone())
96+
.map(|pkg| GetTemplateResponsePayload {
97+
pkg_name: pkg.basic_info.type_and_name.name.clone(),
98+
pkg_version: pkg.basic_info.version.to_string(),
99+
})
91100
.collect();
92101

93102
// Handle case where no packages were found.
94-
if template_names.is_empty() {
103+
if templates.is_empty() {
95104
let error_message = format!(
96105
"Unsupported template combination: pkg_type={}, \
97106
language={}",
@@ -109,7 +118,7 @@ pub async fn get_template_endpoint(
109118

110119
let response = ApiResponse {
111120
status: Status::Ok,
112-
data: GetTemplateResponseData { template_name: template_names },
121+
data: GetTemplateResponseData { templates },
113122
meta: None,
114123
};
115124

core/src/ten_manager/src/graph/nodes/add.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,34 @@ use ten_rust::{
1212
};
1313

1414
/// Checks if a node exists in the graph.
15-
fn check_node_exist(
15+
fn check_extension_node_exist(
1616
graph: &Graph,
1717
app: &Option<String>,
1818
extension: &str,
19+
extension_group: &str,
1920
) -> Result<()> {
20-
// Validate that source node exists.
21-
let src_node_exists = graph
22-
.nodes
23-
.iter()
24-
.any(|node| node.type_and_name.name == extension && node.app == *app);
21+
// Validate that extension node exists.
22+
let node_exists = graph.nodes.iter().any(|node| {
23+
node.type_and_name.name == extension
24+
&& node.app == *app
25+
&& node.extension_group == Some(extension_group.to_string())
26+
});
2527

26-
if src_node_exists {
28+
if node_exists {
29+
if let Some(app) = app {
30+
return Err(anyhow::anyhow!(
31+
"Node with app '{}' and extension group '{}' and extension \
32+
'{}' already exists in the graph",
33+
app,
34+
extension_group,
35+
extension
36+
));
37+
}
2738
return Err(anyhow::anyhow!(
28-
"Node with extension '{}' and app '{:?}' already exists in the \
29-
graph",
30-
extension,
31-
app
39+
"Node with extension group '{}' and extension '{}' already exists \
40+
in the graph",
41+
extension_group,
42+
extension
3243
));
3344
}
3445

@@ -40,10 +51,10 @@ pub fn graph_add_extension_node(
4051
pkg_name: &str,
4152
addon: &str,
4253
app: &Option<String>,
43-
extension_group: &Option<String>,
54+
extension_group: &str,
4455
property: &Option<serde_json::Value>,
4556
) -> Result<()> {
46-
check_node_exist(graph, app, pkg_name)?;
57+
check_extension_node_exist(graph, app, pkg_name, extension_group)?;
4758

4859
// Store the original state in case validation fails.
4960
let original_graph = graph.clone();
@@ -55,7 +66,7 @@ pub fn graph_add_extension_node(
5566
name: pkg_name.to_string(),
5667
},
5768
addon: addon.to_string(),
58-
extension_group: extension_group.clone(),
69+
extension_group: Some(extension_group.to_string()),
5970
app: app.clone(),
6071
property: property.clone(),
6172
};

core/src/ten_manager/tests/test_case/designer/graphs/nodes/add.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ mod tests {
7474
graph_id: Uuid::new_v4(),
7575
name: "test_node".to_string(),
7676
addon: "test_addon".to_string(),
77-
extension_group: None,
77+
extension_group: "custom_group".to_string(),
7878
app: Some("http://test-app-uri.com".to_string()),
7979
property: None,
8080
};
@@ -147,7 +147,7 @@ mod tests {
147147
graph_id: graph_id_clone,
148148
name: "test_node".to_string(),
149149
addon: "test_addon".to_string(),
150-
extension_group: None,
150+
extension_group: "custom_group".to_string(),
151151
app: Some(localhost().to_string()),
152152
property: None,
153153
};
@@ -225,7 +225,7 @@ mod tests {
225225
graph_id: graph_id_clone,
226226
name: "test_node".to_string(),
227227
addon: "test_addon".to_string(),
228-
extension_group: None,
228+
extension_group: "custom_group".to_string(),
229229
app: Some("http://example.com:8000".to_string()),
230230
property: None,
231231
};
@@ -303,7 +303,7 @@ mod tests {
303303
graph_id: graph_id_clone,
304304
name: "test_node".to_string(),
305305
addon: "test_addon".to_string(),
306-
extension_group: None,
306+
extension_group: "custom_group".to_string(),
307307
app: None,
308308
property: None,
309309
};
@@ -413,7 +413,7 @@ mod tests {
413413
graph_id: graph_id_clone,
414414
name: "new_node".to_string(),
415415
addon: "new_addon".to_string(),
416-
extension_group: None,
416+
extension_group: "b".to_string(),
417417
app: None,
418418
property: None,
419419
};
@@ -525,7 +525,6 @@ mod tests {
525525

526526
let designer_state = Arc::new(designer_state);
527527

528-
// First add a node, then delete it.
529528
// Setup the add endpoint.
530529
let app_add = test::init_service(
531530
App::new().app_data(web::Data::new(designer_state.clone())).route(
@@ -540,7 +539,7 @@ mod tests {
540539
graph_id: graph_id_clone,
541540
name: "test_delete_node".to_string(),
542541
addon: "test_addon".to_string(),
543-
extension_group: None,
542+
extension_group: "custom_group".to_string(),
544543
app: Some("http://example.com:8000".to_string()),
545544
property: Some(serde_json::json!({
546545
"test_property": "test_value_for_delete"
@@ -673,7 +672,7 @@ mod tests {
673672
graph_id: graph_id_clone,
674673
name: "test_delete_node".to_string(),
675674
addon: "test_addon".to_string(),
676-
extension_group: None,
675+
extension_group: "custom_group".to_string(),
677676
app: Some("http://example.com:8000".to_string()),
678677
property: Some(serde_json::json!({
679678
"test_property": 13

core/src/ten_manager/tests/test_case/designer/graphs/nodes/delete.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod tests {
7676
graph_id: Uuid::new_v4(),
7777
name: "test_node".to_string(),
7878
addon: "test_addon".to_string(),
79-
extension_group: None,
79+
extension_group: "custom_group".to_string(),
8080
app: Some("http://test-app-uri.com".to_string()),
8181
};
8282

@@ -151,7 +151,7 @@ mod tests {
151151
graph_id: graph_id_clone,
152152
name: "non_existent_node".to_string(),
153153
addon: "test_addon".to_string(),
154-
extension_group: None,
154+
extension_group: "custom_group".to_string(),
155155
app: Some("http://example.com:8000".to_string()),
156156
};
157157

@@ -257,7 +257,7 @@ mod tests {
257257
graph_id: graph_id_clone,
258258
name: "test_delete_node".to_string(),
259259
addon: "test_addon".to_string(),
260-
extension_group: None,
260+
extension_group: "custom_group".to_string(),
261261
app: Some("http://example.com:8000".to_string()),
262262
property: Some(serde_json::json!({
263263
"test_property": "test_value_for_delete"
@@ -308,7 +308,7 @@ mod tests {
308308
graph_id: graph_id_clone,
309309
name: "test_delete_node".to_string(),
310310
addon: "test_addon".to_string(),
311-
extension_group: None,
311+
extension_group: "custom_group".to_string(),
312312
app: Some("http://example.com:8000".to_string()),
313313
};
314314

0 commit comments

Comments
 (0)