Skip to content

Commit 214f41f

Browse files
committed
feat(scheduler): add parser module
Signed-off-by: iverly <github@iverly.net>
1 parent be7591c commit 214f41f

4 files changed

Lines changed: 278 additions & 0 deletions

File tree

scheduler/src/parser/instance.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use proto::scheduler::{Instance, Status};
2+
3+
use super::{port::PortParser, resource::ResourceParser};
4+
5+
pub struct InstanceParser {}
6+
7+
impl InstanceParser {
8+
/// It converts a `Instance` struct to a `proto::agent::Instance` struct
9+
///
10+
/// Arguments:
11+
///
12+
/// * `instance`: Instance - The instance to convert
13+
pub fn to_agent_instance(instance: Instance) -> proto::agent::Instance {
14+
proto::agent::Instance {
15+
id: instance.id,
16+
name: instance.name,
17+
r#type: instance.r#type,
18+
status: instance.status,
19+
uri: instance.uri,
20+
environment: instance.environnement,
21+
resource: instance.resource.map(ResourceParser::to_agent_resource),
22+
ports: PortParser::to_agent_ports(instance.ports),
23+
ip: instance.ip,
24+
}
25+
}
26+
27+
/// It converts a `proto::agent::Instance` struct to a `Instance` struct
28+
///
29+
/// Arguments:
30+
///
31+
/// * `instance`: proto::agent::Instance
32+
///
33+
/// Returns:
34+
///
35+
/// An Instance struct
36+
pub fn from_agent_instance(instance: proto::agent::Instance) -> Instance {
37+
Instance {
38+
id: instance.id,
39+
name: instance.name,
40+
r#type: instance.r#type,
41+
status: instance.status,
42+
uri: instance.uri,
43+
environnement: instance.environment,
44+
resource: instance.resource.map(ResourceParser::from_agent_resource),
45+
ports: PortParser::from_agent_ports(instance.ports),
46+
ip: instance.ip,
47+
}
48+
}
49+
50+
/// It creates a fake agent instance
51+
///
52+
/// Arguments:
53+
///
54+
/// * `id`: The id of the agent instance.
55+
pub fn fake_agent_instance(id: String) -> proto::agent::Instance {
56+
proto::agent::Instance {
57+
id,
58+
name: "".to_string(),
59+
r#type: proto::agent::Type::Container.into(),
60+
status: Status::Stopping.into(),
61+
uri: "".to_string(),
62+
environment: vec![],
63+
resource: None,
64+
ports: vec![],
65+
ip: "".to_string(),
66+
}
67+
}
68+
69+
/// It creates a fake controller instance
70+
///
71+
/// Arguments:
72+
///
73+
/// * `id`: The id of the instance
74+
pub fn fake_controller_instance(id: String) -> proto::controller::Instance {
75+
proto::controller::Instance {
76+
id,
77+
name: "".to_string(),
78+
r#type: proto::agent::Type::Container.into(),
79+
state: Status::Stopping.into(),
80+
uri: "".to_string(),
81+
environnement: vec![],
82+
resource: None,
83+
ports: vec![],
84+
ip: "".to_string(),
85+
}
86+
}
87+
}

scheduler/src/parser/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod instance;
2+
pub mod port;
3+
pub mod resource;

scheduler/src/parser/port.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use proto::scheduler::{InstanceStatus, NodeStatus, Port};
2+
3+
use super::resource::ResourceParser;
4+
5+
pub struct PortParser {}
6+
7+
impl PortParser {
8+
/// `ports` is a vector of `Port`s, and we want to convert it into a vector of `proto::agent::Port`s
9+
///
10+
/// Arguments:
11+
///
12+
/// * `ports`: Vec<Port>
13+
///
14+
/// Returns:
15+
///
16+
/// A vector of proto::agent::Port
17+
pub fn to_agent_ports(ports: Vec<Port>) -> Vec<proto::agent::Port> {
18+
ports
19+
.into_iter()
20+
.map(|port| proto::agent::Port {
21+
source: port.source,
22+
destination: port.destination,
23+
})
24+
.collect()
25+
}
26+
27+
/// `from_agent_ports` takes a vector of `proto::agent::Port`s and returns a vector of `Port`s
28+
///
29+
/// Arguments:
30+
///
31+
/// * `ports`: Vec<proto::agent::Port>
32+
///
33+
/// Returns:
34+
///
35+
/// A vector of Port structs.
36+
pub fn from_agent_ports(ports: Vec<proto::agent::Port>) -> Vec<Port> {
37+
ports
38+
.into_iter()
39+
.map(|port| Port {
40+
source: port.source,
41+
destination: port.destination,
42+
})
43+
.collect()
44+
}
45+
}
46+
47+
pub struct StatusParser {}
48+
49+
impl StatusParser {
50+
/// It takes a `proto::agent::InstanceStatus` and returns an `InstanceStatus`
51+
///
52+
/// Arguments:
53+
///
54+
/// * `status`: proto::agent::InstanceStatus
55+
///
56+
/// Returns:
57+
///
58+
/// A new InstanceStatus struct
59+
pub fn from_agent_instance_status(status: proto::agent::InstanceStatus) -> InstanceStatus {
60+
InstanceStatus {
61+
id: status.id,
62+
status: status.status,
63+
status_description: status.description,
64+
resource: status.resource.map(ResourceParser::from_agent_resource),
65+
}
66+
}
67+
68+
/// It takes a `NodeStatus` struct and returns a `proto::controller::NodeStatus` struct
69+
///
70+
/// Arguments:
71+
///
72+
/// * `status`: NodeStatus - this is the status of the node.
73+
pub fn to_controller_node_status(status: NodeStatus) -> proto::controller::NodeStatus {
74+
proto::controller::NodeStatus {
75+
id: status.id,
76+
state: status.status,
77+
status_description: status.status_description,
78+
resource: status.resource.map(ResourceParser::to_controller_resource),
79+
instances: vec![], // todo;
80+
}
81+
}
82+
}

scheduler/src/parser/resource.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use proto::scheduler::{Resource, ResourceSummary};
2+
3+
pub struct ResourceParser {}
4+
5+
impl ResourceParser {
6+
/// It converts a Resource struct to a proto::agent::Resource struct.
7+
///
8+
/// Arguments:
9+
///
10+
/// * `resource`: Resource
11+
///
12+
/// Returns:
13+
///
14+
/// A proto::agent::Resource struct
15+
pub fn to_agent_resource(resource: Resource) -> proto::agent::Resource {
16+
proto::agent::Resource {
17+
limit: resource.limit.map(Self::to_agent_resourcesummary),
18+
usage: resource.usage.map(Self::to_agent_resourcesummary),
19+
}
20+
}
21+
22+
/// It converts a Resource struct to a proto::controller::Resource struct.
23+
///
24+
/// Arguments:
25+
///
26+
/// * `resource`: Resource
27+
///
28+
/// Returns:
29+
///
30+
/// A proto::controller::Resource struct
31+
pub fn to_controller_resource(resource: Resource) -> proto::controller::Resource {
32+
proto::controller::Resource {
33+
limit: resource.limit.map(Self::to_controller_resourcesummary),
34+
usage: resource.usage.map(Self::to_controller_resourcesummary),
35+
}
36+
}
37+
38+
/// It converts a proto::agent::Resource struct to a Resource struct.
39+
///
40+
/// Arguments:
41+
///
42+
/// * `resource`: proto::agent::Resource
43+
///
44+
/// Returns:
45+
///
46+
/// A Resource struct
47+
pub fn from_agent_resource(resource: proto::agent::Resource) -> Resource {
48+
Resource {
49+
limit: resource.limit.map(Self::from_agent_resourcesummary),
50+
usage: resource.usage.map(Self::from_agent_resourcesummary),
51+
}
52+
}
53+
54+
/// It converts a ResourceSummary to a proto::agent::ResourceSummary struct.
55+
///
56+
/// Arguments:
57+
///
58+
/// * `resource`: ResourceSummary
59+
///
60+
/// Returns:
61+
///
62+
/// A proto::agent::ResourceSummary struct
63+
pub fn to_agent_resourcesummary(resource: ResourceSummary) -> proto::agent::ResourceSummary {
64+
proto::agent::ResourceSummary {
65+
cpu: resource.cpu,
66+
memory: resource.memory,
67+
disk: resource.disk,
68+
}
69+
}
70+
71+
/// It converts a ResourceSummary to a proto::agent::ResourceSummary struct.
72+
///
73+
/// Arguments:
74+
///
75+
/// * `resource`: ResourceSummary
76+
///
77+
/// Returns:
78+
///
79+
/// A proto::agent::ResourceSummary struct
80+
pub fn to_controller_resourcesummary(
81+
resource: ResourceSummary,
82+
) -> proto::controller::ResourceSummary {
83+
proto::controller::ResourceSummary {
84+
cpu: resource.cpu,
85+
memory: resource.memory,
86+
disk: resource.disk,
87+
}
88+
}
89+
90+
/// It converts a proto::agent::ResourceSummary to a ResourceSummary struct.
91+
///
92+
/// Arguments:
93+
///
94+
/// * `resource`: proto::agent::ResourceSummary
95+
///
96+
/// Returns:
97+
///
98+
/// A ResourceSummary struct
99+
pub fn from_agent_resourcesummary(resource: proto::agent::ResourceSummary) -> ResourceSummary {
100+
ResourceSummary {
101+
cpu: resource.cpu,
102+
memory: resource.memory,
103+
disk: resource.disk,
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)