Skip to content

Commit a5a29ef

Browse files
authored
refactor(ssm): clean-up create/update wizard (#2742)
* Refactor SSM create/update wizard * Add test coverage
1 parent 68e6f7d commit a5a29ef

File tree

7 files changed

+180
-402
lines changed

7 files changed

+180
-402
lines changed

src/shared/ui/common/region.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,32 @@ import { createQuickPick, QuickPickPrompter } from '../pickerPrompter'
1313

1414
const localize = nls.loadMessageBundle()
1515

16-
type RegionPrompterOptions = {
17-
defaultRegion?: string
18-
title?: string
19-
buttons?: PrompterButtons<Region>
16+
interface RegionPrompterOptions {
17+
readonly defaultRegion?: string
18+
readonly title?: string
19+
readonly buttons?: PrompterButtons<Region>
20+
readonly serviceFilter?: string
2021
}
2122

2223
export function createRegionPrompter(
23-
regions?: Region[],
24+
regions = getRegionsForActiveCredentials(globals.awsContext, globals.regionProvider),
2425
options: RegionPrompterOptions = {}
2526
): QuickPickPrompter<Region> {
2627
const lastRegionKey = 'lastSelectedRegion'
27-
if (!regions) {
28-
regions = getRegionsForActiveCredentials(globals.awsContext, globals.regionProvider)
29-
}
30-
options.defaultRegion ??= globals.awsContext.getCredentialDefaultRegion()
28+
const defaultRegion = options.defaultRegion ?? globals.awsContext.getCredentialDefaultRegion()
29+
const filteredRegions = regions.filter(
30+
r => !options.serviceFilter || globals.regionProvider.isServiceInRegion(options.serviceFilter, r.id)
31+
)
3132

32-
const items = regions.map(region => ({
33+
const items = filteredRegions.map(region => ({
3334
label: region.name,
3435
detail: region.id,
3536
data: region,
3637
skipEstimate: true,
3738
description: '',
3839
}))
3940

40-
const defaultRegionItem = items.find(item => item.detail === options.defaultRegion)
41+
const defaultRegionItem = items.find(item => item.detail === defaultRegion)
4142

4243
if (defaultRegionItem !== undefined) {
4344
defaultRegionItem.description = localize('AWS.generic.defaultRegion', '(default region)')
@@ -48,13 +49,13 @@ export function createRegionPrompter(
4849
buttons: options.buttons ?? createCommonButtons(),
4950
matchOnDetail: true,
5051
compare: (a, b) => {
51-
return a.detail === options.defaultRegion ? -1 : b.detail === options.defaultRegion ? 1 : 0
52+
return a.detail === defaultRegion ? -1 : b.detail === defaultRegion ? 1 : 0
5253
},
5354
})
5455

5556
const lastRegion = globals.context.globalState.get<Region>(lastRegionKey)
5657
if (lastRegion !== undefined && (lastRegion as any).id) {
57-
const found = regions.find(val => val.id === lastRegion.id)
58+
const found = filteredRegions.find(val => val.id === lastRegion.id)
5859
if (found) {
5960
prompter.recentItem = lastRegion
6061
}

src/shared/wizards/wizard.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export class Wizard<TState extends Partial<Record<keyof TState, unknown>>> {
103103
return this._form
104104
}
105105

106+
public get initialState(): Readonly<Partial<TState>> | undefined {
107+
return this.options.initState
108+
}
109+
106110
private _estimator: ((state: TState) => number) | undefined
107111
public set parentEstimator(estimator: (state: TState) => number) {
108112
this._estimator = estimator

src/ssmDocument/commands/publishDocument.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import { ssmJson, ssmYaml } from '../../shared/constants'
1414
import * as localizedText from '../../shared/localizedText'
1515
import { getLogger, Logger } from '../../shared/logger'
1616
import { RegionProvider } from '../../shared/regions/regionProvider'
17-
import {
18-
DefaultPublishSSMDocumentWizardContext,
19-
PublishSSMDocumentWizard,
20-
PublishSSMDocumentWizardContext,
21-
PublishSSMDocumentWizardResponse,
22-
} from '../wizards/publishDocumentWizard'
17+
import { PublishSSMDocumentWizard, PublishSSMDocumentWizardResponse } from '../wizards/publishDocumentWizard'
2318
import * as telemetry from '../../shared/telemetry/telemetry'
2419
import { Window } from '../../shared/vscode/window'
2520
import { showConfirmationMessage } from '../util/util'
@@ -56,17 +51,11 @@ export async function publishSSMDocument(awsContext: AwsContext, regionProvider:
5651
}
5752

5853
try {
59-
const wizardContext: PublishSSMDocumentWizardContext = new DefaultPublishSSMDocumentWizardContext(
60-
awsContext,
61-
regionProvider
62-
)
63-
const wizardResponse: PublishSSMDocumentWizardResponse | undefined = await new PublishSSMDocumentWizard(
64-
wizardContext
65-
).run()
66-
if (wizardResponse?.PublishSsmDocAction == 'Create') {
67-
await createDocument(wizardResponse, textDocument)
68-
} else if (wizardResponse?.PublishSsmDocAction == 'Update') {
69-
await updateDocument(wizardResponse, textDocument)
54+
const response = await new PublishSSMDocumentWizard().run()
55+
if (response?.action == 'Create') {
56+
await createDocument(response, textDocument)
57+
} else if (response?.action == 'Update') {
58+
await updateDocument(response, textDocument)
7059
}
7160
} catch (err) {
7261
logger.error(err as Error)
@@ -79,7 +68,7 @@ export async function createDocument(
7968
client: SsmDocumentClient = globals.toolkitClientBuilder.createSsmClient(wizardResponse.region)
8069
) {
8170
let result: telemetry.Result = 'Succeeded'
82-
const ssmOperation: telemetry.SsmOperation = wizardResponse.PublishSsmDocAction as telemetry.SsmOperation
71+
const ssmOperation = wizardResponse.action as telemetry.SsmOperation
8372

8473
const logger: Logger = getLogger()
8574
logger.info(`Creating Systems Manager Document '${wizardResponse.name}'`)
@@ -114,7 +103,7 @@ export async function updateDocument(
114103
window = Window.vscode()
115104
) {
116105
let result: telemetry.Result = 'Succeeded'
117-
const ssmOperation: telemetry.SsmOperation = wizardResponse.PublishSsmDocAction as telemetry.SsmOperation
106+
const ssmOperation = wizardResponse.action as telemetry.SsmOperation
118107

119108
const logger: Logger = getLogger()
120109
logger.info(`Updating Systems Manager Document '${wizardResponse.name}'`)

0 commit comments

Comments
 (0)