1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Service endpoint DTOs.
use serde::{Deserialize, Serialize};
use utoipa::IntoParams;
/// Service summary
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ServiceSummary {
/// Service name
pub name: String,
/// Deployment name
pub deployment: String,
/// Service status
pub status: String,
/// Current replica count
pub replicas: u32,
/// Desired replica count
pub desired_replicas: u32,
/// Service endpoints
pub endpoints: Vec<ServiceEndpoint>,
}
/// Service details
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ServiceDetails {
/// Service name
pub name: String,
/// Deployment name
pub deployment: String,
/// Service status
pub status: String,
/// Current replica count
pub replicas: u32,
/// Desired replica count
pub desired_replicas: u32,
/// Service endpoints
pub endpoints: Vec<ServiceEndpoint>,
/// Service metrics
pub metrics: ServiceMetrics,
}
/// Service endpoint
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ServiceEndpoint {
/// Endpoint name
pub name: String,
/// Protocol
pub protocol: String,
/// Port
pub port: u16,
/// URL (if public)
pub url: Option<String>,
}
/// Service metrics
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ServiceMetrics {
/// CPU usage percentage
pub cpu_percent: f64,
/// Memory usage percentage
pub memory_percent: f64,
/// Requests per second
pub rps: Option<f64>,
}
/// Scale request
#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct ScaleRequest {
/// Target replica count
pub replicas: u32,
}
/// Log query parameters
#[derive(Debug, Deserialize, IntoParams)]
pub struct LogQuery {
/// Number of lines to return
#[serde(default = "default_lines")]
pub lines: u32,
/// Follow logs (streaming)
#[serde(default)]
pub follow: bool,
/// Filter by container/instance
pub instance: Option<String>,
}
fn default_lines() -> u32 {
100
}
/// Container summary for API responses
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ContainerSummary {
/// Container identifier (service-rep-N)
pub id: String,
/// Service name
pub service: String,
/// Replica number
pub replica: u32,
/// Image reference the container was created from (canonical form, e.g.
/// `docker.io/library/nginx:1.29-alpine`).
///
/// `#[serde(default)]` keeps older payloads (which lacked this field)
/// deserializable.
#[serde(default)]
pub image: String,
/// Container state
pub state: String,
/// Process ID (if running)
pub pid: Option<u32>,
/// Overlay IP (if assigned)
pub overlay_ip: Option<String>,
/// Raft node ID of the daemon that owns this container (if known).
///
/// Each daemon tags containers it knows about with its own local node ID.
/// `None` when the API is running in read-only mode (no local Raft node).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub node_id: Option<String>,
}
/// Exec request body
#[derive(Debug, Deserialize, utoipa::ToSchema)]
pub struct ExecRequest {
/// Command and arguments to execute
pub command: Vec<String>,
/// Optional replica number to target
#[serde(default)]
pub replica: Option<u32>,
}
/// Exec response body
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct ExecResponse {
/// Exit code from the command
pub exit_code: i32,
/// Standard output
pub stdout: String,
/// Standard error
pub stderr: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_service_summary_serialize() {
let summary = ServiceSummary {
name: "api".to_string(),
deployment: "my-app".to_string(),
status: "running".to_string(),
replicas: 3,
desired_replicas: 3,
endpoints: vec![ServiceEndpoint {
name: "http".to_string(),
protocol: "http".to_string(),
port: 8080,
url: None,
}],
};
let json = serde_json::to_string(&summary).unwrap();
assert!(json.contains("api"));
assert!(json.contains("my-app"));
}
#[test]
fn test_scale_request_deserialize() {
let json = r#"{"replicas": 5}"#;
let request: ScaleRequest = serde_json::from_str(json).unwrap();
assert_eq!(request.replicas, 5);
}
#[test]
fn test_log_query_defaults() {
let query: LogQuery = serde_json::from_str("{}").unwrap();
assert_eq!(query.lines, 100);
assert!(!query.follow);
assert!(query.instance.is_none());
}
}