Skip to content

Commit

Permalink
feat: improved port creation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan committed Jul 9, 2024
1 parent 3180140 commit 8541fd7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 134 deletions.
86 changes: 0 additions & 86 deletions '

This file was deleted.

This file was deleted.

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

16 changes: 13 additions & 3 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kube = { version = "0.92.0", features = ["client"] }
protoc-wkt = "1.0.0"
config = { version = "0.14.0", features = ["toml"] }
rdkafka = "0.36.2"
uuid = { version = "1.10.0", features = ["v4"] }

[dev-dependencies]
mockall = "0.12.1"
Expand Down
22 changes: 6 additions & 16 deletions src/domain/daemon/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@ use kube::{
api::{ApiResource, DynamicObject, ObjectMeta},
ResourceExt,
};
use rand::{distributions::Alphanumeric, Rng};
use std::sync::Arc;
use tracing::info;

use crate::domain::events::PortCreated;

pub async fn create_port(cluster: Arc<dyn PortCluster>, port: PortCreated) -> Result<()> {
let slug: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
let name = format!(
"{}-{}",
port.kind.clone().to_lowercase(),
slug.to_lowercase()
);

let api = ApiResource {
kind: port.kind.clone(),
group: "demeter.run".into(),
Expand All @@ -29,13 +17,13 @@ pub async fn create_port(cluster: Arc<dyn PortCluster>, port: PortCreated) -> Re
api_version: "demeter.run/v1alpha1".into(),
};

let mut obj = DynamicObject::new(&name, &api);
let mut obj = DynamicObject::new(&port.id, &api);
obj.metadata = ObjectMeta {
name: Some(name),
name: Some(port.id),
namespace: Some(port.project),
..Default::default()
};
obj.data = port.resource;
obj.data = serde_json::from_str(&port.data)?;

cluster.create(&obj).await?;

Expand All @@ -53,6 +41,7 @@ pub trait PortCluster: Send + Sync {
#[cfg(test)]
mod tests {
use mockall::mock;
use uuid::Uuid;

use crate::domain::management::port::Port;

Expand All @@ -70,9 +59,10 @@ mod tests {
impl Default for PortCreated {
fn default() -> Self {
Self {
id: Uuid::new_v4().to_string(),
kind: "KupoPort".into(),
project: "prj-xxxxxxx".into(),
resource: "{\"spec\":{\"operatorVersion\":\"1\",\"kupoVersion\":\"v1\",\"network\":\"mainnet\",\"pruneUtxo\":false,\"throughputTier\":\"0\"}}".into()
data: "{\"spec\":{\"operatorVersion\":\"1\",\"kupoVersion\":\"v1\",\"network\":\"mainnet\",\"pruneUtxo\":false,\"throughputTier\":\"0\"}}".into()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/domain/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectCreated {
Expand All @@ -13,9 +12,10 @@ pub struct AccountCreated {
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortCreated {
pub id: String,
pub project: String,
pub kind: String,
pub resource: Value,
pub data: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
26 changes: 21 additions & 5 deletions src/domain/management/port.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Error, Result};
use serde_json::Value;
use std::sync::Arc;
use tracing::info;
use uuid::Uuid;

use crate::domain::events::{Event, EventBridge, PortCreated};

Expand Down Expand Up @@ -32,25 +32,40 @@ pub async fn create_cache(port_cache: Arc<dyn PortCache>, port: PortCreated) ->

#[derive(Debug, Clone)]
pub struct Port {
pub id: String,
pub project: String,
pub kind: String,
pub resource: Value,
pub data: String,
}
impl Port {
pub fn new(project: &str, kind: &str, data: &str) -> Self {
let id = Uuid::new_v4().to_string();

Self {
id,
project: project.into(),
kind: kind.into(),
data: data.into(),
}
}
}
impl From<Port> for PortCreated {
fn from(value: Port) -> Self {
PortCreated {
id: value.id,
project: value.project,
kind: value.kind,
resource: value.resource,
data: value.data,
}
}
}
impl From<PortCreated> for Port {
fn from(value: PortCreated) -> Self {
Port {
id: value.id,
project: value.project,
kind: value.kind,
resource: value.resource,
data: value.data,
}
}
}
Expand Down Expand Up @@ -98,9 +113,10 @@ mod tests {
impl Default for Port {
fn default() -> Self {
Self {
id: Uuid::new_v4().to_string(),
project: "prj-test".into(),
kind: "CardanoNode".into(),
resource: Default::default(),
data: Default::default(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/driven/cache/migrations/20240606_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ CREATE TABLE IF NOT EXISTS projects (
);

CREATE TABLE IF NOT EXISTS ports (
id INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT PRIMARY KEY,
project TEXT NOT NULL,
kind TEXT NOT NULL,
data TEXT NOT NULL,
FOREIGN KEY(project) REFERENCES projects(slug)
);
8 changes: 5 additions & 3 deletions src/driven/cache/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ impl PortCache for SqlitePortCache {
async fn create(&self, port: &Port) -> Result<()> {
sqlx::query!(
r#"
INSERT INTO ports (project, kind)
VALUES ($1, $2)
INSERT INTO ports (id, project, kind, data)
VALUES ($1, $2, $3, $4)
"#,
port.id,
port.project,
port.kind
port.kind,
port.data
)
.execute(&self.sqlite.db)
.await?;
Expand Down
11 changes: 5 additions & 6 deletions src/drivers/grpc/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ impl proto::port_service_server::PortService for PortServiceImpl {
) -> Result<tonic::Response<proto::CreatePortResponse>, tonic::Status> {
let req = request.into_inner();

let port = Port {
kind: req.kind,
project: req.project,
resource: serde_json::from_str(&req.resource).unwrap(),
};
let port = Port::new(&req.project, &req.kind, &req.data);
let result =
management::port::create(self.project_cache.clone(), self.event.clone(), port.clone())
.await;
Expand All @@ -41,7 +37,10 @@ impl proto::port_service_server::PortService for PortServiceImpl {
return Err(Status::failed_precondition(err.to_string()));
}

let message = proto::CreatePortResponse { kind: port.kind };
let message = proto::CreatePortResponse {
id: port.id,
kind: port.kind,
};
Ok(tonic::Response::new(message))
}
}

0 comments on commit 8541fd7

Please sign in to comment.