batata_consul_client/
types.rs

1use std::collections::HashMap;
2use std::time::Duration;
3
4use serde::{Deserialize, Serialize};
5
6/// Query options for read operations
7#[derive(Clone, Debug, Default)]
8pub struct QueryOptions {
9    /// Datacenter to query
10    pub datacenter: Option<String>,
11    /// AllowStale allows stale queries
12    pub allow_stale: bool,
13    /// RequireConsistent forces consistent queries
14    pub require_consistent: bool,
15    /// UseCache uses Consul's caching feature
16    pub use_cache: bool,
17    /// MaxAge limits how old a cached value will be returned
18    pub max_age: Option<Duration>,
19    /// StaleIfError allows stale read on error
20    pub stale_if_error: Option<Duration>,
21    /// WaitIndex for blocking queries
22    pub wait_index: u64,
23    /// WaitTime for blocking queries
24    pub wait_time: Option<Duration>,
25    /// Token for ACL authentication
26    pub token: Option<String>,
27    /// Namespace (Enterprise only)
28    pub namespace: Option<String>,
29    /// Partition (Enterprise only)
30    pub partition: Option<String>,
31    /// Near sorts by nearest node
32    pub near: Option<String>,
33    /// NodeMeta filters by node metadata
34    pub node_meta: HashMap<String, String>,
35    /// Filter expression for filtering results
36    pub filter: Option<String>,
37}
38
39impl QueryOptions {
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    pub fn with_token(mut self, token: &str) -> Self {
45        self.token = Some(token.to_string());
46        self
47    }
48
49    pub fn with_datacenter(mut self, dc: &str) -> Self {
50        self.datacenter = Some(dc.to_string());
51        self
52    }
53
54    pub fn with_namespace(mut self, ns: &str) -> Self {
55        self.namespace = Some(ns.to_string());
56        self
57    }
58
59    pub fn with_wait(mut self, index: u64, wait_time: Duration) -> Self {
60        self.wait_index = index;
61        self.wait_time = Some(wait_time);
62        self
63    }
64
65    pub fn with_stale(mut self) -> Self {
66        self.allow_stale = true;
67        self
68    }
69
70    pub fn with_consistent(mut self) -> Self {
71        self.require_consistent = true;
72        self
73    }
74}
75
76/// Write options for write operations
77#[derive(Clone, Debug, Default)]
78pub struct WriteOptions {
79    /// Datacenter to write to
80    pub datacenter: Option<String>,
81    /// Token for ACL authentication
82    pub token: Option<String>,
83    /// Namespace (Enterprise only)
84    pub namespace: Option<String>,
85    /// Partition (Enterprise only)
86    pub partition: Option<String>,
87}
88
89impl WriteOptions {
90    pub fn new() -> Self {
91        Self::default()
92    }
93
94    pub fn with_token(mut self, token: &str) -> Self {
95        self.token = Some(token.to_string());
96        self
97    }
98
99    pub fn with_datacenter(mut self, dc: &str) -> Self {
100        self.datacenter = Some(dc.to_string());
101        self
102    }
103
104    pub fn with_namespace(mut self, ns: &str) -> Self {
105        self.namespace = Some(ns.to_string());
106        self
107    }
108}
109
110/// Metadata returned from query operations
111#[derive(Clone, Debug, Default)]
112pub struct QueryMeta {
113    /// LastIndex can be used for blocking queries
114    pub last_index: u64,
115    /// LastContact is the time of last contact from leader
116    pub last_contact: Duration,
117    /// KnownLeader indicates if there is a known leader
118    pub known_leader: bool,
119    /// RequestTime is how long the request took
120    pub request_time: Duration,
121    /// AddressTranslationEnabled indicates if address translation is enabled
122    pub address_translation_enabled: bool,
123    /// CacheHit indicates if the response was a cache hit
124    pub cache_hit: bool,
125    /// CacheAge is how old the cached response is
126    pub cache_age: Option<Duration>,
127}
128
129/// Metadata returned from write operations
130#[derive(Clone, Debug, Default)]
131pub struct WriteMeta {
132    /// RequestTime is how long the request took
133    pub request_time: Duration,
134}
135
136/// Node represents a Consul node
137#[derive(Clone, Debug, Serialize, Deserialize)]
138#[serde(rename_all = "PascalCase")]
139pub struct Node {
140    /// Node ID
141    #[serde(rename = "ID")]
142    pub id: String,
143    /// Node name
144    pub node: String,
145    /// Node address
146    pub address: String,
147    /// Datacenter
148    pub datacenter: String,
149    /// Tagged addresses
150    #[serde(default)]
151    pub tagged_addresses: HashMap<String, String>,
152    /// Node metadata
153    #[serde(default)]
154    pub meta: HashMap<String, String>,
155    /// Create index
156    pub create_index: u64,
157    /// Modify index
158    pub modify_index: u64,
159}
160
161/// Service definition for a node
162#[derive(Clone, Debug, Serialize, Deserialize)]
163#[serde(rename_all = "PascalCase")]
164pub struct AgentService {
165    /// Service ID
166    #[serde(rename = "ID")]
167    pub id: String,
168    /// Service name
169    pub service: String,
170    /// Service tags
171    #[serde(default)]
172    pub tags: Vec<String>,
173    /// Service port
174    pub port: u16,
175    /// Service address
176    #[serde(default)]
177    pub address: String,
178    /// Enable tag override
179    #[serde(default)]
180    pub enable_tag_override: bool,
181    /// Service metadata
182    #[serde(default)]
183    pub meta: HashMap<String, String>,
184    /// Service weights
185    #[serde(default)]
186    pub weights: Option<ServiceWeights>,
187}
188
189/// Service weights for load balancing
190#[derive(Clone, Debug, Serialize, Deserialize)]
191#[serde(rename_all = "PascalCase")]
192pub struct ServiceWeights {
193    /// Passing weight
194    pub passing: i32,
195    /// Warning weight
196    pub warning: i32,
197}
198
199impl Default for ServiceWeights {
200    fn default() -> Self {
201        Self {
202            passing: 1,
203            warning: 1,
204        }
205    }
206}
207
208/// Catalog service definition
209#[derive(Clone, Debug, Serialize, Deserialize)]
210#[serde(rename_all = "PascalCase")]
211pub struct CatalogService {
212    /// Node ID
213    #[serde(rename = "ID")]
214    pub id: String,
215    /// Node name
216    pub node: String,
217    /// Node address
218    pub address: String,
219    /// Datacenter
220    pub datacenter: String,
221    /// Tagged addresses
222    #[serde(default)]
223    pub tagged_addresses: HashMap<String, String>,
224    /// Node metadata
225    #[serde(default)]
226    pub node_meta: HashMap<String, String>,
227    /// Service ID
228    #[serde(rename = "ServiceID")]
229    pub service_id: String,
230    /// Service name
231    pub service_name: String,
232    /// Service tags
233    #[serde(default)]
234    pub service_tags: Vec<String>,
235    /// Service address
236    #[serde(default)]
237    pub service_address: String,
238    /// Service port
239    pub service_port: u16,
240    /// Service metadata
241    #[serde(default)]
242    pub service_meta: HashMap<String, String>,
243    /// Service weights
244    #[serde(default)]
245    pub service_weights: Option<ServiceWeights>,
246    /// Create index
247    pub create_index: u64,
248    /// Modify index
249    pub modify_index: u64,
250}
251
252/// Health check definition
253#[derive(Clone, Debug, Serialize, Deserialize)]
254#[serde(rename_all = "PascalCase")]
255pub struct HealthCheck {
256    /// Node name
257    pub node: String,
258    /// Check ID
259    #[serde(rename = "CheckID")]
260    pub check_id: String,
261    /// Check name
262    pub name: String,
263    /// Check status (passing, warning, critical)
264    pub status: String,
265    /// Check notes
266    #[serde(default)]
267    pub notes: String,
268    /// Check output
269    #[serde(default)]
270    pub output: String,
271    /// Service ID
272    #[serde(rename = "ServiceID")]
273    #[serde(default)]
274    pub service_id: String,
275    /// Service name
276    #[serde(default)]
277    pub service_name: String,
278    /// Service tags
279    #[serde(default)]
280    pub service_tags: Vec<String>,
281    /// Check type
282    #[serde(rename = "Type")]
283    #[serde(default)]
284    pub check_type: String,
285    /// Create index
286    pub create_index: u64,
287    /// Modify index
288    pub modify_index: u64,
289}
290
291/// Service entry combining service and health checks
292#[derive(Clone, Debug, Serialize, Deserialize)]
293#[serde(rename_all = "PascalCase")]
294pub struct ServiceEntry {
295    /// Node information
296    pub node: Node,
297    /// Service information
298    pub service: AgentService,
299    /// Health checks
300    pub checks: Vec<HealthCheck>,
301}