use std::collections::HashMap;
use crate::agent::AgentService;
use crate::errors::Result;
use crate::request::get;
use crate::{Client, QueryMeta, QueryOptions};
#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct HealthCheck {
pub Node: String,
pub CheckID: String,
pub Name: String,
pub Status: String,
pub Notes: String,
pub Output: String,
pub ServiceID: String,
pub ServiceName: String,
pub ServiceTags: Option<Vec<String>>,
}
#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct Node {
pub ID: String,
pub Node: String,
pub Address: String,
pub Datacenter: Option<String>,
pub TaggedAddresses: Option<HashMap<String, String>>,
pub Meta: Option<HashMap<String, String>>,
pub CreateIndex: u64,
pub ModifyIndex: u64,
}
#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct ServiceEntry {
pub Node: Node,
pub Service: AgentService,
pub Checks: Vec<HealthCheck>,
}
pub trait Health {
fn service(
&self,
service: &str,
tag: Option<&str>,
passing_only: bool,
options: Option<&QueryOptions>,
) -> Result<(Vec<ServiceEntry>, QueryMeta)>;
}
impl Health for Client {
fn service(
&self,
service: &str,
tag: Option<&str>,
passing_only: bool,
options: Option<&QueryOptions>,
) -> Result<(Vec<ServiceEntry>, QueryMeta)> {
let mut params = HashMap::new();
let path = format!("/v1/health/service/{}", service);
if passing_only {
params.insert(String::from("passing"), String::from("1"));
}
if let Some(tag) = tag {
params.insert(String::from("tag"), tag.to_owned());
}
get(&path, &self.config, params, options)
}
}