Skip to content

Refactor Discover/Server/TestConnection#34952

Merged
ravicious merged 7 commits intomasterfrom
ravicious/refactor-test-connection
Nov 28, 2023
Merged

Refactor Discover/Server/TestConnection#34952
ravicious merged 7 commits intomasterfrom
ravicious/refactor-test-connection

Conversation

@ravicious
Copy link
Copy Markdown
Member

@ravicious ravicious commented Nov 24, 2023

The TestConnection component for Connect My Computer will look very similar to the Server. However, before jumping in and adding a test connection for Connect My Computer, I first wanted to refactor Server/TestConnection away from the container pattern.

Along the way I made a couple of other changes, most significant being the removal of what could be called "reverse hook drilling" in useTestConnection. For now I inlined its implementation into TestConnection. The connection test for Connect My Computer will likely share some implementation through also calling useConnectionDiagnostic, but functions like startSshSession will be shared as just regular functions, without using hooks.

Comment on lines 27 to 28
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Regarding "reverse hook drilling": half of what useTestConnection does is calling useConnectionDiagnostic to pass its return values up.

Instead, TestConnection can call both useConnectionDiagnostic and useTestConnection. If useTestConnection needs some stuff that useConnectionDiagnostic returns, TestConnection can pass it as an argument to useTestConnection.

This makes it easier to see what hooks actually depend on and makes it easier to reuse those hooks in different places.

In this case, I removed useTestConnection altogether and move its contents to TestConnection directly. The only two other things it was doing were:

  • Creating startSshSession and testConnection: these can be shared as functions once I get around to implementing TestConnection for Connect My Computer.
  • Preparing logins: this was inlined and logins will be fetched through other means in the Connect My Computer connection test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same pattern exists in web/packages/teleport/src/Discover/Kubernetes/TestConnection/useTestConnection.ts and web/packages/teleport/src/Discover/Database/TestConnection/useTestConnection.t, do you plan on refactoring those as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't, I wanted to refactor the SSH component so that I can reuse some parts for Connect My Computer connection test more easily.

But I'll add a deprecation comment to those hooks with a link to this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is another good example of "reverse hook drilling". useConnectionDiagnostic calls useTeleport and then passes clusterId up. With useTestConnection inlined into TestConnection, TestConnection could instead call both useTeleport and useConnectionDiagnostic instead.

We could even have useClusterId to avoid having to go through useTeleport().storeUser.getClusterId. But in this case I found out that clusterId can be accessed without calling any hooks. Other connection tests depend on clusterId from here though, so for now I just marked it as deprecated.

@ravicious ravicious marked this pull request as ready for review November 24, 2023 13:19
@ravicious ravicious added the no-changelog Indicates that a PR does not require a changelog entry label Nov 24, 2023
@github-actions github-actions Bot requested review from kimlisa and rudream November 24, 2023 13:19
@ravicious ravicious requested review from ibeckermayer and removed request for rudream November 24, 2023 13:19
@ravicious ravicious force-pushed the ravicious/refactor-test-connection branch from 2392827 to 5c30ead Compare November 24, 2023 13:20
import { TestConnection } from './TestConnection';

export default {
title: 'Teleport/Discover/Shared/ConnectionDiagnostic/Server',
Copy link
Copy Markdown
Member Author

@ravicious ravicious Nov 27, 2023

Choose a reason for hiding this comment

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

BTW, wouldn't it be more convenient if Teleport/Discover/Shared/ConnectionDiagnostic held stories only for code that is shared, but connection tests for specific resources were under Teleport/Discover//TestConnection?

Otherwise you have to jump around to find the story for the connection test vs having everything for a specific resource under a single category.

This is how it looks now:

A screenshot of Storybook sidebar with stories for Discover Storybook sidebar with stories for Discover

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes that's better. i don't remember why i did it like that 🤷‍♀️

Comment on lines 27 to 28
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same pattern exists in web/packages/teleport/src/Discover/Kubernetes/TestConnection/useTestConnection.ts and web/packages/teleport/src/Discover/Database/TestConnection/useTestConnection.t, do you plan on refactoring those as well?

Copy link
Copy Markdown
Contributor

@kimlisa kimlisa left a comment

Choose a reason for hiding this comment

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

thanks for the refactor 👍

import { TestConnection } from './TestConnection';

export default {
title: 'Teleport/Discover/Shared/ConnectionDiagnostic/Server',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes that's better. i don't remember why i did it like that 🤷‍♀️

@ravicious ravicious enabled auto-merge November 28, 2023 09:29
@ravicious ravicious added this pull request to the merge queue Nov 28, 2023
Merged via the queue into master with commit c4ca562 Nov 28, 2023
@ravicious ravicious deleted the ravicious/refactor-test-connection branch November 28, 2023 09:48
@public-teleport-github-review-bot
Copy link
Copy Markdown

@ravicious See the table below for backport results.

Branch Result
branch/v14 Create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog Indicates that a PR does not require a changelog entry size/sm ui

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants