Skip to content

Commit 61479b6

Browse files
authored
[lldpd] make API versioned (#40)
Last part of lldp work for oxidecomputer/omicron#8922.
1 parent be0356d commit 61479b6

File tree

10 files changed

+52
-11
lines changed

10 files changed

+52
-11
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protocol = { path = "protocol" }
2525

2626
# oxide dependencies from github
2727
dpd-client = { git = "https://github.com/oxidecomputer/dendrite", branch = "main" }
28+
omicron-common = { git = "https://github.com/oxidecomputer/omicron", branch = "main" }
2829

2930
# public dependencies from crates.io
3031
anyhow = "1.0"

dropshot-apis/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub fn environment() -> anyhow::Result<Environment> {
3232
pub fn all_apis() -> anyhow::Result<ManagedApis> {
3333
let apis = vec![ManagedApiConfig {
3434
ident: "lldpd",
35-
versions: Versions::Lockstep {
36-
version: semver::Version::new(0, 0, 1),
35+
versions: Versions::Versioned {
36+
supported_versions: lldpd_api::supported_versions(),
3737
},
3838
title: "Oxide LLDP Daemon",
3939
metadata: ManagedApiMetadata {

lldpd-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ license = "MPL-2.0"
66

77
[dependencies]
88
dropshot.workspace = true
9+
dropshot-api-manager-types.workspace = true
910
lldpd-types.workspace = true
1011
protocol.workspace = true
1112
schemars.workspace = true

lldpd-api/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use dropshot::{
1111
HttpResponseOk, HttpResponseUpdatedNoContent, PaginationParams, Path,
1212
Query, RequestContext, ResultsPage, TypedBody,
1313
};
14+
use dropshot_api_manager_types::api_versions;
1415
use lldpd_types::{
1516
build_info::BuildInfo,
1617
interfaces::{Interface, InterfaceAdd},
@@ -20,6 +21,33 @@ use protocol::types::{ChassisId, PortId, SystemCapabilities};
2021
use schemars::JsonSchema;
2122
use serde::{Deserialize, Serialize};
2223

24+
api_versions!([
25+
// WHEN CHANGING THE API (part 1 of 2):
26+
//
27+
// +- Pick a new semver and define it in the list below. The list MUST
28+
// | remain sorted, which generally means that your version should go at
29+
// | the very top.
30+
// |
31+
// | Duplicate this line, uncomment the *second* copy, update that copy for
32+
// | your new API version, and leave the first copy commented out as an
33+
// | example for the next person.
34+
// v
35+
// (next_int, IDENT),
36+
(1, INITIAL),
37+
]);
38+
39+
// WHEN CHANGING THE API (part 2 of 2):
40+
//
41+
// The call to `api_versions!` above defines constants of type
42+
// `semver::Version` that you can use in your Dropshot API definition to specify
43+
// the version when a particular endpoint was added or removed. For example, if
44+
// you used:
45+
//
46+
// (2, ADD_FOOBAR)
47+
//
48+
// Then you could use `VERSION_ADD_FOOBAR` as the version in which endpoints
49+
// were added or removed.
50+
2351
#[dropshot::api_description]
2452
pub trait LldpdApi {
2553
type Context;

lldpd-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub const fn default_port() -> u16 {
1515

1616
// Automatically generate the client bindings using Progenitor.
1717
progenitor::generate_api!(
18-
spec = "../openapi/lldpd.json",
18+
spec = "../openapi/lldpd/lldpd-latest.json",
1919
interface = Positional,
2020
inner_type = slog::Logger,
2121
pre_hook = (|log: &slog::Logger, request: &reqwest::Request| {

lldpd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ http.workspace = true
1717
lldpd-api.workspace = true
1818
lldpd-common.workspace = true
1919
lldpd-types.workspace = true
20+
omicron-common.workspace = true
2021
protocol.workspace = true
2122
signal-hook.workspace = true
2223
serde.workspace = true

lldpd/src/api_server.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::net::Ipv4Addr;
1212
use std::net::SocketAddr;
1313
use std::sync::Arc;
1414

15+
use dropshot::ClientSpecifiesVersionInHeader;
1516
use dropshot::EmptyScanParams;
1617
use dropshot::HttpError;
1718
use dropshot::HttpResponseCreated;
@@ -24,6 +25,7 @@ use dropshot::Query;
2425
use dropshot::RequestContext;
2526
use dropshot::ResultsPage;
2627
use dropshot::TypedBody;
28+
use dropshot::VersionPolicy;
2729
use dropshot::WhichPage;
2830
use lldpd_api::*;
2931
use lldpd_types::build_info::BuildInfo;
@@ -573,17 +575,22 @@ fn launch_server(
573575
default_handler_task_mode: dropshot::HandlerTaskMode::Detached,
574576
log_headers: Vec::new(),
575577
};
576-
let log = global
577-
.log
578-
.new(o!("unit" => "api-server", "server_id" => id.to_string()));
579578

580-
slog::info!(log, "starting api server {id} on {addr}");
581-
dropshot::HttpServerStarter::new(
582-
&config_dropshot,
579+
dropshot::ServerBuilder::new(
583580
http_api(),
584581
global.clone(),
585-
&log,
582+
global
583+
.log
584+
.new(o!("unit" => "api-server", "server_id" => id.to_string())),
586585
)
586+
.config(config_dropshot)
587+
.version_policy(VersionPolicy::Dynamic(Box::new(
588+
ClientSpecifiesVersionInHeader::new(
589+
omicron_common::api::VERSION_HEADER,
590+
lldpd_api::latest_version(),
591+
),
592+
)))
593+
.build_starter()
587594
.map(|s| s.start())
588595
.map_err(|e| anyhow::anyhow!(e.to_string()))
589596
}

openapi/lldpd.json renamed to openapi/lldpd/lldpd-1.0.0-c4e95f.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://oxide.computer",
88
"email": "api@oxide.computer"
99
},
10-
"version": "0.0.1"
10+
"version": "1.0.0"
1111
},
1212
"paths": {
1313
"/build-info": {

openapi/lldpd/lldpd-latest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lldpd-1.0.0-c4e95f.json

0 commit comments

Comments
 (0)