Skip to content

Commit

Permalink
Make Node have as_is and to_be fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-cluml committed Feb 15, 2024
1 parent 8e5ea7a commit 360a5f0
Show file tree
Hide file tree
Showing 10 changed files with 644 additions and 632 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
.vscode
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
### Changed

- Add `apply_target_id` field to `Node` struct for reverting node status.
- Add `apply_in_progress` field to `Node` struct for reverting node status.
- `Node` struct now has `as_is` and `to_be` `NodeSetting`s to support 2-step
node setting save & apply. `NodeInput` accordingly has `as_is` and `to_be`.

### Fixed

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ ipnet = { version = "2", features = ["serde"] }
jsonwebtoken = "9"
lazy_static = "1"
num-traits = "0.2"
oinq = { git = "https://github.com/petabi/oinq.git", tag = "0.9.1" }
# oinq = { git = "https://github.com/petabi/oinq.git", tag = "0.9.1" }
oinq = { git = "https://github.com/sophie-cluml/oinq.git", branch = "sophie/enum-configs" } # pita in discussion https://github.com/petabi/oinq/issues/58
reqwest = { version = "0.11", default-features = false, features = [
"rustls-tls-native-roots",
] }
Expand Down
4 changes: 2 additions & 2 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use self::allow_network::get_allow_networks;
pub use self::block_network::get_block_networks;
pub use self::cert::ParsedCertificate;
pub use self::customer::get_customer_networks;
pub use self::node::{get_customer_id_of_review_host, get_node_settings};
pub use self::node::get_customer_id_of_review_host;
pub use self::trusted_user_agent::get_trusted_user_agent_list;
use async_graphql::{
connection::{Connection, Edge, EmptyFields},
Expand Down Expand Up @@ -83,7 +83,7 @@ pub trait AgentManager: Send + Sync {
async fn broadcast_trusted_user_agent_list(&self, _list: &[u8]) -> Result<(), anyhow::Error>;
async fn online_apps_by_host_id(
&self,
) -> Result<HashMap<String, Vec<(String, String)>>, anyhow::Error>;
) -> Result<HashMap<String, Vec<(String, String)>>, anyhow::Error>; // (hostname, (agent_key, app_name))
async fn send_and_recv(&self, key: &str, msg: &[u8]) -> Result<Vec<u8>, anyhow::Error>;
async fn update_traffic_filter_rules(
&self,
Expand Down
8 changes: 6 additions & 2 deletions src/graphql/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,15 @@ fn convert_sensors(map: &IndexedMap, sensors: &[ID]) -> anyhow::Result<Vec<Strin
let Some(value) = map.get_by_id(i)? else {
bail!("no such sensor")
};
let value: super::node::Node = codec
let node: super::node::Node = codec
.deserialize(value.as_ref())
.context("invalid value in database")?;

converted_sensors.push(value.hostname.clone());
if let Some(as_is) = node.as_is {
converted_sensors.push(as_is.hostname.clone());
} else {
bail!("node is not yet fully configured");
}
}
Ok(converted_sensors)
}
Expand Down
71 changes: 61 additions & 10 deletions src/graphql/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ mod status;
use async_graphql::{types::ID, ComplexObject, Context, InputObject, Object, Result, SimpleObject};
use bincode::Options;
use chrono::{DateTime, TimeZone, Utc};
pub use crud::{get_customer_id_of_review_host, get_node_settings};
pub use crud::get_customer_id_of_review_host;
use input::NodeInput;
use ipnet::Ipv4Net;
use review_database::{Indexable, Indexed};
use review_database::{Indexable, Indexed, IndexedMapUpdate};
use roxy::Process as RoxyProcess;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -101,12 +101,10 @@ impl TryFrom<&NicInput> for Nic {
}
}

#[derive(Clone, Deserialize, Serialize, SimpleObject)]
#[derive(Clone, Deserialize, Serialize, SimpleObject, PartialEq)]
#[graphql(complex)]
#[allow(clippy::struct_excessive_bools)]
pub(super) struct Node {
#[graphql(skip)]
id: u32,
#[allow(clippy::struct_excessive_bools, clippy::module_name_repetitions)]
pub struct NodeSetting {
name: String,
#[graphql(skip)]
customer_id: u32,
Expand All @@ -133,6 +131,7 @@ pub(super) struct Node {
txt: bool,
smtp_eml: bool,
ftp: bool,

giganto: bool,
#[graphql(skip)]
giganto_ingestion_ip: Option<IpAddr>,
Expand Down Expand Up @@ -165,19 +164,65 @@ pub(super) struct Node {

sensors: bool,
sensor_list: HashMap<String, bool>,
}

#[derive(Clone, Deserialize, Serialize, SimpleObject, PartialEq)]
#[graphql(complex)]
pub(super) struct Node {
#[graphql(skip)]
id: u32,
creation_time: DateTime<Utc>,
pub as_is: Option<NodeSetting>,
pub to_be: Option<NodeSetting>,
}

impl IndexedMapUpdate for Node {
type Entry = Self;

fn key(&self) -> Option<&[u8]> {
Some(Indexable::key(self))
}

apply_target_id: Option<u32>,
apply_in_progress: bool,
fn apply(&self, _value: Self::Entry) -> anyhow::Result<Self::Entry> {
Ok(self.clone())
}

fn verify(&self, value: &Self::Entry) -> bool {
self == value
}
}

#[ComplexObject]
impl Node {
async fn id(&self) -> ID {
ID(self.id.to_string())
}
}

impl Node {
fn as_is_hostname_or_fallback(&self) -> &str {
if let Some(as_is) = &self.as_is {
&as_is.hostname
} else if let Some(to_be) = &self.to_be {
&to_be.hostname
} else {
panic!("Both `as_is` and `to_be` are `None`");
}
}

fn as_is_name_or_fallback(&self) -> &str {
if let Some(as_is) = &self.as_is {
&as_is.name
} else if let Some(to_be) = &self.to_be {
&to_be.name
} else {
panic!("Both `as_is` and `to_be` are `None`");
}
}
}

#[ComplexObject]
impl NodeSetting {
async fn customer_id(&self) -> ID {
ID(self.customer_id.to_string())
}
Expand Down Expand Up @@ -224,7 +269,13 @@ impl NodeTotalCount {

impl Indexable for Node {
fn key(&self) -> &[u8] {
self.name.as_bytes()
if let Some(as_is) = &self.as_is {
as_is.name.as_bytes()
} else if let Some(to_be) = &self.to_be {
to_be.name.as_bytes()
} else {
panic!("Both `as_is` and `to_be` are `None`");
}
}

fn value(&self) -> Vec<u8> {
Expand Down
Loading

0 comments on commit 360a5f0

Please sign in to comment.