forked from ghe0/brain-mock
boilerplate rust code
This commit is contained in:
parent
c729f47b8d
commit
04d1c53a97
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
1124
Cargo.lock
generated
Normal file
1124
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "brain-mock"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
prost = "0.13.4"
|
||||
prost-types = "0.13.4"
|
||||
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] }
|
||||
tokio-stream = "0.1.17"
|
||||
tonic = "0.12"
|
||||
|
||||
[build-dependencies]
|
||||
tonic-build = "0.12"
|
6
build.rs
Normal file
6
build.rs
Normal file
@ -0,0 +1,6 @@
|
||||
fn main() {
|
||||
tonic_build::configure()
|
||||
.build_server(true)
|
||||
.compile_protos(&["brain.proto"], &["proto"])
|
||||
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
|
||||
}
|
118
src/data.rs
Normal file
118
src/data.rs
Normal file
@ -0,0 +1,118 @@
|
||||
#![allow(dead_code)]
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub struct BrainData {
|
||||
pub nodes: RwLock<Vec<Node>>,
|
||||
pub contracts: RwLock<Vec<Contract>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Node {
|
||||
pub public_key: String,
|
||||
pub owner_key: String,
|
||||
pub hostname: String,
|
||||
pub avail_mem_mb: u32,
|
||||
pub avail_vcpus: u32,
|
||||
pub avail_storage_gbs: u32,
|
||||
pub avail_ipv4: u32,
|
||||
pub avail_ipv6: u32,
|
||||
pub avail_ports: u32,
|
||||
pub max_ports_per_vm: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Contract {
|
||||
pub uuid: String,
|
||||
pub hostname: String,
|
||||
pub admin_pubkey: String,
|
||||
pub node_pubkey: String,
|
||||
pub exposed_ports: Vec<u32>,
|
||||
pub public_ipv4: String,
|
||||
pub public_ipv6: String,
|
||||
pub disk_size_gb: u32,
|
||||
pub vcpus: u32,
|
||||
pub memory_mb: u32,
|
||||
pub kernel_sha: String,
|
||||
pub dtrfs_sha: String,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
impl BrainData {
|
||||
pub fn new() -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
nodes: RwLock::new(Vec::new()),
|
||||
contracts: RwLock::new(Vec::new()),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn insert_node(&self, node: Node) {
|
||||
let mut nodes = self.nodes.write().await;
|
||||
nodes.push(node);
|
||||
}
|
||||
|
||||
pub async fn insert_contract(&self, contract: Contract) {
|
||||
let mut contracts = self.contracts.write().await;
|
||||
contracts.push(contract);
|
||||
}
|
||||
|
||||
pub async fn find_contract_by_pubkey(&self, public_key: &str) -> Option<Node> {
|
||||
let nodes = self.nodes.read().await;
|
||||
nodes.iter().cloned().find(|n| n.public_key == public_key)
|
||||
}
|
||||
|
||||
pub async fn find_contract_by_owner_key(&self, owner_key: &str) -> Option<Node> {
|
||||
let nodes = self.nodes.read().await;
|
||||
nodes.iter().cloned().find(|n| n.owner_key == owner_key)
|
||||
}
|
||||
|
||||
pub async fn find_nodes_by_avail_vcpus(&self, min_vcpus: u32) -> Vec<Node> {
|
||||
let nodes = self.nodes.read().await;
|
||||
nodes
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|n| n.avail_vcpus >= min_vcpus)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn find_nodes_by_avail_mem(&self, min_mem_mb: u32) -> Vec<Node> {
|
||||
let nodes = self.nodes.read().await;
|
||||
nodes
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|n| n.avail_mem_mb >= min_mem_mb)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn find_nodes_by_avail_storage(&self, min_storage_gb: u32) -> Vec<Node> {
|
||||
let nodes = self.nodes.read().await;
|
||||
nodes
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|n| n.avail_storage_gbs >= min_storage_gb)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn find_contract_by_uuid(&self, uuid: &str) -> Option<Contract> {
|
||||
let contracts = self.contracts.read().await;
|
||||
contracts.iter().cloned().find(|c| c.uuid == uuid)
|
||||
}
|
||||
|
||||
pub async fn find_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<Contract> {
|
||||
let contracts = self.contracts.read().await;
|
||||
contracts
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|c| c.admin_pubkey == admin_pubkey)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn find_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<Contract> {
|
||||
let contracts = self.contracts.read().await;
|
||||
contracts
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|c| c.node_pubkey == node_pubkey)
|
||||
.collect()
|
||||
}
|
||||
}
|
85
src/grpc.rs
Normal file
85
src/grpc.rs
Normal file
@ -0,0 +1,85 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub mod brain {
|
||||
tonic::include_proto!("brain");
|
||||
}
|
||||
|
||||
use crate::data::BrainData;
|
||||
use brain::brain_cli_service_server::BrainCliService;
|
||||
use brain::brain_daemon_service_server::BrainDaemonService;
|
||||
use brain::*;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use tokio_stream::Stream;
|
||||
use tonic::{Request, Response, Status, Streaming};
|
||||
|
||||
struct BrainDaemonMock {
|
||||
data: Arc<BrainData>,
|
||||
}
|
||||
struct BrainCliMock {
|
||||
data: Arc<BrainData>,
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl BrainDaemonService for BrainDaemonMock {
|
||||
async fn register_node(
|
||||
&self,
|
||||
_req: Request<RegisterNodeRequest>,
|
||||
) -> Result<Response<Empty>, Status> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
type NewVMUpdatesStream = Pin<Box<dyn Stream<Item = Result<NewVmRequest, Status>> + Send>>;
|
||||
|
||||
async fn new_vm_updates(
|
||||
&self,
|
||||
_req: Request<Streaming<NewVmConfirmation>>,
|
||||
) -> Result<Response<Self::NewVMUpdatesStream>, Status> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
type DeletedVMUpdatesStream =
|
||||
Pin<Box<dyn Stream<Item = Result<DeletedVmUpdate, Status>> + Send>>;
|
||||
|
||||
async fn deleted_vm_updates(
|
||||
&self,
|
||||
_req: Request<Empty>,
|
||||
) -> Result<Response<Self::DeletedVMUpdatesStream>, Status> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
type ListVMContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
|
||||
|
||||
async fn list_vm_contracts(
|
||||
&self,
|
||||
_req: Request<Empty>,
|
||||
) -> Result<Response<Self::ListVMContractsStream>, Status> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl BrainCliService for BrainCliMock {
|
||||
async fn create_vm_contract(
|
||||
&self,
|
||||
_req: Request<NewVmRequest>,
|
||||
) -> Result<Response<NewVmConfirmation>, Status> {
|
||||
todo!();
|
||||
}
|
||||
|
||||
type ListVMContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
|
||||
async fn list_vm_contracts(
|
||||
&self,
|
||||
_req: Request<Empty>,
|
||||
) -> Result<Response<Self::ListVMContractsStream>, Status> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
type ListNodesStream = Pin<Box<dyn Stream<Item = Result<NodeListResponse, Status>> + Send>>;
|
||||
async fn list_nodes(
|
||||
&self,
|
||||
_req: Request<NodeFilters>,
|
||||
) -> Result<Response<Self::ListNodesStream>, tonic::Status> {
|
||||
todo!()
|
||||
}
|
||||
}
|
5
src/main.rs
Normal file
5
src/main.rs
Normal file
@ -0,0 +1,5 @@
|
||||
mod grpc;
|
||||
mod data;
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
Loading…
Reference in New Issue
Block a user