Skip to content

Commit

Permalink
add utility function for doing paging and use it for ACM certs and AP…
Browse files Browse the repository at this point in the history
…I mappings
  • Loading branch information
aoskotsky-amplify committed May 15, 2020
1 parent 19a37f7 commit 9744b5c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "serverless-domain-manager",
"version": "4.0.1",
"version": "4.1.0",
"engines": {
"node": ">=4.0"
},
Expand Down
71 changes: 50 additions & 21 deletions src/index.ts
@@ -1,5 +1,6 @@
"use strict";

import {Service} from "aws-sdk";
import chalk from "chalk";
import DomainConfig = require("./DomainConfig");
import DomainInfo = require("./DomainInfo");
Expand Down Expand Up @@ -315,14 +316,14 @@ class ServerlessCustomDomain {
let certificateName = domain.certificateName; // The certificate name

try {
let certificates = [];
let nextToken;
do {
const certData = await this.acm.listCertificates(
{ CertificateStatuses: certStatuses, NextToken: nextToken }).promise();
certificates = certificates.concat(certData.CertificateSummaryList);
nextToken = certData.NextToken;
} while (nextToken);
const certificates = await this.getAWSPagedResults(
this.acm,
"listCertificates",
"CertificateSummaryList",
"NextToken",
"NextToken",
{ CertificateStatuses: certStatuses },
);

// The more specific name will be the longest
let nameLength = 0;
Expand Down Expand Up @@ -555,20 +556,19 @@ class ServerlessCustomDomain {
}

public async getBasePathMapping(domain: DomainConfig): Promise<AWS.ApiGatewayV2.GetApiMappingResponse> {
const params = {
DomainName: domain.givenDomainName,
};
try {
const mappings = await this.apigatewayV2.getApiMappings(params).promise();

if (mappings.Items.length === 0) {
return;
} else {
for (const mapping of mappings.Items) {
if (mapping.ApiId === domain.apiId
|| (mapping.ApiMappingKey === domain.basePath && domain.allowPathMatching) ) {
return mapping;
}
const mappings = await this.getAWSPagedResults(
this.apigatewayV2,
"getApiMappings",
"Items",
"NextToken",
"NextToken",
{ DomainName: domain.givenDomainName },
);
for (const mapping of mappings) {
if (mapping.ApiId === domain.apiId
|| (mapping.ApiMappingKey === domain.basePath && domain.allowPathMatching) ) {
return mapping;
}
}
} catch (err) {
Expand Down Expand Up @@ -786,6 +786,35 @@ class ServerlessCustomDomain {
this.serverless.cli.consoleLog(` Target Domain: ${domain.domainInfo.domainName}`);
this.serverless.cli.consoleLog(` Hosted Zone Id: ${domain.domainInfo.hostedZoneId}`);
}

/**
* Iterate through the pages of a AWS SDK response and collect them into a single array
*
* @param service - The AWS service instance to use to make the calls
* @param funcName - The function name in the service to call
* @param resultsKey - The key name in the response that contains the items to return
* @param nextTokenKey - The request key name to append to the request that has the paging token value
* @param nextRequestTokenKey - The response key name that has the next paging token value
* @param params - Parameters to send in the request
*/
private async getAWSPagedResults(
service: Service,
funcName: string,
resultsKey: string,
nextTokenKey: string,
nextRequestTokenKey: string,
params: object,
): Promise<any[]> {
let results = [];
let response = await service[funcName](params).promise();
results = results.concat(response[resultsKey]);
while (response.hasOwnProperty(nextRequestTokenKey)) {
params[nextTokenKey] = response[nextRequestTokenKey];
response = await service[funcName](params).promise();
results = results.concat(response[resultsKey]);
}
return results;
}
}

export = ServerlessCustomDomain;

0 comments on commit 9744b5c

Please sign in to comment.