haproxy_api/
proxy.rs

1use std::collections::HashMap;
2use std::ops::Deref;
3
4use mlua::{FromLua, Lua, ObjectLike, Result, String as LuaString, Table, Value};
5
6use crate::{listener::Listener, Server, StickTable};
7
8/// The "Proxy" class provides a way for manipulating proxy
9/// and retrieving information like statistics.
10#[derive(Clone)]
11pub struct Proxy(Table);
12
13#[derive(Debug, PartialEq, Eq)]
14pub enum ProxyCapability {
15    Frontend,
16    Backend,
17    Proxy,
18    Ruleset,
19}
20
21#[derive(Debug, PartialEq, Eq)]
22pub enum ProxyMode {
23    Tcp,
24    Http,
25    Health,
26    Unknown,
27}
28
29impl Proxy {
30    /// Returns the name of the proxy.
31    #[inline]
32    pub fn get_name(&self) -> Result<String> {
33        self.0.call_method("get_name", ())
34    }
35
36    /// Returns the UUID of the proxy.
37    #[inline]
38    pub fn get_uuid(&self) -> Result<String> {
39        self.0.call_method("get_uuid", ())
40    }
41
42    /// Returns a map with the attached servers.
43    /// The map is indexed by server name.
44    #[inline]
45    pub fn get_servers(&self) -> Result<HashMap<String, Server>> {
46        self.0.get("servers")
47    }
48
49    /// Returns the stick table attached to the proxy.
50    #[inline]
51    pub fn get_stktable(&self) -> Result<Option<StickTable>> {
52        self.0.get("stktable")
53    }
54
55    /// Returns a table with the attached listeners.
56    /// The table is indexed by listener name.
57    #[inline]
58    pub fn get_listeners(&self) -> Result<HashMap<String, Listener>> {
59        self.0.get("listeners")
60    }
61
62    /// Pauses the proxy.
63    /// See the management socket documentation for more information.
64    #[inline]
65    pub fn pause(&self) -> Result<()> {
66        self.0.call_method("pause", ())
67    }
68
69    /// Resumes the proxy.
70    /// See the management socket documentation for more information.
71    #[inline]
72    pub fn resume(&self) -> Result<()> {
73        self.0.call_method("resume", ())
74    }
75
76    /// Stops the proxy.
77    /// See the management socket documentation for more information.
78    #[inline]
79    pub fn stop(&self) -> Result<()> {
80        self.0.call_method("stop", ())
81    }
82
83    /// Kills the session attached to a backup server.
84    /// See the management socket documentation for more information.
85    #[inline]
86    pub fn shut_bcksess(&self) -> Result<()> {
87        self.0.call_method("shut_bcksess", ())
88    }
89
90    /// Returns a enum describing the capabilities of the proxy.
91    #[inline]
92    pub fn get_cap(&self) -> Result<ProxyCapability> {
93        let cap: LuaString = self.0.call_method("get_cap", ())?;
94        match cap.to_str()?.deref() {
95            "frontend" => Ok(ProxyCapability::Frontend),
96            "backend" => Ok(ProxyCapability::Backend),
97            "proxy" => Ok(ProxyCapability::Proxy),
98            _ => Ok(ProxyCapability::Ruleset),
99        }
100    }
101
102    /// Returns a enum describing the mode of the current proxy.
103    #[inline]
104    pub fn get_mode(&self) -> Result<ProxyMode> {
105        let mode: LuaString = self.0.call_method("get_mode", ())?;
106        match mode.to_str()?.deref() {
107            "tcp" => Ok(ProxyMode::Tcp),
108            "http" => Ok(ProxyMode::Http),
109            "health" => Ok(ProxyMode::Health),
110            _ => Ok(ProxyMode::Unknown),
111        }
112    }
113
114    /// Returns the number of current active servers for the current proxy
115    /// that are eligible for LB.
116    #[inline]
117    pub fn get_srv_act(&self) -> Result<usize> {
118        self.0.call_method("get_srv_act", ())
119    }
120
121    /// Returns the number backup servers for the current proxy that are eligible for LB.
122    #[inline]
123    pub fn get_srv_bck(&self) -> Result<usize> {
124        self.0.call_method("get_srv_bck", ())
125    }
126
127    /// Returns a table containing the proxy statistics.
128    /// The statistics returned are not the same if the proxy is frontend or a backend.
129    #[inline]
130    pub fn get_stats(&self) -> Result<Table> {
131        self.0.call_method("get_stats", ())
132    }
133}
134
135impl FromLua for Proxy {
136    #[inline]
137    fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
138        let class = Table::from_lua(value, lua)?;
139        Ok(Proxy(class))
140    }
141}
142
143impl Deref for Proxy {
144    type Target = Table;
145
146    #[inline]
147    fn deref(&self) -> &Self::Target {
148        &self.0
149    }
150}