-
Notifications
You must be signed in to change notification settings - Fork 235
feat: delete & list clients using wgc #2795
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
Open
comatory
wants to merge
11
commits into
main
Choose a base branch
from
ondrej/eng-7122-delete-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1c18ccf
feat: prepare command
comatory 6b83cb9
feat: client list command
comatory 0598b4f
feat: prepare back-end for deleting clients and related operations
comatory dfd762d
feat: delete clients CLI integration
comatory 48062b1
feat: drop listing of deleted operations
comatory c7a6696
feat: check traffic when deleting clients (+ operations)
comatory ace36b9
feat: add operation count + traffic when listing clients
comatory cf0b788
feat: provide URL when previewing deleting of clients
comatory d2cb92c
fix: missing generated client code
comatory 9f00b12
Merge remote-tracking branch 'origin/main' into ondrej/eng-7122-delet…
comatory dc3b81c
fix: missing client code
comatory File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,311 @@ | ||
| import { Command, program } from 'commander'; | ||
| import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; | ||
| import type { | ||
| DeleteClientResponse, | ||
| PreviewDeleteClientResponse, | ||
| } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_pb.js'; | ||
| import inquirer from 'inquirer'; | ||
| import pc from 'picocolors'; | ||
| import { config, getBaseHeaders } from '../../../core/config.js'; | ||
| import { BaseCommandOptions } from '../../../core/types/types.js'; | ||
|
|
||
| const createJsonSuccessOutput = ( | ||
| client: Awaited<DeleteClientResponse['client']>, | ||
| deletedOperationsCount: number, | ||
| persistedOperationsCount?: number, | ||
| hasTraffic?: boolean, | ||
| ) => ({ | ||
| status: 'success' as const, | ||
| client: { | ||
| ...client, | ||
| persistedOperationsCount: persistedOperationsCount ?? 0, | ||
| hasTraffic: hasTraffic ?? false, | ||
| }, | ||
| deletedOperationsCount, | ||
| }); | ||
|
|
||
| const createJsonErrorOutput = ({ | ||
| code, | ||
| details, | ||
| url, | ||
| hasTraffic, | ||
| operationsCount, | ||
| }: { | ||
| code: EnumStatusCode; | ||
| details?: string; | ||
| url?: string; | ||
| hasTraffic?: boolean; | ||
| operationsCount?: number; | ||
| }) => ({ | ||
| status: 'error' as const, | ||
| code, | ||
| message: 'Could not delete client.', | ||
| details, | ||
| url, | ||
| hasTraffic, | ||
| operationsCount, | ||
| }); | ||
|
|
||
| const createDeleteWarning = ({ | ||
| clientName, | ||
| operationsCount, | ||
| hasTraffic, | ||
| url, | ||
| }: { | ||
| clientName: string; | ||
| operationsCount: number; | ||
| hasTraffic: boolean; | ||
| url: string; | ||
| }) => { | ||
| const message = `Client '${clientName}' has ${operationsCount} persisted operation(s).`; | ||
|
|
||
| if (hasTraffic) { | ||
| return `${message} One or more operations have traffic. See details:\n${url}\n`; | ||
| } | ||
|
|
||
| return message; | ||
| }; | ||
|
|
||
| const fetchPreviewDeleteClient = async ( | ||
| client: BaseCommandOptions['client'], | ||
| { | ||
| fedGraphName, | ||
| namespace, | ||
| clientName, | ||
| }: { | ||
| fedGraphName: string; | ||
| namespace: string; | ||
| clientName: string; | ||
| }, | ||
| ): Promise< | ||
| | { | ||
| status: 'success'; | ||
| response: PreviewDeleteClientResponse; | ||
| } | ||
| | { | ||
| status: 'error'; | ||
| error: Error; | ||
| } | ||
| > => { | ||
| try { | ||
| const response = await client.platform.previewDeleteClient( | ||
| { | ||
| fedGraphName, | ||
| namespace, | ||
| clientName, | ||
| }, | ||
| { | ||
| headers: getBaseHeaders(), | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| status: 'success' as const, | ||
| response, | ||
| }; | ||
| } catch (err) { | ||
| return { | ||
| status: 'error' as const, | ||
| error: err instanceof Error ? err : new Error('An unknown error occurred.'), | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| const deleteClient = async ( | ||
| client: BaseCommandOptions['client'], | ||
| { | ||
| fedGraphName, | ||
| namespace, | ||
| clientName, | ||
| }: { | ||
| fedGraphName: string; | ||
| namespace: string; | ||
| clientName: string; | ||
| }, | ||
| ): Promise< | ||
| | { | ||
| status: 'success'; | ||
| response: DeleteClientResponse; | ||
| } | ||
| | { | ||
| status: 'error'; | ||
| error: Error; | ||
| } | ||
| > => { | ||
| try { | ||
| const response = await client.platform.deleteClient( | ||
| { | ||
| fedGraphName, | ||
| namespace, | ||
| clientName, | ||
| }, | ||
| { | ||
| headers: getBaseHeaders(), | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| status: 'success' as const, | ||
| response, | ||
| }; | ||
| } catch (err) { | ||
| return { | ||
| status: 'error', | ||
| error: err instanceof Error ? err : new Error('An unknown error occurred.'), | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| export default (opts: BaseCommandOptions) => { | ||
| const command = new Command('delete'); | ||
| command.description('Deletes a registered GraphQL client'); | ||
| command.option('-n, --namespace <string>', 'The namespace of the federated graph or monograph.', 'default'); | ||
| command.option('-j, --json', 'Prints to the console in json format instead of text'); | ||
| command.option( | ||
| '-f, --force', | ||
| 'Deletes the client without confirmation. Required with --json if operations would be deleted or have traffic.', | ||
| ); | ||
| command.argument('<graph-name>', 'The name of the federated graph or monograph.'); | ||
| command.argument('<client-name>', 'The name of the registered GraphQL client.'); | ||
|
|
||
| command.action(async (graphName, clientName, options) => { | ||
| const previewResponseMetadata = await fetchPreviewDeleteClient(opts.client, { | ||
| fedGraphName: graphName, | ||
| namespace: options.namespace, | ||
| clientName, | ||
| }); | ||
|
|
||
| if (previewResponseMetadata.status === 'error') { | ||
| if (options.json) { | ||
| console.log( | ||
| JSON.stringify( | ||
| createJsonErrorOutput({ | ||
| code: EnumStatusCode.ERR, | ||
| details: previewResponseMetadata.error.message, | ||
| }), | ||
| ), | ||
| ); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| program.error(pc.red(previewResponseMetadata.error.message)); | ||
| } | ||
|
|
||
| const previewResp = previewResponseMetadata.response; | ||
|
|
||
| if (previewResp.response?.code !== EnumStatusCode.OK) { | ||
| if (options.json) { | ||
| const output = createJsonErrorOutput({ | ||
| code: previewResp.response?.code ?? EnumStatusCode.ERR, | ||
| details: previewResp.response?.details, | ||
| }); | ||
| console.log(JSON.stringify(output)); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| console.log(pc.red(previewResp.response?.details)); | ||
| program.error(pc.red('Could not delete client.')); | ||
| } | ||
|
|
||
| if ((previewResp.persistedOperationsCount > 0 || previewResp.hasTraffic) && !options.force) { | ||
| const studioUrlObj = new URL( | ||
| `${previewResp.organizationSlug}/${options.namespace}/graph/${graphName}/operations`, | ||
| config.webURL, | ||
| ); | ||
| studioUrlObj.searchParams.set('clientNames', previewResp.client?.name ?? ''); | ||
| const studioUrl = studioUrlObj.toString(); | ||
| const warning = createDeleteWarning({ | ||
| clientName, | ||
| operationsCount: previewResp.persistedOperationsCount, | ||
| hasTraffic: previewResp.hasTraffic, | ||
| url: studioUrl, | ||
| }); | ||
|
|
||
| if (options.json) { | ||
| const output = createJsonErrorOutput({ | ||
| code: EnumStatusCode.ERR, | ||
| details: warning, | ||
| url: studioUrl, | ||
| hasTraffic: previewResp.hasTraffic, | ||
| operationsCount: previewResp.persistedOperationsCount, | ||
| }); | ||
| console.log(JSON.stringify(output)); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| const deletionConfirmed = await inquirer.prompt({ | ||
| name: 'confirmDeletion', | ||
| type: 'confirm', | ||
| message: `${warning} Deleting it will also delete those operations. Continue?`, | ||
| }); | ||
|
|
||
| if (!deletionConfirmed.confirmDeletion) { | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const deleteClientResponseMetadata = await deleteClient(opts.client, { | ||
| fedGraphName: graphName, | ||
| namespace: options.namespace, | ||
| clientName, | ||
| }); | ||
|
|
||
| if (deleteClientResponseMetadata.status === 'error') { | ||
| if (options.json) { | ||
| console.log( | ||
| JSON.stringify( | ||
| createJsonErrorOutput({ | ||
| code: EnumStatusCode.ERR, | ||
| details: deleteClientResponseMetadata.error.message, | ||
| }), | ||
| ), | ||
| ); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
| program.error(pc.red(deleteClientResponseMetadata.error.message)); | ||
| } | ||
|
|
||
| const resp = deleteClientResponseMetadata.response; | ||
|
|
||
| if (resp.response?.code !== EnumStatusCode.OK) { | ||
| if (options.json) { | ||
| const output = createJsonErrorOutput({ | ||
| code: resp.response?.code ?? EnumStatusCode.ERR, | ||
| details: resp.response?.details, | ||
| }); | ||
| console.log(JSON.stringify(output)); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| console.log(pc.red(resp.response?.details)); | ||
| program.error(pc.red('Could not delete client.')); | ||
| } | ||
|
|
||
| if (options.json) { | ||
| const output = createJsonSuccessOutput( | ||
| resp.client, | ||
| resp.deletedOperationsCount, | ||
| previewResp.persistedOperationsCount, | ||
| previewResp.hasTraffic, | ||
| ); | ||
| console.log(JSON.stringify(output)); | ||
| return; | ||
| } | ||
|
|
||
| console.log( | ||
| pc.dim( | ||
| pc.green( | ||
| `Client '${clientName}' was deleted. Deleted ${resp.deletedOperationsCount} related persisted operation(s).`, | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| return command; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.