-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatic_admin.rs
116 lines (106 loc) · 3.35 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
113
114
115
116
// 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;
use bgp::session::DEFAULT_ROUTE_PRIORITY;
use dropshot::{
endpoint, HttpError, HttpResponseDeleted, HttpResponseOk,
HttpResponseUpdatedNoContent, RequestContext, TypedBody,
};
use rdb::{Prefix4, Route4ImportKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{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,
}
impl From<StaticRoute4> for Route4ImportKey {
fn from(val: StaticRoute4) -> Self {
Route4ImportKey {
prefix: val.prefix,
nexthop: val.nexthop,
// Having an ID of zero indicates this entry did not come from BGP.
// TODO: this could likely be done in a more rust-y way, or just
// have a cleaner data structure organization.
id: 0,
//
priority: DEFAULT_ROUTE_PRIORITY,
}
}
}
impl From<Route4ImportKey> for StaticRoute4 {
fn from(value: Route4ImportKey) -> Self {
Self {
prefix: value.prefix,
nexthop: value.nexthop,
}
}
}
#[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<Route4ImportKey> = request
.into_inner()
.routes
.list
.into_iter()
.map(Into::into)
.collect();
for r in routes {
ctx.context()
.db
.set_nexthop4(r, true)
.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<Route4ImportKey> = request
.into_inner()
.routes
.list
.into_iter()
.map(Into::into)
.collect();
for r in routes {
ctx.context()
.db
.remove_nexthop4(r, true)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
}
Ok(HttpResponseDeleted())
}
#[endpoint { method = GET, path = "/static/route4" }]
pub async fn static_list_v4_routes(
ctx: RequestContext<Arc<HandlerContext>>,
) -> Result<HttpResponseOk<StaticRoute4List>, HttpError> {
let list = ctx
.context()
.db
.get_imported4()
.into_iter()
.filter(|x| x.id == 0) // indicates not from bgp
.map(Into::into)
.collect();
Ok(HttpResponseOk(StaticRoute4List { list }))
}