Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: (controller) remove useless code #97

Open
wants to merge 1 commit into
base: project-demo-202208
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions controller/lib/src/external_api/instance/controller.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::sync::Arc;

use crate::external_api::generic::model::{APIResponse, APIResponseMetadata};
use crate::external_api::generic::model::{APIResponse, APIResponseMetadata, Pagination};
use crate::external_api::interface::ActixAppState;
use crate::external_api::workload::controller::WorkloadControllerError;
use crate::external_api::workload::service::WorkloadServiceError;

use super::super::workload::service::WorkloadService;
use super::model::{Instance, InstanceDTO, InstanceVector, Pagination};
use super::model::{Instance, InstanceDTO};
use super::service::{InstanceService, InstanceServiceError};
use actix_web::http::StatusCode;
use actix_web::{web, HttpResponse, Responder, Scope};
Expand Down Expand Up @@ -224,19 +224,18 @@ impl InstanceController {
}
};

match pagination {
let instances = match pagination {
Some(pagination) => {
let instances = instance_service
instance_service
.get_instances(pagination.limit, pagination.offset, &namespace)
.await;

InstanceVector::new(instances).to_http()
.await
}
None => {
let instances = instance_service.get_instances(0, 0, &namespace).await;
None => instance_service.get_instances(0, 0, &namespace).await,
};

InstanceVector::new(instances).to_http()
}
}
HttpResponse::build(StatusCode::OK).json(APIResponse::<Vec<Instance>> {
data: instances,
metadata: APIResponseMetadata::default(),
})
}
}
61 changes: 0 additions & 61 deletions controller/lib/src/external_api/instance/filter.rs

This file was deleted.

1 change: 0 additions & 1 deletion controller/lib/src/external_api/instance/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod controller;
mod filter;
pub mod model;
pub mod service;
34 changes: 4 additions & 30 deletions controller/lib/src/external_api/instance/model.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::net::Ipv4Addr;

use actix_web::HttpResponse;
use proto::controller::{InstanceState, Type};
use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize};

use crate::external_api::generic::model::{APIResponse, APIResponseMetadata};
#[derive(Deserialize, Serialize)]
pub struct InstanceDTO {
pub workload_name: String,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Instance {
Expand All @@ -23,40 +25,12 @@ pub struct Instance {
pub namespace: String,
}

#[derive(Deserialize, Serialize)]
pub struct InstanceVector {
pub instances: Vec<Instance>,
}
impl InstanceVector {
pub fn new(instances: Vec<Instance>) -> InstanceVector {
InstanceVector { instances }
}
pub fn to_http(self) -> HttpResponse {
HttpResponse::Ok().json(APIResponse::<Vec<Instance>> {
data: self.instances,
metadata: APIResponseMetadata::default(),
})
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Resource {
pub limit: Option<ResourceSummary>,
pub usage: Option<ResourceSummary>,
}

#[derive(Deserialize, Serialize)]
pub struct InstanceDTO {
pub workload_name: String,
}

#[derive(Deserialize, Serialize)]

pub struct Pagination {
pub limit: u32,
pub offset: u32,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct ResourceSummary {
pub cpu: u64,
Expand Down
6 changes: 3 additions & 3 deletions controller/lib/src/external_api/instance/service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::net::{Ipv4Addr, SocketAddr};
use std::sync::Arc;

use super::filter::InstanceFilterService;
use super::model::Instance;
use crate::etcd::{EtcdClient, EtcdClientError};
use crate::external_api::generic::filter::FilterService;
use crate::external_api::workload::model::Workload;
use crate::grpc_client::interface::{SchedulerClientInterface, SchedulerClientInterfaceError};
use log::{debug, trace};
Expand Down Expand Up @@ -32,7 +32,7 @@ pub enum InstanceServiceError {
pub struct InstanceService {
grpc_service: SchedulerClientInterface,
etcd_service: EtcdClient,
filter_service: InstanceFilterService,
filter_service: FilterService,
}

// `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.
Expand All @@ -54,7 +54,7 @@ impl InstanceService {
etcd_service: EtcdClient::new(etcd_address.to_string())
.await
.map_err(InstanceServiceError::EtcdError)?,
filter_service: InstanceFilterService::new(),
filter_service: FilterService::new(),
})
}

Expand Down
19 changes: 10 additions & 9 deletions controller/lib/src/external_api/namespace/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,19 @@ impl NamespaceController {
Err(err) => return NamespaceControllerError::NamespaceServiceError(err).into(),
};

match pagination {
let namespaces = match pagination {
Some(pagination) => {
let namespaces = namespace_service
namespace_service
.get_all_namespace(pagination.limit, pagination.offset)
.await;
namespaces.to_http()
.await
}
None => {
let namespaces = namespace_service.get_all_namespace(0, 0).await;
namespaces.to_http()
}
}
None => namespace_service.get_all_namespace(0, 0).await,
};

HttpResponse::build(StatusCode::OK).json(APIResponse::<Vec<Namespace>> {
metadata: APIResponseMetadata::default(),
data: namespaces,
})
}

pub async fn patch_namespace(
Expand Down
37 changes: 5 additions & 32 deletions controller/lib/src/external_api/namespace/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use actix_web::HttpResponse;
use serde::{Deserialize, Serialize};

use crate::external_api::generic::model::{APIResponse, APIResponseMetadata};
#[derive(Serialize, Deserialize)]
pub struct NamespaceDTO {
pub name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Metadata {}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Namespace {
pub id: String,
Expand All @@ -13,31 +13,4 @@ pub struct Namespace {
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NamespaceDTO {
pub name: String,
}

#[derive(Deserialize, Serialize)]
pub struct NamespaceVector {
pub namespaces: Vec<Namespace>,
}
impl NamespaceVector {
pub fn new(namespaces: Vec<Namespace>) -> NamespaceVector {
NamespaceVector { namespaces }
}
pub fn to_http(&self) -> HttpResponse {
match serde_json::to_string(&self.namespaces) {
Ok(json) => HttpResponse::Ok().json(APIResponse {
data: Some(json),
metadata: APIResponseMetadata::default(),
}),
Err(_) => HttpResponse::InternalServerError().json(APIResponse::<()> {
metadata: APIResponseMetadata {
error: Some("Internal Server Error".to_string()),
..Default::default()
},
..Default::default()
}),
}
}
}
pub struct Metadata {}
10 changes: 5 additions & 5 deletions controller/lib/src/external_api/namespace/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde_json;
use std::net::SocketAddr;
use thiserror::Error;

use super::model::{Metadata, Namespace, NamespaceDTO, NamespaceVector};
use super::model::{Metadata, Namespace, NamespaceDTO};

#[derive(Debug, Error)]
pub enum NamespaceServiceError {
Expand Down Expand Up @@ -55,7 +55,7 @@ impl NamespaceService {
}
}

pub async fn get_all_namespace(&mut self, limit: u32, offset: u32) -> NamespaceVector {
pub async fn get_all_namespace(&mut self, limit: u32, offset: u32) -> Vec<Namespace> {
let mut new_vec: Vec<Namespace> = Vec::new();
match self.etcd_service.get_all().await {
Some(namespaces) => {
Expand All @@ -68,17 +68,17 @@ impl NamespaceService {
if offset > 0 {
match self.filter_service.offset(&new_vec, offset) {
Ok(namespaces) => new_vec = namespaces,
Err(_) => return NamespaceVector::new(vec![]),
Err(_) => return vec![],
}
}
if limit > 0 {
new_vec = self.filter_service.limit(&new_vec, limit);
}

trace!("Namespaces found: {:?}", new_vec);
NamespaceVector::new(new_vec)
new_vec
}
None => NamespaceVector::new(vec![]),
None => vec![],
}
}

Expand Down
25 changes: 10 additions & 15 deletions controller/lib/src/external_api/node/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use crate::external_api::{
use actix_web::{http::StatusCode, web, HttpResponse, Responder, Scope};
use thiserror::Error;

use super::{
model::NodeVector,
service::{NodeService, NodeServiceError},
};
use super::service::{NodeService, NodeServiceError};

use log::{debug, error};

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

match pagination {
let nodes = match pagination {
Some(pagination) => {
let nodes = node_service
node_service
.get_all_nodes(pagination.limit, pagination.offset)
.await;
NodeVector::new(nodes).to_http()
}
None => {
let nodes = node_service.get_all_nodes(0, 0).await;
NodeVector::new(nodes).to_http()
.await
}
}
None => node_service.get_all_nodes(0, 0).await,
};

// let nodes = node_service.get_all_nodes().await;
// web::Json(nodes)
HttpResponse::build(StatusCode::OK).json(APIResponse {
data: nodes,
metadata: APIResponseMetadata::default(),
})
}
}
Loading