Skip to main content

north_consul/
health.rs

1use async_trait::async_trait;
2use std::collections::HashMap;
3
4use crate::agent::AgentService;
5use crate::errors::Result;
6use crate::request::get;
7use crate::{Client, QueryMeta, QueryOptions};
8
9#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
10#[serde(default)]
11pub struct HealthCheck {
12    pub Node: String,
13    pub CheckID: String,
14    pub Name: String,
15    pub Status: String,
16    pub Notes: String,
17    pub Output: String,
18    pub ServiceID: String,
19    pub ServiceName: String,
20    pub ServiceTags: Option<Vec<String>>,
21}
22
23#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
24#[serde(default)]
25pub struct Node {
26    pub ID: String,
27    pub Node: String,
28    pub Address: String,
29    pub Datacenter: Option<String>,
30    pub TaggedAddresses: Option<HashMap<String, String>>,
31    pub Meta: Option<HashMap<String, String>>,
32    pub CreateIndex: u64,
33    pub ModifyIndex: u64,
34}
35
36#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
37#[serde(default)]
38pub struct ServiceEntry {
39    pub Node: Node,
40    pub Service: AgentService,
41    pub Checks: Vec<HealthCheck>,
42}
43
44#[async_trait]
45pub trait Health {
46    async fn service(
47        &self,
48        service: &str,
49        tag: Option<&str>,
50        passing_only: bool,
51        options: Option<&QueryOptions>,
52    ) -> Result<(Vec<ServiceEntry>, QueryMeta)>;
53}
54
55#[async_trait]
56impl Health for Client {
57    async fn service(
58        &self,
59        service: &str,
60        tag: Option<&str>,
61        passing_only: bool,
62        options: Option<&QueryOptions>,
63    ) -> Result<(Vec<ServiceEntry>, QueryMeta)> {
64        let mut params = HashMap::new();
65        let path = format!("/v1/health/service/{}", service);
66        if passing_only {
67            params.insert(String::from("passing"), String::from("1"));
68        }
69        if let Some(tag) = tag {
70            params.insert(String::from("tag"), tag.to_owned());
71        }
72        get(&path, &self.config, params, options).await
73    }
74}