Skip to content

Commit

Permalink
Refactored error handling of CARL's store_/delete_cluster_deployment.
Browse files Browse the repository at this point in the history
  • Loading branch information
kKdH committed Dec 21, 2023
1 parent 69284cf commit 686ed87
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,63 +138,83 @@ message ListClusterConfigurationsFailure {}
// StoreClusterDeployment
//
message StoreClusterDeploymentRequest {
opendut.types.cluster.ClusterDeployment deployment = 1;
opendut.types.cluster.ClusterDeployment cluster_deployment = 1;
}

message StoreClusterDeploymentResponse {
oneof result {
oneof reply {
StoreClusterDeploymentFailure failure = 1;
StoreClusterDeploymentSuccess success = 15;
}
}

message StoreClusterDeploymentSuccess {}
message StoreClusterDeploymentSuccess {
opendut.types.cluster.ClusterId cluster_id = 1;
}

message StoreClusterDeploymentFailure {
oneof error {
StoreClusterDeploymentFailureIllegalClusterState illegal_cluster_state = 1;
StoreClusterDeploymentFailureInternal internal = 2;
}
}

message StoreClusterDeploymentFailureIllegalClusterState {
opendut.types.cluster.ClusterId cluster_id = 1;
opendut.types.cluster.ClusterName cluster_name = 2;
opendut.types.cluster.ClusterState actual_state = 3;
repeated opendut.types.cluster.ClusterState required_states = 4;
}

message StoreClusterDeploymentFailure {}
message StoreClusterDeploymentFailureInternal {
opendut.types.cluster.ClusterId cluster_id = 1;
opendut.types.cluster.ClusterName cluster_name = 2;
string cause = 3;
}

//
// DeleteClusterDeployment
//
message DeleteClusterDeploymentRequest {
opendut.types.cluster.ClusterId id = 1;
opendut.types.cluster.ClusterId cluster_id = 1;
}

message DeleteClusterDeploymentResponse {
oneof result {
oneof reply {
DeleteClusterDeploymentFailure failure = 1;
DeleteClusterDeploymentSuccess success = 15;
}
}

message DeleteClusterDeploymentSuccess {
opendut.types.cluster.ClusterDeployment deployment = 1;
opendut.types.cluster.ClusterDeployment cluster_deployment = 1;
}

message DeleteClusterDeploymentFailure {
oneof reason {
DeleteClusterDeploymentFailureClusterIdRequired cluster_id_required = 1;
DeleteClusterDeploymentFailureInvalidClusterId invalid_cluster_id = 2;
DeleteClusterDeploymentFailureNotFound cluster_not_found = 3;
DeleteClusterDeploymentFailureInternal internal = 4;
oneof error {
DeleteClusterDeploymentFailureClusterDeploymentNotFound cluster_deployment_not_found = 1;
DeleteClusterDeploymentFailureIllegalClusterState illegal_cluster_state = 2;
DeleteClusterDeploymentFailureInternal internal = 3;
}
}

message DeleteClusterDeploymentFailureClusterIdRequired {}

message DeleteClusterDeploymentFailureInvalidClusterId {
string cause = 1;
message DeleteClusterDeploymentFailureClusterDeploymentNotFound {
opendut.types.cluster.ClusterId cluster_id = 1;
}

message DeleteClusterDeploymentFailureNotFound {
opendut.types.cluster.ClusterId id = 1;
message DeleteClusterDeploymentFailureIllegalClusterState {
opendut.types.cluster.ClusterId cluster_id = 1;
opendut.types.cluster.ClusterName cluster_name = 2;
opendut.types.cluster.ClusterState actual_state = 3;
repeated opendut.types.cluster.ClusterState required_states = 4;
}

message DeleteClusterDeploymentFailureInternal {
opendut.types.cluster.ClusterId id = 1;
string cause = 2;
opendut.types.cluster.ClusterId cluster_id = 1;
opendut.types.cluster.ClusterName cluster_name = 2;
string cause = 3;
}


//
// ListClusterDeployments
//
Expand Down
105 changes: 53 additions & 52 deletions opendut-carl/opendut-carl-api/src/carl/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(any(feature = "client", feature = "wasm-client"))]
pub use client::*;

use opendut_types::cluster::{ClusterId, ClusterName};
use opendut_types::cluster::state::{ClusterState, ClusterStates};

Expand Down Expand Up @@ -54,28 +53,42 @@ pub struct ListClusterConfigurationsError {
message: String,
}


#[derive(thiserror::Error, Debug)]
#[error("{message}")]
pub struct StoreClusterDeploymentError {
message: String,
pub enum StoreClusterDeploymentError {
#[error("ClusterDeployment for cluster '{cluster_name}' <{cluster_id}> cannot be changed when cluster is in state '{actual_state}'! A cluster can be updated when: {required_states}")]
IllegalClusterState {
cluster_id: ClusterId,
cluster_name: ClusterName,
actual_state: ClusterState,
required_states: ClusterStates,
},
#[error("ClusterDeployment for cluster '{cluster_name}' <{cluster_id}> could not be changed, due to internal errors:\n {cause}")]
Internal {
cluster_id: ClusterId,
cluster_name: ClusterName,
cause: String
}
}

#[derive(thiserror::Error, Debug)]
pub enum DeleteClusterDeploymentError {
#[error("Invalid ClusterId: {cause}")]
InvalidClusterId {
cause: String
#[error("ClusterDeployment for cluster <{cluster_id}> could not be deleted, because a ClusterDeployment with that id does not exist!")]
ClusterDeploymentNotFound {
cluster_id: ClusterId
},
#[error("Unknown cluster id <{id}>")]
ClusterNotFound {
id: ClusterId
#[error("ClusterDeployment for cluster '{cluster_name}' <{cluster_id}> cannot be deleted when cluster is in state '{actual_state}'! A peer can be deleted when: {required_states}")]
IllegalClusterState {
cluster_id: ClusterId,
cluster_name: ClusterName,
actual_state: ClusterState,
required_states: ClusterStates,
},
#[error("Internal error when deleting cluster with id <{id}>.\n{cause}")]
#[error("ClusterDeployment for cluster '{cluster_name}' <{cluster_id}> deleted with internal errors:\n {cause}")]
Internal {
id: ClusterId,
cause: String,
},
cluster_id: ClusterId,
cluster_name: ClusterName,
cause: String
}
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -150,8 +163,8 @@ mod client {
Err(ClientError::UsageError(error))
}
cluster_manager::delete_cluster_configuration_response::Reply::Success(success) => {
let peer_id = extract!(success.cluster_configuration)?;
Ok(peer_id)
let cluster_id = extract!(success.cluster_configuration)?;
Ok(cluster_id)
}
}
}
Expand Down Expand Up @@ -208,56 +221,44 @@ mod client {
}
}

pub async fn store_cluster_deployment(&mut self, deployment: &ClusterDeployment) -> Result<(), StoreClusterDeploymentError> {
pub async fn store_cluster_deployment(&mut self, deployment: ClusterDeployment) -> Result<ClusterId, ClientError<StoreClusterDeploymentError>> {

let request = tonic::Request::new(cluster_manager::StoreClusterDeploymentRequest {
deployment: Some(Clone::clone(deployment).into()),
cluster_deployment: Some(deployment.into()),
});

match self.inner.store_cluster_deployment(request).await {
Ok(_) => {
Ok(())
},
Err(status) => {
Err(StoreClusterDeploymentError { message: format!("gRPC failure: {status}") })
let response = self.inner.store_cluster_deployment(request).await?
.into_inner();

match extract!(response.reply)? {
cluster_manager::store_cluster_deployment_response::Reply::Failure(failure) => {
let error = StoreClusterDeploymentError::try_from(failure)?;
Err(ClientError::UsageError(error))
}
cluster_manager::store_cluster_deployment_response::Reply::Success(success) => {
let cluster_id = extract!(success.cluster_id)?;
Ok(cluster_id)
}
}
}

pub async fn delete_cluster_deployment(&mut self, cluster_id: ClusterId) -> Result<ClusterDeployment, ClientError<DeleteClusterDeploymentError>> {

let request = tonic::Request::new(cluster_manager::DeleteClusterDeploymentRequest {
id: Some(cluster_id.into()),
cluster_id: Some(cluster_id.into()),
});

let response = self.inner.delete_cluster_deployment(request)
.await?
let response = self.inner.delete_cluster_deployment(request).await?
.into_inner();

let result = extract!(response.result)?;

match result {
cluster_manager::delete_cluster_deployment_response::Result::Failure(failure) => {
match failure.reason {
Some(cluster_manager::delete_cluster_deployment_failure::Reason::ClusterNotFound(cluster_manager::DeleteClusterDeploymentFailureNotFound { .. })) => {
Err(ClientError::UsageError(DeleteClusterDeploymentError::ClusterNotFound { id: cluster_id } ))
}
Some(cluster_manager::delete_cluster_deployment_failure::Reason::InvalidClusterId(cluster_manager::DeleteClusterDeploymentFailureInvalidClusterId { cause })) => {
Err(ClientError::UsageError(DeleteClusterDeploymentError::InvalidClusterId { cause } ))
}
Some(cluster_manager::delete_cluster_deployment_failure::Reason::ClusterIdRequired(_)) => {
Err(ClientError::InvalidRequest(format!("DeleteClusterDeploymentRequest requires ClusterId!")))
}
None => {
Err(ClientError::InvalidRequest(format!("DeleteClusterDeploymentFailure contains no reason!")))
}
Some(cluster_manager::delete_cluster_deployment_failure::Reason::Internal(cluster_manager::DeleteClusterDeploymentFailureInternal { cause, .. })) => {
Err(ClientError::UsageError(DeleteClusterDeploymentError::Internal { id: cluster_id, cause } ))
}
}
match extract!(response.reply)? {
cluster_manager::delete_cluster_deployment_response::Reply::Failure(failure) => {
let error = DeleteClusterDeploymentError::try_from(failure)?;
Err(ClientError::UsageError(error))
}
cluster_manager::delete_cluster_deployment_response::Result::Success(success) => {
let cluster_deployment = extract!(success.deployment)?;
Ok(cluster_deployment)
cluster_manager::delete_cluster_deployment_response::Reply::Success(success) => {
let cluster_id = extract!(success.cluster_deployment)?;
Ok(cluster_id)
}
}
}
Expand Down
Loading

0 comments on commit 686ed87

Please sign in to comment.