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

Add utility function for doing paging and use it for ACM certs and API mappings #345

Merged
merged 6 commits into from May 18, 2020
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
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) && response[nextRequestTokenKey]) {
params[nextTokenKey] = response[nextRequestTokenKey];
response = await service[funcName](params).promise();
results = results.concat(response[resultsKey]);
}
return results;
}
}

export = ServerlessCustomDomain;