Refactor extraneous Structs into one. Update README.md

This commit is contained in:
Anthony J. Martinez 2021-02-10 16:29:45 +01:00
parent a7c12d3964
commit d16ab3283f
3 changed files with 44 additions and 66 deletions

View File

@ -28,38 +28,44 @@ To get `connchk` run `cargo install connchk` on a system with [Cargo](https://do
### Example TOML Config
```toml
# example.toml
[[tcp]]
[[target]]
kind = "Tcp"
desc = "GitLab SSH"
addr = "gitlab.com:22"
[[tcp]]
[[target]]
kind = "Tcp"
desc = "Freenode IRC"
addr = "irc.freenode.net:6667"
[[http]]
[[target]]
kind = "Tcp"
desc = "httpbin IP endpoint"
addr = "https://httpbin.org/ip"
# Posts as a form and reports success if the status code returned is 400
# which it will be for this bad request to this particular endpoint
[[http]]
[[target]]
kind = "Http"
desc = "httpbin POST endpoint (form)"
addr = "https://httpbin.org/status/undefined"
custom = { params = { someKey = "SpecialValue" }, ok = 400 }
# Posts as JSON and reports success if the status code returned is 400
# as it will be for this particular endpoint
[[http]]
[[target]]
kind = "Http"
desc = "httpbin JSON endpoint"
addr = "https://httpbin.org/status/400"
custom = { json = { someKey = "SpecialValue" }, ok = 400 }
# An example failure - this endpoint will return a 502 status code,
# An example failure - this endpoing will return a 502 status code,
# but our configuration expects a 400
[[http]]
[[target]]
kind = "Http"
desc = "httpbin JSON endpoint - Error"
addr = "https://httpbin.org/status/502"
custom = { json = { someKey = [3, "AnotherValue", false], anotherKey = { nested = "value", count = [1, 2, 3] } }, ok = 400 }
custom = { json = { someKey = [3, "AnotherValue", false], anotherKey = { nested = "value", count = [1, 2, 3] } }, ok = 400 }
```
### Example Usage
@ -84,6 +90,14 @@ has been explored, so if issues are encountered please [let it be known](https:/
### Major Changes
- v0.6.0
- Refactored away `TcpResource` and `HttpResource` structs differentiating individual `Resource` kinds with the `ResType` enum
- Added `Resource.kind` to hold `ResType` variants
- Modified `NetworkResources` to hold `Vec<Resources>` in `NetworkResources.target`
- Above changes are **BREAKING** with respect to all existing configuration files. Users should:
- Replace all `[[http]]` or `[[tcp]]` lines with `[[target]]`
- Add `kind = "Http"` to any block that previously stared with `[[http]]`
- Add `kind = "Tcp"` to any block that previously stared with `[[tcp]]`
- v0.5.0
- Refactored to produce both binary and library crates
- Created a common `Resource` struct to map `TcpResource` and `HttpResources` onto for consumption by `par_iter()` by `rayon`

View File

@ -1,32 +1,38 @@
[[tcp]]
[[target]]
kind = "Tcp"
desc = "GitLab SSH"
addr = "gitlab.com:22"
[[tcp]]
[[target]]
kind = "Tcp"
desc = "Freenode IRC"
addr = "irc.freenode.net:6667"
[[http]]
[[target]]
kind = "Http"
desc = "httpbin IP endpoint"
addr = "https://httpbin.org/ip"
# Posts as a form and reports success if the status code returned is 400
# which it will be for this bad request to this particular endpoint
[[http]]
[[target]]
kind = "Http"
desc = "httpbin POST endpoint (form)"
addr = "https://httpbin.org/status/undefined"
custom = { params = { someKey = "SpecialValue" }, ok = 400 }
# Posts as JSON and reports success if the status code returned is 400
# as it will be for this particular endpoint
[[http]]
[[target]]
kind = "Http"
desc = "httpbin JSON endpoint"
addr = "https://httpbin.org/status/400"
custom = { json = { someKey = "SpecialValue" }, ok = 400 }
# An example failure - this endpoing will return a 502 status code,
# but our configuration expects a 400
[[http]]
[[target]]
kind = "Http"
desc = "httpbin JSON endpoint - Error"
addr = "https://httpbin.org/status/502"
custom = { json = { someKey = [3, "AnotherValue", false], anotherKey = { nested = "value", count = [1, 2, 3] } }, ok = 400 }
custom = { json = { someKey = [3, "AnotherValue", false], anotherKey = { nested = "value", count = [1, 2, 3] } }, ok = 400 }

View File

@ -41,35 +41,19 @@ pub struct HttpOptions {
pub ok: u16,
}
/// Provides a deserialize target for general parameters
/// for HTTP(s) checks.
#[derive(Deserialize, Debug)]
pub struct HttpResource {
pub desc: String,
pub addr: String,
pub custom: Option<HttpOptions>,
}
/// Provides a deserialize target for TCP checks
#[derive(Deserialize, Debug)]
pub struct TcpResource {
pub desc: String,
pub addr: String,
}
/// A generic resource combining all possible fields into a common type
#[derive(Debug)]
#[derive(Deserialize, Debug)]
pub struct Resource {
pub desc: String,
pub addr: String,
pub custom: Option<HttpOptions>,
pub res_type: ResType,
pub kind: ResType,
}
impl Resource {
/// Executes connectivity checks for each type defined in [`ResType`]
pub fn check(&self) -> Result<(), Box<dyn std::error::Error>> {
match self.res_type {
match self.kind {
ResType::Tcp => {
self.check_tcp()?;
},
@ -151,7 +135,7 @@ impl Resource {
}
/// Classifies the resource type for the top-level [`Resource`] struct
#[derive(Debug)]
#[derive(Deserialize, Debug)]
pub enum ResType {
/// An HTTP(s) resource
Http,
@ -160,44 +144,18 @@ pub enum ResType {
}
/// Provides a deserialize target for TOML configuration files
/// defining multiple [`TcpResource`] or [`HttpResource`] entities
/// defining multiple [`Resource`] entities
#[derive(Deserialize, Debug)]
pub struct NetworkResources {
pub http: Option<Vec<HttpResource>>,
pub tcp: Option<Vec<TcpResource>>,
pub target: Vec<Resource>,
}
impl NetworkResources {
/// Executes parallel connectivity checks for all [`TcpResource`] and
/// [`HttpResource`] objects contained within the higher level [`NetworkResources`]
/// Executes parallel connectivity checks for all [`Resource`]
/// objects contained within the higher level [`NetworkResources`]
/// struct.
pub fn check_resources(self) {
let mut res_vec: Vec<Resource> = Vec::new();
if let Some(v) = &self.tcp {
for tcp in v.iter() {
let res = Resource {
desc: tcp.desc.clone(),
addr: tcp.addr.clone(),
custom: None,
res_type: ResType::Tcp,
};
res_vec.push(res);
}
}
if let Some(v) = &self.http {
for http in v.iter() {
let res = Resource {
desc: http.desc.clone(),
addr: http.addr.clone(),
custom: http.custom.clone(),
res_type: ResType::Http,
};
res_vec.push(res);
}
}
res_vec.par_iter()
self.target.par_iter()
.for_each(|el| match el.check() {
Ok(_) => println!("Successfully connected to {}", el.description()),
Err(e) => println!("Failed to connect to {} with: {}", el.description(), e)