Skip to content

Commit

Permalink
Support multiple AWS regions (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
artnc authored Sep 14, 2021
1 parent a70a37b commit 89dbf8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ engines:
# ~/.aws. You should mount the host's .aws folder into the container if
# running Metasearch in Docker, e.g. -v "$HOME/.aws:/root/.aws"
aws:
# AWS region
# Comma-separated AWS regions
region: us-east-1

# Confluence pages
Expand Down
88 changes: 47 additions & 41 deletions src/engines/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,51 +147,57 @@ let getResources: (() => Promise<Set<Result>>) | undefined;

const engine: Engine = {
id: "aws",
init: ({ region }: { region: string }) => {
init: ({ region: regions }: { region: string }) => {
getResources = rateLimit(async () => {
let token: string | undefined;
const resources: Result[] = [];
const start = Date.now();
do {
const { PaginationToken, ResourceTagMappingList = [] } = await new RGT({
region,
})
.getResources({ PaginationToken: token, ResourcesPerPage: 100 })
.promise();
token = PaginationToken;
resources.push(
...ResourceTagMappingList.map(r => {
if (!r.ResourceARN) {
throw Error("Missing ARN");
}
await Promise.all(
regions.split(",").map(async region => {
const client = new RGT({ region });
let token: string | undefined;
do {
const {
PaginationToken,
ResourceTagMappingList = [],
} = await client
.getResources({ PaginationToken: token, ResourcesPerPage: 100 })
.promise();
token = PaginationToken;
resources.push(
...ResourceTagMappingList.map(r => {
if (!r.ResourceARN) {
throw Error("Missing ARN");
}

/**
* ["arn", "aws", service, region, accountId, ...resourceName]
*
* https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
*/
const arnPieces = r.ResourceARN.split(":");
const resourceIdPieces = arnPieces.slice(5);
const serviceName = SERVICE_NAMES[arnPieces[2]];
return {
snippet: [
`ARN = ${r.ResourceARN}`,
...(r.Tags ?? [])
.sort((a, b) => (a.Key > b.Key ? 1 : -1))
.map(r => `${r.Key} = ${r.Value}`),
].join("<br>"),
title:
(serviceName ? `${serviceName}: ` : "") +
resourceIdPieces.join(":"),
url: `https://${serviceToUrl(arnPieces[2])({
arn: r.ResourceARN,
id: resourceIdPieces,
region: `?region=${region}`,
})}`,
};
}),
);
} while (token);
/**
* ["arn", "aws", service, region, accountId, ...resourceName]
*
* https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
*/
const arnPieces = r.ResourceARN.split(":");
const resourceIdPieces = arnPieces.slice(5);
const serviceName = SERVICE_NAMES[arnPieces[2]];
return {
snippet: [
`ARN = ${r.ResourceARN}`,
...(r.Tags ?? [])
.sort((a, b) => (a.Key > b.Key ? 1 : -1))
.map(r => `${r.Key} = ${r.Value}`),
].join("<br>"),
title:
(serviceName ? `${serviceName}: ` : "") +
resourceIdPieces.join(":"),
url: `https://${serviceToUrl(arnPieces[2])({
arn: r.ResourceARN,
id: resourceIdPieces,
region: `?region=${region}`,
})}`,
};
}),
);
} while (token);
}),
);
console.log(
`Scraped ${resources.length} AWS resources in ${
(Date.now() - start) / 1000
Expand Down

0 comments on commit 89dbf8d

Please sign in to comment.