-
Notifications
You must be signed in to change notification settings - Fork 40
/
http_entrypoints.rs
311 lines (287 loc) · 10 KB
/
http_entrypoints.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// 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/.
/// Handler functions (entrypoints) for HTTP APIs internal to the control plane
use crate::context::OpContext;
use crate::ServerContext;
use super::params::{
DatasetPutRequest, DatasetPutResponse, OximeterInfo, ServicePutRequest,
SledAgentStartupInfo, ZpoolPutRequest, ZpoolPutResponse,
};
use dropshot::endpoint;
use dropshot::ApiDescription;
use dropshot::FreeformBody;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpResponseUpdatedNoContent;
use dropshot::Path;
use dropshot::RequestContext;
use dropshot::TypedBody;
use hyper::Body;
use omicron_common::api::internal::nexus::DiskRuntimeState;
use omicron_common::api::internal::nexus::InstanceRuntimeState;
use omicron_common::api::internal::nexus::ProducerEndpoint;
use omicron_common::api::internal::nexus::UpdateArtifact;
use oximeter::types::ProducerResults;
use oximeter_producer::{collect, ProducerIdPathParams};
use schemars::JsonSchema;
use serde::Deserialize;
use std::sync::Arc;
use uuid::Uuid;
type NexusApiDescription = ApiDescription<Arc<ServerContext>>;
/// Returns a description of the internal nexus API
pub fn internal_api() -> NexusApiDescription {
fn register_endpoints(api: &mut NexusApiDescription) -> Result<(), String> {
api.register(cpapi_sled_agents_post)?;
api.register(rack_initialization_complete)?;
api.register(zpool_put)?;
api.register(dataset_put)?;
api.register(cpapi_instances_put)?;
api.register(cpapi_disks_put)?;
api.register(cpapi_producers_post)?;
api.register(cpapi_collectors_post)?;
api.register(cpapi_metrics_collect)?;
api.register(cpapi_artifact_download)?;
Ok(())
}
let mut api = NexusApiDescription::new();
if let Err(err) = register_endpoints(&mut api) {
panic!("failed to register entrypoints: {}", err);
}
api
}
/// Path parameters for Sled Agent requests (internal API)
#[derive(Deserialize, JsonSchema)]
struct SledAgentPathParam {
sled_id: Uuid,
}
/// Report that the sled agent for the specified sled has come online.
// TODO: Should probably be "PUT", since:
// 1. We're upserting the value
// 2. The client supplies the UUID
// 3. This call is idempotent (mod "time_modified").
#[endpoint {
method = POST,
path = "/sled-agents/{sled_id}",
}]
async fn cpapi_sled_agents_post(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<SledAgentPathParam>,
sled_info: TypedBody<SledAgentStartupInfo>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let path = path_params.into_inner();
let si = sled_info.into_inner();
let sled_id = &path.sled_id;
let handler = async {
nexus.upsert_sled(*sled_id, si.sa_address).await?;
Ok(HttpResponseUpdatedNoContent())
};
apictx.internal_latencies.instrument_dropshot_handler(&rqctx, handler).await
}
/// Path parameters for Rack requests.
#[derive(Deserialize, JsonSchema)]
struct RackPathParam {
rack_id: Uuid,
}
/// Report that the Rack Setup Service initialization is complete
///
/// See RFD 278 for more details.
#[endpoint {
method = PUT,
path = "/racks/{rack_id}/initialization-complete",
}]
async fn rack_initialization_complete(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<RackPathParam>,
info: TypedBody<Vec<ServicePutRequest>>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let path = path_params.into_inner();
let svcs = info.into_inner();
let opctx = OpContext::for_internal_api(&rqctx).await;
nexus.rack_initialize(&opctx, path.rack_id, svcs).await?;
Ok(HttpResponseUpdatedNoContent())
}
/// Path parameters for Sled Agent requests (internal API)
#[derive(Deserialize, JsonSchema)]
struct ZpoolPathParam {
sled_id: Uuid,
zpool_id: Uuid,
}
/// Report that a pool for a specified sled has come online.
#[endpoint {
method = PUT,
path = "/sled-agents/{sled_id}/zpools/{zpool_id}",
}]
async fn zpool_put(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<ZpoolPathParam>,
pool_info: TypedBody<ZpoolPutRequest>,
) -> Result<HttpResponseOk<ZpoolPutResponse>, HttpError> {
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let path = path_params.into_inner();
let pi = pool_info.into_inner();
nexus.upsert_zpool(path.zpool_id, path.sled_id, pi).await?;
Ok(HttpResponseOk(ZpoolPutResponse {}))
}
#[derive(Deserialize, JsonSchema)]
struct DatasetPathParam {
zpool_id: Uuid,
dataset_id: Uuid,
}
/// Report that a dataset within a pool has come online.
#[endpoint {
method = PUT,
path = "/zpools/{zpool_id}/dataset/{dataset_id}",
}]
async fn dataset_put(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<DatasetPathParam>,
info: TypedBody<DatasetPutRequest>,
) -> Result<HttpResponseOk<DatasetPutResponse>, HttpError> {
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let path = path_params.into_inner();
let info = info.into_inner();
nexus
.upsert_dataset(
path.dataset_id,
path.zpool_id,
info.address,
info.kind.into(),
)
.await?;
Ok(HttpResponseOk(DatasetPutResponse { reservation: None, quota: None }))
}
/// Path parameters for Instance requests (internal API)
#[derive(Deserialize, JsonSchema)]
struct InstancePathParam {
instance_id: Uuid,
}
/// Report updated state for an instance.
#[endpoint {
method = PUT,
path = "/instances/{instance_id}",
}]
async fn cpapi_instances_put(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<InstancePathParam>,
new_runtime_state: TypedBody<InstanceRuntimeState>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let path = path_params.into_inner();
let new_state = new_runtime_state.into_inner();
let handler = async {
nexus.notify_instance_updated(&path.instance_id, &new_state).await?;
Ok(HttpResponseUpdatedNoContent())
};
apictx.internal_latencies.instrument_dropshot_handler(&rqctx, handler).await
}
/// Path parameters for Disk requests (internal API)
#[derive(Deserialize, JsonSchema)]
struct DiskPathParam {
disk_id: Uuid,
}
/// Report updated state for a disk.
#[endpoint {
method = PUT,
path = "/disks/{disk_id}",
}]
async fn cpapi_disks_put(
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<DiskPathParam>,
new_runtime_state: TypedBody<DiskRuntimeState>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let path = path_params.into_inner();
let new_state = new_runtime_state.into_inner();
let handler = async {
let opctx = OpContext::for_internal_api(&rqctx).await;
nexus.notify_disk_updated(&opctx, path.disk_id, &new_state).await?;
Ok(HttpResponseUpdatedNoContent())
};
apictx.internal_latencies.instrument_dropshot_handler(&rqctx, handler).await
}
/// Accept a registration from a new metric producer
#[endpoint {
method = POST,
path = "/metrics/producers",
}]
async fn cpapi_producers_post(
request_context: Arc<RequestContext<Arc<ServerContext>>>,
producer_info: TypedBody<ProducerEndpoint>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let context = request_context.context();
let nexus = &context.nexus;
let producer_info = producer_info.into_inner();
let handler = async {
nexus.assign_producer(producer_info).await?;
Ok(HttpResponseUpdatedNoContent())
};
context
.internal_latencies
.instrument_dropshot_handler(&request_context, handler)
.await
}
/// Accept a notification of a new oximeter collection server.
#[endpoint {
method = POST,
path = "/metrics/collectors",
}]
async fn cpapi_collectors_post(
request_context: Arc<RequestContext<Arc<ServerContext>>>,
oximeter_info: TypedBody<OximeterInfo>,
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
let context = request_context.context();
let nexus = &context.nexus;
let oximeter_info = oximeter_info.into_inner();
let handler = async {
nexus.upsert_oximeter_collector(&oximeter_info).await?;
Ok(HttpResponseUpdatedNoContent())
};
context
.internal_latencies
.instrument_dropshot_handler(&request_context, handler)
.await
}
/// Endpoint for oximeter to collect nexus server metrics.
#[endpoint {
method = GET,
path = "/metrics/collect/{producer_id}",
}]
async fn cpapi_metrics_collect(
request_context: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<ProducerIdPathParams>,
) -> Result<HttpResponseOk<ProducerResults>, HttpError> {
let context = request_context.context();
let producer_id = path_params.into_inner().producer_id;
let handler =
async { collect(&context.producer_registry, producer_id).await };
context
.internal_latencies
.instrument_dropshot_handler(&request_context, handler)
.await
}
/// Endpoint used by Sled Agents to download cached artifacts.
#[endpoint {
method = GET,
path = "/artifacts/{kind}/{name}/{version}",
}]
async fn cpapi_artifact_download(
request_context: Arc<RequestContext<Arc<ServerContext>>>,
path_params: Path<UpdateArtifact>,
) -> Result<HttpResponseOk<FreeformBody>, HttpError> {
let context = request_context.context();
let nexus = &context.nexus;
let opctx = OpContext::for_internal_api(&request_context).await;
// TODO: return 404 if the error we get here says that the record isn't found
let body =
nexus.download_artifact(&opctx, path_params.into_inner()).await?;
Ok(HttpResponseOk(Body::from(body).into()))
}