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

cli: adopt existing tunnel if one exists by name #190110

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
cli: adopt existing tunnel if one exists by name
Gets rid of an error state users could sometimes get into.

Fixes #189830
  • Loading branch information
connor4312 committed Aug 9, 2023
commit 5a1d71974c201a44e25ed5c74488a94f1b00a711
34 changes: 17 additions & 17 deletions cli/src/tunnels/dev_tunnels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,16 @@ impl DevTunnels {
info!(self.log, "Creating tunnel with the name: {}", name);

let tunnel = match self.get_existing_tunnel_with_name(name).await? {
Some(e) => e,
Some(e) => {
let loc = TunnelLocator::try_from(&e).unwrap();
info!(self.log, "Adopting existing tunnel (ID={:?})", loc);
spanf!(
self.log,
self.log.span("dev-tunnel.tag.get"),
self.client.get_tunnel(&loc, &HOST_TUNNEL_REQUEST_OPTIONS)
)
.map_err(|e| wrap(e, "failed to lookup tunnel"))?
}
None => {
let new_tunnel = Tunnel {
tags: self.get_tags(name),
Expand Down Expand Up @@ -552,13 +561,13 @@ impl DevTunnels {
};

let pt = PersistedTunnel {
cluster: tunnel.cluster_id.unwrap(),
id: tunnel.tunnel_id.unwrap(),
cluster: tunnel.cluster_id.clone().unwrap(),
id: tunnel.tunnel_id.clone().unwrap(),
name: name.to_string(),
};

self.launcher_tunnel.save(Some(pt.clone()))?;
return Ok((pt, t));
Ok((pt, tunnel))
}

/// Gets the expected tunnel tags
Expand Down Expand Up @@ -668,31 +677,22 @@ impl DevTunnels {
Ok(tunnels)
}

async fn get_existing_tunnel_with_name() -> Result<Option<Tunnel>, AnyError> {
async fn get_existing_tunnel_with_name(&self, name: &str) -> Result<Option<Tunnel>, AnyError> {
let existing: Vec<Tunnel> = spanf!(
self.log,
self.log.span("dev-tunnel.rename.search"),
self.client.list_all_tunnels(&TunnelRequestOptions {
tags: vec![self.tag.to_string(), name.to_string()],
require_all_tags: true,
limit: 1,
include_ports: true,
token_scopes: vec!["host".to_string()],
..Default::default()
})
)
.map_err(|e| wrap(e, "failed to list existing tunnels"))?;

Ok(existing.first())
}

async fn check_is_name_free(&mut self, name: &str) -> Result<(), AnyError> {
if get_existing_tunnel_with_name(name).await?.is_some() {
return Err(AnyError::from(TunnelCreationFailed(
name.to_string(),
"tunnel name already in use".to_string(),
)));
};

Ok(())
Ok(existing.into_iter().next())
}

fn get_placeholder_name() -> String {
Expand Down