|  | 
| 1 | 1 | //! Structures stored to the database. | 
| 2 | 2 | 
 | 
| 3 | 3 | use super::schema::{ | 
| 4 |  | -    disk, instance, metricproducer, networkinterface, oximeter, project, vpc, | 
| 5 |  | -    vpcsubnet, | 
|  | 4 | +    disk, instance, metricproducer, networkinterface, oximeter, project, sled, | 
|  | 5 | +    vpc, vpcsubnet, | 
| 6 | 6 | }; | 
| 7 | 7 | use chrono::{DateTime, Utc}; | 
| 8 | 8 | use diesel::backend::{Backend, RawValue}; | 
| @@ -33,20 +33,60 @@ impl Into<external::Rack> for Rack { | 
| 33 | 33 |     } | 
| 34 | 34 | } | 
| 35 | 35 | 
 | 
| 36 |  | -// NOTE: This object is not currently stored in the database. | 
| 37 |  | -// | 
| 38 |  | -// However, it likely will be in the future. At the moment, | 
| 39 |  | -// Nexus simply reports all the live connections it knows about. | 
|  | 36 | +/// Database representation of a Sled. | 
|  | 37 | +#[derive(Queryable, Identifiable, Insertable, Debug, Clone)] | 
|  | 38 | +#[table_name = "sled"] | 
| 40 | 39 | pub struct Sled { | 
| 41 |  | -    pub identity: IdentityMetadata, | 
| 42 |  | -    pub service_address: SocketAddr, | 
|  | 40 | +    // IdentityMetadata | 
|  | 41 | +    pub id: Uuid, | 
|  | 42 | +    pub time_created: DateTime<Utc>, | 
|  | 43 | +    pub time_modified: DateTime<Utc>, | 
|  | 44 | +    pub time_deleted: Option<DateTime<Utc>>, | 
|  | 45 | + | 
|  | 46 | +    // ServiceAddress (Sled Agent). | 
|  | 47 | +    pub ip: ipnetwork::IpNetwork, | 
|  | 48 | +    pub port: i32, | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +impl Sled { | 
|  | 52 | +    pub fn new( | 
|  | 53 | +        id: Uuid, | 
|  | 54 | +        addr: SocketAddr, | 
|  | 55 | +        params: external::IdentityMetadataCreateParams, | 
|  | 56 | +    ) -> Self { | 
|  | 57 | +        let identity = IdentityMetadata::new(id, params); | 
|  | 58 | +        Self { | 
|  | 59 | +            id, | 
|  | 60 | +            time_created: identity.time_created, | 
|  | 61 | +            time_modified: identity.time_modified, | 
|  | 62 | +            time_deleted: identity.time_deleted, | 
|  | 63 | +            ip: addr.ip().into(), | 
|  | 64 | +            port: addr.port().into(), | 
|  | 65 | +        } | 
|  | 66 | +    } | 
|  | 67 | + | 
|  | 68 | +    pub fn id(&self) -> &Uuid { | 
|  | 69 | +        &self.id | 
|  | 70 | +    } | 
|  | 71 | + | 
|  | 72 | +    pub fn address(&self) -> SocketAddr { | 
|  | 73 | +        // TODO: avoid this unwrap | 
|  | 74 | +        SocketAddr::new(self.ip.ip(), u16::try_from(self.port).unwrap()) | 
|  | 75 | +    } | 
| 43 | 76 | } | 
| 44 | 77 | 
 | 
| 45 | 78 | impl Into<external::Sled> for Sled { | 
| 46 | 79 |     fn into(self) -> external::Sled { | 
|  | 80 | +        let service_address = self.address(); | 
| 47 | 81 |         external::Sled { | 
| 48 |  | -            identity: self.identity.into(), | 
| 49 |  | -            service_address: self.service_address, | 
|  | 82 | +            identity: external::IdentityMetadata { | 
|  | 83 | +                id: self.id, | 
|  | 84 | +                name: external::Name::try_from("sled").unwrap(), | 
|  | 85 | +                description: "sled description".to_string(), | 
|  | 86 | +                time_created: self.time_created, | 
|  | 87 | +                time_modified: self.time_modified, | 
|  | 88 | +            }, | 
|  | 89 | +            service_address, | 
| 50 | 90 |         } | 
| 51 | 91 |     } | 
| 52 | 92 | } | 
|  | 
0 commit comments