haproxy_api/
proxy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::collections::HashMap;
use std::ops::Deref;

use mlua::{FromLua, Lua, Result, String as LuaString, Table, TableExt, Value};

use crate::{listener::Listener, Server, StickTable};

/// The "Proxy" class provides a way for manipulating proxy
/// and retrieving information like statistics.
#[derive(Clone)]
pub struct Proxy<'lua> {
    class: Table<'lua>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum ProxyCapability {
    Frontend,
    Backend,
    Proxy,
    Ruleset,
}

#[derive(Debug, PartialEq, Eq)]
pub enum ProxyMode {
    Tcp,
    Http,
    Health,
    Unknown,
}

impl<'lua> Proxy<'lua> {
    /// Returns the name of the proxy.
    #[inline]
    pub fn get_name(&self) -> Result<String> {
        self.class.call_method("get_name", ())
    }

    /// Returns the UUID of the proxy.
    #[inline]
    pub fn get_uuid(&self) -> Result<String> {
        self.class.call_method("get_uuid", ())
    }

    /// Returns a map with the attached servers.
    /// The map is indexed by server name.
    #[inline]
    pub fn get_servers(&self) -> Result<HashMap<String, Server<'lua>>> {
        self.class.get("servers")
    }

    /// Returns the stick table attached to the proxy.
    #[inline]
    pub fn get_stktable(&self) -> Result<Option<StickTable<'lua>>> {
        self.class.get("stktable")
    }

    /// Returns a table with the attached listeners.
    /// The table is indexed by listener name.
    #[inline]
    pub fn get_listeners(&self) -> Result<HashMap<String, Listener<'lua>>> {
        self.class.get("listeners")
    }

    /// Pauses the proxy.
    /// See the management socket documentation for more information.
    #[inline]
    pub fn pause(&self) -> Result<()> {
        self.class.call_method("pause", ())
    }

    /// Resumes the proxy.
    /// See the management socket documentation for more information.
    #[inline]
    pub fn resume(&self) -> Result<()> {
        self.class.call_method("resume", ())
    }

    /// Stops the proxy.
    /// See the management socket documentation for more information.
    #[inline]
    pub fn stop(&self) -> Result<()> {
        self.class.call_method("stop", ())
    }

    /// Kills the session attached to a backup server.
    /// See the management socket documentation for more information.
    #[inline]
    pub fn shut_bcksess(&self) -> Result<()> {
        self.class.call_method("shut_bcksess", ())
    }

    /// Returns a enum describing the capabilities of the proxy.
    #[inline]
    pub fn get_cap(&self) -> Result<ProxyCapability> {
        let cap: LuaString = self.class.call_method::<_, LuaString>("get_cap", ())?;
        match cap.to_str()? {
            "frontend" => Ok(ProxyCapability::Frontend),
            "backend" => Ok(ProxyCapability::Backend),
            "proxy" => Ok(ProxyCapability::Proxy),
            _ => Ok(ProxyCapability::Ruleset),
        }
    }

    /// Returns a enum describing the mode of the current proxy.
    #[inline]
    pub fn get_mode(&self) -> Result<ProxyMode> {
        let mode: LuaString = self.class.call_method("get_mode", ())?;
        match mode.to_str()? {
            "tcp" => Ok(ProxyMode::Tcp),
            "http" => Ok(ProxyMode::Http),
            "health" => Ok(ProxyMode::Health),
            _ => Ok(ProxyMode::Unknown),
        }
    }

    /// Returns the number of current active servers for the current proxy
    /// that are eligible for LB.
    #[inline]
    pub fn get_srv_act(&self) -> Result<usize> {
        self.class.call_method("get_srv_act", ())
    }

    /// Returns the number backup servers for the current proxy that are eligible for LB.
    #[inline]
    pub fn get_srv_bck(&self) -> Result<usize> {
        self.class.call_method("get_srv_bck", ())
    }

    /// Returns a table containing the proxy statistics.
    /// The statistics returned are not the same if the proxy is frontend or a backend.
    #[inline]
    pub fn get_stats(&self) -> Result<Table<'lua>> {
        self.class.call_method("get_stats", ())
    }
}

impl<'lua> FromLua<'lua> for Proxy<'lua> {
    #[inline]
    fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
        let class = Table::from_lua(value, lua)?;
        Ok(Proxy { class })
    }
}

impl<'lua> Deref for Proxy<'lua> {
    type Target = Table<'lua>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.class
    }
}