-
Notifications
You must be signed in to change notification settings - Fork 737
feat(cwl): Initialize TailLogGroup command with Wizard #5722
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
108 changes: 108 additions & 0 deletions
108
packages/core/src/awsService/cloudWatchLogs/commands/tailLogGroup.ts
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,108 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as nls from 'vscode-nls' | ||
| import { DefaultCloudWatchLogsClient } from '../../../shared/clients/cloudWatchLogsClient' | ||
| import { createBackButton, createExitButton, createHelpButton } from '../../../shared/ui/buttons' | ||
| import { createInputBox } from '../../../shared/ui/inputPrompter' | ||
| import { DataQuickPickItem } from '../../../shared/ui/pickerPrompter' | ||
| import { Wizard } from '../../../shared/wizards/wizard' | ||
| import { CloudWatchLogsGroupInfo } from '../registry/logDataRegistry' | ||
| import { RegionSubmenu, RegionSubmenuResponse } from '../../../shared/ui/common/regionSubmenu' | ||
| import { CancellationError } from '../../../shared/utilities/timeoutUtils' | ||
| import { LogStreamFilterResponse, LogStreamFilterSubmenu } from '../liveTailLogStreamSubmenu' | ||
| import { getLogger, ToolkitError } from '../../../shared' | ||
| import { cwlFilterPatternHelpUrl } from '../../../shared/constants' | ||
|
|
||
| const localize = nls.loadMessageBundle() | ||
|
|
||
| export interface TailLogGroupWizardResponse { | ||
| regionLogGroupSubmenuResponse: RegionSubmenuResponse<string> | ||
| logStreamFilter: LogStreamFilterResponse | ||
| filterPattern: string | ||
| } | ||
|
|
||
| export async function tailLogGroup(logData?: { regionName: string; groupName: string }): Promise<void> { | ||
| const wizard = new TailLogGroupWizard(logData) | ||
| const wizardResponse = await wizard.run() | ||
| if (!wizardResponse) { | ||
| throw new CancellationError('user') | ||
| } | ||
|
|
||
| //TODO: Remove Log. For testing while we aren't yet consuming the wizardResponse. | ||
| getLogger().info(JSON.stringify(wizardResponse)) | ||
| } | ||
|
|
||
| export class TailLogGroupWizard extends Wizard<TailLogGroupWizardResponse> { | ||
| public constructor(logGroupInfo?: CloudWatchLogsGroupInfo) { | ||
| super({ | ||
| initState: { | ||
| regionLogGroupSubmenuResponse: logGroupInfo | ||
| ? { | ||
| data: logGroupInfo.groupName, | ||
| region: logGroupInfo.regionName, | ||
| } | ||
| : undefined, | ||
| }, | ||
| }) | ||
| this.form.regionLogGroupSubmenuResponse.bindPrompter(createRegionLogGroupSubmenu) | ||
| this.form.logStreamFilter.bindPrompter((state) => { | ||
| if (!state.regionLogGroupSubmenuResponse?.data) { | ||
| throw new ToolkitError('LogGroupName is null') | ||
| } | ||
| return new LogStreamFilterSubmenu( | ||
| state.regionLogGroupSubmenuResponse.data, | ||
| state.regionLogGroupSubmenuResponse.region | ||
| ) | ||
| }) | ||
| this.form.filterPattern.bindPrompter((state) => createFilterPatternPrompter()) | ||
| } | ||
| } | ||
|
|
||
| export function createRegionLogGroupSubmenu(): RegionSubmenu<string> { | ||
| return new RegionSubmenu( | ||
| getLogGroupQuickPickOptions, | ||
| { | ||
| title: localize('AWS.cwl.tailLogGroup.logGroupPromptTitle', 'Select Log Group to tail'), | ||
| buttons: [createExitButton()], | ||
| }, | ||
| { title: localize('AWS.cwl.tailLogGroup.regionPromptTitle', 'Select Region for Log Group') }, | ||
| 'LogGroups' | ||
| ) | ||
| } | ||
|
|
||
| async function getLogGroupQuickPickOptions(regionCode: string): Promise<DataQuickPickItem<string>[]> { | ||
| const client = new DefaultCloudWatchLogsClient(regionCode) | ||
| const logGroups = client.describeLogGroups() | ||
|
|
||
| const logGroupsOptions: DataQuickPickItem<string>[] = [] | ||
|
|
||
| for await (const logGroupObject of logGroups) { | ||
| if (!logGroupObject.arn || !logGroupObject.logGroupName) { | ||
| throw new ToolkitError('LogGroupObject name or arn undefined') | ||
| } | ||
|
|
||
| logGroupsOptions.push({ | ||
| label: logGroupObject.logGroupName, | ||
| data: formatLogGroupArn(logGroupObject.arn), | ||
| }) | ||
| } | ||
|
|
||
| return logGroupsOptions | ||
| } | ||
|
|
||
| function formatLogGroupArn(logGroupArn: string): string { | ||
| return logGroupArn.endsWith(':*') ? logGroupArn.substring(0, logGroupArn.length - 2) : logGroupArn | ||
| } | ||
|
|
||
| export function createFilterPatternPrompter() { | ||
| const helpUri = cwlFilterPatternHelpUrl | ||
| return createInputBox({ | ||
| title: 'Provide log event filter pattern', | ||
| placeholder: 'filter pattern (case sensitive; empty matches all)', | ||
| prompt: 'Optional pattern to use to filter the results to include only log events that match the pattern.', | ||
| buttons: [createHelpButton(helpUri), createBackButton(), createExitButton()], | ||
| }) | ||
| } |
166 changes: 166 additions & 0 deletions
166
packages/core/src/awsService/cloudWatchLogs/liveTailLogStreamSubmenu.ts
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,166 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import { Prompter, PromptResult } from '../../shared/ui/prompter' | ||
| import { DefaultCloudWatchLogsClient } from '../../shared/clients/cloudWatchLogsClient' | ||
| import { createCommonButtons } from '../../shared/ui/buttons' | ||
| import { createInputBox, InputBoxPrompter } from '../../shared/ui/inputPrompter' | ||
| import { createQuickPick, DataQuickPickItem, QuickPickPrompter } from '../../shared/ui/pickerPrompter' | ||
| import { pageableToCollection } from '../../shared/utilities/collectionUtils' | ||
| import { CloudWatchLogs } from 'aws-sdk' | ||
| import { isValidResponse, StepEstimator } from '../../shared/wizards/wizard' | ||
| import { isNonNullable } from '../../shared/utilities/tsUtils' | ||
| import { | ||
| startLiveTailHelpUrl, | ||
| startLiveTailLogStreamNamesHelpUrl, | ||
| startLiveTailLogStreamPrefixHelpUrl, | ||
| } from '../../shared/constants' | ||
|
|
||
| export type LogStreamFilterType = 'menu' | 'prefix' | 'specific' | 'all' | ||
|
|
||
| export interface LogStreamFilterResponse { | ||
| readonly filter?: string | ||
| readonly type: LogStreamFilterType | ||
| } | ||
|
|
||
| export class LogStreamFilterSubmenu extends Prompter<LogStreamFilterResponse> { | ||
| private logStreamPrefixRegEx = /^[^:*]*$/ | ||
| private currentState: LogStreamFilterType = 'menu' | ||
| private steps?: [current: number, total: number] | ||
| private region: string | ||
| private logGroupArn: string | ||
| public defaultPrompter: QuickPickPrompter<LogStreamFilterType> = this.createMenuPrompter() | ||
|
|
||
| public constructor(logGroupArn: string, region: string) { | ||
| super() | ||
| this.region = region | ||
| this.logGroupArn = logGroupArn | ||
| } | ||
|
|
||
| public createMenuPrompter() { | ||
| const helpUri = startLiveTailHelpUrl | ||
| const prompter = createQuickPick(this.menuOptions, { | ||
| title: 'Select LogStream filter type', | ||
| buttons: createCommonButtons(helpUri), | ||
| }) | ||
| return prompter | ||
| } | ||
|
|
||
| private get menuOptions(): DataQuickPickItem<LogStreamFilterType>[] { | ||
| const options: DataQuickPickItem<LogStreamFilterType>[] = [] | ||
| options.push({ | ||
| label: 'All', | ||
| detail: 'Include log events from all LogStreams in the selected LogGroup', | ||
| data: 'all', | ||
| }) | ||
| options.push({ | ||
| label: 'Specific', | ||
| detail: 'Include log events from only a specific LogStream', | ||
| data: 'specific', | ||
| }) | ||
| options.push({ | ||
| label: 'Prefix', | ||
| detail: 'Include log events from LogStreams that begin with a provided prefix', | ||
| data: 'prefix', | ||
| }) | ||
| return options | ||
| } | ||
|
|
||
| public createLogStreamPrefixBox(): InputBoxPrompter { | ||
| const helpUri = startLiveTailLogStreamPrefixHelpUrl | ||
| return createInputBox({ | ||
| title: 'Enter LogStream prefix', | ||
| placeholder: 'logStream prefix (case sensitive; empty matches all)', | ||
| prompt: 'Only log events in the LogStreams that have names that start with the prefix that you specify here are included in the Live Tail session', | ||
| validateInput: (input) => this.validateLogStreamPrefix(input), | ||
| buttons: createCommonButtons(helpUri), | ||
| }) | ||
| } | ||
|
|
||
| public validateLogStreamPrefix(prefix: string) { | ||
| if (prefix.length > 512) { | ||
| return 'LogStream prefix cannot be longer than 512 characters' | ||
| } | ||
|
|
||
| if (!this.logStreamPrefixRegEx.test(prefix)) { | ||
| return 'LogStream prefix must match pattern: [^:*]*' | ||
| } | ||
| } | ||
|
|
||
| public createLogStreamSelector(): QuickPickPrompter<string> { | ||
| const helpUri = startLiveTailLogStreamNamesHelpUrl | ||
| const client = new DefaultCloudWatchLogsClient(this.region) | ||
| const request: CloudWatchLogs.DescribeLogStreamsRequest = { | ||
| logGroupIdentifier: this.logGroupArn, | ||
| orderBy: 'LastEventTime', | ||
| descending: true, | ||
| } | ||
| const requester = (request: CloudWatchLogs.DescribeLogStreamsRequest) => client.describeLogStreams(request) | ||
| const collection = pageableToCollection(requester, request, 'nextToken', 'logStreams') | ||
|
|
||
| const items = collection | ||
| .filter(isNonNullable) | ||
| .map((streams) => streams!.map((stream) => ({ data: stream.logStreamName!, label: stream.logStreamName! }))) | ||
|
|
||
| return createQuickPick(items, { | ||
| title: 'Select LogStream', | ||
| buttons: createCommonButtons(helpUri), | ||
| }) | ||
| } | ||
|
|
||
| private switchState(newState: LogStreamFilterType) { | ||
| this.currentState = newState | ||
| } | ||
|
|
||
| protected async promptUser(): Promise<PromptResult<LogStreamFilterResponse>> { | ||
| while (true) { | ||
| switch (this.currentState) { | ||
| case 'menu': { | ||
| const prompter = this.createMenuPrompter() | ||
| this.steps && prompter.setSteps(this.steps[0], this.steps[1]) | ||
|
|
||
| const resp = await prompter.prompt() | ||
| if (resp === 'prefix') { | ||
| this.switchState('prefix') | ||
| } else if (resp === 'specific') { | ||
| this.switchState('specific') | ||
| } else if (resp === 'all') { | ||
| return { filter: undefined, type: resp } | ||
| } else { | ||
| return undefined | ||
| } | ||
|
|
||
| break | ||
| } | ||
| case 'prefix': { | ||
| const resp = await this.createLogStreamPrefixBox().prompt() | ||
| if (isValidResponse(resp)) { | ||
| return { filter: resp, type: 'prefix' } | ||
| } | ||
| this.switchState('menu') | ||
| break | ||
| } | ||
| case 'specific': { | ||
| const resp = await this.createLogStreamSelector().prompt() | ||
| if (isValidResponse(resp)) { | ||
| return { filter: resp, type: 'specific' } | ||
| } | ||
| this.switchState('menu') | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public setSteps(current: number, total: number): void { | ||
| this.steps = [current, total] | ||
| } | ||
|
|
||
| // Unused | ||
| public get recentItem(): any { | ||
| return | ||
| } | ||
| public set recentItem(response: any) {} | ||
| public setStepEstimator(estimator: StepEstimator<LogStreamFilterResponse>): void {} | ||
| } | ||
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
28 changes: 28 additions & 0 deletions
28
packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts
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,28 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { TailLogGroupWizard } from '../../../../awsService/cloudWatchLogs/commands/tailLogGroup' | ||
| import { createWizardTester } from '../../../shared/wizards/wizardTestUtils' | ||
|
|
||
| describe('TailLogGroupWizard', async function () { | ||
keeganirby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| it('prompts regionLogGroup submenu first if context not provided', async function () { | ||
| const wizard = new TailLogGroupWizard() | ||
| const tester = await createWizardTester(wizard) | ||
| tester.regionLogGroupSubmenuResponse.assertShowFirst() | ||
| tester.logStreamFilter.assertShowSecond() | ||
| tester.filterPattern.assertShowThird() | ||
| }) | ||
|
|
||
| it('skips regionLogGroup submenu if context provided', async function () { | ||
| const wizard = new TailLogGroupWizard({ | ||
| groupName: 'test-groupName', | ||
| regionName: 'test-regionName', | ||
| }) | ||
| const tester = await createWizardTester(wizard) | ||
| tester.regionLogGroupSubmenuResponse.assertDoesNotShow() | ||
| tester.logStreamFilter.assertShowFirst() | ||
| tester.filterPattern.assertShowSecond() | ||
| }) | ||
| }) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this have a specific test or do you think its covered by the existing tests you wrote?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be covered via the existing tests