Skip to content

Commit

Permalink
Add a Diff Visitor for BlueprintZoneConfigs (#7336)
Browse files Browse the repository at this point in the history
This visitor walks a diffus generated diff and provides callbacks for
users to hook into the parts of the diff that they care about.

To keep things minimal, we don't provide callback trait methods for
copies (when nothing changes), and we don't provide methods for most
diffus_derive generated types (the ones that start with `Edited`).

We anticipate composing these visitors to build up a full diff of a
Blueprint. They can also be used individually as demonstrated with the
included tests.
  • Loading branch information
andrewjstone authored Jan 15, 2025
1 parent 1c92838 commit c03eb51
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 13 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ zone = { version = "0.3", default-features = false, features = ["async"] }
# the kinds). However, uses of omicron-uuid-kinds _within omicron_ will have
# std and the other features enabled because they'll refer to it via
# omicron-uuid-kinds.workspace = true.
newtype-uuid = { version = "1.1.3", default-features = false }
newtype-uuid = { version = "1.2.1", default-features = false }
omicron-uuid-kinds = { path = "uuid-kinds", features = ["serde", "schemars08", "uuid-v4"] }

# NOTE: The test profile inherits from the dev profile, so settings under
Expand Down
25 changes: 23 additions & 2 deletions common/src/api/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,27 @@ impl From<ByteCount> for i64 {
PartialEq,
PartialOrd,
Serialize,
Diffus,
)]
pub struct Generation(u64);

// We have to manually implement `Diffable` because this is newtype with private
// data, and we want to see the diff on the newtype not the inner data.
impl<'a> Diffable<'a> for Generation {
type Diff = (&'a Generation, &'a Generation);

fn diff(&'a self, other: &'a Self) -> edit::Edit<'a, Self> {
if self == other {
edit::Edit::Copy(self)
} else {
edit::Edit::Change {
before: self,
after: other,
diff: (self, other),
}
}
}
}

impl Generation {
pub const fn new() -> Generation {
Generation(1)
Expand Down Expand Up @@ -1947,7 +1964,11 @@ impl<'a> Diffable<'a> for MacAddr {
if self == other {
edit::Edit::Copy(self)
} else {
edit::Edit::Change((self, other))
edit::Edit::Change {
before: self,
after: other,
diff: (self, other),
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions nexus/types/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use strum::IntoEnumIterator;
mod blueprint_diff;
mod blueprint_display;
mod clickhouse;
pub mod diff_visitors;
pub mod execution;
pub mod id_map;
mod network_resources;
Expand All @@ -62,6 +63,7 @@ mod tri_map;
mod zone_type;

pub use clickhouse::ClickhouseClusterConfig;
pub use diff_visitors::BpVisitorContext;
pub use network_resources::AddNetworkResourceError;
pub use network_resources::OmicronZoneExternalFloatingAddr;
pub use network_resources::OmicronZoneExternalFloatingIp;
Expand Down
55 changes: 55 additions & 0 deletions nexus/types/src/deployment/diff_visitors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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/.

//! An API for visiting diffs between blueprints generated via [diffus](https://github.com/oxidecomputer/diffus)
//!
//! Modelled after [`syn::visit`](https://docs.rs/syn/1/syn/visit).
pub mod visit_blueprint_zones_config;

use diffus::edit::enm;
use omicron_uuid_kinds::{OmicronZoneUuid, SledUuid};

use super::{
BlueprintZoneConfig, BlueprintZoneDisposition, BlueprintZoneType,
BlueprintZonesConfig, EditedBlueprintZoneConfig,
};

/// A context for blueprint related visitors
#[derive(Debug, Clone, Default)]
pub struct BpVisitorContext {
pub sled_id: Option<SledUuid>,
pub zone_id: Option<OmicronZoneUuid>,
}

#[derive(Debug, Clone, Copy)]
pub struct Change<'e, T> {
pub before: &'e T,
pub after: &'e T,
}

impl<'e, T> Change<'e, T> {
pub fn new(before: &'e T, after: &'e T) -> Change<'e, T> {
Change { before, after }
}
}

impl<'e, T> From<(&'e T, &'e T)> for Change<'e, T> {
fn from(value: (&'e T, &'e T)) -> Self {
Change::new(value.0, value.1)
}
}

impl<'e, T, Diff> From<&enm::Edit<'e, T, Diff>> for Change<'e, T> {
fn from(value: &enm::Edit<'e, T, Diff>) -> Self {
match value {
enm::Edit::VariantChanged(before, after) => {
Change::new(before, after)
}
enm::Edit::AssociatedChanged { before, after, .. } => {
Change::new(before, after)
}
}
}
}
Loading

0 comments on commit c03eb51

Please sign in to comment.