diff --git a/proto/backend/cluster.proto b/proto/backend/cluster.proto index 976edc5386..f5459d02c1 100644 --- a/proto/backend/cluster.proto +++ b/proto/backend/cluster.proto @@ -21,10 +21,10 @@ message Datacenter { rivet.common.Uuid cluster_id = 2; string name_id = 3; string display_name = 4; - + Provider provider = 5; string provider_datacenter_id = 6; - + repeated Pool pools = 7; BuildDeliveryMethod build_delivery_method = 8; // Nomad drain time in seconds. @@ -63,5 +63,5 @@ message Server { optional int64 cloud_destroy_ts = 7; - // TODO: Add the rest + // TODO: Add the rest of the sql columns } diff --git a/svc/pkg/cluster/db/cluster/migrations/20231201000927_init.up.sql b/svc/pkg/cluster/db/cluster/migrations/20231201000927_init.up.sql index b9a5c4a52d..e6394d5b2a 100644 --- a/svc/pkg/cluster/db/cluster/migrations/20231201000927_init.up.sql +++ b/svc/pkg/cluster/db/cluster/migrations/20231201000927_init.up.sql @@ -8,8 +8,14 @@ CREATE TABLE clusters ( CREATE TABLE datacenters ( datacenter_id UUID PRIMARY KEY, cluster_id UUID NOT NULL REFERENCES clusters (cluster_id), - config BYTES NOT NULL, name_id TEXT NOT NULL, + display_name TEXT NOT NULL, + provider INT NOT NULL, + provider_datacenter_id TEXT NOT NULL, + provider_api_token TEXT, + pools BYTES NOT NULL, + build_delivery_method INT NOT NULL, + drain_timeout INT NOT NULL, UNIQUE (cluster_id, name_id), INDEX (cluster_id) diff --git a/svc/pkg/cluster/ops/datacenter-get/src/lib.rs b/svc/pkg/cluster/ops/datacenter-get/src/lib.rs index 42d37c5aab..cc414987e0 100644 --- a/svc/pkg/cluster/ops/datacenter-get/src/lib.rs +++ b/svc/pkg/cluster/ops/datacenter-get/src/lib.rs @@ -1,6 +1,41 @@ +use std::convert::{TryFrom, TryInto}; + use proto::backend::{self, pkg::*}; use rivet_operation::prelude::*; +#[derive(sqlx::FromRow)] +struct Datacenter { + datacenter_id: Uuid, + cluster_id: Uuid, + name_id: String, + display_name: String, + provider: i64, + provider_datacenter_id: String, + pools: Vec, + build_delivery_method: i64, + drain_timeout: i64, +} + +impl TryFrom for backend::cluster::Datacenter { + type Error = GlobalError; + + fn try_from(value: Datacenter) -> GlobalResult { + let pools = cluster::msg::datacenter_create::Pools::decode(value.pools.as_slice())?.pools; + + Ok(backend::cluster::Datacenter { + datacenter_id: Some(value.datacenter_id.into()), + cluster_id: Some(value.cluster_id.into()), + name_id: value.name_id, + display_name: value.display_name, + provider: value.provider as i32, + provider_datacenter_id: value.provider_datacenter_id, + pools, + build_delivery_method: value.build_delivery_method as i32, + drain_timeout: value.drain_timeout as u64, + }) + } +} + #[operation(name = "cluster-datacenter-get")] pub async fn handle( ctx: OperationContext, @@ -12,23 +47,30 @@ pub async fn handle( .collect::>(); let configs = sql_fetch_all!( - [ctx, (Vec,)] + [ctx, Datacenter] " SELECT - config + datacenter_id, + cluster_id, + name_id, + display_name, + provider, + provider_datacenter_id, + provider_api_token, + pools, + build_delivery_method, + drain_timeout FROM db_cluster.datacenters WHERE datacenter_id = ANY($1) ", - datacenter_ids + datacenter_ids, ) .await?; Ok(cluster::datacenter_get::Response { datacenters: configs .into_iter() - .map(|(config_bytes,)| { - backend::cluster::Datacenter::decode(config_bytes.as_slice()).map_err(Into::into) - }) + .map(TryInto::try_into) .collect::>>()?, }) } diff --git a/svc/pkg/cluster/types/msg/datacenter-create.proto b/svc/pkg/cluster/types/msg/datacenter-create.proto index 8e8419e38d..f74a4d9731 100644 --- a/svc/pkg/cluster/types/msg/datacenter-create.proto +++ b/svc/pkg/cluster/types/msg/datacenter-create.proto @@ -12,3 +12,8 @@ import "proto/backend/cluster.proto"; message Message { rivet.backend.cluster.Datacenter config = 1; } + +// Helper proto for writing to sql +message Pools { + repeated rivet.backend.cluster.Pool pools = 1; +} diff --git a/svc/pkg/cluster/worker/src/workers/datacenter_create.rs b/svc/pkg/cluster/worker/src/workers/datacenter_create.rs index ceedeaf3ef..5079618d9a 100644 --- a/svc/pkg/cluster/worker/src/workers/datacenter_create.rs +++ b/svc/pkg/cluster/worker/src/workers/datacenter_create.rs @@ -16,8 +16,13 @@ async fn worker( } } - let mut config_buf = Vec::with_capacity(config.encoded_len()); - config.encode(&mut config_buf)?; + // Copy pools config to write to db + let pools = cluster::msg::datacenter_create::Pools { + pools: config.pools.clone(), + }; + + let mut pools_buf = Vec::with_capacity(pools.encoded_len()); + pools.encode(&mut pools_buf)?; sql_execute!( [ctx] @@ -25,16 +30,25 @@ async fn worker( INSERT INTO db_cluster.datacenters ( datacenter_id, cluster_id, - config, - name_id + name_id, + display_name, + provider, + provider_datacenter_id, + pools, + build_delivery_method, + drain_timeout ) - VALUES ($1, $2, $3, $4) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ", datacenter_id, cluster_id, - config_buf, - // Datacenters have a unique constraint on name ids &config.name_id, + &config.display_name, + config.provider as i64, + &config.provider_datacenter_id, + pools_buf, + config.build_delivery_method as i64, + config.drain_timeout as i64 ) .await?;