-
Notifications
You must be signed in to change notification settings - Fork 2
/
static_admin.rs
112 lines (100 loc) · 3.17 KB
/
static_admin.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::{admin::HandlerContext, register};
use dropshot::{
endpoint, ApiDescription, HttpError, HttpResponseDeleted, HttpResponseOk,
HttpResponseUpdatedNoContent, RequestContext, TypedBody,
};
use rdb::{Path, Prefix4, StaticRouteKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
net::Ipv4Addr,
sync::Arc,
};
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct AddStaticRoute4Request {
routes: StaticRoute4List,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct DeleteStaticRoute4Request {
routes: StaticRoute4List,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct StaticRoute4List {
list: Vec<StaticRoute4>,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
pub struct StaticRoute4 {
pub prefix: Prefix4,
pub nexthop: Ipv4Addr,
pub vlan_id: Option<u16>,
pub rib_priority: u8,
}
impl From<StaticRoute4> for StaticRouteKey {
fn from(val: StaticRoute4) -> Self {
StaticRouteKey {
prefix: val.prefix.into(),
nexthop: val.nexthop.into(),
vlan_id: val.vlan_id,
rib_priority: val.rib_priority,
}
}
}
pub(crate) fn api_description(api: &mut ApiDescription<Arc<HandlerContext>>) {
register!(api, static_add_v4_route);
register!(api, static_remove_v4_route);
register!(api, static_list_v4_routes);
}
#[endpoint { method = PUT, path = "/static/route4" }]
pub async fn static_add_v4_route(
ctx: RequestContext<Arc<HandlerContext>>,
request: TypedBody<AddStaticRoute4Request>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let routes: Vec<StaticRouteKey> = request
.into_inner()
.routes
.list
.into_iter()
.map(Into::into)
.collect();
ctx.context()
.db
.add_static_routes(&routes)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
Ok(HttpResponseUpdatedNoContent())
}
#[endpoint { method = DELETE, path = "/static/route4" }]
pub async fn static_remove_v4_route(
ctx: RequestContext<Arc<HandlerContext>>,
request: TypedBody<DeleteStaticRoute4Request>,
) -> Result<HttpResponseDeleted, HttpError> {
let routes: Vec<StaticRouteKey> = request
.into_inner()
.routes
.list
.into_iter()
.map(Into::into)
.collect();
ctx.context()
.db
.remove_static_routes(&routes)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
Ok(HttpResponseDeleted())
}
pub type GetRibResult = BTreeMap<String, BTreeSet<Path>>;
#[endpoint { method = GET, path = "/static/route4" }]
pub async fn static_list_v4_routes(
ctx: RequestContext<Arc<HandlerContext>>,
) -> Result<HttpResponseOk<GetRibResult>, HttpError> {
let static_rib = ctx
.context()
.db
.static_rib()
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect();
Ok(HttpResponseOk(static_rib))
}