haproxy_api/
server.rs

1use std::ops::Deref;
2
3use mlua::{AsChunk, Chunk, FromLua, Lua, ObjectLike, Result, Table, Value};
4
5use crate::{EventSub, Proxy};
6
7/// The "Server" class provides a way for manipulating servers and retrieving information.
8#[derive(Clone)]
9pub struct Server(Table);
10
11impl Server {
12    /// Returns the name of the server.
13    #[inline]
14    pub fn get_name(&self) -> Result<String> {
15        self.0.call_method("get_name", ())
16    }
17
18    /// Returns the proxy unique identifier of the server.
19    #[inline]
20    pub fn get_puid(&self) -> Result<String> {
21        self.0.call_method("get_puid", ())
22    }
23
24    /// Returns the rid (revision ID) of the server.
25    #[inline]
26    pub fn get_rid(&self) -> Result<u64> {
27        self.0.call_method("get_rid", ())
28    }
29
30    /// Returns true if the server is currently draining sticky connections.
31    #[inline]
32    pub fn is_draining(&self) -> Result<bool> {
33        self.0.call_method("is_draining", ())
34    }
35
36    /// Return true if the server is a backup server.
37    #[inline]
38    pub fn is_backup(&self) -> Result<bool> {
39        self.0.call_method("is_backup", ())
40    }
41
42    /// Return true if the server was instantiated at runtime (e.g.: from the cli).
43    #[inline]
44    pub fn is_dynamic(&self) -> Result<bool> {
45        self.0.call_method("is_dynamic", ())
46    }
47
48    /// Return the number of currently active sessions on the server.
49    pub fn get_cur_sess(&self) -> Result<u64> {
50        self.0.call_method("get_cur_sess", ())
51    }
52
53    /// Return the number of pending connections to the server.
54    #[inline]
55    pub fn get_pend_conn(&self) -> Result<u64> {
56        self.0.call_method("get_pend_conn", ())
57    }
58
59    /// Dynamically changes the maximum connections of the server.
60    #[inline]
61    pub fn set_maxconn(&self, maxconn: u64) -> Result<()> {
62        self.0.call_method("set_maxconn", maxconn)
63    }
64
65    /// Returns an integer representing the server maximum connections.
66    #[inline]
67    pub fn get_maxconn(&self) -> Result<u64> {
68        self.0.call_method("get_maxconn", ())
69    }
70
71    /// Dynamically changes the weight of the server.
72    /// See the management socket documentation for more information about the format of the string.
73    #[inline]
74    pub fn set_weight(&self, weight: &str) -> Result<()> {
75        self.0.call_method("set_weight", weight)
76    }
77
78    /// Returns an integer representing the server weight.
79    #[inline]
80    pub fn get_weight(&self) -> Result<u32> {
81        self.0.call_method("get_weight", ())
82    }
83
84    /// Dynamically changes the address of the server.
85    #[inline]
86    pub fn set_addr(&self, addr: String, port: Option<u16>) -> Result<()> {
87        self.0.call_method("set_addr", (addr, port))
88    }
89
90    /// Returns a string describing the address of the server.
91    #[inline]
92    pub fn get_addr(&self) -> Result<String> {
93        self.0.call_method("get_addr", ())
94    }
95
96    /// Returns a table containing the server statistics.
97    #[inline]
98    pub fn get_stats(&self) -> Result<Table> {
99        self.0.call_method("get_stats", ())
100    }
101
102    /// Returns the parent proxy to which the server belongs.
103    pub fn get_proxy(&self) -> Result<Proxy> {
104        self.0.call_method("get_proxy", ())
105    }
106
107    /// Shutdowns all the sessions attached to the server.
108    #[inline]
109    pub fn shut_sess(&self) -> Result<()> {
110        self.0.call_method("shut_sess", ())
111    }
112
113    /// Drains sticky sessions.
114    #[inline]
115    pub fn set_drain(&self) -> Result<()> {
116        self.0.call_method("set_drain", ())
117    }
118
119    /// Sets maintenance mode.
120    #[inline]
121    pub fn set_maint(&self) -> Result<()> {
122        self.0.call_method("set_maint", ())
123    }
124
125    /// Sets normal mode.
126    #[inline]
127    pub fn set_ready(&self) -> Result<()> {
128        self.0.call_method("set_ready", ())
129    }
130
131    /// Enables health checks.
132    #[inline]
133    pub fn check_enable(&self) -> Result<()> {
134        self.0.call_method("check_enable", ())
135    }
136
137    /// Disables health checks.
138    #[inline]
139    pub fn check_disable(&self) -> Result<()> {
140        self.0.call_method("check_disable", ())
141    }
142
143    /// Forces health-check up.
144    #[inline]
145    pub fn check_force_up(&self) -> Result<()> {
146        self.0.call_method("check_force_up", ())
147    }
148
149    /// Forces health-check nolb mode.
150    #[inline]
151    pub fn check_force_nolb(&self) -> Result<()> {
152        self.0.call_method("check_force_nolb", ())
153    }
154
155    /// Forces health-check down.
156    #[inline]
157    pub fn check_force_down(&self) -> Result<()> {
158        self.0.call_method("check_force_down", ())
159    }
160
161    /// Enables agent check.
162    #[inline]
163    pub fn agent_enable(&self) -> Result<()> {
164        self.0.call_method("agent_enable", ())
165    }
166
167    /// Disables agent check.
168    #[inline]
169    pub fn agent_disable(&self) -> Result<()> {
170        self.0.call_method("agent_disable", ())
171    }
172
173    /// Forces agent check up.
174    #[inline]
175    pub fn agent_force_up(&self) -> Result<()> {
176        self.0.call_method("agent_force_up", ())
177    }
178
179    /// Forces agent check down.
180    #[inline]
181    pub fn agent_force_down(&self) -> Result<()> {
182        self.0.call_method("agent_force_down", ())
183    }
184
185    /// Check if the current server is tracking another server.
186    #[inline]
187    pub fn tracking(&self) -> Result<Option<Server>> {
188        self.0.call_method("tracking(", ())
189    }
190
191    /// Check if the current server is being tracked by other servers.
192    #[inline]
193    pub fn get_trackers(&self) -> Result<Vec<Server>> {
194        self.0.call_method("get_trackers", ())
195    }
196
197    /// Register a function that will be called on specific server events.
198    ///
199    /// It works exactly like `core.event_sub()` except that the subscription
200    /// will be performed within the server dedicated subscription list instead of the global one.
201    pub fn event_sub(&self, event_types: &[&str], code: impl AsChunk) -> Result<EventSub> {
202        self.0
203            .call_function("event_sub", (event_types, Chunk::wrap(code)))
204    }
205}
206
207impl FromLua for Server {
208    #[inline]
209    fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
210        let class = Table::from_lua(value, lua)?;
211        Ok(Server(class))
212    }
213}
214
215impl Deref for Server {
216    type Target = Table;
217
218    #[inline]
219    fn deref(&self) -> &Self::Target {
220        &self.0
221    }
222}