|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | import { RegionSubmenu, RegionSubmenuResponse } from '../shared/ui/common/regionSubmenu'
|
7 |
| -import { Ec2Selection, getInstancesFromRegion } from './utils' |
8 | 7 | import { DataQuickPickItem } from '../shared/ui/pickerPrompter'
|
9 |
| -import { Ec2Instance } from '../shared/clients/ec2Client' |
| 8 | +import { Ec2Client, Ec2Instance } from '../shared/clients/ec2Client' |
10 | 9 | import { isValidResponse } from '../shared/wizards/wizard'
|
11 | 10 | import { CancellationError } from '../shared/utilities/timeoutUtils'
|
| 11 | +import { AsyncCollection } from '../shared/utilities/asyncCollection' |
12 | 12 |
|
13 |
| -function asQuickpickItem(instance: Ec2Instance): DataQuickPickItem<string> { |
14 |
| - return { |
15 |
| - label: '$(terminal) \t' + (instance.name ?? '(no name)'), |
16 |
| - detail: instance.InstanceId, |
17 |
| - data: instance.InstanceId, |
18 |
| - } |
| 13 | +export type instanceFilter = (instance: Ec2Instance) => boolean |
| 14 | +export interface Ec2Selection { |
| 15 | + instanceId: string |
| 16 | + region: string |
19 | 17 | }
|
20 | 18 |
|
21 |
| -export async function promptUserForEc2Selection(): Promise<Ec2Selection> { |
22 |
| - const prompter = createEc2ConnectPrompter() |
23 |
| - const response = await prompter.prompt() |
| 19 | +export class Ec2Prompter { |
| 20 | + public constructor(protected filter?: instanceFilter) {} |
24 | 21 |
|
25 |
| - if (isValidResponse(response)) { |
26 |
| - return handleEc2ConnectPrompterResponse(response) |
27 |
| - } else { |
28 |
| - throw new CancellationError('user') |
| 22 | + protected static asQuickPickItem(instance: Ec2Instance): DataQuickPickItem<string> { |
| 23 | + return { |
| 24 | + label: '$(terminal) \t' + (instance.name ?? '(no name)'), |
| 25 | + detail: instance.InstanceId, |
| 26 | + data: instance.InstanceId, |
| 27 | + } |
29 | 28 | }
|
30 |
| -} |
31 | 29 |
|
32 |
| -export function handleEc2ConnectPrompterResponse(response: RegionSubmenuResponse<string>): Ec2Selection { |
33 |
| - return { |
34 |
| - instanceId: response.data, |
35 |
| - region: response.region, |
| 30 | + protected static getSelectionFromResponse(response: RegionSubmenuResponse<string>): Ec2Selection { |
| 31 | + return { |
| 32 | + instanceId: response.data, |
| 33 | + region: response.region, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public async promptUser(): Promise<Ec2Selection> { |
| 38 | + const prompter = this.createEc2ConnectPrompter() |
| 39 | + const response = await prompter.prompt() |
| 40 | + |
| 41 | + if (isValidResponse(response)) { |
| 42 | + return Ec2Prompter.getSelectionFromResponse(response) |
| 43 | + } else { |
| 44 | + throw new CancellationError('user') |
| 45 | + } |
36 | 46 | }
|
37 |
| -} |
38 | 47 |
|
39 |
| -export function createEc2ConnectPrompter(): RegionSubmenu<string> { |
40 |
| - return new RegionSubmenu( |
41 |
| - async region => (await getInstancesFromRegion(region)).map(asQuickpickItem).promise(), |
42 |
| - { title: 'Select EC2 Instance', matchOnDetail: true }, |
43 |
| - { title: 'Select Region for EC2 Instance' }, |
44 |
| - 'Instances' |
45 |
| - ) |
| 48 | + protected async getInstancesFromRegion(regionCode: string): Promise<AsyncCollection<Ec2Instance>> { |
| 49 | + const client = new Ec2Client(regionCode) |
| 50 | + return await client.getInstances() |
| 51 | + } |
| 52 | + |
| 53 | + protected async getInstancesAsQuickPickItems(region: string): Promise<DataQuickPickItem<string>[]> { |
| 54 | + return (await this.getInstancesFromRegion(region)) |
| 55 | + .filter(this.filter ? instance => this.filter!(instance) : instance => true) |
| 56 | + .map(instance => Ec2Prompter.asQuickPickItem(instance)) |
| 57 | + .promise() |
| 58 | + } |
| 59 | + |
| 60 | + private createEc2ConnectPrompter(): RegionSubmenu<string> { |
| 61 | + return new RegionSubmenu( |
| 62 | + async region => this.getInstancesAsQuickPickItems(region), |
| 63 | + { title: 'Select EC2 Instance', matchOnDetail: true }, |
| 64 | + { title: 'Select Region for EC2 Instance' }, |
| 65 | + 'Instances' |
| 66 | + ) |
| 67 | + } |
46 | 68 | }
|
0 commit comments