-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstatic_admin.rs
More file actions
187 lines (169 loc) · 5.74 KB
/
Copy pathstatic_admin.rs
File metadata and controls
187 lines (169 loc) · 5.74 KB
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
// 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 crate::validation::validate_prefixes;
use dropshot::{
HttpError, HttpResponseDeleted, HttpResponseOk,
HttpResponseUpdatedNoContent, RequestContext, TypedBody,
};
use mg_api_types::rdb::rib::AddressFamily;
use mg_api_types::rib::GetRibResult;
use mg_api_types::static_routes::{
AddStaticRoute4Request, AddStaticRoute6Request, DeleteStaticRoute4Request,
DeleteStaticRoute6Request, StaticRoute4, StaticRoute6,
};
use mg_api_types::switch::SwitchIdentifiers;
use oxnet::IpNet;
use rdb::StaticRouteKey;
use std::{collections::BTreeMap, sync::Arc};
// `From<StaticRouteN>` impls cannot live in `mg-api-types-versions` (would
// force a `rdb` dep) nor in `rdb` (would force an `mg-api-types-versions` dep).
// Both source and target types are foreign to `mgd`, so we expose the
// conversion as free fns here at the call site.
fn static_route_key_from_v4(v: StaticRoute4) -> StaticRouteKey {
// Compile barrier: a new StaticRoute4 field will fail to bind here,
// forcing a deliberate decision about how (or whether) it should
// appear in the rdb runtime key.
let StaticRoute4 {
prefix,
nexthop,
vlan_id,
rib_priority,
} = v;
StaticRouteKey {
prefix: prefix.into(),
nexthop,
vlan_id,
rib_priority,
}
}
fn static_route_key_from_v6(v: StaticRoute6) -> StaticRouteKey {
// Compile barrier: a new StaticRoute6 field will fail to bind here,
// forcing a deliberate decision about how (or whether) it should
// appear in the rdb runtime key.
let StaticRoute6 {
prefix,
nexthop,
vlan_id,
rib_priority,
} = v;
StaticRouteKey {
prefix: prefix.into(),
nexthop: nexthop.into(),
vlan_id,
rib_priority,
}
}
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(static_route_key_from_v4)
.collect();
// Validate that all prefixes have host bits unset
let prefixes: Vec<IpNet> = routes.iter().map(|r| r.prefix).collect();
validate_prefixes(&prefixes)?;
ctx.context()
.db
.add_static_routes(&routes)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
Ok(HttpResponseUpdatedNoContent())
}
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(static_route_key_from_v4)
.collect();
ctx.context()
.db
.remove_static_routes(&routes)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
Ok(HttpResponseDeleted())
}
pub async fn static_list_v4_routes(
ctx: RequestContext<Arc<HandlerContext>>,
) -> Result<HttpResponseOk<GetRibResult>, HttpError> {
let static_db = ctx
.context()
.db
.get_static(Some(AddressFamily::Ipv4))
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
let mut static_rib: GetRibResult = BTreeMap::new();
for srk in static_db {
let key = srk.prefix.to_string();
let paths = static_rib.entry(key).or_default();
paths.insert(srk.into());
}
Ok(HttpResponseOk(static_rib))
}
pub async fn static_add_v6_route(
ctx: RequestContext<Arc<HandlerContext>>,
request: TypedBody<AddStaticRoute6Request>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let routes: Vec<StaticRouteKey> = request
.into_inner()
.routes
.list
.into_iter()
.map(static_route_key_from_v6)
.collect();
// Validate that all prefixes have host bits unset
let prefixes: Vec<IpNet> = routes.iter().map(|r| r.prefix).collect();
validate_prefixes(&prefixes)?;
ctx.context()
.db
.add_static_routes(&routes)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
Ok(HttpResponseUpdatedNoContent())
}
pub async fn static_remove_v6_route(
ctx: RequestContext<Arc<HandlerContext>>,
request: TypedBody<DeleteStaticRoute6Request>,
) -> Result<HttpResponseDeleted, HttpError> {
let routes: Vec<StaticRouteKey> = request
.into_inner()
.routes
.list
.into_iter()
.map(static_route_key_from_v6)
.collect();
ctx.context()
.db
.remove_static_routes(&routes)
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
Ok(HttpResponseDeleted())
}
pub async fn static_list_v6_routes(
ctx: RequestContext<Arc<HandlerContext>>,
) -> Result<HttpResponseOk<GetRibResult>, HttpError> {
let static_db = ctx
.context()
.db
.get_static(Some(AddressFamily::Ipv6))
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
let mut static_rib: GetRibResult = BTreeMap::new();
for srk in static_db {
let key = srk.prefix.to_string();
let paths = static_rib.entry(key).or_default();
paths.insert(srk.into());
}
Ok(HttpResponseOk(static_rib))
}
pub(crate) async fn switch_identifiers(
ctx: RequestContext<Arc<HandlerContext>>,
) -> Result<HttpResponseOk<SwitchIdentifiers>, HttpError> {
let slot = ctx.context().db.slot();
Ok(HttpResponseOk(SwitchIdentifiers { slot }))
}