Skip to content

Commit

Permalink
feat(rust): Improve JSON output of ockam node show
Browse files Browse the repository at this point in the history
Improves the output of the command `ockam node show --output json`
adding all the information that is being displayed with the plain
output. Refactors the plain response to build it using the Output
trait erasing the a clippy alert in the process.

Closes #6142
  • Loading branch information
kriogenia committed Oct 3, 2023
1 parent eae2711 commit 70938a2
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 196 deletions.
19 changes: 19 additions & 0 deletions implementations/rust/ockam/ockam_api/src/nodes/models/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use ockam_core::{Address, Route};
use serde::Serializer;

use crate::{addr_to_multiaddr, route_to_multiaddr};

pub fn serialize_as_multiaddr<T, S>(t: &T, s: S) -> Result<S::Ok, S::Error>
where
T: Into<Address> + Clone,
S: Serializer,
{
s.serialize_some(&addr_to_multiaddr(t.clone()))
}

pub fn serialize_route_as_multiaddr<S>(route: &String, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_some(&Route::parse(route).and_then(|r| route_to_multiaddr(&r)))
}
2 changes: 2 additions & 0 deletions implementations/rust/ockam/ockam_api/src/nodes/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
///
/// This module is only a type facade and should not have any logic of
/// its own
pub mod address;
pub mod base;
pub mod credentials;
pub mod flow_controls;
Expand All @@ -10,5 +11,6 @@ pub mod portal;
pub mod relay;
pub mod secure_channel;
pub mod services;
pub mod show;
pub mod transport;
pub mod workers;
17 changes: 14 additions & 3 deletions implementations/rust/ockam/ockam_api/src/nodes/models/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ impl CreateOutlet {
#[rustfmt::skip]
#[cbor(map)]
pub struct InletStatus {
#[serde(rename = "listen_address")]
#[n(1)] pub bind_addr: String,
#[serde(skip)]
#[n(2)] pub worker_addr: String,
#[serde(skip)]
#[n(3)] pub alias: String,
/// An optional status payload
#[serde(skip)]
#[n(4)] pub payload: Option<String>,
#[serde(rename = "route_to_outlet", serialize_with = "super::address::serialize_route_as_multiaddr")]
#[n(5)] pub outlet_route: String,
}

Expand Down Expand Up @@ -191,10 +196,14 @@ impl InletStatus {
#[rustfmt::skip]
#[cbor(map)]
pub struct OutletStatus {
#[serde(rename = "forward_address")]
#[n(1)] pub socket_addr: SocketAddr,
#[serde(rename = "address", serialize_with = "super::address::serialize_as_multiaddr")]
#[n(2)] pub worker_addr: Address,
#[serde(skip)]
#[n(3)] pub alias: String,
/// An optional status payload
#[serde(skip)]
#[n(4)] pub payload: Option<String>,
}

Expand Down Expand Up @@ -237,10 +246,11 @@ impl OutletStatus {
}

/// Response body when returning a list of Inlets
#[derive(Debug, Clone, Decode, Encode)]
#[derive(Debug, Clone, Decode, Encode, Serialize)]
#[rustfmt::skip]
#[cbor(map)]
pub struct InletList {
#[serde(rename = "inlets")]
#[n(1)] pub list: Vec<InletStatus>
}

Expand All @@ -251,15 +261,16 @@ impl InletList {
}

/// Response body when returning a list of Outlets
#[derive(Debug, Clone, Decode, Encode)]
#[derive(Debug, Clone, Decode, Encode, Serialize)]
#[rustfmt::skip]
#[cbor(map)]
pub struct OutletList {
#[serde(rename = "outlets")]
#[n(1)] pub list: Vec<OutletStatus>
}

impl OutletList {
pub fn new(list: Vec<OutletStatus>) -> Self {
Self { list }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::time::Duration;

use minicbor::{Decode, Encode};
use serde::Serialize;

use ockam::identity::{Identifier, DEFAULT_TIMEOUT};
use ockam_core::flow_control::FlowControlId;
use ockam_core::{route, Address, Result};
use ockam_multiaddr::MultiAddr;
use serde::Serialize;

use crate::error::ApiError;
use crate::nodes::registry::{SecureChannelInfo, SecureChannelListenerInfo};
Expand Down Expand Up @@ -144,11 +144,13 @@ impl ShowSecureChannelListenerRequest {
}

/// Response body to show a Secure Channel Listener
#[derive(Debug, Clone, Decode, Encode)]
#[derive(Debug, Clone, Decode, Encode, Serialize)]
#[rustfmt::skip]
#[cbor(map)]
pub struct ShowSecureChannelListenerResponse {
#[n(1)] pub addr: Address,
#[serde(rename = "address", serialize_with = "super::address::serialize_as_multiaddr")]
#[n(1)] pub addr: Address,
#[serde(rename = "flow_control")]
#[n(2)] pub flow_control_id: FlowControlId,
}

Expand Down Expand Up @@ -236,10 +238,11 @@ impl ShowSecureChannelResponse {
}
}

#[derive(Debug, Clone, Decode, Encode)]
#[derive(Debug, Clone, Decode, Encode, Serialize)]
#[rustfmt::skip]
#[cbor(map)]
pub struct SecureChannelListenersList {
#[serde(rename = "secure_channel_listeners")]
#[n(1)] pub list: Vec<ShowSecureChannelListenerResponse>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ impl StartOktaIdentityProviderRequest {
#[rustfmt::skip]
#[cbor(map)]
pub struct ServiceStatus {
#[serde(rename = "address", serialize_with = "super::address::serialize_as_multiaddr")]
#[n(2)] pub addr: String,
#[serde(rename = "type")]
#[n(3)] pub service_type: String,
}

Expand All @@ -383,6 +385,7 @@ impl ServiceStatus {
#[rustfmt::skip]
#[cbor(map)]
pub struct ServiceList {
#[serde(rename = "services")]
#[n(1)] pub list: Vec<ServiceStatus>
}

Expand Down
76 changes: 76 additions & 0 deletions implementations/rust/ockam/ockam_api/src/nodes/models/show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use ockam_multiaddr::{
proto::{DnsAddr, Node, Tcp},
MultiAddr,
};
use serde::Serialize;

use super::{
portal::{InletList, OutletList},
secure_channel::SecureChannelListenersList,
services::ServiceList,
transport::TransportList,
};

#[derive(Debug, Serialize)]
pub struct ShowNodeResponse<'a> {
pub is_default: bool,
pub name: &'a str,
pub is_up: bool,
pub route: RouteToNode,
#[serde(skip_serializing_if = "Option::is_none")]
pub identity: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub tcp_listeners: Option<TransportList>,
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub secure_channel_listeners: Option<SecureChannelListenersList>,
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub inlets: Option<InletList>,
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub outlets: Option<OutletList>,
#[serde(skip_serializing_if = "Option::is_none", flatten)]
pub service_list: Option<ServiceList>,
}
#[derive(Debug, Serialize)]
pub struct RouteToNode {
#[serde(skip_serializing_if = "Option::is_none")]
pub short: Option<MultiAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub verbose: Option<MultiAddr>,
}

impl<'a> ShowNodeResponse<'a> {
pub fn new(
is_default: bool,
name: &'a str,
is_up: bool,
node_port: Option<u16>,
) -> ShowNodeResponse {
let mut m = MultiAddr::default();
let short = m.push_back(Node::new(name)).ok().map(|_| m);

let verbose = node_port
.and_then(|port| {
let mut m = MultiAddr::default();
if m.push_back(DnsAddr::new("localhost")).is_ok()
&& m.push_back(Tcp::new(port)).is_ok()
{
Some(m)
} else {
None
}
});

ShowNodeResponse {
is_default,
name,
is_up,
route: RouteToNode { short, verbose },
identity: None,
tcp_listeners: None,
secure_channel_listeners: None,
inlets: None,
outlets: None,
service_list: None,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ use ockam_core::flow_control::FlowControlId;
use ockam_core::{Error, Result};
use ockam_multiaddr::proto::Worker;
use ockam_multiaddr::MultiAddr;
use serde::Serialize;
use std::net::SocketAddrV4;

/// Response body when interacting with a transport
#[derive(Debug, Clone, Decode, Encode)]
#[derive(Debug, Clone, Decode, Encode, Serialize)]
#[rustfmt::skip]
#[cbor(map)]
pub struct TransportStatus {
/// The type of transport to create
#[serde(rename = "type")]
#[n(1)] pub tt: TransportType,
/// The mode the transport should operate in
#[serde(rename = "mode")]
#[n(2)] pub tm: TransportMode,
/// Corresponding socket address
#[serde(rename = "socket")]
#[n(3)] pub socket_addr: String,
/// Corresponding worker address
#[serde(rename = "worker")]
#[n(4)] pub worker_addr: String,
/// Corresponding worker address
#[serde(skip)]
#[n(5)] pub processor_address: String,
/// Corresponding flow control id
#[serde(rename = "flow_control")]
#[n(6)] pub flow_control_id: FlowControlId,
}

Expand Down Expand Up @@ -58,15 +65,16 @@ impl TransportStatus {
}

/// Response body when interacting with a transport
#[derive(Debug, Clone, Decode, Encode)]
#[derive(Debug, Clone, Decode, Encode, Serialize)]
#[rustfmt::skip]
#[cbor(map)]
pub struct TransportList {
#[serde(rename = "transports")]
#[n(1)] pub list: Vec<TransportStatus>
}

impl TransportList {
pub fn new(list: Vec<TransportStatus>) -> Self {
Self { list }
}
}
}
Loading

0 comments on commit 70938a2

Please sign in to comment.