Skip to content
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

feat(rust): change the type of the name and from arguments to Address #8719

Open
wants to merge 1 commit into
base: adrian/args-updates-portal-commands
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
7 changes: 6 additions & 1 deletion implementations/rust/ockam/ockam_api/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::{SocketAddr, TcpListener};
use std::str::FromStr;

use ockam_core::Result;
use ockam_core::{Address, Result};
use ockam_multiaddr::proto::{Node, Project, Service};
use ockam_multiaddr::{MultiAddr, Protocol};

Expand Down Expand Up @@ -51,6 +51,11 @@ pub fn extract_address_value(input: &str) -> Result<String, ApiError> {
Ok(addr)
}

pub fn extract_address(input: &str) -> Result<Address, ApiError> {
let value = extract_address_value(input)?;
Ok(Address::from(value))
}

pub fn get_free_address() -> Result<SocketAddr, ApiError> {
get_free_address_for("127.0.0.1")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use miette::miette;
use ockam::transport::SchemeHostnamePort;
use ockam::{Address, Context};
use ockam_abac::PolicyExpression;
use ockam_api::address::extract_address_value;
use ockam_api::address::{extract_address, extract_address_value};
use ockam_api::colors::color_primary;
use ockam_api::influxdb::portal::{InfluxDBOutletConfig, LeaseManagerConfig};
use ockam_api::influxdb::InfluxDBPortals;
Expand All @@ -26,8 +26,8 @@ pub struct CreateCommand {
/// Examples are `/service/my-outlet` or `my-outlet`.
/// If not provided, `outlet` will be used, or a random address will be generated if `outlet` is taken.
/// You will need this address when creating a InfluxDB Inlet using `ockam influxdb-inlet create`.
#[arg(value_parser = extract_address_value)]
pub name: Option<String>,
#[arg(value_parser = extract_address)]
pub name: Option<Address>,

/// Address where your InfluxDB server is running, in the format `<scheme>://<hostname>:<port>`.
/// At least the port must be provided. The default scheme is `tcp` and the default hostname is `127.0.0.1`.
Expand All @@ -40,8 +40,8 @@ pub struct CreateCommand {

/// Alternative to the <NAME> positional argument.
/// Address of your InfluxDB Outlet, which is part of a route used in other commands.
#[arg(long, display_order = 902, id = "OUTLET_ADDRESS", value_parser = extract_address_value)]
pub from: Option<String>,
#[arg(long, display_order = 902, id = "OUTLET_ADDRESS", value_parser = extract_address)]
pub from: Option<Address>,

/// Your InfluxDB Outlet will be created on this node. If you don't provide it, the default
/// node will be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use std::fmt::Write;
use ockam::transport::SchemeHostnamePort;
use ockam::Context;
use ockam_abac::PolicyExpression;
use ockam_api::address::extract_address_value;
use ockam_api::address::extract_address;
use ockam_api::colors::{color_primary, color_warn};
use ockam_api::nodes::models::services::StartKafkaOutletRequest;
use ockam_api::nodes::models::services::StartServiceRequest;
use ockam_api::nodes::BackgroundNodeClient;
use ockam_api::output::Output;
use ockam_api::{fmt_log, fmt_ok, fmt_warn};
use ockam_api::{fmt_log, fmt_ok, fmt_warn, DefaultAddress};
use ockam_core::api::Request;
use ockam_core::Address;

use crate::node::util::initialize_default_node;
use crate::util::parsers::hostname_parser;
Expand All @@ -34,16 +35,16 @@ pub struct CreateCommand {
/// Examples are `/service/my-outlet` or `my-outlet`.
/// If not provided, `/service/kafka_outlet` will be used.
/// You will need this address when creating a Kafka Inlet using `ockam kafka-inlet create`.
#[arg(default_value_t = kafka_default_outlet_addr(), value_parser = extract_address_value)]
pub name: String,
#[arg(default_value = DefaultAddress::KAFKA_OUTLET, value_parser = extract_address)]
Copy link
Contributor Author

@takaebato takaebato Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used default_value = DefaultAddress::KAFKA_OUTLET instead of the Address type like default_value_t = Address::from(DefaultAddress::KAFKA_OUTLET) because the latter outputs 0#kafka_outlet as the default value of name in the message of the kafka-outlet create --help command, which seems undesirable.

pub name: Address,

#[command(flatten)]
pub node_opts: NodeOpts,

/// Alternative to the <NAME> positional argument.
/// Address of your Kafka Outlet, which is part of a route used in other commands.
#[arg(long, id = "OUTLET_ADDRESS", visible_alias = "addr", default_value_t = kafka_default_outlet_addr(), value_parser = extract_address_value)]
pub from: String,
#[arg(long, id = "OUTLET_ADDRESS", visible_alias = "addr", default_value = DefaultAddress::KAFKA_OUTLET, value_parser = extract_address)]
pub from: Address,

/// The address of the kafka bootstrap broker
#[arg(long, visible_alias = "to", default_value_t = kafka_default_outlet_server(), value_parser = hostname_parser)]
Expand Down Expand Up @@ -83,7 +84,7 @@ impl Command for CreateCommand {
cmd.tls || cmd.bootstrap_server.is_tls(),
cmd.policy_expression,
);
let payload = StartServiceRequest::new(payload, &cmd.name);
let payload = StartServiceRequest::new(payload, cmd.name);
let req = Request::post("/node/services/kafka_outlet").body(payload);
let node =
BackgroundNodeClient::create(ctx, &opts.state, &cmd.node_opts.at_node).await?;
Expand All @@ -109,8 +110,8 @@ impl Command for CreateCommand {

impl CreateCommand {
async fn parse_args(mut self, opts: &CommandGlobalOpts) -> miette::Result<Self> {
if self.from != kafka_default_outlet_addr() {
if self.name != kafka_default_outlet_addr() {
if self.from != kafka_default_outlet_addr().into() {
if self.name != kafka_default_outlet_addr().into() {
opts.terminal.write_line(
fmt_warn!("The <NAME> argument is being overridden by the --from/--addr flag")
+ &fmt_log!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl InfluxDBOutlets {
mod tests {
use super::*;
use ockam::transport::SchemeHostnamePort;
use ockam_core::Address;

#[test]
fn tcp_outlet_config() {
Expand All @@ -74,13 +75,13 @@ mod tests {
.into_parsed_commands(Some(&default_node_name))
.unwrap();
assert_eq!(cmds.len(), 2);
assert_eq!(cmds[0].name, Some("ti1".to_string()));
assert_eq!(cmds[0].from.clone().unwrap(), "my_outlet");
assert_eq!(cmds[0].name.clone().unwrap(), Address::from("ti1"));
assert_eq!(cmds[0].from.clone().unwrap(), Address::from("my_outlet"));
assert_eq!(
cmds[0].to,
SchemeHostnamePort::new("tcp", "127.0.0.1", 6060).unwrap()
);
assert_eq!(cmds[1].name, Some("ti2".to_string()));
assert_eq!(cmds[1].name.clone().unwrap(), Address::from("ti2"));
assert!(cmds[1].from.is_none());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl KafkaOutlet {
mod tests {
use super::*;
use ockam::transport::SchemeHostnamePort;
use ockam_core::Address;

#[test]
fn kafka_outlet_config() {
Expand Down Expand Up @@ -84,7 +85,7 @@ mod tests {
let cmds = parsed
.into_parsed_commands(Some(&default_node_name))
.unwrap();
assert_eq!(cmds[0].name, "ko".to_string());
assert_eq!(cmds[0].name.clone(), Address::from("ko"));
assert_eq!(cmds[0].node_opts.at_node.as_ref(), Some(&default_node_name));

// check if the default node name is used when the configuration does not specify it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl TcpOutlets {
mod tests {
use super::*;
use ockam::transport::SchemeHostnamePort;
use ockam_core::Address;
use std::str::FromStr;

#[test]
Expand All @@ -72,15 +73,15 @@ mod tests {
.into_parsed_commands(Some(&default_node_name))
.unwrap();
assert_eq!(cmds.len(), 2);
assert_eq!(cmds[0].name.clone().unwrap(), "to1");
assert_eq!(cmds[0].name.clone().unwrap(), Address::from("to1"));
assert!(cmds[0].from.is_none());
assert_eq!(
cmds[0].to,
SchemeHostnamePort::from_str("tcp://127.0.0.1:6060").unwrap()
);
assert_eq!(cmds[0].at.as_ref().unwrap(), "n");
assert_eq!(cmds[1].name.clone().unwrap(), "to2");
assert_eq!(cmds[1].from.clone().unwrap(), "my_outlet");
assert_eq!(cmds[1].name.clone().unwrap(), Address::from("to2"));
assert_eq!(cmds[1].from.clone().unwrap(), Address::from("my_outlet"));
assert_eq!(
cmds[1].to,
SchemeHostnamePort::from_str("tls://127.0.0.1:6061").unwrap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ockam::transport::SchemeHostnamePort;
use ockam::Address;
use ockam::Context;
use ockam_abac::PolicyExpression;
use ockam_api::address::extract_address_value;
use ockam_api::address::{extract_address, extract_address_value};
use ockam_api::cli_state::journeys::{
JourneyEvent, NODE_NAME, TCP_OUTLET_AT, TCP_OUTLET_FROM, TCP_OUTLET_TO,
};
Expand All @@ -36,8 +36,8 @@ pub struct CreateCommand {
/// Examples are `/service/my-outlet` or `my-outlet`.
/// If not provided, `outlet` will be used, or a random address will be generated if `outlet` is taken.
/// You will need this address when creating a TCP Inlet using `ockam tcp-inlet create`.
#[arg(value_parser = extract_address_value)]
pub name: Option<String>,
#[arg(value_parser = extract_address)]
pub name: Option<Address>,

/// TCP address where your TCP server is running: domain:port. Your Outlet will send raw TCP traffic to it
#[arg(long, id = "SOCKET_ADDRESS", display_order = 900, value_parser = hostname_parser)]
Expand All @@ -49,8 +49,8 @@ pub struct CreateCommand {

/// Alternative to the <NAME> positional argument.
/// Address of your TCP Outlet, which is part of a route used in other commands.
#[arg(long, display_order = 902, id = "OUTLET_ADDRESS", value_parser = extract_address_value)]
pub from: Option<String>,
#[arg(long, display_order = 902, id = "OUTLET_ADDRESS", value_parser = extract_address)]
pub from: Option<Address>,

/// Your TCP Outlet will be created on this node. If you don't provide it, the default
/// node will be used
Expand Down
Loading