Skip to content

Commit

Permalink
ConfigState::cluster_state return Option<ClusterInformation>
Browse files Browse the repository at this point in the history
display "no cluster found" if response is empty
  • Loading branch information
Keksoj committed Dec 6, 2023
1 parent 95de156 commit 86303a2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions bin/src/command/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ impl CommandServer {
),
Some(RequestType::QueryClusterById(cluster_id)) => Some(
ContentType::Clusters(ClusterInformations {
vec: vec![self.state.cluster_state(cluster_id)],
vec: self.state.cluster_state(cluster_id).into_iter().collect(),
})
.into(),
),
Expand All @@ -1030,7 +1030,7 @@ impl CommandServer {
.get_cluster_ids_by_domain(domain.hostname.clone(), domain.path.clone());
let vec = cluster_ids
.iter()
.map(|cluster_id| self.state.cluster_state(cluster_id))
.filter_map(|cluster_id| self.state.cluster_state(cluster_id))
.collect();
Some(ContentType::Clusters(ClusterInformations { vec }).into())
}
Expand Down
5 changes: 5 additions & 0 deletions command/src/proto/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,11 @@ fn print_cluster_infos(worker_responses: &WorkerResponses) -> Result<(), Display
}
}

if cluster_infos.is_empty() {
println!("no cluster found");
return Ok(());
}

println!("Cluster level configuration:\n");

for (cluster_info, workers_the_cluster_is_present_on) in cluster_infos.iter() {
Expand Down
64 changes: 36 additions & 28 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,43 +1173,51 @@ impl ConfigState {

/// Gives details about a given cluster.
/// Types like `HttpFrontend` are converted into protobuf ones, like `RequestHttpFrontend`
pub fn cluster_state(&self, cluster_id: &str) -> ClusterInformation {
let mut http_frontends = Vec::new();
let mut https_frontends = Vec::new();
let mut tcp_frontends = Vec::new();
let mut backends = Vec::new();

for http_frontend in self.http_fronts.values() {
if let Some(id) = &http_frontend.cluster_id {
if id == cluster_id {
http_frontends.push(http_frontend.clone().into());
}
}
pub fn cluster_state(&self, cluster_id: &str) -> Option<ClusterInformation> {
let configuration = self.clusters.get(cluster_id).cloned();
if configuration.is_none() {
return None;
}

for https_frontend in self.https_fronts.values() {
if let Some(id) = &https_frontend.cluster_id {
if id == cluster_id {
https_frontends.push(https_frontend.clone().into());
}
}
}
let http_frontends: Vec<RequestHttpFrontend> = self
.http_fronts
.values()
.filter(|front| front.cluster_id.as_deref() == Some(cluster_id))
.map(|front| front.clone().into())
.collect();

for tcp_f in self.tcp_fronts.get(cluster_id).cloned().unwrap_or_default() {
tcp_frontends.push(tcp_f.clone().into());
}
let https_frontends: Vec<RequestHttpFrontend> = self
.https_fronts
.values()
.filter(|front| front.cluster_id.as_deref() == Some(cluster_id))
.map(|front| front.clone().into())
.collect();

for backend in self.backends.get(cluster_id).cloned().unwrap_or_default() {
backends.push(backend.clone().into())
}
let tcp_frontends: Vec<RequestTcpFrontend> = self
.tcp_fronts
.get(cluster_id)
.cloned()
.unwrap_or_default()
.iter()
.map(|front| front.clone().into())
.collect();

ClusterInformation {
configuration: self.clusters.get(cluster_id).cloned(),
let backends: Vec<AddBackend> = self
.backends
.get(cluster_id)
.cloned()
.unwrap_or_default()
.iter()
.map(|backend| backend.clone().into())
.collect();

Some(ClusterInformation {
configuration,
http_frontends,
https_frontends,
tcp_frontends,
backends,
}
})
}

pub fn count_backends(&self) -> usize {
Expand Down
8 changes: 6 additions & 2 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,11 @@ impl Server {
push_queue(WorkerResponse::ok_with_content(
message.id.clone(),
ContentType::Clusters(ClusterInformations {
vec: vec![self.config_state.cluster_state(cluster_id)],
vec: self
.config_state
.cluster_state(cluster_id)
.into_iter()
.collect(),
})
.into(),
));
Expand All @@ -909,7 +913,7 @@ impl Server {
.get_cluster_ids_by_domain(domain.hostname.clone(), domain.path.clone());
let vec = cluster_ids
.iter()
.map(|cluster_id| self.config_state.cluster_state(cluster_id))
.filter_map(|cluster_id| self.config_state.cluster_state(cluster_id))
.collect();

push_queue(WorkerResponse::ok_with_content(
Expand Down

0 comments on commit 86303a2

Please sign in to comment.