Connect: Create dedicated functions for connecting to resources#24445
Connect: Create dedicated functions for connecting to resources#24445
Conversation
00c26be to
d424957
Compare
ravicious
left a comment
There was a problem hiding this comment.
Looks good, the only real change I'd make is not using the spread operator.
| connectToDatabase( | ||
| appContext, | ||
| { ...db, dbUser }, | ||
| { origin: 'resource_table' } | ||
| ); |
There was a problem hiding this comment.
db includes a whole bunch of stuff that's not required by the second argument to connectToDatabase. Spreading it like this also makes it harder to find references to fields returned from makeDatabase.
I think it might be a good idea to do something like this:
| connectToDatabase( | |
| appContext, | |
| { ...db, dbUser }, | |
| { origin: 'resource_table' } | |
| ); | |
| const { uri, name, protocol } = db; | |
| connectToDatabase( | |
| appContext, | |
| { uri, name, protocol, dbUser }, | |
| { origin: 'resource_table' } | |
| ); |
There was a problem hiding this comment.
I started with a direct assignment, but then switched to the spread operator 😏 Changed.
| protocol: string; | ||
| dbUser: string; | ||
| }, | ||
| params: { |
There was a problem hiding this comment.
idk if it's just me but I'm used to params being "the" params for the given function. I like the current split into an object describing the target and a separate object with irrelevant stuff,
Maybe something like options/opts would work better here? ChatGPT suggests just calling it telemetryOptions/telemetryData, I suppose we could go with just telemetry as well.
There was a problem hiding this comment.
telemetry sounds good.
| const connectionToReuse = ctx.connectionTracker.findConnectionByDocument(doc); | ||
|
|
||
| if (connectionToReuse) { | ||
| await ctx.connectionTracker.activateItem(connectionToReuse.id, { |
There was a problem hiding this comment.
When I was working on the prototype, I totally forgot that activateItem already changes the workspace as well. 👍
| const connectionToReuse = ctx.connectionTracker.findConnectionByDocument(doc); | ||
|
|
||
| if (connectionToReuse) { | ||
| await ctx.connectionTracker.activateItem(connectionToReuse.id, { |
There was a problem hiding this comment.
I kinda wish this whole logic was more centralized, as in there would be a single place encapsulating all this logic. Because right now we create a gateway doc here, if a matching connection exists then we pass the execution to connectionTracker which by itself also handles creating a doc if one is missing.
But this is good enough for now and refactoring this would require a much bigger effort.
| server.uri | ||
| connectToNode( | ||
| appContext, | ||
| { ...server, login }, |
There was a problem hiding this comment.
Same consideration as for db above.
|
|
||
| import { DocumentOrigin } from './types'; | ||
|
|
||
| export async function connectToNode( |
There was a problem hiding this comment.
Why node and not server? I know that we call them nodes on the Teleport side but AFAIK through Connect codebase we consistently call them servers.
There was a problem hiding this comment.
No particular reason, changed to server.
| perform: login => | ||
| connectToNode( | ||
| ctx, | ||
| { ...result.resource, login }, |
There was a problem hiding this comment.
As I mentioned, it'd probably be a good idea to be more explicit here and other places in this file as well with regards to not spreading and instead explicitly listing needed fields.
There was a problem hiding this comment.
I'm only unsure about the naming - I wanted to avoid names like
createDatabaseDocumentas we already have such functions inDocumentsService. They only create documents objects, but here we do more (we change the workspace, create a document and set it as active). It seems to me that all these operations can live in a functionconnectToX.
I think that's a good decision. I was wondering what would be the best place for those "global" functions. Previously, the command launcher was a place for those global actions, at the moment I think WorkspacesService is good enough to hold that.
* Create dedicated functions for connecting to resources * Do not use spread operator * Rename `connectToNode` -> `connectToServer` * Remove unused imports
* Create dedicated functions for connecting to resources * Do not use spread operator * Rename `params` -> `telemetry` * Rename `connectToNode` -> `connectToServer` * Remove unused imports
This PR makes connecting to the node faster (without using
commandLauncher), since we know all the details needed to establish an SSH connection.I also created functions for dbs and kubes, so we can share the same code between resource tables/search bar.
I'm only unsure about the naming - I wanted to avoid names like
createDatabaseDocumentas we already have such functions inDocumentsService. They only create documents objects, but here we do more (we change the workspace, create a document and set it as active). It seems to me that all these operations can live in a functionconnectToX.For now I left
ssh-sshcommand as it is used by QuickInput.