Skip to content

Commit 2f46e3c

Browse files
authored
ec2: expose connect to instance command on explorer node. (#3625)
Problem: Currently the explorer doesn't have any functionality. It is purely visual. Solution: Expose the connect to instance command on right click of an explorer node.
1 parent 5156ce8 commit 2f46e3c

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,11 @@
13081308
"when": "viewItem == awsCodeCatalystNodeSaved",
13091309
"group": "inline@1"
13101310
},
1311+
{
1312+
"command": "aws.ec2.connectToInstance",
1313+
"group": "0@1",
1314+
"when": "viewItem == awsEc2Node"
1315+
},
13111316
{
13121317
"command": "aws.ecr.createRepository",
13131318
"when": "view == aws.explorer && viewItem == awsEcrNode",

src/ec2/activation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import { ExtContext } from '../shared/extensions'
66
import { Commands } from '../shared/vscode/commands2'
77
import { tryConnect } from './commands'
88
import { telemetry } from '../shared/telemetry/telemetry'
9+
import { Ec2InstanceNode } from './explorer/ec2InstanceNode'
910

1011
export async function activate(ctx: ExtContext): Promise<void> {
1112
ctx.extensionContext.subscriptions.push(
12-
Commands.register('aws.ec2.connectToInstance', async (param?: unknown) => {
13+
Commands.register('aws.ec2.connectToInstance', async (node?: Ec2InstanceNode) => {
1314
await telemetry.ec2_connectToInstance.run(async span => {
1415
span.record({ ec2ConnectionType: 'ssm' })
15-
await tryConnect()
16+
await (node ? tryConnect(node.toSelection()) : tryConnect())
1617
})
1718
})
1819
)

src/ec2/commands.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66
import { createEc2ConnectPrompter, handleEc2ConnectPrompterResponse } from './prompter'
77
import { isValidResponse } from '../shared/wizards/wizard'
88
import { Ec2ConnectionManager } from './model'
9+
import { Ec2Selection } from './utils'
10+
import { RegionSubmenuResponse } from '../shared/ui/common/regionSubmenu'
11+
import { PromptResult } from '../shared/ui/prompter'
912
import { CancellationError } from '../shared/utilities/timeoutUtils'
1013

11-
export async function tryConnect(): Promise<void> {
12-
const prompter = createEc2ConnectPrompter()
13-
const response = await prompter.prompt()
14-
14+
function getSelectionFromResponse(response: PromptResult<RegionSubmenuResponse<string>>): Ec2Selection {
1515
if (isValidResponse(response)) {
16-
const selection = handleEc2ConnectPrompterResponse(response)
17-
const ec2Client = new Ec2ConnectionManager(selection.region)
18-
await ec2Client.attemptEc2Connection(selection)
16+
return handleEc2ConnectPrompterResponse(response)
1917
} else {
2018
throw new CancellationError('user')
2119
}
2220
}
21+
22+
export async function tryConnect(selection?: Ec2Selection): Promise<void> {
23+
if (!selection) {
24+
const prompter = createEc2ConnectPrompter()
25+
const response = await prompter.prompt()
26+
27+
selection = getSelectionFromResponse(response)
28+
}
29+
30+
const ec2Client = new Ec2ConnectionManager(selection.region)
31+
await ec2Client.attemptEc2Connection(selection)
32+
}

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AWSTreeNodeBase } from '../../shared/treeview/nodes/awsTreeNodeBase'
99
import { Ec2Instance } from '../../shared/clients/ec2Client'
1010
import { build } from '@aws-sdk/util-arn-parser'
1111
import globals from '../../shared/extensionGlobals'
12+
import { Ec2Selection } from '../utils'
1213

1314
export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode {
1415
public constructor(
@@ -31,6 +32,13 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
3132
this.instance = newInstance
3233
}
3334

35+
public toSelection(): Ec2Selection {
36+
return {
37+
region: this.regionCode,
38+
instanceId: this.InstanceId,
39+
}
40+
}
41+
3442
public get name(): string {
3543
return getNameOfInstance(this.instance) ?? 'Unnamed instance'
3644
}

0 commit comments

Comments
 (0)