Skip to content

Show transceiver errors in wicket UI #8118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod disk;
pub mod ledger;
pub mod policy;
pub mod progenitor_operation_retry;
pub mod snake_case_result;
pub mod update;
pub mod vlan;
pub mod zpool_name;
Expand Down
94 changes: 94 additions & 0 deletions common/src/snake_case_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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/.

//! A serializable Result that plays nicely with OpenAPI lints.

use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

#[derive(Serialize, Deserialize)]
#[serde(rename = "Result{T}Or{E}")]
#[serde(rename_all = "snake_case")]
pub enum SnakeCaseResult<T, E> {
Ok(T),
Err(E),
}

impl<T, E> JsonSchema for SnakeCaseResult<T, E>
where
T: JsonSchema,
E: JsonSchema,
{
fn schema_name() -> String {
format!("Result{}Or{}", T::schema_name(), E::schema_name())
}

fn json_schema(
generator: &mut schemars::r#gen::SchemaGenerator,
) -> schemars::schema::Schema {
let mut ok_schema = schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::Object.into()),
..Default::default()
};
let obj = ok_schema.object();
obj.required.insert("ok".to_owned());
obj.properties.insert("ok".to_owned(), generator.subschema_for::<T>());
let mut err_schema = schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::Object.into()),
..Default::default()
};
let obj = err_schema.object();
obj.required.insert("err".to_owned());
obj.properties.insert("err".to_owned(), generator.subschema_for::<E>());
let mut schema = schemars::schema::SchemaObject::default();
schema.subschemas().one_of =
Some(vec![ok_schema.into(), err_schema.into()]);
schema.extensions.insert(
String::from("x-rust-type"),
serde_json::json!({
"crate": "std",
"version": "*",
"path": "::std::result::Result",
"parameters": [
generator.subschema_for::<T>(),
generator.subschema_for::<E>(),
],
}),
);
schema.into()
}
}

/// Serialize a result as a `SnakeCaseResult`.
pub fn serialize<S, T, E>(
value: &Result<T, E>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
T: Serialize,
E: Serialize,
{
match value {
Ok(val) => SnakeCaseResult::Ok(val),
Err(err) => SnakeCaseResult::Err(err),
}
.serialize(serializer)
}

/// Deserialize a `SnakeCaseResult` into a Result.
pub fn deserialize<'de, D, T, E>(
deserializer: D,
) -> Result<Result<T, E>, D::Error>
where
D: serde::Deserializer<'de>,
T: Deserialize<'de>,
E: Deserialize<'de>,
{
SnakeCaseResult::<T, E>::deserialize(deserializer).map(|snek| match snek {
SnakeCaseResult::Ok(x) => Ok(x),
SnakeCaseResult::Err(x) => Err(x),
})
}
28 changes: 3 additions & 25 deletions nexus/types/src/internal_api/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use futures::stream::StreamExt;
use omicron_common::api::external::MacAddr;
use omicron_common::api::external::ObjectStream;
use omicron_common::api::external::Vni;
use omicron_common::snake_case_result;
use omicron_common::snake_case_result::SnakeCaseResult;
use omicron_uuid_kinds::DemoSagaUuid;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -418,7 +420,7 @@ pub struct CompletedAttempt {
pub time_done: DateTime<Utc>,
pub elapsed: Duration,
pub request: PendingMgsUpdate,
#[serde(serialize_with = "serialize_snake_case_result")]
#[serde(serialize_with = "snake_case_result::serialize")]
#[schemars(
schema_with = "SnakeCaseResult::<UpdateCompletedHow, String>::json_schema"
)]
Expand Down Expand Up @@ -467,27 +469,3 @@ pub struct WaitingStatus {
pub next_attempt_time: DateTime<Utc>,
pub nattempts_done: u32,
}

#[derive(JsonSchema, Serialize)]
#[serde(rename = "Result{T}Or{E}")]
#[serde(rename_all = "snake_case")]
enum SnakeCaseResult<T, E> {
Ok(T),
Err(E),
}

fn serialize_snake_case_result<S, T, E>(
value: &Result<T, E>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
T: Serialize,
E: Serialize,
{
match value {
Ok(val) => SnakeCaseResult::Ok(val),
Err(err) => SnakeCaseResult::Err(err),
}
.serialize(serializer)
}
19 changes: 15 additions & 4 deletions openapi/nexus-internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,19 @@
"$ref": "#/components/schemas/PendingMgsUpdate"
},
"result": {
"x-rust-type": {
"crate": "std",
"parameters": [
{
"$ref": "#/components/schemas/UpdateCompletedHow"
},
{
"type": "string"
}
],
"path": "::std::result::Result",
"version": "*"
},
"oneOf": [
{
"type": "object",
Expand All @@ -3504,8 +3517,7 @@
},
"required": [
"ok"
],
"additionalProperties": false
]
},
{
"type": "object",
Expand All @@ -3516,8 +3528,7 @@
},
"required": [
"err"
],
"additionalProperties": false
]
}
]
},
Expand Down
Loading
Loading