Skip to content

Commit

Permalink
feat(svc): resolve cluster name id op (#751)
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 May 2, 2024
1 parent 27ab366 commit 58200ec
Show file tree
Hide file tree
Showing 7 changed files with 123 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 @@ -69,6 +69,7 @@ members = [
"pkg/cluster/ops/get",
"pkg/cluster/ops/get-for-game",
"pkg/cluster/ops/list",
"pkg/cluster/ops/resolve-for-name-id",
"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/resolve-for-name-id/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "cluster-resolve-for-name-id"
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/resolve-for-name-id/Service.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[service]
name = "cluster-resolve-for-name-id"

[runtime]
kind = "rust"

[operation]

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

#[derive(sqlx::FromRow)]
struct Cluster {
cluster_id: Uuid,
name_id: String,
}

#[operation(name = "cluster-resolve-for-name-id")]
pub async fn handle(
ctx: OperationContext<cluster::resolve_for_name_id::Request>,
) -> GlobalResult<cluster::resolve_for_name_id::Response> {
let clusters = sql_fetch_all!(
[ctx, Cluster]
"
SELECT
cluster_id,
name_id
FROM db_cluster.clusters
WHERE
name_id = ANY($1)
",
&ctx.name_ids,
)
.await?
.into_iter()
.map(|dc| cluster::resolve_for_name_id::response::Cluster {
cluster_id: Some(dc.cluster_id.into()),
name_id: dc.name_id,
})
.collect::<Vec<_>>();

Ok(cluster::resolve_for_name_id::Response { clusters })
}
29 changes: 29 additions & 0 deletions svc/pkg/cluster/ops/resolve-for-name-id/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use chirp_worker::prelude::*;
use proto::backend::{self, pkg::*};

#[worker_test]
async fn empty(ctx: TestCtx) {
let cluster_id = Uuid::new_v4();
let name_id = util::faker::ident();

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

let res = op!([ctx] cluster_resolve_for_name_id {
name_ids: vec![name_id],
})
.await
.unwrap();

let cluster = res.clusters.first().expect("cluster not found");
assert_eq!(
cluster_id,
cluster.cluster_id.unwrap().as_uuid(),
"wrong cluster returned"
);
}
19 changes: 19 additions & 0 deletions svc/pkg/cluster/types/resolve-for-name-id.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";

package rivet.backend.pkg.cluster.resolve_for_name_id;

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

message Request {
repeated string name_ids = 1;
}

message Response {
message Cluster {
rivet.common.Uuid cluster_id = 1;
string name_id = 2;
}

repeated Cluster clusters = 1;
}

0 comments on commit 58200ec

Please sign in to comment.