cli: improve code tunnel with existing tunnels (#188091)

- Apply the name/tunnel-name from the CLI to automatically
	(do a normal tag sync). Previously the CLI could host tunnels that
	were unusable unless the consumer did the tag setup, which they
	should not.
- Allow "tunnel ID" to be specified in the new `<id>.<cluster>` format
  that devtunnels has adopted.
This commit is contained in:
Connor Peet
2023-07-17 10:56:27 -07:00
committed by GitHub
parent 1bd2fc7937
commit bafd442c4e
3 changed files with 59 additions and 20 deletions

View File

@@ -229,7 +229,7 @@ lazy_static! {
#[derive(Clone, Debug)]
pub struct ExistingTunnel {
/// Name you'd like to assign preexisting tunnel to use to connect to the VS Code Server
pub tunnel_name: String,
pub tunnel_name: Option<String>,
/// Token to authenticate and use preexisting tunnel
pub host_token: String,
@@ -393,7 +393,12 @@ impl DevTunnels {
};
tunnel = self
.sync_tunnel_tags(&persisted.name, tunnel, &HOST_TUNNEL_REQUEST_OPTIONS)
.sync_tunnel_tags(
&self.client,
&persisted.name,
tunnel,
&HOST_TUNNEL_REQUEST_OPTIONS,
)
.await?;
let locator = TunnelLocator::try_from(&tunnel).unwrap();
@@ -532,6 +537,7 @@ impl DevTunnels {
/// other version tags.
async fn sync_tunnel_tags(
&self,
client: &TunnelManagementClient,
name: &str,
tunnel: Tunnel,
options: &TunnelRequestOptions,
@@ -558,7 +564,7 @@ impl DevTunnels {
let result = spanf!(
self.log,
self.log.span("dev-tunnel.protocol-tag-update"),
self.client.update_tunnel(&tunnel_update, options)
client.update_tunnel(&tunnel_update, options)
);
result.map_err(|e| wrap(e, "tunnel tag update failed").into())
@@ -639,6 +645,12 @@ impl DevTunnels {
Ok(())
}
fn get_placeholder_name() -> String {
let mut n = clean_hostname_for_tunnel(&gethostname::gethostname().to_string_lossy());
n.make_ascii_lowercase();
n
}
async fn get_name_for_tunnel(
&mut self,
preferred_name: Option<&str>,
@@ -670,10 +682,7 @@ impl DevTunnels {
use_random_name = true;
}
let mut placeholder_name =
clean_hostname_for_tunnel(&gethostname::gethostname().to_string_lossy());
placeholder_name.make_ascii_lowercase();
let mut placeholder_name = Self::get_placeholder_name();
if !is_name_free(&placeholder_name) {
for i in 2.. {
let fixed_name = format!("{}{}", placeholder_name, i);
@@ -715,7 +724,10 @@ impl DevTunnels {
tunnel: ExistingTunnel,
) -> Result<ActiveTunnel, AnyError> {
let tunnel_details = PersistedTunnel {
name: tunnel.tunnel_name,
name: match tunnel.tunnel_name {
Some(n) => n,
None => Self::get_placeholder_name(),
},
id: tunnel.tunnel_id,
cluster: tunnel.cluster,
};
@@ -725,10 +737,23 @@ impl DevTunnels {
tunnel.host_token.clone(),
));
let client = mgmt.into();
self.sync_tunnel_tags(
&client,
&tunnel_details.name,
Tunnel {
cluster_id: Some(tunnel_details.cluster.clone()),
tunnel_id: Some(tunnel_details.id.clone()),
..Default::default()
},
&HOST_TUNNEL_REQUEST_OPTIONS,
)
.await?;
self.start_tunnel(
tunnel_details.locator(),
&tunnel_details,
mgmt.into(),
client,
StaticAccessTokenProvider::new(tunnel.host_token),
)
.await