Skip to content

Commit

Permalink
fix(rust): check if tcp inlet exists before deleting it
Browse files Browse the repository at this point in the history
  • Loading branch information
0x61nas authored and adrianbenavides committed Oct 13, 2023
1 parent e263d67 commit 751d8d9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
23 changes: 18 additions & 5 deletions implementations/rust/ockam/ockam_command/src/tcp/inlet/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clap::Args;
use colorful::Colorful;
use miette::{miette, IntoDiagnostic};

use ockam::Context;
use ockam_api::nodes::models::portal::InletStatus;
use ockam_api::nodes::BackgroundNode;
use ockam_core::api::Request;

Expand Down Expand Up @@ -41,21 +43,32 @@ pub async fn run_impl(
ctx: Context,
(opts, cmd): (CommandGlobalOpts, DeleteCommand),
) -> miette::Result<()> {
let node_name = get_node_name(&opts.state, &cmd.node_opts.at_node);
let node_name = parse_node_name(&node_name)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;

// Check if alias exists
let alias = cmd.alias;
node.ask_and_get_reply::<_, InletStatus>(&ctx, Request::get(format!("/node/inlet/{alias}")))
.await?
.found()
.into_diagnostic()?
.ok_or(miette!(
"TCP inlet with alias {alias} was not found on Node {node_name}"
))?;

// Proceed with the deletion
if opts
.terminal
.confirmed_with_flag_or_prompt(cmd.yes, "Are you sure you want to delete this TCP inlet?")?
{
let alias = cmd.alias.clone();
let node_name = get_node_name(&opts.state, &cmd.node_opts.at_node);
let node_name = parse_node_name(&node_name)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;
node.tell(&ctx, Request::delete(format!("/node/inlet/{alias}")))
.await?;

opts.terminal
.stdout()
.plain(fmt_ok!(
"TCP inlet with alias {alias} on Node {node_name} has been deleted."
"TCP inlet with alias {alias} on Node {node_name} has been deleted"
))
.machine(&alias)
.json(serde_json::json!({ "tcp-inlet": { "alias": alias, "node": node_name } }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use clap::Args;
use colorful::Colorful;
use miette::{miette, IntoDiagnostic};

use ockam::Context;

use ockam_api::nodes::models::transport::TransportStatus;
use ockam_api::nodes::{models, BackgroundNode};
use ockam_core::api::Request;

Expand Down Expand Up @@ -38,21 +41,36 @@ async fn run_impl(
ctx: Context,
(opts, cmd): (CommandGlobalOpts, DeleteCommand),
) -> miette::Result<()> {
let node_name = get_node_name(&opts.state, &cmd.node_opts.at_node);
let node_name = parse_node_name(&node_name)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;

// Check if there an TCP listener with the provided address exists
let address = cmd.address;
node.ask_and_get_reply::<_, TransportStatus>(
&ctx,
Request::get(format!("/node/tcp/listener/{address}")),
)
.await?
.found()
.into_diagnostic()?
.ok_or(miette!(
"TCP listener with address {address} was not found on Node {node_name}"
))?;

// Proceed with the deletion
if opts.terminal.confirmed_with_flag_or_prompt(
cmd.yes,
"Are you sure you want to delete this TCP listener?",
)? {
let node_name = get_node_name(&opts.state, &cmd.node_opts.at_node);
let node_name = parse_node_name(&node_name)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;
let req = Request::delete("/node/tcp/listener")
.body(models::transport::DeleteTransport::new(cmd.address.clone()));
.body(models::transport::DeleteTransport::new(address.clone()));
node.tell(&ctx, req).await?;

opts.terminal
.stdout()
.plain(fmt_ok!(
"TCP listener {node_name} has been successfully deleted."
"TCP listener with address {address} on Node {node_name} has been deleted"
))
.json(serde_json::json!({ "tcp-listener": {"node": node_name } }))
.write_line()
Expand Down
23 changes: 18 additions & 5 deletions implementations/rust/ockam/ockam_command/src/tcp/outlet/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clap::Args;
use colorful::Colorful;
use miette::{miette, IntoDiagnostic};

use ockam::Context;
use ockam_api::nodes::models::portal::OutletStatus;
use ockam_api::nodes::BackgroundNode;
use ockam_core::api::Request;

Expand Down Expand Up @@ -41,21 +43,32 @@ pub async fn run_impl(
ctx: Context,
(opts, cmd): (CommandGlobalOpts, DeleteCommand),
) -> miette::Result<()> {
let node_name = get_node_name(&opts.state, &cmd.node_opts.at_node);
let node_name = parse_node_name(&node_name)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;

// Check if there an outlet with the provided alias/name exists
let alias = cmd.alias;
node.ask_and_get_reply::<_, OutletStatus>(&ctx, Request::get(format!("/node/outlet/{alias}")))
.await?
.found()
.into_diagnostic()?
.ok_or(miette!(
"TCP outlet with alias {alias} was not found on Node {node_name}"
))?;

// Proceed with the deletion
if opts.terminal.confirmed_with_flag_or_prompt(
cmd.yes,
"Are you sure you want to delete this TCP outlet?",
)? {
let alias = cmd.alias.clone();
let node_name = get_node_name(&opts.state, &cmd.node_opts.at_node);
let node_name = parse_node_name(&node_name)?;
let node = BackgroundNode::create(&ctx, &opts.state, &node_name).await?;
node.tell(&ctx, Request::delete(format!("/node/outlet/{alias}")))
.await?;

opts.terminal
.stdout()
.plain(fmt_ok!(
"TCP outlet with alias {alias} on node {node_name} has been deleted."
"TCP outlet with alias {alias} on Node {node_name} has been deleted"
))
.machine(&alias)
.json(serde_json::json!({ "tcp-outlet": { "alias": alias, "node": node_name } }))
Expand Down

0 comments on commit 751d8d9

Please sign in to comment.