Skip to content

Commit

Permalink
feat: headroom provisioner models (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd authored Jun 14, 2024
1 parent fbc011a commit 0de2730
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
11 changes: 5 additions & 6 deletions backends/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize};

/// Used by the runner service to send requests to control plane, where the requested resources
/// will be provisioned.
#[derive(Serialize, Deserialize)]
pub struct ResourceRequest {
/// The resource input returned from the runtime::load call.
pub resources: Vec<Vec<u8>>,
/// Used by the control plane to return provisioned resource data to the runner.
#[derive(Serialize, Deserialize, Clone)]
pub struct ResourceResponse {
/// The resource output returned from provisioning.
pub resource: Vec<u8>,
}
3 changes: 3 additions & 0 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ impl Shuttle {
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: res,
custom: shuttle_resource.custom,
state: None
})
.unwrap();
}
Expand All @@ -1385,6 +1386,7 @@ impl Shuttle {
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: secrets.clone(),
custom: shuttle_resource.custom,
state: None
})
.unwrap();
}
Expand All @@ -1403,6 +1405,7 @@ impl Shuttle {
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: res,
custom: shuttle_resource.custom,
state: None
})
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extract_propagation = [
]
models = ["async-trait", "reqwest", "service"]
persist = ["sqlx", "rand"]
sqlx = ["dep:sqlx", "sqlx/sqlite"]
sqlx = ["dep:sqlx", "sqlx/sqlite", "sqlx/postgres"]
service = ["chrono/serde", "display", "tracing", "tracing-subscriber", "uuid"]
test-utils = ["wiremock"]
tonic = ["dep:tonic"]
Expand Down
45 changes: 43 additions & 2 deletions common/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ pub enum ResourceInput {
Custom(Value),
}

/// The resource state represents the stage of the provisioning process the resource is in.
#[derive(
Debug, Clone, PartialEq, Eq, strum::Display, strum::EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "lowercase")]
pub enum ResourceState {
Authorizing,
Provisioning,
Failed,
Ready,
Deleting,
}

/// Returned when provisioning a Shuttle resource
#[derive(Serialize, Deserialize)]
pub struct ShuttleResourceOutput<T> {
Expand All @@ -49,6 +62,9 @@ pub struct ShuttleResourceOutput<T> {

/// Arbitrary extra data in this resource
pub custom: Value,

/// The state of the resource.
pub state: Option<ResourceState>,
}

/// Common type to hold all the information we need for a generic resource
Expand Down Expand Up @@ -136,11 +152,12 @@ mod _sqlx {
use std::{borrow::Cow, str::FromStr};

use sqlx::{
postgres::{PgArgumentBuffer, PgValueRef},
sqlite::{SqliteArgumentValue, SqliteValueRef},
Database, Sqlite,
Database, Postgres, Sqlite,
};

use super::Type;
use super::{ResourceState, Type};

impl<DB: Database> sqlx::Type<DB> for Type
where
Expand All @@ -166,6 +183,30 @@ mod _sqlx {
Self::from_str(value).map_err(Into::into)
}
}

impl<DB: sqlx::Database> sqlx::Type<DB> for ResourceState
where
str: sqlx::Type<DB>,
{
fn type_info() -> <DB as sqlx::Database>::TypeInfo {
<str as sqlx::Type<DB>>::type_info()
}
}

impl<'q> sqlx::Encode<'q, sqlx::Postgres> for ResourceState {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull {
<&str as sqlx::Encode<Postgres>>::encode(&self.to_string(), buf)
}
}

impl<'r> sqlx::Decode<'r, Postgres> for ResourceState {
fn decode(value: PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
let value = <&str as sqlx::Decode<Postgres>>::decode(value)?;

let state = ResourceState::from_str(value)?;
Ok(state)
}
}
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions deployer/src/deployment/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ async fn provision(
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output,
custom: shuttle_resource.custom,
state: None
})
.expect("to serialize struct");
}
Expand All @@ -549,6 +550,7 @@ async fn provision(
*bytes = serde_json::to_vec(&ShuttleResourceOutput {
output: new_secrets.clone(),
custom: shuttle_resource.custom,
state: None
})
.expect("to serialize struct");
}
Expand Down

0 comments on commit 0de2730

Please sign in to comment.