Skip to content

Commit 9c754b4

Browse files
committed
refactor: (controller) remove useless code
Removed duplicated code & some "vector" struct that was not needed Signed-off-by: Alexandre Gomez <[email protected]>
1 parent 0c4105c commit 9c754b4

File tree

13 files changed

+97
-261
lines changed

13 files changed

+97
-261
lines changed

controller/lib/src/external_api/instance/controller.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::sync::Arc;
22

3-
use crate::external_api::generic::model::{APIResponse, APIResponseMetadata};
3+
use crate::external_api::generic::model::{APIResponse, APIResponseMetadata, Pagination};
44
use crate::external_api::interface::ActixAppState;
55
use crate::external_api::workload::controller::WorkloadControllerError;
66
use crate::external_api::workload::service::WorkloadServiceError;
77

88
use super::super::workload::service::WorkloadService;
9-
use super::model::{Instance, InstanceDTO, InstanceVector, Pagination};
9+
use super::model::{Instance, InstanceDTO};
1010
use super::service::{InstanceService, InstanceServiceError};
1111
use actix_web::http::StatusCode;
1212
use actix_web::{web, HttpResponse, Responder, Scope};
@@ -224,19 +224,18 @@ impl InstanceController {
224224
}
225225
};
226226

227-
match pagination {
227+
let instances = match pagination {
228228
Some(pagination) => {
229-
let instances = instance_service
229+
instance_service
230230
.get_instances(pagination.limit, pagination.offset, &namespace)
231-
.await;
232-
233-
InstanceVector::new(instances).to_http()
231+
.await
234232
}
235-
None => {
236-
let instances = instance_service.get_instances(0, 0, &namespace).await;
233+
None => instance_service.get_instances(0, 0, &namespace).await,
234+
};
237235

238-
InstanceVector::new(instances).to_http()
239-
}
240-
}
236+
HttpResponse::build(StatusCode::OK).json(APIResponse::<Vec<Instance>> {
237+
data: instances,
238+
metadata: APIResponseMetadata::default(),
239+
})
241240
}
242241
}

controller/lib/src/external_api/instance/filter.rs

-61
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod controller;
2-
mod filter;
32
pub mod model;
43
pub mod service;

controller/lib/src/external_api/instance/model.rs

+4-30
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::net::Ipv4Addr;
22

3-
use actix_web::HttpResponse;
43
use proto::controller::{InstanceState, Type};
54
use rand::{distributions::Alphanumeric, Rng};
65
use serde::{Deserialize, Serialize};
76

8-
use crate::external_api::generic::model::{APIResponse, APIResponseMetadata};
7+
#[derive(Deserialize, Serialize)]
8+
pub struct InstanceDTO {
9+
pub workload_name: String,
10+
}
911

1012
#[derive(Deserialize, Serialize, Clone, Debug)]
1113
pub struct Instance {
@@ -23,40 +25,12 @@ pub struct Instance {
2325
pub namespace: String,
2426
}
2527

26-
#[derive(Deserialize, Serialize)]
27-
pub struct InstanceVector {
28-
pub instances: Vec<Instance>,
29-
}
30-
impl InstanceVector {
31-
pub fn new(instances: Vec<Instance>) -> InstanceVector {
32-
InstanceVector { instances }
33-
}
34-
pub fn to_http(self) -> HttpResponse {
35-
HttpResponse::Ok().json(APIResponse::<Vec<Instance>> {
36-
data: self.instances,
37-
metadata: APIResponseMetadata::default(),
38-
})
39-
}
40-
}
41-
4228
#[derive(Deserialize, Serialize, Clone, Debug)]
4329
pub struct Resource {
4430
pub limit: Option<ResourceSummary>,
4531
pub usage: Option<ResourceSummary>,
4632
}
4733

48-
#[derive(Deserialize, Serialize)]
49-
pub struct InstanceDTO {
50-
pub workload_name: String,
51-
}
52-
53-
#[derive(Deserialize, Serialize)]
54-
55-
pub struct Pagination {
56-
pub limit: u32,
57-
pub offset: u32,
58-
}
59-
6034
#[derive(Deserialize, Serialize, Clone, Debug)]
6135
pub struct ResourceSummary {
6236
pub cpu: u64,

controller/lib/src/external_api/instance/service.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::net::{Ipv4Addr, SocketAddr};
22
use std::sync::Arc;
33

4-
use super::filter::InstanceFilterService;
54
use super::model::Instance;
65
use crate::etcd::{EtcdClient, EtcdClientError};
6+
use crate::external_api::generic::filter::FilterService;
77
use crate::external_api::workload::model::Workload;
88
use crate::grpc_client::interface::{SchedulerClientInterface, SchedulerClientInterfaceError};
99
use log::{debug, trace};
@@ -32,7 +32,7 @@ pub enum InstanceServiceError {
3232
pub struct InstanceService {
3333
grpc_service: SchedulerClientInterface,
3434
etcd_service: EtcdClient,
35-
filter_service: InstanceFilterService,
35+
filter_service: FilterService,
3636
}
3737

3838
// `InstanceService` is a struct that is inspired from Controllers Provider Modules architectures. It is used as a service in the InstanceController. A service can use other services.
@@ -54,7 +54,7 @@ impl InstanceService {
5454
etcd_service: EtcdClient::new(etcd_address.to_string())
5555
.await
5656
.map_err(InstanceServiceError::EtcdError)?,
57-
filter_service: InstanceFilterService::new(),
57+
filter_service: FilterService::new(),
5858
})
5959
}
6060

controller/lib/src/external_api/namespace/controller.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,19 @@ impl NamespaceController {
115115
Err(err) => return NamespaceControllerError::NamespaceServiceError(err).into(),
116116
};
117117

118-
match pagination {
118+
let namespaces = match pagination {
119119
Some(pagination) => {
120-
let namespaces = namespace_service
120+
namespace_service
121121
.get_all_namespace(pagination.limit, pagination.offset)
122-
.await;
123-
namespaces.to_http()
122+
.await
124123
}
125-
None => {
126-
let namespaces = namespace_service.get_all_namespace(0, 0).await;
127-
namespaces.to_http()
128-
}
129-
}
124+
None => namespace_service.get_all_namespace(0, 0).await,
125+
};
126+
127+
HttpResponse::build(StatusCode::OK).json(APIResponse::<Vec<Namespace>> {
128+
metadata: APIResponseMetadata::default(),
129+
data: namespaces,
130+
})
130131
}
131132

132133
pub async fn patch_namespace(
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use actix_web::HttpResponse;
21
use serde::{Deserialize, Serialize};
32

4-
use crate::external_api::generic::model::{APIResponse, APIResponseMetadata};
3+
#[derive(Serialize, Deserialize)]
4+
pub struct NamespaceDTO {
5+
pub name: String,
6+
}
57

6-
#[derive(Debug, Serialize, Deserialize, Clone)]
7-
pub struct Metadata {}
88
#[derive(Debug, Serialize, Deserialize, Clone)]
99
pub struct Namespace {
1010
pub id: String,
@@ -13,31 +13,4 @@ pub struct Namespace {
1313
}
1414

1515
#[derive(Debug, Serialize, Deserialize, Clone)]
16-
pub struct NamespaceDTO {
17-
pub name: String,
18-
}
19-
20-
#[derive(Deserialize, Serialize)]
21-
pub struct NamespaceVector {
22-
pub namespaces: Vec<Namespace>,
23-
}
24-
impl NamespaceVector {
25-
pub fn new(namespaces: Vec<Namespace>) -> NamespaceVector {
26-
NamespaceVector { namespaces }
27-
}
28-
pub fn to_http(&self) -> HttpResponse {
29-
match serde_json::to_string(&self.namespaces) {
30-
Ok(json) => HttpResponse::Ok().json(APIResponse {
31-
data: Some(json),
32-
metadata: APIResponseMetadata::default(),
33-
}),
34-
Err(_) => HttpResponse::InternalServerError().json(APIResponse::<()> {
35-
metadata: APIResponseMetadata {
36-
error: Some("Internal Server Error".to_string()),
37-
..Default::default()
38-
},
39-
..Default::default()
40-
}),
41-
}
42-
}
43-
}
16+
pub struct Metadata {}

controller/lib/src/external_api/namespace/service.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde_json;
66
use std::net::SocketAddr;
77
use thiserror::Error;
88

9-
use super::model::{Metadata, Namespace, NamespaceDTO, NamespaceVector};
9+
use super::model::{Metadata, Namespace, NamespaceDTO};
1010

1111
#[derive(Debug, Error)]
1212
pub enum NamespaceServiceError {
@@ -55,7 +55,7 @@ impl NamespaceService {
5555
}
5656
}
5757

58-
pub async fn get_all_namespace(&mut self, limit: u32, offset: u32) -> NamespaceVector {
58+
pub async fn get_all_namespace(&mut self, limit: u32, offset: u32) -> Vec<Namespace> {
5959
let mut new_vec: Vec<Namespace> = Vec::new();
6060
match self.etcd_service.get_all().await {
6161
Some(namespaces) => {
@@ -68,17 +68,17 @@ impl NamespaceService {
6868
if offset > 0 {
6969
match self.filter_service.offset(&new_vec, offset) {
7070
Ok(namespaces) => new_vec = namespaces,
71-
Err(_) => return NamespaceVector::new(vec![]),
71+
Err(_) => return vec![],
7272
}
7373
}
7474
if limit > 0 {
7575
new_vec = self.filter_service.limit(&new_vec, limit);
7676
}
7777

7878
trace!("Namespaces found: {:?}", new_vec);
79-
NamespaceVector::new(new_vec)
79+
new_vec
8080
}
81-
None => NamespaceVector::new(vec![]),
81+
None => vec![],
8282
}
8383
}
8484

controller/lib/src/external_api/node/controller.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use crate::external_api::{
55
use actix_web::{http::StatusCode, web, HttpResponse, Responder, Scope};
66
use thiserror::Error;
77

8-
use super::{
9-
model::NodeVector,
10-
service::{NodeService, NodeServiceError},
11-
};
8+
use super::service::{NodeService, NodeServiceError};
129

1310
use log::{debug, error};
1411

@@ -98,20 +95,18 @@ impl NodeController {
9895
Err(err) => return NodeControllerError::NodeServiceError(err).into(),
9996
};
10097

101-
match pagination {
98+
let nodes = match pagination {
10299
Some(pagination) => {
103-
let nodes = node_service
100+
node_service
104101
.get_all_nodes(pagination.limit, pagination.offset)
105-
.await;
106-
NodeVector::new(nodes).to_http()
107-
}
108-
None => {
109-
let nodes = node_service.get_all_nodes(0, 0).await;
110-
NodeVector::new(nodes).to_http()
102+
.await
111103
}
112-
}
104+
None => node_service.get_all_nodes(0, 0).await,
105+
};
113106

114-
// let nodes = node_service.get_all_nodes().await;
115-
// web::Json(nodes)
107+
HttpResponse::build(StatusCode::OK).json(APIResponse {
108+
data: nodes,
109+
metadata: APIResponseMetadata::default(),
110+
})
116111
}
117112
}

0 commit comments

Comments
 (0)