Skip to content

Commit

Permalink
feat: add operation to list all clusters (#717)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
AngelOnFira committed Apr 21, 2024
1 parent ee7547b commit 1f4b169
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 0 deletions.
11 changes: 11 additions & 0 deletions svc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions svc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ members = [
"pkg/cluster/ops/datacenter-topology-get",
"pkg/cluster/ops/get",
"pkg/cluster/ops/get-for-game",
"pkg/cluster/ops/list",
"pkg/cluster/ops/server-get",
"pkg/cluster/ops/server-list",
"pkg/cluster/ops/server-resolve-for-ip",
Expand Down
18 changes: 18 additions & 0 deletions svc/pkg/cluster/ops/list/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "cluster-list"
version = "0.0.1"
edition = "2018"
authors = ["Rivet Gaming, LLC <developer@rivet.gg>"]
license = "Apache-2.0"

[dependencies]
chirp-client = { path = "../../../../../lib/chirp/client" }
prost = "0.10"
rivet-operation = { path = "../../../../../lib/operation/core" }

[dependencies.sqlx]
version = "0.7"
default-features = false

[dev-dependencies]
chirp-worker = { path = "../../../../../lib/chirp/worker" }
10 changes: 10 additions & 0 deletions svc/pkg/cluster/ops/list/Service.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[service]
name = "cluster-list"

[runtime]
kind = "rust"

[operation]

[databases]
db-cluster = {}
46 changes: 46 additions & 0 deletions svc/pkg/cluster/ops/list/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use proto::backend::{self, pkg::*};
use rivet_operation::prelude::*;

#[derive(sqlx::FromRow)]
struct Cluster {
cluster_id: Uuid,
name_id: String,
owner_team_id: Option<Uuid>,
create_ts: i64,
}

impl From<Cluster> for backend::cluster::Cluster {
fn from(value: Cluster) -> Self {
backend::cluster::Cluster {
cluster_id: Some(value.cluster_id.into()),
name_id: value.name_id,
owner_team_id: value.owner_team_id.map(Into::into),
create_ts: value.create_ts,
}
}
}

#[operation(name = "cluster-list")]
pub async fn handle(
ctx: OperationContext<cluster::list::Request>,
) -> GlobalResult<cluster::list::Response> {
let crdb = ctx.crdb().await?;

let cluster_ids = sql_fetch_all!(
[ctx, Cluster, &crdb]
"
SELECT
cluster_id,
name_id,
owner_team_id,
create_ts
FROM db_cluster.clusters
",
)
.await?
.into_iter()
.map(|cluster| cluster.cluster_id.into())
.collect::<Vec<_>>();

Ok(cluster::list::Response { cluster_ids })
}
20 changes: 20 additions & 0 deletions svc/pkg/cluster/ops/list/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use chirp_worker::prelude::*;
use proto::backend::pkg::*;

#[worker_test]
async fn list_single_cluster(ctx: TestCtx) {
let cluster_id = Uuid::new_v4();

msg!([ctx] cluster::msg::create(cluster_id) -> cluster::msg::create_complete {
cluster_id: Some(cluster_id.into()),
name_id: util::faker::ident(),
owner_team_id: None,
})
.await
.unwrap();

let res = op!([ctx] cluster_list {}).await.unwrap();
let new_cluster_id = res.cluster_ids.first().expect("cluster id not found");

assert_eq!(cluster_id, new_cluster_id.as_uuid());
}
12 changes: 12 additions & 0 deletions svc/pkg/cluster/types/list.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package rivet.backend.pkg.cluster.list;

import "proto/common.proto";
import "proto/backend/cluster.proto";

message Request {}

message Response {
repeated rivet.common.Uuid cluster_ids = 1;
}

0 comments on commit 1f4b169

Please sign in to comment.