Skip to content

Commit

Permalink
[wicketd-client] use more replace directives (#5671)
Browse files Browse the repository at this point in the history
I'm trying to change the format of these structs slightly as part of r8
work, and the whole process is really, unnecessarily cumbersome. It doesn't
spark joy. This is an internal API (wicket <-> wicketd) that has no
cross-version concerns in any case.
  • Loading branch information
sunshowers authored Apr 30, 2024
1 parent 09627ea commit 9a22dfb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 115 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions clients/wicketd-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MPL-2.0"
chrono.workspace = true
installinator-common.workspace = true
ipnetwork.workspace = true
omicron-common.workspace = true
progenitor.workspace = true
regress.workspace = true
reqwest = { workspace = true, features = ["rustls-tls", "stream"] }
Expand Down
33 changes: 18 additions & 15 deletions clients/wicketd-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,36 @@ progenitor::generate_api!(
RackOperationStatus = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
RackNetworkConfigV1 = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
UplinkConfig = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
PortConfigV1 = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
BgpPeerConfig = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
BgpConfig = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
RouteConfig = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
CurrentRssUserConfigInsensitive = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
CurrentRssUserConfigInsensitive = { derives = [ PartialEq, Eq, Serialize, Deserialize ] },
CurrentRssUserConfigSensitive = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
CurrentRssUserConfig = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
UserSpecifiedRackNetworkConfig = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
CurrentRssUserConfig = { derives = [ PartialEq, Eq, Serialize, Deserialize ] },
GetLocationResponse = { derives = [ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize ] },
},
replace = {
BgpConfig = omicron_common::api::internal::shared::BgpConfig,
BgpPeerConfig = omicron_common::api::internal::shared::BgpPeerConfig,
ClearUpdateStateResponse = wicket_common::rack_update::ClearUpdateStateResponse,
Duration = std::time::Duration,
EventReportForWicketdEngineSpec = wicket_common::update_events::EventReport,
IpNetwork = ipnetwork::IpNetwork,
Ipv4Network = ipnetwork::Ipv4Network,
Ipv6Network = ipnetwork::Ipv6Network,
IpNetwork = ipnetwork::IpNetwork,
M2Slot = installinator_common::M2Slot,
PortConfigV1 = omicron_common::api::internal::shared::PortConfigV1,
PortFec = omicron_common::api::internal::shared::PortFec,
PortSpeed = omicron_common::api::internal::shared::PortSpeed,
ProgressEventForGenericSpec = update_engine::events::ProgressEvent<update_engine::NestedSpec>,
ProgressEventForInstallinatorSpec = installinator_common::ProgressEvent,
ProgressEventForWicketdEngineSpec = wicket_common::update_events::ProgressEvent,
PutRssUserConfigInsensitive = wicket_common::rack_setup::PutRssUserConfigInsensitive,
ClearUpdateStateResponse = wicket_common::rack_update::ClearUpdateStateResponse,
RouteConfig = omicron_common::api::internal::shared::RouteConfig,
SpIdentifier = wicket_common::rack_update::SpIdentifier,
SpType = wicket_common::rack_update::SpType,
EventReportForWicketdEngineSpec = wicket_common::update_events::EventReport,
StepEventForWicketdEngineSpec = wicket_common::update_events::StepEvent,
ProgressEventForWicketdEngineSpec = wicket_common::update_events::ProgressEvent,
StepEventForGenericSpec = update_engine::events::StepEvent<update_engine::NestedSpec>,
ProgressEventForGenericSpec = update_engine::events::ProgressEvent<update_engine::NestedSpec>,
StepEventForInstallinatorSpec = installinator_common::StepEvent,
ProgressEventForInstallinatorSpec = installinator_common::ProgressEvent,
M2Slot = installinator_common::M2Slot,
StepEventForWicketdEngineSpec = wicket_common::update_events::StepEvent,
SwitchLocation = omicron_common::api::internal::shared::SwitchLocation,
UserSpecifiedRackNetworkConfig = wicket_common::rack_setup::UserSpecifiedRackNetworkConfig,
}
);

Expand Down
42 changes: 34 additions & 8 deletions common/src/api/internal/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::Display,
fmt,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
str::FromStr,
};
Expand Down Expand Up @@ -174,15 +174,15 @@ pub struct RackNetworkConfigV1 {
pub bfd: Vec<BfdPeerConfig>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
pub struct BgpConfig {
/// The autonomous system number for the BGP configuration.
pub asn: u32,
/// The set of prefixes for the BGP router to originate.
pub originate: Vec<Ipv4Network>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
pub struct BgpPeerConfig {
/// The autonomous sysetm number of the router the peer belongs to.
pub asn: u32,
Expand All @@ -203,7 +203,7 @@ pub struct BgpPeerConfig {
pub keepalive: Option<u64>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
pub struct BfdPeerConfig {
pub local: Option<IpAddr>,
pub remote: IpAddr,
Expand All @@ -213,15 +213,15 @@ pub struct BfdPeerConfig {
pub switch: SwitchLocation,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
pub struct RouteConfig {
/// The destination of the route.
pub destination: IpNetwork,
/// The nexthop/gateway address.
pub nexthop: IpAddr,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, JsonSchema)]
pub struct PortConfigV1 {
/// The set of routes associated with this port.
pub routes: Vec<RouteConfig>,
Expand Down Expand Up @@ -325,8 +325,8 @@ pub enum SwitchLocation {
Switch1,
}

impl Display for SwitchLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for SwitchLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SwitchLocation::Switch0 => write!(f, "switch0"),
SwitchLocation::Switch1 => write!(f, "switch1"),
Expand Down Expand Up @@ -386,6 +386,22 @@ pub enum PortSpeed {
Speed400G,
}

impl fmt::Display for PortSpeed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PortSpeed::Speed0G => write!(f, "0G"),
PortSpeed::Speed1G => write!(f, "1G"),
PortSpeed::Speed10G => write!(f, "10G"),
PortSpeed::Speed25G => write!(f, "25G"),
PortSpeed::Speed40G => write!(f, "40G"),
PortSpeed::Speed50G => write!(f, "50G"),
PortSpeed::Speed100G => write!(f, "100G"),
PortSpeed::Speed200G => write!(f, "200G"),
PortSpeed::Speed400G => write!(f, "400G"),
}
}
}

/// Switchport FEC options
#[derive(
Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Hash,
Expand All @@ -396,3 +412,13 @@ pub enum PortFec {
None,
Rs,
}

impl fmt::Display for PortFec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PortFec::Firecode => write!(f, "Firecode R-FEC"),
PortFec::None => write!(f, "None"),
PortFec::Rs => write!(f, "RS-FEC"),
}
}
}
2 changes: 1 addition & 1 deletion wicket-common/src/rack_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::net::Ipv4Addr;

/// User-specified parts of
/// [`RackNetworkConfig`](omicron_common::api::internal::shared::RackNetworkConfig).
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
pub struct UserSpecifiedRackNetworkConfig {
pub infra_ip_first: Ipv4Addr,
pub infra_ip_last: Ipv4Addr,
Expand Down
98 changes: 7 additions & 91 deletions wicket/src/cli/rack_setup/config_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use toml_edit::InlineTable;
use toml_edit::Item;
use toml_edit::Table;
use toml_edit::Value;
use wicket_common::rack_setup::UserSpecifiedRackNetworkConfig;
use wicket_common::rack_update::SpType;
use wicketd_client::types::BootstrapSledDescription;
use wicketd_client::types::CurrentRssUserConfigInsensitive;
use wicketd_client::types::IpRange;
use wicketd_client::types::UserSpecifiedRackNetworkConfig;

static TEMPLATE: &str = include_str!("config_template.toml");

Expand Down Expand Up @@ -349,31 +349,19 @@ fn populate_network_table(
#[cfg(test)]
mod tests {
use super::*;
use omicron_common::api::external::SwitchLocation;
use omicron_common::api::internal::shared::{
BgpConfig, BgpPeerConfig, PortConfigV1, PortFec, PortSpeed, RouteConfig,
};
use std::net::Ipv6Addr;
use wicket_common::rack_setup::PutRssUserConfigInsensitive;
use wicket_common::rack_update::SpIdentifier;
use wicketd_client::types::Baseboard;
use wicketd_client::types::BgpConfig;
use wicketd_client::types::BgpPeerConfig;
use wicketd_client::types::PortConfigV1;
use wicketd_client::types::PortFec;
use wicketd_client::types::PortSpeed;
use wicketd_client::types::RouteConfig;
use wicketd_client::types::SwitchLocation;

fn put_config_from_current_config(
value: CurrentRssUserConfigInsensitive,
) -> PutRssUserConfigInsensitive {
use omicron_common::api::internal::shared::BgpConfig as InternalBgpConfig;
use omicron_common::api::internal::shared::BgpPeerConfig as InternalBgpPeerConfig;
use omicron_common::api::internal::shared::PortConfigV1 as InternalPortConfig;
use omicron_common::api::internal::shared::PortFec as InternalPortFec;
use omicron_common::api::internal::shared::PortSpeed as InternalPortSpeed;
use omicron_common::api::internal::shared::RouteConfig as InternalRouteConfig;
use omicron_common::api::internal::shared::SwitchLocation as InternalSwitchLocation;
use wicket_common::rack_setup::UserSpecifiedRackNetworkConfig as InternalUserSpecifiedRackNetworkConfig;

let rnc = value.rack_network_config.unwrap();
let rack_network_config = value.rack_network_config.unwrap();

PutRssUserConfigInsensitive {
bootstrap_sleds: value
Expand All @@ -400,79 +388,7 @@ mod tests {
.collect(),
external_dns_ips: value.external_dns_ips,
ntp_servers: value.ntp_servers,
rack_network_config: InternalUserSpecifiedRackNetworkConfig {
infra_ip_first: rnc.infra_ip_first,
infra_ip_last: rnc.infra_ip_last,
ports: rnc
.ports
.iter()
.map(|config| InternalPortConfig {
routes: config
.routes
.iter()
.map(|r| InternalRouteConfig {
destination: r.destination,
nexthop: r.nexthop,
})
.collect(),
addresses: config.addresses.clone(),
bgp_peers: config
.bgp_peers
.iter()
.map(|p| InternalBgpPeerConfig {
asn: p.asn,
port: p.port.clone(),
addr: p.addr,
hold_time: p.hold_time,
connect_retry: p.connect_retry,
delay_open: p.delay_open,
idle_hold_time: p.idle_hold_time,
keepalive: p.keepalive,
})
.collect(),
port: config.port.clone(),
uplink_port_speed: match config.uplink_port_speed {
PortSpeed::Speed0G => InternalPortSpeed::Speed0G,
PortSpeed::Speed1G => InternalPortSpeed::Speed1G,
PortSpeed::Speed10G => InternalPortSpeed::Speed10G,
PortSpeed::Speed25G => InternalPortSpeed::Speed25G,
PortSpeed::Speed40G => InternalPortSpeed::Speed40G,
PortSpeed::Speed50G => InternalPortSpeed::Speed50G,
PortSpeed::Speed100G => {
InternalPortSpeed::Speed100G
}
PortSpeed::Speed200G => {
InternalPortSpeed::Speed200G
}
PortSpeed::Speed400G => {
InternalPortSpeed::Speed400G
}
},
uplink_port_fec: match config.uplink_port_fec {
PortFec::Firecode => InternalPortFec::Firecode,
PortFec::None => InternalPortFec::None,
PortFec::Rs => InternalPortFec::Rs,
},
autoneg: config.autoneg,
switch: match config.switch {
SwitchLocation::Switch0 => {
InternalSwitchLocation::Switch0
}
SwitchLocation::Switch1 => {
InternalSwitchLocation::Switch1
}
},
})
.collect(),
bgp: rnc
.bgp
.iter()
.map(|config| InternalBgpConfig {
asn: config.asn,
originate: config.originate.clone(),
})
.collect(),
},
rack_network_config,
}
}

Expand Down

0 comments on commit 9a22dfb

Please sign in to comment.