Skip to content

Commit c6b2b35

Browse files
committed
refactor: generalize findFirst pattern
1 parent 596f72a commit c6b2b35

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

packages/core/src/shared/clients/clientWrapper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export abstract class ClientWrapper<C extends AwsClient> implements vscode.Dispo
5454
}
5555
}
5656

57+
protected async getFirstResult<CommandInput extends object, CommandOutput extends object, Output extends object>(
58+
paginator: SDKPaginator<C, CommandInput, CommandOutput>,
59+
input: CommandInput,
60+
extractPage: (page: CommandOutput) => Output[] | undefined
61+
): Promise<Output> {
62+
const results = await this.makePaginatedRequest(paginator, input, extractPage).flatten().promise()
63+
return results[0]
64+
}
65+
5766
public dispose() {
5867
this.client?.destroy()
5968
}

packages/core/src/shared/clients/ec2.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,13 @@ export class Ec2Client extends ClientWrapper<EC2Client> {
105105
}
106106

107107
public async getInstanceStatus(instanceId: string): Promise<InstanceStateName> {
108-
const instanceStatuses = await this.makePaginatedRequest(
108+
const instance = await this.getFirstResult(
109109
paginateDescribeInstanceStatus,
110110
{ InstanceIds: [instanceId], IncludeAllInstances: true },
111111
(page) => page.InstanceStatuses
112112
)
113-
.flatten()
114-
.promise()
115113

116-
return instanceStatuses[0].InstanceState!.Name!
114+
return instance.InstanceState!.Name!
117115
}
118116

119117
public async isInstanceRunning(instanceId: string): Promise<boolean> {
@@ -215,17 +213,11 @@ export class Ec2Client extends ClientWrapper<EC2Client> {
215213
* @returns IAM Association for instance
216214
*/
217215
private async getIamInstanceProfileAssociation(instanceId: string): Promise<IamInstanceProfileAssociation> {
218-
const instanceFilter = this.getInstancesFilter([instanceId])
219-
220-
const associations = await this.makePaginatedRequest(
216+
return await this.getFirstResult(
221217
paginateDescribeIamInstanceProfileAssociations,
222-
{ Filters: instanceFilter },
218+
{ Filters: this.getInstancesFilter([instanceId]) },
223219
(page) => page.IamInstanceProfileAssociations
224220
)
225-
.flatten()
226-
.promise()
227-
228-
return associations[0]
229221
}
230222

231223
/**

packages/core/src/shared/clients/ssm.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,13 @@ export class SsmClient extends ClientWrapper<SSMClient> {
5252
}
5353

5454
public async describeInstance(target: string): Promise<InstanceInformation> {
55-
return (
56-
await this.makePaginatedRequest(
57-
paginateDescribeInstanceInformation,
58-
{
59-
InstanceInformationFilterList: [{ key: 'InstanceIds', valueSet: [target] }],
60-
} satisfies DescribeInstanceInformationCommandInput,
61-
(page) => page.InstanceInformationList
62-
)
63-
.flatten()
64-
.promise()
65-
)[0]!
55+
return await this.getFirstResult(
56+
paginateDescribeInstanceInformation,
57+
{
58+
InstanceInformationFilterList: [{ key: 'InstanceIds', valueSet: [target] }],
59+
} satisfies DescribeInstanceInformationCommandInput,
60+
(page) => page.InstanceInformationList
61+
)
6662
}
6763

6864
public async getTargetPlatformName(target: string): Promise<string> {

0 commit comments

Comments
 (0)