Skip to content

Commit

Permalink
Filter by stage only for removing API mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
rddimon committed Mar 6, 2024
1 parent bbfebce commit 3533b7a
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [7.3.7] - 2023-03-06

### Fixed
- Added filtering by stage for the `getBasePathMappings` method.
- Added filtering by stage for removing API mappings. That filter is skipped in case `allowPathMatching` is enabled.

## [7.3.6] - 2023-02-13

Expand Down
16 changes: 9 additions & 7 deletions src/aws/api-gateway-v1-wrapper.ts
Expand Up @@ -81,7 +81,12 @@ class APIGatewayV1Wrapper extends APIGatewayBase {
}
}

public async getCustomDomain (domain: DomainConfig): Promise<DomainInfo> {
/**
* Get Custom Domain Info
* @param domain: DomainConfig
* @param silent: To issue an error or not. Not by default.
*/
public async getCustomDomain (domain: DomainConfig, silent: boolean = true): Promise<DomainInfo> {
// Make API call
try {
const domainInfo: GetDomainNameCommandOutput = await this.apiGateway.send(
Expand All @@ -91,7 +96,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase {
);
return new DomainInfo(domainInfo);
} catch (err) {
if (!err.$metadata || err.$metadata.httpStatusCode !== 404) {
if (!err.$metadata || err.$metadata.httpStatusCode !== 404 || !silent) {
throw new Error(
`V1 - Unable to fetch information about '${domain.givenDomainName}':\n${err.message}`
);
Expand Down Expand Up @@ -122,8 +127,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase {
Logging.logInfo(`V1 - Created API mapping '${domain.basePath}' for '${domain.givenDomainName}'`);
} catch (err) {
throw new Error(
`V1 - Make sure the '${domain.givenDomainName}' exists.
Unable to create base path mapping for '${domain.givenDomainName}':\n${err.message}`
`V1 - Unable to create base path mapping for '${domain.givenDomainName}':\n${err.message}`
);
}
}
Expand All @@ -139,9 +143,7 @@ class APIGatewayV1Wrapper extends APIGatewayBase {
domainName: domain.givenDomainName
})
);
return items.filter((item) => {
return item.stage === domain.stage;
}).map((item) => {
return items.map((item) => {
return new ApiGatewayMap(item.restApiId, item.basePath, item.stage, null);
});
} catch (err) {
Expand Down
9 changes: 4 additions & 5 deletions src/aws/api-gateway-v2-wrapper.ts
Expand Up @@ -83,8 +83,9 @@ class APIGatewayV2Wrapper extends APIGatewayBase {
/**
* Get Custom Domain Info
* @param domain: DomainConfig
* @param silent: To issue an error or not. Not by default.
*/
public async getCustomDomain (domain: DomainConfig): Promise<DomainInfo> {
public async getCustomDomain (domain: DomainConfig, silent: boolean = true): Promise<DomainInfo> {
// Make API call
try {
const domainInfo: GetDomainNameCommandOutput = await this.apiGateway.send(
Expand All @@ -94,7 +95,7 @@ class APIGatewayV2Wrapper extends APIGatewayBase {
);
return new DomainInfo(domainInfo);
} catch (err) {
if (!err.$metadata || err.$metadata.httpStatusCode !== 404) {
if (!err.$metadata || err.$metadata.httpStatusCode !== 404 || !silent) {
throw new Error(
`V2 - Unable to fetch information about '${domain.givenDomainName}':\n${err.message}`
);
Expand Down Expand Up @@ -167,9 +168,7 @@ class APIGatewayV2Wrapper extends APIGatewayBase {
DomainName: domain.givenDomainName
})
);
return items.filter((item) => {
return item.Stage === domain.stage;
}).map(
return items.map(
(item) => new ApiGatewayMap(item.ApiId, item.ApiMappingKey, item.Stage, item.ApiMappingId)
);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -353,7 +353,7 @@ class ServerlessCustomDomain {
return mapping.apiId === domain.apiId;
});
domain.apiMapping = filteredMappings ? filteredMappings[0] : null;
domain.domainInfo = await apiGateway.getCustomDomain(domain);
domain.domainInfo = await apiGateway.getCustomDomain(domain, false);

if (!domain.apiMapping) {
await apiGateway.createBasePathMapping(domain);
Expand Down Expand Up @@ -385,7 +385,7 @@ class ServerlessCustomDomain {
if (domain.allowPathMatching) {
return mapping.basePath === domain.basePath;
}
return mapping.apiId === domain.apiId;
return mapping.apiId === domain.apiId && mapping.stage === domain.stage;
});
if (domain.preserveExternalPathMappings) {
externalBasePathExists = mappings.length > filteredMappings.length;
Expand Down
2 changes: 1 addition & 1 deletion src/models/apigateway-base.ts
Expand Up @@ -5,7 +5,7 @@ import DomainConfig = require("./domain-config");
abstract class APIGatewayBase {
abstract createCustomDomain(domain: DomainConfig): Promise<DomainInfo>;

abstract getCustomDomain(domain: DomainConfig): Promise<DomainInfo>;
abstract getCustomDomain(domain: DomainConfig, silent?: boolean): Promise<DomainInfo>;

abstract deleteCustomDomain(domain: DomainConfig): Promise<void>;

Expand Down
8 changes: 2 additions & 6 deletions test/unit-tests/aws/api-gateway-v1-wrapper.test.ts
Expand Up @@ -339,13 +339,9 @@ describe("API Gateway V1 wrapper checks", () => {
restApiId: "test_rest_api_id",
basePath: "test",
stage: "test"
}, {
},{
restApiId: "test_rest_api_id2",
basePath: "test2",
stage: "test"
}, {
restApiId: "test_rest_api_id3",
basePath: "test3",
stage: "dummy"
}]
});
Expand All @@ -359,7 +355,7 @@ describe("API Gateway V1 wrapper checks", () => {
// should be filtered by stage
const expectedResult = [
new ApiGatewayMap("test_rest_api_id", "test", "test", null),
new ApiGatewayMap("test_rest_api_id2", "test2", "test", null)
new ApiGatewayMap("test_rest_api_id2", "test2", "dummy", null)
];

expect(actualResult).to.eql(expectedResult);
Expand Down
11 changes: 3 additions & 8 deletions test/unit-tests/aws/api-gateway-v2-wrapper.test.ts
Expand Up @@ -372,16 +372,11 @@ describe("API Gateway V2 wrapper checks", () => {
ApiMappingKey: "test",
Stage: "test",
ApiMappingId: "test_id"
}, {
},{
ApiId: "test_rest_api_id2",
ApiMappingKey: "test2",
Stage: "test",
ApiMappingId: "test_id2"
}, {
ApiId: "test_rest_api_id3",
ApiMappingKey: "test3",
Stage: "dummy",
ApiMappingId: "test_id3"
ApiMappingId: "test_id2"
}]
});

Expand All @@ -394,7 +389,7 @@ describe("API Gateway V2 wrapper checks", () => {
// should be filtered by stage
const expectedResult = [
new ApiGatewayMap("test_rest_api_id", "test", "test", "test_id"),
new ApiGatewayMap("test_rest_api_id2", "test2", "test", "test_id2")
new ApiGatewayMap("test_rest_api_id2", "test2", "dummy", "test_id2")
];

expect(actualResult).to.eql(expectedResult);
Expand Down
6 changes: 4 additions & 2 deletions test/unit-tests/index.test.ts
Expand Up @@ -490,6 +490,10 @@ describe("Custom Domain Plugin", () => {
});
APIGatewayMock.on(GetBasePathMappingsCommand).resolves({
items: [{
restApiId: "test_rest_api_id",
basePath: "test2",
stage: "dummy"
}, {
restApiId: "test_rest_api_id",
basePath: "test",
stage: "test"
Expand Down Expand Up @@ -518,8 +522,6 @@ describe("Custom Domain Plugin", () => {
const deleteDomainSpy = chaiSpy.on(plugin, "deleteDomain");
await plugin.hooks["before:remove:remove"]();

const commandCalls = APIGatewayMock.commandCalls(DeleteBasePathMappingCommand);
expect(commandCalls.length).to.equal(1);
expect(deleteDomainSpy).to.have.been.called();
});

Expand Down

0 comments on commit 3533b7a

Please sign in to comment.