Skip to main content

claw_spawn/domain/
droplet.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Droplet {
6    pub id: i64,
7    pub name: String,
8    pub region: String,
9    pub size: String,
10    pub image: String,
11    pub status: DropletStatus,
12    pub ip_address: Option<String>,
13    pub bot_id: Option<uuid::Uuid>,
14    pub created_at: DateTime<Utc>,
15    pub destroyed_at: Option<DateTime<Utc>>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
19pub enum DropletStatus {
20    New,
21    Active,
22    Off,
23    Destroyed,
24    Error,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct DropletCreateRequest {
29    pub name: String,
30    pub region: String,
31    pub size: String,
32    pub image: String,
33    pub user_data: String,
34    pub tags: Vec<String>,
35}
36
37impl Droplet {
38    pub fn from_do_response(response: DigitalOceanDropletResponse) -> Self {
39        Self {
40            id: response.id,
41            name: response.name,
42            region: response.region.slug,
43            size: response.size_slug,
44            image: response.image.slug.unwrap_or_default(),
45            status: DropletStatus::from_do_status(&response.status),
46            ip_address: response.networks.v4.first().map(|n| n.ip_address.clone()),
47            bot_id: None,
48            created_at: Utc::now(),
49            destroyed_at: None,
50        }
51    }
52}
53
54impl DropletStatus {
55    fn from_do_status(status: &str) -> Self {
56        match status {
57            "new" => DropletStatus::New,
58            "active" => DropletStatus::Active,
59            "off" => DropletStatus::Off,
60            _ => DropletStatus::Error,
61        }
62    }
63}
64
65#[derive(Debug, Deserialize)]
66pub struct DigitalOceanDropletResponse {
67    pub id: i64,
68    pub name: String,
69    pub region: Region,
70    pub size_slug: String,
71    pub image: Image,
72    pub status: String,
73    pub networks: Networks,
74}
75
76#[derive(Debug, Deserialize)]
77pub struct Region {
78    pub slug: String,
79}
80
81#[derive(Debug, Deserialize)]
82pub struct Image {
83    pub slug: Option<String>,
84}
85
86#[derive(Debug, Deserialize)]
87pub struct Networks {
88    pub v4: Vec<NetworkV4>,
89}
90
91#[derive(Debug, Deserialize)]
92pub struct NetworkV4 {
93    pub ip_address: String,
94    pub type_: String,
95}