Skip to main content

north_consul/
catalog.rs

1use async_trait::async_trait;
2use std::collections::HashMap;
3
4use crate::agent::{AgentCheck, AgentService};
5use crate::errors::Result;
6use crate::request::{get, put};
7use crate::{Client, QueryMeta, QueryOptions, WriteMeta, WriteOptions};
8
9#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
10#[serde(default)]
11pub struct Weights {
12    pub Passing: u32,
13    pub Warning: u32,
14}
15
16#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
17#[serde(default)]
18pub struct Node {
19    pub ID: String,
20    pub Node: String,
21    pub Address: String,
22    pub Datacenter: String,
23    pub TaggedAddresses: HashMap<String, String>,
24    pub Meta: HashMap<String, String>,
25    pub CreateIndex: u64,
26    pub ModifyIndex: u64,
27}
28
29#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
30#[serde(default)]
31pub struct CatalogService {
32    pub ID: String,
33    pub Node: String,
34    pub Address: String,
35    pub Datacenter: String,
36    pub TaggedAddresses: HashMap<String, String>,
37    pub NodeMeta: HashMap<String, String>,
38    pub ServiceID: String,
39    pub ServiceName: String,
40    pub ServiceAddress: String,
41    pub ServiceTags: Vec<String>,
42    pub ServiceMeta: HashMap<String, String>,
43    pub ServicePort: u32,
44    pub ServiceWeights: Weights,
45    pub ServiceEnableTagOverride: bool,
46    pub CreateIndex: u64,
47    pub ModifyIndex: u64,
48}
49
50#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
51#[serde(default)]
52pub struct CatalogNode {
53    pub Node: Option<Node>,
54    pub Services: HashMap<String, AgentService>,
55}
56
57#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug, Clone)]
58#[serde(default)]
59pub struct CatalogRegistration {
60    pub ID: String,
61    pub Node: String,
62    pub Address: String,
63    pub TaggedAddresses: HashMap<String, String>,
64    pub NodeMeta: HashMap<String, String>,
65    pub Datacenter: String,
66    pub Service: Option<AgentService>,
67    pub Check: Option<AgentCheck>,
68    pub SkipNodeUpdate: bool,
69}
70
71#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
72#[serde(default)]
73pub struct CatalogDeregistration {
74    pub Node: String,
75    pub Address: String,
76    pub Datacenter: String,
77    pub ServiceID: String,
78    pub CheckID: String,
79}
80
81#[async_trait]
82pub trait Catalog {
83    async fn register(
84        &self,
85        reg: &CatalogRegistration,
86        q: Option<&WriteOptions>,
87    ) -> Result<((), WriteMeta)>;
88    async fn deregister(
89        &self,
90        dereg: &CatalogDeregistration,
91        q: Option<&WriteOptions>,
92    ) -> Result<((), WriteMeta)>;
93    async fn datacenters(&self) -> Result<(Vec<String>, QueryMeta)>;
94    async fn nodes(&self, q: Option<&QueryOptions>) -> Result<(Vec<Node>, QueryMeta)>;
95    async fn services(
96        &self,
97        q: Option<&QueryOptions>,
98    ) -> Result<(HashMap<String, Vec<String>>, QueryMeta)>;
99}
100
101#[async_trait]
102impl Catalog for Client {
103    /// https://www.consul.io/api/catalog.html#register-entity
104    async fn register(
105        &self,
106        reg: &CatalogRegistration,
107        q: Option<&WriteOptions>,
108    ) -> Result<((), WriteMeta)> {
109        put(
110            "/v1/session/create",
111            Some(reg),
112            &self.config,
113            HashMap::new(),
114            q,
115        )
116        .await
117    }
118
119    /// https://www.consul.io/api/catalog.html#deregister-entity
120    async fn deregister(
121        &self,
122        dereg: &CatalogDeregistration,
123        q: Option<&WriteOptions>,
124    ) -> Result<((), WriteMeta)> {
125        put(
126            "/v1/catalog/deregister",
127            Some(dereg),
128            &self.config,
129            HashMap::new(),
130            q,
131        )
132        .await
133    }
134
135    /// https://www.consul.io/api/catalog.html#list-datacenters
136    async fn datacenters(&self) -> Result<(Vec<String>, QueryMeta)> {
137        get(
138            "/v1/catalog/datacenters",
139            &self.config,
140            HashMap::new(),
141            None,
142        )
143        .await
144    }
145
146    /// https://www.consul.io/api/catalog.html#list-nodes
147    async fn nodes(&self, q: Option<&QueryOptions>) -> Result<(Vec<Node>, QueryMeta)> {
148        get("/v1/catalog/nodes", &self.config, HashMap::new(), q).await
149    }
150
151    async fn services(
152        &self,
153        q: Option<&QueryOptions>,
154    ) -> Result<(HashMap<String, Vec<String>>, QueryMeta)> {
155        get("/v1/catalog/services", &self.config, HashMap::new(), q).await
156    }
157}