1use serde::{Deserialize, Serialize};
10use time::OffsetDateTime;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum SailboxStatus {
18 Running,
20 Paused,
22 Sleeping,
24 Failed,
26 Terminated,
28 Other(String),
30}
31
32impl Default for SailboxStatus {
33 fn default() -> Self {
34 SailboxStatus::Other(String::new())
35 }
36}
37
38impl SailboxStatus {
39 pub fn as_str(&self) -> &str {
41 match self {
42 SailboxStatus::Running => "running",
43 SailboxStatus::Paused => "paused",
44 SailboxStatus::Sleeping => "sleeping",
45 SailboxStatus::Failed => "failed",
46 SailboxStatus::Terminated => "terminated",
47 SailboxStatus::Other(s) => s,
48 }
49 }
50}
51
52impl From<&str> for SailboxStatus {
53 fn from(s: &str) -> SailboxStatus {
54 match s {
55 "running" => SailboxStatus::Running,
56 "paused" => SailboxStatus::Paused,
57 "sleeping" => SailboxStatus::Sleeping,
58 "failed" => SailboxStatus::Failed,
59 "terminated" => SailboxStatus::Terminated,
60 other => SailboxStatus::Other(other.to_string()),
61 }
62 }
63}
64
65impl std::fmt::Display for SailboxStatus {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 f.write_str(self.as_str())
68 }
69}
70
71impl Serialize for SailboxStatus {
72 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
73 serializer.serialize_str(self.as_str())
74 }
75}
76
77impl<'de> Deserialize<'de> for SailboxStatus {
78 fn deserialize<D: serde::Deserializer<'de>>(
79 deserializer: D,
80 ) -> Result<SailboxStatus, D::Error> {
81 Ok(SailboxStatus::from(
82 String::deserialize(deserializer)?.as_str(),
83 ))
84 }
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub enum SailboxStatusFilter {
92 Running,
94 Paused,
96 Sleeping,
98 Failed,
100 Terminated,
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
106pub enum SailboxListOrder {
107 #[default]
109 Activity,
110 CreatedAtDesc,
112}
113
114impl SailboxListOrder {
115 pub fn as_str(&self) -> &'static str {
117 match self {
118 SailboxListOrder::Activity => "activity",
119 SailboxListOrder::CreatedAtDesc => "created_at_desc",
120 }
121 }
122
123 pub fn parse(raw: &str) -> Option<SailboxListOrder> {
125 match raw.trim() {
126 "activity" => Some(SailboxListOrder::Activity),
127 "created_at_desc" => Some(SailboxListOrder::CreatedAtDesc),
128 _ => None,
129 }
130 }
131}
132
133impl SailboxStatusFilter {
134 pub fn as_str(&self) -> &'static str {
136 match self {
137 SailboxStatusFilter::Running => "running",
138 SailboxStatusFilter::Paused => "paused",
139 SailboxStatusFilter::Sleeping => "sleeping",
140 SailboxStatusFilter::Failed => "failed",
141 SailboxStatusFilter::Terminated => "terminated",
142 }
143 }
144
145 pub fn parse(s: &str) -> Option<SailboxStatusFilter> {
147 match s {
148 "running" => Some(SailboxStatusFilter::Running),
149 "paused" => Some(SailboxStatusFilter::Paused),
150 "sleeping" => Some(SailboxStatusFilter::Sleeping),
151 "failed" => Some(SailboxStatusFilter::Failed),
152 "terminated" => Some(SailboxStatusFilter::Terminated),
153 _ => None,
154 }
155 }
156}
157
158#[derive(Debug, Clone, PartialEq, Eq)]
161pub enum ListenerProtocol {
162 Tcp,
164 Http,
166 Other(String),
168}
169
170impl ListenerProtocol {
171 pub fn as_str(&self) -> &str {
173 match self {
174 ListenerProtocol::Tcp => "tcp",
175 ListenerProtocol::Http => "http",
176 ListenerProtocol::Other(s) => s,
177 }
178 }
179}
180
181impl From<&str> for ListenerProtocol {
182 fn from(s: &str) -> ListenerProtocol {
183 match s {
184 "tcp" => ListenerProtocol::Tcp,
185 "http" => ListenerProtocol::Http,
186 other => ListenerProtocol::Other(other.to_string()),
187 }
188 }
189}
190
191impl std::fmt::Display for ListenerProtocol {
192 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
193 f.write_str(self.as_str())
194 }
195}
196
197impl Serialize for ListenerProtocol {
198 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
199 serializer.serialize_str(self.as_str())
200 }
201}
202
203impl<'de> Deserialize<'de> for ListenerProtocol {
204 fn deserialize<D: serde::Deserializer<'de>>(
205 deserializer: D,
206 ) -> Result<ListenerProtocol, D::Error> {
207 Ok(ListenerProtocol::from(
208 String::deserialize(deserializer)?.as_str(),
209 ))
210 }
211}
212
213#[derive(Debug, Clone, PartialEq, Eq)]
216pub enum ListenerRouteStatus {
217 Unspecified,
219 Pending,
221 Active,
223 Restoring,
225 Unavailable,
227 Other(String),
229}
230
231impl ListenerRouteStatus {
232 pub fn as_str(&self) -> &str {
236 match self {
237 ListenerRouteStatus::Unspecified => "unspecified",
238 ListenerRouteStatus::Pending => "pending",
239 ListenerRouteStatus::Active => "active",
240 ListenerRouteStatus::Restoring => "restoring",
241 ListenerRouteStatus::Unavailable => "unavailable",
242 ListenerRouteStatus::Other(s) => s,
243 }
244 }
245}
246
247impl From<&str> for ListenerRouteStatus {
248 fn from(s: &str) -> ListenerRouteStatus {
251 match s {
252 "LISTENER_ROUTE_STATUS_UNSPECIFIED" | "unspecified" => ListenerRouteStatus::Unspecified,
253 "LISTENER_ROUTE_STATUS_PENDING" | "pending" => ListenerRouteStatus::Pending,
254 "LISTENER_ROUTE_STATUS_ACTIVE" | "active" => ListenerRouteStatus::Active,
255 "LISTENER_ROUTE_STATUS_RESTORING" | "restoring" => ListenerRouteStatus::Restoring,
256 "LISTENER_ROUTE_STATUS_UNAVAILABLE" | "unavailable" => ListenerRouteStatus::Unavailable,
257 other => ListenerRouteStatus::Other(other.to_string()),
258 }
259 }
260}
261
262impl std::fmt::Display for ListenerRouteStatus {
263 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
264 f.write_str(self.as_str())
265 }
266}
267
268impl Serialize for ListenerRouteStatus {
269 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
270 serializer.serialize_str(self.as_str())
271 }
272}
273
274impl<'de> Deserialize<'de> for ListenerRouteStatus {
275 fn deserialize<D: serde::Deserializer<'de>>(
276 deserializer: D,
277 ) -> Result<ListenerRouteStatus, D::Error> {
278 Ok(ListenerRouteStatus::from(
279 String::deserialize(deserializer)?.as_str(),
280 ))
281 }
282}
283
284#[derive(Debug, Clone, Copy, PartialEq, Eq)]
288pub enum IngressProtocol {
289 Tcp,
291 Http,
293}
294
295impl IngressProtocol {
296 pub fn as_str(&self) -> &'static str {
298 match self {
299 IngressProtocol::Tcp => "tcp",
300 IngressProtocol::Http => "http",
301 }
302 }
303
304 pub fn parse(s: &str) -> Option<IngressProtocol> {
307 match s {
308 "tcp" => Some(IngressProtocol::Tcp),
309 "http" => Some(IngressProtocol::Http),
310 _ => None,
311 }
312 }
313}
314
315impl std::fmt::Display for IngressProtocol {
316 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
317 f.write_str(self.as_str())
318 }
319}
320
321impl Serialize for IngressProtocol {
322 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
323 serializer.serialize_str(self.as_str())
324 }
325}
326
327#[derive(Debug, Clone, Default)]
329pub struct CheckpointOptions {
330 pub name: Option<String>,
332 pub ttl: Option<std::time::Duration>,
334}
335
336fn normalized_architecture<'de, D: serde::Deserializer<'de>>(
339 deserializer: D,
340) -> Result<String, D::Error> {
341 let raw = String::deserialize(deserializer)?;
342 Ok(match raw.as_str() {
343 "x86_64" | "amd64" => "amd64".to_string(),
344 "aarch64" | "arm64" => "arm64".to_string(),
345 _ => raw,
346 })
347}
348
349#[derive(Debug, Clone, Copy, PartialEq, Eq)]
355pub enum SailboxSize {
356 Small,
359 Medium,
361}
362
363impl SailboxSize {
364 pub fn as_str(&self) -> &'static str {
366 match self {
367 SailboxSize::Small => "s",
368 SailboxSize::Medium => "m",
369 }
370 }
371
372 pub fn parse(s: &str) -> Option<SailboxSize> {
374 match s {
375 "s" => Some(SailboxSize::Small),
376 "m" => Some(SailboxSize::Medium),
377 _ => None,
378 }
379 }
380
381 pub fn allowed() -> &'static [&'static str] {
383 &["s", "m"]
384 }
385}
386
387impl std::fmt::Display for SailboxSize {
388 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
389 f.write_str(self.as_str())
390 }
391}
392
393impl Serialize for SailboxSize {
394 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
395 serializer.serialize_str(self.as_str())
396 }
397}
398
399#[derive(Debug, Clone, Default, Serialize)]
402pub struct SailboxHandle {
403 pub sailbox_id: String,
405 pub name: String,
407 pub status: SailboxStatus,
409 pub worker_address: String,
411 pub exec_endpoint: String,
413}
414
415#[derive(Debug, Clone, Serialize, Deserialize)]
418pub struct SailboxInfo {
419 pub sailbox_id: String,
421 pub app_id: String,
423 pub app_name: String,
425 pub image_id: String,
427 pub name: String,
429 pub status: SailboxStatus,
431 pub memory_mib: i64,
433 pub vcpu_count: i64,
435 pub state_disk_size_gib: i64,
437 pub cpu_requested_vcpu: i64,
439 pub cpu_used_vcpu: f64,
441 pub memory_requested_bytes: i64,
443 pub memory_used_bytes: i64,
445 pub disk_requested_bytes: i64,
447 pub disk_used_bytes: i64,
449 #[serde(deserialize_with = "normalized_architecture", default)]
452 pub architecture: String,
453 #[serde(default)]
455 pub guest_schema_version: Option<i64>,
456 #[serde(default)]
458 pub error_message: Option<String>,
459 pub checkpoint_generation: i64,
461 #[serde(with = "time::serde::rfc3339::option", default)]
463 pub started_at: Option<OffsetDateTime>,
464 #[serde(with = "time::serde::rfc3339::option", default)]
466 pub last_checkpointed_at: Option<OffsetDateTime>,
467 #[serde(with = "time::serde::rfc3339")]
469 pub created_at: OffsetDateTime,
470 #[serde(with = "time::serde::rfc3339")]
472 pub updated_at: OffsetDateTime,
473}
474
475#[derive(Debug, Clone, Serialize)]
477pub struct SailboxPage {
478 pub items: Vec<SailboxInfo>,
480 pub limit: i64,
482 pub offset: i64,
484 pub total: i64,
486 pub has_more: bool,
488}
489
490#[derive(Debug, Clone, Serialize)]
494pub struct SailboxCheckpoint {
495 pub checkpoint_id: String,
497 pub sailbox_id: String,
499 pub checkpoint_generation: i64,
501 #[serde(with = "time::serde::rfc3339::option")]
505 pub expires_at: Option<OffsetDateTime>,
506 pub status: SailboxStatus,
508}
509
510#[derive(Debug, Clone)]
512pub struct ListSailboxesQuery {
513 pub app_id: Option<String>,
515 pub status: Option<SailboxStatusFilter>,
517 pub search: Option<String>,
519 pub max_guest_schema_version: Option<i64>,
521 pub order: SailboxListOrder,
523 pub limit: i64,
525 pub offset: i64,
527}
528
529pub const DEFAULT_LIST_LIMIT: i64 = 50;
532
533impl Default for ListSailboxesQuery {
534 fn default() -> ListSailboxesQuery {
535 ListSailboxesQuery {
536 app_id: None,
537 status: None,
538 search: None,
539 max_guest_schema_version: None,
540 order: SailboxListOrder::Activity,
541 limit: DEFAULT_LIST_LIMIT,
542 offset: 0,
543 }
544 }
545}
546
547#[derive(Debug, Clone, Serialize)]
549pub struct IngressPort {
550 pub guest_port: u32,
552 pub protocol: IngressProtocol,
554 pub allowlist: Vec<String>,
558}
559
560#[derive(Debug, Clone, Serialize)]
562pub struct VolumeMount {
563 pub volume_id: String,
565 pub mount_path: String,
567}
568
569#[derive(Debug, Clone, Serialize, Deserialize)]
572pub struct VolumeInfo {
573 pub volume_id: String,
575 pub name: String,
577 pub backend: String,
579 pub status: String,
581 #[serde(with = "time::serde::rfc3339::option", default)]
583 pub created_at: Option<OffsetDateTime>,
584 #[serde(with = "time::serde::rfc3339::option", default)]
586 pub updated_at: Option<OffsetDateTime>,
587}
588
589#[derive(Debug, Clone, Deserialize)]
594pub(crate) struct AddListenerWire {
595 pub(crate) guest_port: u32,
596 pub(crate) protocol: ListenerProtocol,
597 #[serde(default)]
598 pub(crate) public_url: String,
599 #[serde(default, rename = "tcp_public_host")]
600 pub(crate) public_host: String,
601 #[serde(default, rename = "tcp_public_port")]
602 pub(crate) public_port: u32,
603}
604
605impl From<AddListenerWire> for crate::worker::Listener {
606 fn from(wire: AddListenerWire) -> crate::worker::Listener {
607 crate::worker::Listener {
608 guest_port: wire.guest_port,
609 protocol: wire.protocol,
610 route_status: ListenerRouteStatus::Unspecified,
613 public_url: wire.public_url,
614 public_host: wire.public_host,
615 public_port: wire.public_port,
616 }
617 }
618}
619
620#[derive(Debug, Clone)]
623pub struct IssuedUserCert {
624 pub certificate: String,
626 pub key_id: String,
628}
629
630#[derive(Debug, Clone, PartialEq, Eq)]
633pub enum ListenerEndpoint {
634 Http {
636 url: String,
638 },
639 Tcp {
641 host: String,
643 port: u32,
645 },
646}
647
648impl std::fmt::Display for ListenerEndpoint {
649 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
650 match self {
651 ListenerEndpoint::Http { url } => f.write_str(url),
652 ListenerEndpoint::Tcp { host, port } => write!(f, "{host}:{port}"),
653 }
654 }
655}
656
657#[derive(Debug, Clone)]
661pub struct CreateSailboxRequest {
662 pub app_id: String,
664 pub name: String,
666 pub ingress_ports: Vec<IngressPort>,
668 pub volume_mounts: Vec<VolumeMount>,
670 pub image: crate::image::ImageSpec,
673 pub size: Option<SailboxSize>,
675 pub memory_gib: Option<u32>,
678 pub disk_gib: Option<u32>,
681 pub autosleep_timeout_minutes: Option<i64>,
683 pub ssh: bool,
687}
688
689impl Default for CreateSailboxRequest {
690 fn default() -> CreateSailboxRequest {
693 CreateSailboxRequest {
694 app_id: String::new(),
695 name: String::new(),
696 ingress_ports: Vec::new(),
697 volume_mounts: Vec::new(),
698 image: crate::image::ImageSpec {
699 base: Some(crate::image::BaseImage::Debian),
700 ..crate::image::ImageSpec::default()
701 },
702 size: None,
703 memory_gib: None,
704 disk_gib: None,
705 autosleep_timeout_minutes: None,
706 ssh: false,
707 }
708 }
709}
710
711#[cfg(test)]
712mod tests {
713 use super::*;
714
715 #[test]
716 fn list_query_default_uses_a_valid_nonzero_limit() {
717 let query = ListSailboxesQuery::default();
720 assert_eq!(query.limit, DEFAULT_LIST_LIMIT);
721 assert!(query.limit > 0);
722 assert_eq!(query.offset, 0);
723 }
724}