Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: update resources with resourcebuilder changes #747

Merged
merged 8 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion common/src/models/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use comfy_table::{
};
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display};
use std::{collections::HashMap, fmt::Display, path::PathBuf};
use uuid::Uuid;

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -130,6 +130,8 @@ pub fn get_resources_table(resources: &Vec<resource::Response>) -> String {
let title = match x.r#type {
Type::Database(_) => "Databases",
Type::Secrets => "Secrets",
Type::StaticFolder => "Static Folder",
Type::Persist => "Persist",
};

let elements = acc.entry(title).or_insert(Vec::new());
Expand All @@ -148,6 +150,14 @@ pub fn get_resources_table(resources: &Vec<resource::Response>) -> String {
output.push(get_secrets_table(secrets));
};

if let Some(static_folders) = resource_groups.get("Static Folder") {
output.push(get_static_folder_table(static_folders));
};

if let Some(persist) = resource_groups.get("Persist") {
output.push(get_persist_table(persist));
};

output.join("\n")
}
}
Expand Down Expand Up @@ -200,3 +210,52 @@ fn get_secrets_table(secrets: &[&resource::Response]) -> String {
"#,
)
}

fn get_static_folder_table(static_folders: &[&resource::Response]) -> String {
let mut table = Table::new();

table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("Static Folders").set_alignment(CellAlignment::Center)
]);

for folder in static_folders {
let path = serde_json::from_value::<PathBuf>(folder.data.clone())
.unwrap()
.display()
.to_string();

table.add_row(vec![path]);
}

format!(
r#"These static folders can be accessed by the service
{table}
"#,
)
}

fn get_persist_table(persist_instances: &[&resource::Response]) -> String {
let mut table = Table::new();

table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("Persist Instances").set_alignment(CellAlignment::Center)
]);

for _ in persist_instances {
table.add_row(vec!["Instance"]);
}

format!(
r#"These instances are linked to this service
{table}
"#,
)
}
4 changes: 4 additions & 0 deletions common/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct Response {
pub enum Type {
Database(database::Type),
Secrets,
StaticFolder,
Persist,
}

impl Response {
Expand All @@ -44,6 +46,8 @@ impl Display for Type {
match self {
Type::Database(db_type) => write!(f, "database::{db_type}"),
Type::Secrets => write!(f, "secrets"),
Type::StaticFolder => write!(f, "static_folder"),
Type::Persist => write!(f, "persist"),
}
}
}
8 changes: 8 additions & 0 deletions deployer/src/persistence/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ impl From<Resource> for shuttle_common::resource::Response {
pub enum Type {
Database(DatabaseType),
Secrets,
StaticFolder,
Persist,
}

impl From<Type> for shuttle_common::resource::Type {
fn from(r#type: Type) -> Self {
match r#type {
Type::Database(r#type) => Self::Database(r#type.into()),
Type::Secrets => Self::Secrets,
Type::StaticFolder => Self::StaticFolder,
Type::Persist => Self::Persist,
}
}
}
Expand All @@ -56,6 +60,8 @@ impl From<shuttle_common::resource::Type> for Type {
match r#type {
shuttle_common::resource::Type::Database(r#type) => Self::Database(r#type.into()),
shuttle_common::resource::Type::Secrets => Self::Secrets,
shuttle_common::resource::Type::StaticFolder => Self::StaticFolder,
shuttle_common::resource::Type::Persist => Self::Persist,
}
}
}
Expand All @@ -65,6 +71,8 @@ impl Display for Type {
match self {
Type::Database(db_type) => write!(f, "database::{db_type}"),
Type::Secrets => write!(f, "secrets"),
Type::StaticFolder => write!(f, "static folder"),
Type::Persist => write!(f, "persist"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions resources/aws-rds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ keywords = ["shuttle-service", "rds"]
[dependencies]
async-trait = "0.1.56"
paste = "1.0.7"
serde = { version = "1.0.148", features = ["derive"] }
shuttle-service = { path = "../../service", version = "0.12.0", default-features = false }
sqlx = { version = "0.6.2", features = ["runtime-tokio-native-tls"] }

Expand Down
45 changes: 34 additions & 11 deletions resources/aws-rds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

use async_trait::async_trait;
use paste::paste;
use serde::{Deserialize, Serialize};
use shuttle_service::{
database::{AwsRdsEngine, Type},
database::{self, AwsRdsEngine},
error::CustomError,
Factory, ResourceBuilder,
Factory, ResourceBuilder, Type,
};

#[derive(Deserialize, Serialize)]
pub enum AwsRdsOutput {
Rds(shuttle_service::DatabaseReadyInfo),
Local(String),
}

macro_rules! aws_engine {
($feature:expr, $pool_path:path, $options_path:path, $struct_ident:ident) => {
paste! {
#[derive(Serialize)]
#[cfg(feature = $feature)]
#[doc = "A resource connected to an AWS RDS " $struct_ident " instance"]
pub struct $struct_ident{
Expand All @@ -21,28 +29,43 @@ macro_rules! aws_engine {
#[doc = "Gets a `sqlx::Pool` connected to an AWS RDS " $struct_ident " instance"]
#[async_trait]
impl ResourceBuilder<$pool_path> for $struct_ident {
const TYPE: Type = Type::Database(database::Type::AwsRds(AwsRdsEngine::$struct_ident));

type Output = AwsRdsOutput;

fn new() -> Self {
Self { local_uri: None }
}

async fn build(self, factory: &mut dyn Factory) -> Result<$pool_path, shuttle_service::Error> {
let connection_string = match factory.get_environment() {
shuttle_service::Environment::Production => {
async fn output(self, factory: &mut dyn Factory) -> Result<Self::Output, shuttle_service::Error> {
let info = match factory.get_environment() {
shuttle_service::Environment::Production => AwsRdsOutput::Rds(
factory
.get_db_connection_string(Type::AwsRds(AwsRdsEngine::$struct_ident))
.get_db_connection(database::Type::AwsRds(AwsRdsEngine::$struct_ident))
.await?
}
),
shuttle_service::Environment::Local => {
if let Some(local_uri) = self.local_uri {
local_uri
AwsRdsOutput::Local(local_uri)
} else {
factory
.get_db_connection_string(Type::AwsRds(AwsRdsEngine::$struct_ident))
.await?
AwsRdsOutput::Rds(
factory
.get_db_connection(database::Type::AwsRds(AwsRdsEngine::$struct_ident))
.await?
)
}
}
};

Ok(info)
}

async fn build(build_data: &Self::Output) -> Result<$pool_path, shuttle_service::Error> {
let connection_string = match build_data {
AwsRdsOutput::Local(local_uri) => local_uri.clone(),
AwsRdsOutput::Rds(info) => info.connection_string_private(),
};

let pool = $options_path::new()
.min_connections(1)
.max_connections(5)
Expand Down
17 changes: 14 additions & 3 deletions resources/persist/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use async_trait::async_trait;
use bincode::{deserialize_from, serialize_into, Error as BincodeError};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use shuttle_common::project::ProjectName;
use shuttle_service::Type;
use shuttle_service::{Factory, ResourceBuilder};
use std::fs;
use std::fs::File;
Expand All @@ -23,8 +24,10 @@ pub enum PersistError {
Deserialize(BincodeError),
}

#[derive(Serialize)]
pub struct Persist;

#[derive(Deserialize, Serialize, Clone)]
pub struct PersistInstance {
service_name: ProjectName,
}
Expand Down Expand Up @@ -66,18 +69,26 @@ impl PersistInstance {

#[async_trait]
impl ResourceBuilder<PersistInstance> for Persist {
const TYPE: Type = Type::Persist;

type Output = PersistInstance;

fn new() -> Self {
Self {}
}

async fn build(
async fn output(
self,
factory: &mut dyn Factory,
) -> Result<PersistInstance, shuttle_service::Error> {
) -> Result<Self::Output, shuttle_service::Error> {
Ok(PersistInstance {
service_name: factory.get_service_name(),
})
}

async fn build(build_data: &Self::Output) -> Result<PersistInstance, shuttle_service::Error> {
Ok(build_data.clone())
}
}

#[cfg(test)]
Expand Down
Loading