haproxy_api/
server.rs

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