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 #[serde(default)]
106 pub image: String,
107 pub state: String,
109 pub pid: Option<u32>,
111 pub overlay_ip: Option<String>,
113 #[serde(default, skip_serializing_if = "Option::is_none")]
118 pub node_id: Option<String>,
119}
120
121#[derive(Debug, Deserialize, utoipa::ToSchema)]
123pub struct ExecRequest {
124 pub command: Vec<String>,
126 #[serde(default)]
128 pub replica: Option<u32>,
129}
130
131#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
133pub struct ExecResponse {
134 pub exit_code: i32,
136 pub stdout: String,
138 pub stderr: String,
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn test_service_summary_serialize() {
148 let summary = ServiceSummary {
149 name: "api".to_string(),
150 deployment: "my-app".to_string(),
151 status: "running".to_string(),
152 replicas: 3,
153 desired_replicas: 3,
154 endpoints: vec![ServiceEndpoint {
155 name: "http".to_string(),
156 protocol: "http".to_string(),
157 port: 8080,
158 url: None,
159 }],
160 };
161 let json = serde_json::to_string(&summary).unwrap();
162 assert!(json.contains("api"));
163 assert!(json.contains("my-app"));
164 }
165
166 #[test]
167 fn test_scale_request_deserialize() {
168 let json = r#"{"replicas": 5}"#;
169 let request: ScaleRequest = serde_json::from_str(json).unwrap();
170 assert_eq!(request.replicas, 5);
171 }
172
173 #[test]
174 fn test_log_query_defaults() {
175 let query: LogQuery = serde_json::from_str("{}").unwrap();
176 assert_eq!(query.lines, 100);
177 assert!(!query.follow);
178 assert!(query.instance.is_none());
179 }
180}