Skip to content

Commit c8e4939

Browse files
committed
Remove duplicated code in branches
1 parent 2049690 commit c8e4939

1 file changed

Lines changed: 77 additions & 78 deletions

File tree

packages/blocks/aws/src/lib/actions/ec2/ec2-get-instances-action.ts

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -82,51 +82,24 @@ export const ec2GetInstancesAction = createAction({
8282
);
8383

8484
const partial = allowPartialResults === true;
85+
const batches = buildEc2GetInstancesBatches(
86+
filterByARNs,
87+
filterProperty,
88+
credentials,
89+
filters,
90+
);
8591

8692
if (partial) {
87-
const partialPromises = [];
88-
if (filterByARNs) {
89-
const arns = convertToStringArrayWithValidation(
90-
filterProperty['arns'] as unknown as string[],
91-
'Invalid input for ARNs: input should be a single string or an array of strings',
92-
);
93-
const groupedARNs = groupARNsByRegion(arns);
94-
95-
for (const region in groupedARNs) {
96-
const arnsForRegion = groupedARNs[region];
97-
const instanceIdFilter = {
98-
Name: 'instance-id',
99-
Values: arnsForRegion.map((arn) => parseArn(arn).resourceId),
100-
};
101-
partialPromises.push(
102-
...credentials.map((creds) =>
103-
getEc2InstancesWithPartialResults(
104-
creds,
105-
[region] as [string, ...string[]],
106-
dryRun,
107-
[...filters, instanceIdFilter],
108-
),
109-
),
110-
);
111-
}
112-
} else {
113-
const regions = convertToStringArrayWithValidation(
114-
filterProperty['regions'],
115-
'Invalid input for regions: input should be a single string or an array of strings',
116-
);
117-
partialPromises.push(
118-
...credentials.map((creds) =>
119-
getEc2InstancesWithPartialResults(
120-
creds,
121-
regions as [string, ...string[]],
122-
dryRun,
123-
filters,
124-
),
93+
const partialOutcomes = await Promise.all(
94+
batches.map((batch) =>
95+
getEc2InstancesWithPartialResults(
96+
batch.creds,
97+
batch.regions,
98+
dryRun,
99+
batch.fetchFilters,
125100
),
126-
);
127-
}
128-
129-
const partialOutcomes = await Promise.all(partialPromises);
101+
),
102+
);
130103
let instances = partialOutcomes.flatMap((o) => o.results);
131104
const failedRegions = partialOutcomes.flatMap((o) => o.failedRegions);
132105

@@ -139,44 +112,18 @@ export const ec2GetInstancesAction = createAction({
139112
return { results: instances, failedRegions };
140113
}
141114

142-
const promises: any[] = [];
143-
if (filterByARNs) {
144-
const arns = convertToStringArrayWithValidation(
145-
filterProperty['arns'] as unknown as string[],
146-
'Invalid input for ARNs: input should be a single string or an array of strings',
147-
);
148-
const groupedARNs = groupARNsByRegion(arns);
149-
150-
for (const region in groupedARNs) {
151-
const arnsForRegion = groupedARNs[region];
152-
const instanceIdFilter = {
153-
Name: 'instance-id',
154-
Values: arnsForRegion.map((arn) => parseArn(arn).resourceId),
155-
};
156-
promises.push(
157-
...credentials.map((creds) =>
158-
getEc2Instances(
159-
creds,
160-
[region] as [string, ...string[]],
161-
dryRun,
162-
[...filters, instanceIdFilter],
163-
),
115+
const instances = (
116+
await Promise.all(
117+
batches.map((batch) =>
118+
getEc2Instances(
119+
batch.creds,
120+
batch.regions,
121+
dryRun,
122+
batch.fetchFilters,
164123
),
165-
);
166-
}
167-
} else {
168-
const regions = convertToStringArrayWithValidation(
169-
filterProperty['regions'],
170-
'Invalid input for regions: input should be a single string or an array of strings',
171-
);
172-
promises.push(
173-
...credentials.map((creds) =>
174-
getEc2Instances(creds, regions, dryRun, filters),
175124
),
176-
);
177-
}
178-
179-
const instances = (await Promise.all(promises)).flat();
125+
)
126+
).flat();
180127

181128
if (tags?.length) {
182129
return instances.filter((instance) =>
@@ -193,6 +140,58 @@ export const ec2GetInstancesAction = createAction({
193140
},
194141
});
195142

143+
type Ec2GetInstancesBatch = {
144+
creds: any;
145+
regions: [string, ...string[]];
146+
fetchFilters: Filter[];
147+
};
148+
149+
function buildEc2GetInstancesBatches(
150+
filterByARNs: boolean,
151+
filterProperty: Record<string, unknown>,
152+
credentials: any[],
153+
filters: Filter[],
154+
): Ec2GetInstancesBatch[] {
155+
const batches: Ec2GetInstancesBatch[] = [];
156+
157+
if (filterByARNs) {
158+
const arns = convertToStringArrayWithValidation(
159+
filterProperty['arns'] as unknown as string[],
160+
'Invalid input for ARNs: input should be a single string or an array of strings',
161+
);
162+
const groupedARNs = groupARNsByRegion(arns);
163+
164+
for (const region in groupedARNs) {
165+
const arnsForRegion = groupedARNs[region];
166+
const instanceIdFilter: Filter = {
167+
Name: 'instance-id',
168+
Values: arnsForRegion.map((arn) => parseArn(arn).resourceId),
169+
};
170+
for (const creds of credentials) {
171+
batches.push({
172+
creds,
173+
regions: [region] as [string, ...string[]],
174+
fetchFilters: [...filters, instanceIdFilter],
175+
});
176+
}
177+
}
178+
} else {
179+
const regions = convertToStringArrayWithValidation(
180+
filterProperty['regions'],
181+
'Invalid input for regions: input should be a single string or an array of strings',
182+
);
183+
for (const creds of credentials) {
184+
batches.push({
185+
creds,
186+
regions: regions as [string, ...string[]],
187+
fetchFilters: filters,
188+
});
189+
}
190+
}
191+
192+
return batches;
193+
}
194+
196195
function getFilters(context: any): Filter[] {
197196
const filters: Filter[] = [];
198197

0 commit comments

Comments
 (0)