apisix_admin_client/models/
admin_route_requests.rs

1use serde::{Deserialize, Serialize};
2use crate::models::{generate_identifier, Plugins};
3use crate::UpstreamRequest;
4use crate::{Result};
5use crate::models::common::ApisixTimeout;
6
7#[serde_with::skip_serializing_none]
8#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct RouteBuilder {
10    pub id: Option<String>,
11    pub name: Option<String>,
12    pub desc: Option<String>,
13    pub status: Option<i64>,
14    pub plugins: Option<Plugins>,
15    pub uri: Option<String>,
16    pub uris: Option<Vec<String>>,
17    pub host: Option<String>,
18    pub hosts: Option<Vec<String>>,
19    pub remote_addr: Option<String>,
20    pub remote_addrs: Option<Vec<String>>,
21    pub methods: Option<Vec<String>>,
22    pub upstream: Option<UpstreamRequest>,
23    pub upstream_id: Option<String>,
24    pub service_id: Option<String>,
25    pub timeout: Option<ApisixTimeout>,
26    pub enable_websocket: Option<bool>,
27    pub priority: Option<i64>,
28}
29
30impl RouteBuilder {
31    pub fn new() -> Self {
32        RouteRequest::default().into()
33    }
34
35    pub fn with_id(mut self, id: String) -> Self {
36        self.id = Some(id);
37        self
38    }
39
40    /// Identifier for the Route
41    pub fn with_name(mut self, name: String) -> Self {
42        self.name = Some(name);
43        self
44    }
45
46    /// Description of usage scenarios
47    pub fn with_desc(mut self, desc: String) -> Self {
48        self.desc = Some(desc);
49        self
50    }
51
52    /// Enables the current Route. Set to 1 (enabled) by default
53    /// 1 to enable, 0 to disable
54    pub fn with_status(mut self, status: i64) -> Self {
55        self.status = Some(status);
56        self
57    }
58
59    /// Plugins that are executed during the request/response cycle. See [Plugins] for more
60    pub fn with_plugins(mut self, plugins: Plugins) -> Self {
61        self.plugins = Some(plugins);
62        self
63    }
64
65    /// Matches the uri. For more advanced matching see Router
66    /// Example: "/hello"
67    pub fn with_uri(mut self, uri: String) -> Self {
68        self.uri = Some(uri);
69        self
70    }
71
72    /// Matches with any one of the multiple uris specified in the form of a non-empty list
73    /// Example: ["/hello", "/word"]
74    pub fn with_uris(mut self, uris: Vec<String>) -> Self {
75        self.uris = Some(uris);
76        self
77    }
78
79    /// Matches with domain names such as foo.com or PAN domain names like *.foo.com.
80    /// Example: "foo.com"
81    pub fn with_host(mut self, host: String) -> Self {
82        self.host = Some(host);
83        self
84    }
85
86    /// Matches with any one of the multiple hosts specified in the form of a non-empty list
87    /// Example: ["foo.com", "bar.com"]
88    pub fn with_hosts(mut self, hosts: Vec<String>) -> Self {
89        self.hosts = Some(hosts);
90        self
91    }
92
93    /// Matches with the specified IP address in standard IPv4 format (192.168.1.101),
94    /// CIDR format (192.168.1.0/24), or in IPv6 format (::1, fe80::1, fe80::1/64)
95    /// Example: "192.168.1.0/24"
96    pub fn with_remote_addr(mut self, remote_addr: String) -> Self {
97        self.remote_addr = Some(remote_addr);
98        self
99    }
100
101    /// Matches with any one of the multiple remote_addrs specified in the form of a non-empty list
102    /// Example: ["127.0.0.1", "192.0.0.0/8", "::1"]
103    pub fn with_remote_addrs(mut self, remote_addrs: Vec<String>) -> Self {
104        self.remote_addrs = Some(remote_addrs);
105        self
106    }
107
108    /// Matches with the specified methods. Matches all methods if empty or unspecified
109    /// Example: ["GET", "POST"]
110    pub fn with_methods(mut self, methods: Vec<String>) -> Self {
111        self.methods = Some(methods);
112        self
113    }
114
115    /// Configuration of the Upstream [@UpstreamRequest]
116    pub fn with_upstream(mut self, upstream: UpstreamRequest) -> Self {
117        self.upstream = Some(upstream);
118        self
119    }
120
121    /// Id of the Upstream service
122    pub fn with_upstream_id(mut self, upstream_id: String) -> Self {
123        self.upstream_id = Some(upstream_id);
124        self
125    }
126
127    /// Configuration of the bound Service
128    pub fn with_service_id(mut self, service_id: String) -> Self {
129        self.service_id = Some(service_id);
130        self
131    }
132
133    /// Sets the timeout (in seconds) for connecting to, and sending and receiving messages
134    /// between the Upstream and the Route.
135    /// This will overwrite the timeout value configured in your Upstream
136    /// Example: {"connect": 0.5,"send": 0.5,"read": 0.5}
137    pub fn with_timeout(mut self, timeout: ApisixTimeout) -> Self {
138        self.timeout = Some(timeout);
139        self
140    }
141
142    /// Enables a websocket. Set to false by default
143    pub fn with_enable_websocket(mut self, enable_websocket: bool) -> Self {
144        self.enable_websocket = Some(enable_websocket);
145        self
146    }
147
148    /// If different Routes matches to the same uri,
149    /// then the Route is matched based on its priority.
150    /// A higher value corresponds to higher priority. It is set to 0 by default.
151    pub fn priority(mut self, priority: i64) -> Self {
152        self.priority = Some(priority);
153        self
154    }
155
156    pub fn build(self) -> Result<RouteRequest> {
157        Ok(RouteRequest {
158            id: self.id,
159            name: self.name,
160            desc: self.desc,
161            status: self.status,
162            plugins: self.plugins,
163            uri: self.uri,
164            uris: self.uris,
165            host: self.host,
166            hosts: self.hosts,
167            remote_addr: self.remote_addr,
168            remote_addrs: self.remote_addrs,
169            methods: self.methods,
170            upstream: self.upstream,
171            upstream_id: self.upstream_id,
172            service_id: self.service_id,
173            timeout: self.timeout,
174            enable_websocket: self.enable_websocket,
175            priority: self.priority,
176        })
177    }
178}
179
180#[serde_with::skip_serializing_none]
181#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
182pub struct RouteRequest {
183    pub id: Option<String>,
184    pub name: Option<String>,
185    pub desc: Option<String>,
186    pub status: Option<i64>,
187    pub plugins: Option<Plugins>,
188    pub uri: Option<String>,
189    pub uris: Option<Vec<String>>,
190    pub host: Option<String>,
191    pub hosts: Option<Vec<String>>,
192    pub remote_addr: Option<String>,
193    pub remote_addrs: Option<Vec<String>>,
194    pub methods: Option<Vec<String>>,
195    pub upstream: Option<UpstreamRequest>,
196    pub upstream_id: Option<String>,
197    pub service_id: Option<String>,
198    pub timeout: Option<ApisixTimeout>,
199    pub enable_websocket: Option<bool>,
200    pub priority: Option<i64>,
201}
202
203impl Default for RouteRequest {
204    fn default() -> Self {
205        RouteRequest {
206            id: Some(generate_identifier()),
207            name: None,
208            desc: None,
209            status: None,
210            plugins: None,
211            uri: None,
212            uris: None,
213            host: None,
214            hosts: None,
215            remote_addr: None,
216            remote_addrs: None,
217            methods: None,
218            upstream: None,
219            upstream_id: None,
220            service_id: None,
221            timeout: None,
222            enable_websocket: None,
223            priority: None,
224        }
225    }
226}
227
228impl From<RouteRequest> for RouteBuilder {
229    fn from(route: RouteRequest) -> Self {
230        RouteBuilder {
231            id: route.id,
232            name: route.name,
233            desc: route.desc,
234            status: route.status,
235            plugins: route.plugins,
236            uri: route.uri,
237            uris: route.uris,
238            host: route.host,
239            hosts: route.hosts,
240            remote_addr: route.remote_addr,
241            remote_addrs: route.remote_addrs,
242            methods: route.methods,
243            upstream: route.upstream,
244            upstream_id: route.upstream_id,
245            service_id: route.service_id,
246            timeout: route.timeout,
247            enable_websocket: route.enable_websocket,
248            priority: route.priority,
249        }
250    }
251}