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

Fix stage name handling #579

Merged
merged 21 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [7.1.0] - 2023-06-30

### Added
rddimon marked this conversation as resolved.
Show resolved Hide resolved
- Support for the custom stages.

## [7.0.4] - 2023-05-10

### Added
Expand Down
3,086 changes: 1,860 additions & 1,226 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-domain-manager",
"version": "7.0.4",
"version": "7.1.0",
"engines": {
"node": ">=14"
},
Expand Down Expand Up @@ -34,7 +34,8 @@
"test:debug": "NODE_OPTIONS='--inspect-brk' mocha -j 1 -r ts-node/register --project tsconfig.json test/unit-tests/index.test.ts",
"integration-test": "npm run integration-basic && npm run integration-deploy",
"lint": "tslint --project . && tslint --project tsconfig.json",
"build": "tsc --project ."
"build": "tsc --project .",
"prepare": "npm run build"
},
"files": [
"*.js",
Expand All @@ -49,21 +50,21 @@
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^20.1.1",
"@types/node": "^20.3.2",
"@types/shelljs": "^0.8.12",
"aws-sdk-client-mock": "^2.1.1",
"aws-sdk-client-mock": "^2.2.0",
"chai": "^4.3.7",
"chai-spies": "^1.0.0",
"mocha": "^10.2.0",
"mocha-param": "^2.0.1",
"nyc": "^15.1.0",
"randomstring": "^1.2.3",
"serverless": "^3.30.1",
"serverless-plugin-split-stacks": "^1.12.0",
"randomstring": "^1.3.0",
"serverless": "^3.33.0",
"serverless-plugin-split-stacks": "^1.13.0",
"shelljs": "^0.8.5",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^5.0.4"
"typescript": "^5.1.6"
},
"dependencies": {
"@aws-sdk/client-acm": "^3.329.0",
Expand All @@ -75,7 +76,8 @@
"@aws-sdk/config-resolver": "^3.329.0",
"@aws-sdk/credential-providers": "^3.329.0",
"@aws-sdk/node-config-provider": "^3.329.0",
"@aws-sdk/smithy-client": "^3.329.0"
"@aws-sdk/smithy-client": "^3.329.0",
"@aws-sdk/util-retry": "^3.329.0"
},
"peerDependencies": {
"serverless": "^2.60 || ^3.0.0"
Expand Down
5 changes: 3 additions & 2 deletions src/aws/api-gateway-v1-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class APIGatewayV1Wrapper extends APIGatewayBase {
super();
this.apiGateway = new APIGatewayClient({
credentials,
region: Globals.getRegion()
region: Globals.getRegion(),
retryStrategy: APIGatewayBase.getRetryStrategy()
});
}

Expand Down Expand Up @@ -110,7 +111,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase {
basePath: domain.basePath,
domainName: domain.givenDomainName,
restApiId: domain.apiId,
stage: domain.baseStage,
stage: domain.stage,
}));
Logging.logInfo(`V1 - Created API mapping '${domain.basePath}' for '${domain.givenDomainName}'`);
} catch (err) {
Expand Down
19 changes: 4 additions & 15 deletions src/aws/api-gateway-v2-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class APIGatewayV2Wrapper extends APIGatewayBase {
super();
this.apiGateway = new ApiGatewayV2Client({
credentials,
region: Globals.getRegion()
region: Globals.getRegion(),
retryStrategy: APIGatewayBase.getRetryStrategy()
});
}

Expand Down Expand Up @@ -121,19 +122,13 @@ class APIGatewayV2Wrapper extends APIGatewayBase {
* @param domain: DomainConfig
*/
public async createBasePathMapping(domain: DomainConfig): Promise<void> {
let stage = domain.baseStage;
if (domain.apiType === Globals.apiTypes.http) {
// find a better way how to implement custom stage for the HTTP API type
stage = Globals.defaultStage;
}

try {
await this.apiGateway.send(
new CreateApiMappingCommand({
ApiId: domain.apiId,
ApiMappingKey: domain.basePath,
DomainName: domain.givenDomainName,
Stage: stage,
Stage: domain.stage,
})
);
Logging.logInfo(`V2 - Created API mapping '${domain.basePath}' for '${domain.givenDomainName}'`);
Expand Down Expand Up @@ -171,20 +166,14 @@ class APIGatewayV2Wrapper extends APIGatewayBase {
* @param domain: DomainConfig
*/
public async updateBasePathMapping(domain: DomainConfig): Promise<void> {
let stage = domain.baseStage;
if (domain.apiType === Globals.apiTypes.http) {
// find a better way how to implement custom stage for the HTTP API type
stage = Globals.defaultStage;
}

try {
await this.apiGateway.send(
new UpdateApiMappingCommand({
ApiId: domain.apiId,
ApiMappingId: domain.apiMapping.apiMappingId,
ApiMappingKey: domain.basePath,
DomainName: domain.givenDomainName,
Stage: stage,
Stage: domain.stage,
})
);
Logging.logInfo(`V2 - Updated API mapping to '${domain.basePath}' for '${domain.givenDomainName}'`);
Expand Down
1 change: 1 addition & 0 deletions src/aws/cloud-formation-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CloudFormationWrapper {
public stackName: string;

constructor(credentials?: any) {
// for the CloudFormation stack we should use the `base` stage not the plugin custom stage
const defaultStackName = Globals.serverless.service.service + "-" + Globals.getBaseStage();
this.stackName = Globals.serverless.service.provider.stackName || defaultStackName;
this.cloudFormation = new CloudFormationClient({
Expand Down
1 change: 0 additions & 1 deletion src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default class Globals {

public static defaultRegion = "us-east-1";
public static defaultBasePath = "(none)";
public static defaultStage = "$default";

// https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html
public static reservedBasePaths = ["ping", "sping"];
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ class ServerlessCustomDomain {
domain.domainInfo = await apiGateway.getCustomDomain(domain);

if (!domain.apiMapping) {
if (domain.apiType === Globals.apiTypes.http) {
Logging.logWarning(
"Make sure that the API Mapping with the `$default` stage already exists. " +
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm what are people supposed to do when they see this warning?

"More info here https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html"
)
}
await apiGateway.createBasePathMapping(domain);
} else {
await apiGateway.updateBasePathMapping(domain);
Expand Down Expand Up @@ -464,7 +470,7 @@ class ServerlessCustomDomain {
}

// Remove all special characters
const safeStage = domain.baseStage.replace(/[^a-zA-Z0-9]/g, "");
const safeStage = domain.stage.replace(/[^a-zA-Z\d]/g, "");
service.provider.compiledCloudFormationTemplate.Outputs[domainNameOutputKey] = {
Value: domain.givenDomainName,
Export: {
Expand Down
10 changes: 10 additions & 0 deletions src/models/apigateway-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import DomainInfo = require("./domain-info");
import ApiGatewayMap = require("./api-gateway-map");
import DomainConfig = require("./domain-config");
import {Client} from "@aws-sdk/smithy-client";
import {ConfiguredRetryStrategy} from "@aws-sdk/util-retry";

abstract class APIGatewayBase {
public apiGateway: Client<any, any, any, any>;

public static getRetryStrategy() {
rddimon marked this conversation as resolved.
Show resolved Hide resolved
return new ConfiguredRetryStrategy(
5, // max attempts.
// This example sets the backoff at 100ms plus 5s per attempt.
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_util_retry.html#aws-sdkutil-retry
(attempt: number) => 100 + attempt * 5000 // backoff function.
rddimon marked this conversation as resolved.
Show resolved Hide resolved
)
}

abstract createCustomDomain(domain: DomainConfig): Promise<DomainInfo>;

abstract getCustomDomain(domain: DomainConfig): Promise<DomainInfo>;
Expand Down
2 changes: 0 additions & 2 deletions src/models/domain-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Logging from "../logging";
class DomainConfig {
public givenDomainName: string;
public basePath: string | undefined;
public baseStage: string | undefined;
public stage: string | undefined;
public certificateName: string | undefined;
public certificateArn: string | undefined;
Expand Down Expand Up @@ -55,7 +54,6 @@ class DomainConfig {
this.autoDomainWaitFor = config.autoDomainWaitFor;
this.preserveExternalPathMappings = evaluateBoolean(config.preserveExternalPathMappings, false);
this.basePath = this._getBasePath(config.basePath);
this.baseStage = Globals.getBaseStage();
this.stage = config.stage || Globals.getBaseStage();
this.endpointType = this._getEndpointType(config.endpointType);
this.apiType = this._getApiType(config.apiType);
Expand Down
6 changes: 5 additions & 1 deletion test/integration-tests/apigateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import {
GetResourcesCommand,
GetResourcesCommandOutput
} from "@aws-sdk/client-api-gateway";
import APIGatewayBase = require("../../src/models/apigateway-base");

export default class APIGatewayWrap {
private client: APIGatewayClient;

constructor(region: string) {
this.client = new APIGatewayClient({region});
this.client = new APIGatewayClient({
region,
retryStrategy: APIGatewayBase.getRetryStrategy()
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/integration-tests/basic/http-api-multiple/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ custom:
basePath: ''
endpointType: REGIONAL
autoDomain: true
stage: '$default'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required now? This could be considered a breaking change. Is it possible to default to $default for http APIs?

- http:
domainName: ${env:PLUGIN_IDENTIFIER}-http-api-milti2-${env:RANDOM_STRING}.${env:TEST_DOMAIN}
basePath: ''
endpointType: REGIONAL
autoDomain: true
stage: '$default'

package:
patterns:
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/aws/api-gateway-v1-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe("API Gateway V1 wrapper checks", () => {
basePath: dc.basePath,
domainName: dc.givenDomainName,
restApiId: dc.apiId,
stage: dc.baseStage,
stage: dc.stage,
}
const commandCalls = APIGatewayMock.commandCalls(CreateBasePathMappingCommand, expectedParams, true);

Expand Down
6 changes: 3 additions & 3 deletions test/unit-tests/aws/api-gateway-v2-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe("API Gateway V2 wrapper checks", () => {
ApiMappingKey: dc.basePath,
DomainName: dc.givenDomainName,
ApiId: dc.apiId,
Stage: dc.baseStage,
Stage: dc.stage,
}
const commandCalls = APIGatewayMock.commandCalls(CreateApiMappingCommand, expectedParams, true);

Expand All @@ -336,7 +336,7 @@ describe("API Gateway V2 wrapper checks", () => {
ApiMappingKey: dc.basePath,
DomainName: dc.givenDomainName,
ApiId: dc.apiId,
Stage: Globals.defaultStage,
Stage: dc.stage,
}
const commandCalls = APIGatewayMock.commandCalls(CreateApiMappingCommand, expectedParams, true);

Expand Down Expand Up @@ -472,7 +472,7 @@ describe("API Gateway V2 wrapper checks", () => {
ApiMappingId: dc.apiMapping.apiMappingId,
ApiMappingKey: dc.basePath,
DomainName: dc.givenDomainName,
Stage: Globals.defaultStage
Stage: dc.stage
}
const commandCalls = APIGatewayMock.commandCalls(UpdateApiMappingCommand, expectedParams, true);

Expand Down