Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all paginated AWS resources #1146

Merged
merged 5 commits into from Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 22 additions & 34 deletions extensions/amazon-aws/src/codepipelines.tsx
Expand Up @@ -44,42 +44,30 @@ export default function DescribeInstances() {

useEffect(() => {
if (!preferences.region) return;
async function fetch() {
pipeline.listPipelines({}, async (err, data) => {
if (err) {
setState({
hasError: true,
loaded: false,
pipelines: [],
});
} else {
const _pipelines: PipelineSummary[] = [];
for (const p of data?.pipelines ?? []) {
if (!p.name) {
continue;
}
_pipelines.push(p);
}
setState({
hasError: false,
loaded: true,
pipelines: _pipelines ?? [],
});
async function fetch(token?: string, accPipelines?: PipelineSummary[]): Promise<PipelineSummary[]> {
const { nextToken, pipelines } = await pipeline.listPipelines({ nextToken: token }).promise();
const combinedPipelines = [...(accPipelines || []), ...(pipelines || [])];

for (const p of data?.pipelines ?? []) {
if (!p.name) {
continue;
}
const execution = await getExecutionState(pipeline, p.name);
setState((state) => ({
...state,
pipelines: state.pipelines.map((el) => (el.name === p.name ? { ...p, execution } : el)),
}));
}
}
});
if (nextToken) {
return fetch(nextToken, combinedPipelines);
}

return combinedPipelines.filter((p) => !!p.name);
}
fetch();

fetch()
.then((pipelines) => {
setState({ hasError: false, loaded: true, pipelines });

pipelines.forEach(async (p) => {
const execution = await getExecutionState(pipeline, p.name as string);
setState((state) => ({
...state,
pipelines: state.pipelines.map((el) => (el.name === p.name ? { ...p, execution } : el)),
}));
});
})
.catch(() => setState({ hasError: true, loaded: false, pipelines: [] }));
}, []);

if (state.hasError) {
Expand Down
38 changes: 17 additions & 21 deletions extensions/amazon-aws/src/ec2.tsx
Expand Up @@ -21,7 +21,6 @@ export default function DescribeInstances() {
const preferences: Preferences = getPreferenceValues();
AWS.config.update({ region: preferences.region });
const ec2 = new AWS.EC2({ apiVersion: "2016-11-15" });
const params = { DryRun: false };

const [state, setState] = useState<{ instances: AWS.EC2.Instance[]; loaded: boolean; hasError: boolean }>({
instances: [],
Expand All @@ -30,27 +29,24 @@ export default function DescribeInstances() {
});

useEffect(() => {
async function fetch() {
ec2.describeInstances(params, (err, data) => {
if (err) {
setState({
hasError: true,
loaded: false,
instances: [],
});
} else {
setState({
hasError: false,
loaded: true,
instances:
data.Reservations?.map((r) => r.Instances)
.filter(notEmpty)
.flat() || [],
});
}
});
async function fetch(token?: string, accInstances?: AWS.EC2.Instance[]): Promise<AWS.EC2.Instance[]> {
const { NextToken, Reservations } = await ec2.describeInstances({ NextToken: token }).promise();
const instances = (Reservations || []).reduce<AWS.EC2.Instance[]>(
(acc, reservation) => [...acc, ...(reservation.Instances || [])],
[]
);
const combinedInstances = [...(accInstances || []), ...instances];

if (NextToken) {
return fetch(NextToken, combinedInstances);
}

return combinedInstances.filter(notEmpty);
}
fetch();

fetch()
.then((instances) => setState({ instances, loaded: true, hasError: false }))
.catch(() => setState({ instances: [], loaded: true, hasError: true }));
}, []);

if (state.hasError) {
Expand Down
34 changes: 16 additions & 18 deletions extensions/amazon-aws/src/queues.tsx
Expand Up @@ -46,25 +46,23 @@ export default function ListSQSQueues() {
};

useEffect(() => {
async function fetch() {
await sqs.listQueues({}, function (err, data) {
if (err) {
setState((o) => ({
...o,
hasError: true,
}));
} else {
setState((o) => ({
...o,
loaded: true,
queues: data.QueueUrls || [],
showingQueues: data.QueueUrls || [],
}));
loadItems(data.QueueUrls || []);
}
});
async function fetch(token?: string, queues?: string[]): Promise<string[]> {
const { NextToken, QueueUrls } = await sqs.listQueues({ NextToken: token }).promise();
const combinedQueues = [...(queues ?? []), ...(QueueUrls ?? [])];

if (NextToken) {
fetch(NextToken, combinedQueues);
}

return combinedQueues;
}
fetch();

fetch()
.then((queues) => {
setState((o) => ({ ...o, loaded: true, queues, showingQueues: queues }));
loadItems(queues);
})
.catch(() => setState((o) => ({ ...o, hasError: true })));
}, []);

if (state.hasError) {
Expand Down