From a78a52eb6aefa682cc7b4424df53184c124b1355 Mon Sep 17 00:00:00 2001 From: Alexandre Gomez Date: Sat, 20 Aug 2022 21:48:28 +0200 Subject: [PATCH] feat: implement grpc client Signed-off-by: Alexandre Gomez --- controller/lib/src/grpc_client/interface.rs | 124 ++++++++++++++++++++ controller/lib/src/grpc_client/mod.rs | 1 + controller/lib/src/lib.rs | 1 + 3 files changed, 126 insertions(+) create mode 100644 controller/lib/src/grpc_client/interface.rs create mode 100644 controller/lib/src/grpc_client/mod.rs diff --git a/controller/lib/src/grpc_client/interface.rs b/controller/lib/src/grpc_client/interface.rs new file mode 100644 index 00000000..3f93a53b --- /dev/null +++ b/controller/lib/src/grpc_client/interface.rs @@ -0,0 +1,124 @@ +use log::{error, info}; +use proto::scheduler::instance_service_client::InstanceServiceClient; +use proto::scheduler::{Instance, InstanceIdentifier, InstanceStatus}; +use tonic::transport::{Channel, Error}; +use tonic::{Request, Response, Status, Streaming}; + +#[derive(Debug)] +pub enum SchedulerClientInterfaceError { + ConnectionError(Error), + RequestFailed(Status), +} + +pub struct SchedulerClientInterface { + instance_client: InstanceServiceClient, +} + +impl SchedulerClientInterface { + pub async fn new( + instance_client_address: String, + ) -> Result { + info!( + "Starting gRPC client for scheduler Instance Service on {}", + instance_client_address, + ); + + let instance_client = InstanceServiceClient::connect(instance_client_address) + .await + .map_err(SchedulerClientInterfaceError::ConnectionError)?; + + Ok(Self { instance_client }) + } + + pub async fn create_instance( + &mut self, + request: Request, + ) -> Result>, SchedulerClientInterfaceError> { + let remote_address = match request.remote_addr() { + Some(addr) => addr.to_string(), + None => { + error!("\"create_instance\" Failed to get remote address from request"); + "Error getting address".to_string() + } + }; + + info!( + "Calling gRPC procedure \"create_instance\" to {}", + remote_address + ); + + self.instance_client + .create(request) + .await + .map_err(SchedulerClientInterfaceError::RequestFailed) + } + + pub async fn destroy_instance( + &mut self, + request: Request, + ) -> Result, SchedulerClientInterfaceError> { + let remote_address = match request.remote_addr() { + Some(addr) => addr.to_string(), + None => { + error!("\"create_instance\" Failed to get remote address from request"); + "Error getting address".to_string() + } + }; + + info!( + "Calling gRPC procedure \"destroy_instance\" to {}", + remote_address + ); + + self.instance_client + .destroy(request) + .await + .map_err(SchedulerClientInterfaceError::RequestFailed) + } + + pub async fn start_instance( + &mut self, + request: Request, + ) -> Result, SchedulerClientInterfaceError> { + let remote_address = match request.remote_addr() { + Some(addr) => addr.to_string(), + None => { + error!("\"create_instance\" Failed to get remote address from request"); + "Error getting address".to_string() + } + }; + + info!( + "Calling gRPC procedure \"start_instance\" to {}", + remote_address + ); + + self.instance_client + .start(request) + .await + .map_err(SchedulerClientInterfaceError::RequestFailed) + } + + pub async fn stop_instance( + &mut self, + request: Request, + ) -> Result, SchedulerClientInterfaceError> { + let remote_address = match request.remote_addr() { + Some(addr) => addr.to_string(), + None => { + error!("\"create_instance\" Failed to get remote address from request"); + "Error getting address".to_string() + } + }; + + info!( + "Calling gRPC procedure \"stop_instance\" to {}", + remote_address + ); + + self.instance_client + .stop(request) + .await + .map_err(SchedulerClientInterfaceError::RequestFailed) + } +} diff --git a/controller/lib/src/grpc_client/mod.rs b/controller/lib/src/grpc_client/mod.rs new file mode 100644 index 00000000..8d3d626b --- /dev/null +++ b/controller/lib/src/grpc_client/mod.rs @@ -0,0 +1 @@ +pub mod interface; diff --git a/controller/lib/src/lib.rs b/controller/lib/src/lib.rs index 91866edd..1bacc8d0 100644 --- a/controller/lib/src/lib.rs +++ b/controller/lib/src/lib.rs @@ -1,2 +1,3 @@ pub mod external_api; +pub mod grpc_client; pub mod internal_api;