Skip to content

Commit

Permalink
feat: implement grpc client
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Gomez <[email protected]>
  • Loading branch information
Roxxas96 committed Aug 23, 2022
1 parent 225a53a commit 62f91da
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
124 changes: 124 additions & 0 deletions controller/lib/src/grpc_client/interface.rs
Original file line number Diff line number Diff line change
@@ -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<Channel>,
}

impl SchedulerClientInterface {
pub async fn new(
instance_client_address: String,
) -> Result<Self, SchedulerClientInterfaceError> {
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<Instance>,
) -> Result<Response<Streaming<InstanceStatus>>, 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<InstanceIdentifier>,
) -> Result<Response<()>, 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<InstanceIdentifier>,
) -> Result<Response<()>, 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<InstanceIdentifier>,
) -> Result<Response<()>, 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)
}
}
1 change: 1 addition & 0 deletions controller/lib/src/grpc_client/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod interface;
1 change: 1 addition & 0 deletions controller/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod external_api;
pub mod grpc_client;
pub mod internal_api;

0 comments on commit 62f91da

Please sign in to comment.