Skip to content

Commit

Permalink
isolate method ConfigState::list_frontends
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed May 19, 2023
1 parent 1c138c7 commit c85f65f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
43 changes: 2 additions & 41 deletions bin/src/command/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sozu_command_lib::{
parser::parse_several_commands,
proto::command::{
request::RequestType, response_content::ContentType, AggregatedMetrics, AvailableMetrics,
ClusterHashes, ClusterInformations, FrontendFilters, ListedFrontends, ListenersList,
ClusterHashes, ClusterInformations, FrontendFilters, ListenersList,
MetricsConfiguration, Request, Response, ResponseContent, ResponseStatus,
ReturnListenSockets, RunState, SoftStop, Status, WorkerInfo, WorkerInfos, WorkerResponses,
},
Expand Down Expand Up @@ -338,46 +338,7 @@ impl CommandServer {
filters
);

// if no http / https / tcp filter is provided, list all of them
let list_all = !filters.http && !filters.https && !filters.tcp;

let mut listed_frontends = ListedFrontends::default();

if filters.http || list_all {
for http_frontend in self.state.http_fronts.iter().filter(|f| {
if let Some(domain) = &filters.domain {
f.1.hostname.contains(domain)
} else {
true
}
}) {
listed_frontends
.http_frontends
.push(http_frontend.1.to_owned().into());
}
}

if filters.https || list_all {
for https_frontend in self.state.https_fronts.iter().filter(|f| {
if let Some(domain) = &filters.domain {
f.1.hostname.contains(domain)
} else {
true
}
}) {
listed_frontends
.https_frontends
.push(https_frontend.1.to_owned().into());
}
}

if (filters.tcp || list_all) && filters.domain.is_none() {
for tcp_frontend in self.state.tcp_fronts.values().flat_map(|v| v.iter()) {
listed_frontends
.tcp_frontends
.push(tcp_frontend.to_owned().into())
}
}
let listed_frontends = self.state.list_frontends(filters);

Ok(Some(Success::ListFrontends(
ContentType::FrontendList(listed_frontends).into(),
Expand Down
53 changes: 49 additions & 4 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use crate::{
certificate::{calculate_fingerprint, Fingerprint},
proto::command::{
request::RequestType, ActivateListener, AddBackend, AddCertificate, CertificateAndKey,
CertificateWithNames, Cluster, ClusterInformation, DeactivateListener, HttpListenerConfig,
HttpsListenerConfig, ListenerType, PathRule, RemoveBackend, RemoveCertificate,
RemoveListener, ReplaceCertificate, Request, RequestHttpFrontend, RequestTcpFrontend,
TcpListenerConfig,
CertificateWithNames, Cluster, ClusterInformation, DeactivateListener, FrontendFilters,
HttpListenerConfig, HttpsListenerConfig, ListedFrontends, ListenerType, PathRule,
RemoveBackend, RemoveCertificate, RemoveListener, ReplaceCertificate, Request,
RequestHttpFrontend, RequestTcpFrontend, TcpListenerConfig,
},
response::{Backend, HttpFrontend, TcpFrontend},
};
Expand Down Expand Up @@ -1236,6 +1236,51 @@ impl ConfigState {
})
.next()
}

pub fn list_frontends(&self, filters: FrontendFilters) -> ListedFrontends {
// if no http / https / tcp filter is provided, list all of them
let list_all = !filters.http && !filters.https && !filters.tcp;

let mut listed_frontends = ListedFrontends::default();

if filters.http || list_all {
for http_frontend in self.http_fronts.iter().filter(|f| {
if let Some(domain) = &filters.domain {
f.1.hostname.contains(domain)
} else {
true
}
}) {
listed_frontends
.http_frontends
.push(http_frontend.1.to_owned().into());
}
}

if filters.https || list_all {
for https_frontend in self.https_fronts.iter().filter(|f| {
if let Some(domain) = &filters.domain {
f.1.hostname.contains(domain)
} else {
true
}
}) {
listed_frontends
.https_frontends
.push(https_frontend.1.to_owned().into());
}
}

if (filters.tcp || list_all) && filters.domain.is_none() {
for tcp_frontend in self.tcp_fronts.values().flat_map(|v| v.iter()) {
listed_frontends
.tcp_frontends
.push(tcp_frontend.to_owned().into())
}
}

listed_frontends
}
}

fn domain_check(
Expand Down

0 comments on commit c85f65f

Please sign in to comment.