zlayer_types/api/
services.rs1use serde::{Deserialize, Serialize};
4
5use utoipa::IntoParams;
6
7#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
9pub struct ServiceSummary {
10 pub name: String,
12 pub deployment: String,
14 pub status: String,
16 pub replicas: u32,
18 pub desired_replicas: u32,
20 pub endpoints: Vec<ServiceEndpoint>,
22}
23
24#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
26pub struct ServiceDetails {
27 pub name: String,
29 pub deployment: String,
31 pub status: String,
33 pub replicas: u32,
35 pub desired_replicas: u32,
37 pub endpoints: Vec<ServiceEndpoint>,
39 pub metrics: ServiceMetrics,
41}
42
43#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
45pub struct ServiceEndpoint {
46 pub name: String,
48 pub protocol: String,
50 pub port: u16,
52 pub url: Option<String>,
54}
55
56#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
58pub struct ServiceMetrics {
59 pub cpu_percent: f64,
61 pub memory_percent: f64,
63 pub rps: Option<f64>,
65}
66
67#[derive(Debug, Deserialize, utoipa::ToSchema)]
69pub struct ScaleRequest {
70 pub replicas: u32,
72}
73
74#[derive(Debug, Deserialize, IntoParams)]
76pub struct LogQuery {
77 #[serde(default = "default_lines")]
79 pub lines: u32,
80 #[serde(default)]
82 pub follow: bool,
83 pub instance: Option<String>,
85}
86
87fn default_lines() -> u32 {
88 100
89}
90
91#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
93pub struct ContainerSummary {
94 pub id: String,
96 pub service: String,
98 pub replica: u32,
100 pub state: String,
102 pub pid: Option<u32>,
104 pub overlay_ip: Option<String>,
106}
107
108#[derive(Debug, Deserialize, utoipa::ToSchema)]
110pub struct ExecRequest {
111 pub command: Vec<String>,
113 #[serde(default)]
115 pub replica: Option<u32>,
116}
117
118#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
120pub struct ExecResponse {
121 pub exit_code: i32,
123 pub stdout: String,
125 pub stderr: String,
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn test_service_summary_serialize() {
135 let summary = ServiceSummary {
136 name: "api".to_string(),
137 deployment: "my-app".to_string(),
138 status: "running".to_string(),
139 replicas: 3,
140 desired_replicas: 3,
141 endpoints: vec![ServiceEndpoint {
142 name: "http".to_string(),
143 protocol: "http".to_string(),
144 port: 8080,
145 url: None,
146 }],
147 };
148 let json = serde_json::to_string(&summary).unwrap();
149 assert!(json.contains("api"));
150 assert!(json.contains("my-app"));
151 }
152
153 #[test]
154 fn test_scale_request_deserialize() {
155 let json = r#"{"replicas": 5}"#;
156 let request: ScaleRequest = serde_json::from_str(json).unwrap();
157 assert_eq!(request.replicas, 5);
158 }
159
160 #[test]
161 fn test_log_query_defaults() {
162 let query: LogQuery = serde_json::from_str("{}").unwrap();
163 assert_eq!(query.lines, 100);
164 assert!(!query.follow);
165 assert!(query.instance.is_none());
166 }
167}