apisix_admin_client/models/
admin_upstream_requests.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use serde::{Deserialize, Serialize, Serializer};
use serde::ser::SerializeSeq;
use serde_json::Value;
use strum_macros::{Display, EnumString};
use crate::models::generate_identifier;
use crate::{Result};
use crate::common::ApisixTimeout;

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpstreamBuilder {
    pub id: Option<String>,
    pub retries: Option<i32>,
    pub retry_timeout: Option<i32>,
    pub timeout: Option<ApisixTimeout>,
    pub nodes: Option<Value>,
    pub service_name: Option<String>,
    pub discovery_type: Option<String>,
    #[serde(rename = "type")]
    pub type_field: Option<UpstreamType>,
    pub name: Option<String>,
    pub desc: Option<String>,
    pub scheme: Option<UpstreamSchema>,
}

impl UpstreamBuilder {
    pub fn new() -> Self {
        UpstreamRequest::default().into()
    }

    /// Upstream ID
    /// TODO validate id See [generate_identifier]
    pub fn id(mut self, id: String) -> Self {
        self.id = Some(id);
        self
    }

    /// Load balancing algorithm to be used, and the default value is roundrobin.
    /// See [UpstreamType]
    pub fn u_type(mut self, u_type: UpstreamType) -> Self {
        self.type_field = Some(u_type);
        self
    }

    /// IP addresses (with optional ports) of the Upstream nodes represented as a hash table or an array.
    /// In the hash table, the key is the IP address and the value is the weight of the node for the load balancing algorithm.
    /// For hash table case, if the key is IPv6 address with port, then the IPv6 address must be quoted with square brackets.
    /// In the array, each item is a hash table with keys host, weight, and the optional port and priority (defaults to 0).
    /// Nodes with lower priority are used only when all nodes with a higher priority are tried and are unavailable.
    /// Empty nodes are treated as placeholders and clients trying to access this Upstream will receive a 502 response.
    ///
    /// Restrictions: can not be used with `service_name`
    ///
    /// Example: `192.168.1.100:80`, `[::1]:80`
    pub fn nodes(mut self, nodes: Value) -> Self {
        self.nodes = Some(nodes);
        self
    }

    /// Service name used for service discovery
    ///
    /// Restrictions: can not be used with `nodes`
    pub fn service_name(mut self, service_name: String) -> Self {
        self.service_name = Some(service_name);
        self.discovery_type = Some("eureka".to_string()); //default
        self.nodes = None; //reset nodes when service name is used
        self
    }

    /// The type of service discovery to be used. The default value is eureka.
    /// Required when `service_name` is defined
    pub fn discovery_type(mut self, discovery_type: String) -> Self {
        self.discovery_type = Some(discovery_type);
        self
    }

    /// Sets the number of retries while passing the request to Upstream using the underlying Nginx mechanism.
    /// Set according to the number of available backend nodes by default.
    /// Setting this to 0 disables retry.
    pub fn retries(mut self, retries: i32) -> Self {
        self.retries = Some(retries);
        self
    }

    /// Timeout to continue with retries. Setting this to 0 disables the retry timeout.
    pub fn retry_timeout(mut self, retry_timeout: i32) -> Self {
        self.retry_timeout = Some(retry_timeout);
        self
    }

    /// Sets the timeout (in seconds) for connecting to,
    /// and sending and receiving messages to and from the Upstream.
    ///
    /// Example: {"connect": 0.5,"send": 0.5,"read": 0.5}
    pub fn timeout(mut self, timeout: ApisixTimeout) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Identifier for the Upstream
    pub fn name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    /// Description of usage scenarios
    pub fn desc(mut self, desc: String) -> Self {
        self.desc = Some(desc);
        self
    }

    /// The scheme used when communicating with the Upstream.
    /// For an L7 proxy, this value can be one of http, https, grpc, grpcs.
    /// For an L4 proxy, this value could be one of tcp, udp, tls.
    /// Defaults to http.
    pub fn schema(mut self, scheme: UpstreamSchema) -> Self {
        self.scheme = Some(scheme);
        self
    }

    pub fn build(&self) -> Result<UpstreamRequest> {
        Ok(UpstreamRequest {
            id: self.id.clone(),
            retries: self.retries.clone(),
            retry_timeout: self.retry_timeout.clone(),
            timeout: self.timeout.clone(),
            nodes: self.nodes.clone(),
            service_name: self.service_name.clone(),
            discovery_type: self.discovery_type.clone(),
            type_field: self.type_field.clone(),
            name: self.name.clone(),
            desc: self.desc.clone(),
            scheme: self.scheme.clone(),
        })
    }

}

// TODO: health checks => Configures the parameters for the health check.
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpstreamRequest {
    pub id: Option<String>,
    pub retries: Option<i32>,
    pub retry_timeout: Option<i32>,
    pub timeout: Option<ApisixTimeout>,
    pub nodes: Option<Value>,
    pub service_name: Option<String>,
    pub discovery_type: Option<String>,
    #[serde(rename = "type")]
    pub type_field: Option<UpstreamType>,
    pub name: Option<String>,
    pub desc: Option<String>,
    pub scheme: Option<UpstreamSchema>,
}

impl Default for UpstreamRequest {
    fn default() -> Self {
        let nodes = r#"
        {
            "localhost:9000": 1
        }"#;
        let nodes = serde_json::from_str(nodes).unwrap();
        UpstreamRequest {
            id: Some(generate_identifier()),
            retries: Some(0_i32), //disabled by default
            retry_timeout: Some(0_i32),
            timeout: None,
            nodes: Some(nodes),
            service_name: None,
            discovery_type: None,
            type_field: None,
            name: None,
            desc: None,
            scheme: Some(UpstreamSchema::http),
        }
    }
}

impl From<UpstreamRequest> for UpstreamBuilder {
    fn from(upstream: UpstreamRequest) -> Self {
        UpstreamBuilder {
            id: upstream.id,
            retries: upstream.retries,
            retry_timeout: upstream.retry_timeout,
            timeout: upstream.timeout,
            nodes: upstream.nodes,
            service_name: upstream.service_name,
            discovery_type: upstream.discovery_type,
            type_field: upstream.type_field,
            name: upstream.name,
            desc: upstream.desc,
            scheme: upstream.scheme,
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Display, EnumString)]
#[allow(non_camel_case_types)]
#[strum(ascii_case_insensitive)]
#[non_exhaustive]
pub enum UpstreamType {
    roundrobin,
    chash,
    ewma,
    least_conn,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Display, EnumString)]
#[allow(non_camel_case_types)]
#[strum(ascii_case_insensitive)]
#[non_exhaustive]
pub enum UpstreamTypeChashAuxiliary {
    vars,
    header,
    cookie,
    consumer,
}

impl From<String> for UpstreamTypeChashAuxiliary {
    fn from(value: String) -> Self {
        match value.to_uppercase().as_str() {
            "vars" => UpstreamTypeChashAuxiliary::vars,
            "header" => UpstreamTypeChashAuxiliary::header,
            "cookie" => UpstreamTypeChashAuxiliary::cookie,
            "consumer" => UpstreamTypeChashAuxiliary::consumer,
            _ => UpstreamTypeChashAuxiliary::vars
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Display, EnumString)]
#[allow(non_camel_case_types)]
#[strum(ascii_case_insensitive)]
#[non_exhaustive]
pub enum UpstreamSchema {
    http,
    https,
    grpc,
    grpcs,
    tcp,
    udp,
    tls
}

// region: tests
#[cfg(test)]
mod tests {
    use serde_json::{to_string, to_string_pretty};
    use super::*;
    use tracing::{error, info};
    use tracing_test::traced_test;
    use crate::models::admin_upstream_requests::UpstreamType;

    #[traced_test]
    #[tokio::test]
    async fn test_generate_upstream_request() {
        let nodes = r#"
        {
            "localhost:9000": 1
        }"#;
        let nodes = serde_json::from_str(nodes).unwrap();

        let upstream_req = UpstreamBuilder::new()
            .id("test_upstream".to_string())
            .name("Test Upstream".to_string())
            .desc("Test Upstream Description".to_string())
            .schema(UpstreamSchema::https)
            .u_type(UpstreamType::roundrobin)
            .nodes(nodes)
            .retries(3)
            .retry_timeout(5)
            .timeout(ApisixTimeout { connect: Some(0.5), send: Some(0.5), read: Some(0.5) })
            .build().unwrap();
        info!("Upstream Request: {:?}", to_string(&upstream_req));
        assert!(true)
    }
}
// endregion: tests