-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prefixers for apigatewayv2 api and stage resources (#38)
* add prefixers for apigatewayv2 api and stage resources * feat: drop commit lint on commits * update resource prefixers README to show where tests fixtures can be found * Fix linting Co-authored-by: MalcyL <[email protected]>
- Loading branch information
Showing
15 changed files
with
260 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,14 +50,6 @@ jobs: | |
- "ee:b4:00:e6:23:5b:55:bb:fd:07:bc:73:9e:f7:89:9c" # [email protected] 'talis-cdk-constructs Deploy Key' | ||
- checkout | ||
- node/install-packages | ||
- run: | ||
name: Define environment variable with lastest commit's message | ||
command: | | ||
echo 'export COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s")' >> $BASH_ENV | ||
source $BASH_ENV | ||
- run: | ||
name: Lint commit message | ||
command: echo "$COMMIT_MESSAGE" | npx commitlint | ||
- run: | ||
name: Lint code before building, to lint only source files | ||
command: npm run lint | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/aspects/resource_prefixer/prefixers/apigatewayv2_cfn_api_prefixer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CfnApi } from "@aws-cdk/aws-apigatewayv2"; | ||
import { IConstruct } from "@aws-cdk/core"; | ||
import { CfnResourcePrefixer } from "../cfn_resource_prefixer"; | ||
|
||
export class Apigatewayv2CfnApiPrefixer implements CfnResourcePrefixer { | ||
private node: CfnApi; | ||
private resourcePrefix: string; | ||
|
||
constructor(node: IConstruct, resourcePrefix: string) { | ||
if (!(node instanceof CfnApi)) { | ||
throw new Error( | ||
"Specified node is not an instance of CfnApi and cannot be prefixed using this prefixer" | ||
); | ||
} | ||
this.node = node; | ||
this.resourcePrefix = resourcePrefix; | ||
} | ||
|
||
public prefix(): void { | ||
this.node.addPropertyOverride( | ||
"Name", | ||
`${this.resourcePrefix}${this.node.name}` | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
lib/aspects/resource_prefixer/prefixers/apigatewayv2_cfn_stage_prefixer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { CfnStage } from "@aws-cdk/aws-apigatewayv2"; | ||
import { IConstruct } from "@aws-cdk/core"; | ||
import { CfnResourcePrefixer } from "../cfn_resource_prefixer"; | ||
|
||
export class Apigatewayv2CfnStagePrefixer implements CfnResourcePrefixer { | ||
private node: CfnStage; | ||
private resourcePrefix: string; | ||
|
||
constructor(node: IConstruct, resourcePrefix: string) { | ||
if (!(node instanceof CfnStage)) { | ||
throw new Error( | ||
"Specified node is not an instance of CfnStage and cannot be prefixed using this prefixer" | ||
); | ||
} | ||
this.node = node; | ||
this.resourcePrefix = resourcePrefix; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
public prefix(): void {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./apigatewayv2_cfn_api_prefixer"; | ||
export * from "./apigatewayv2_cfn_stage_prefixer"; | ||
export * from "./dynamodb_cfn_table_prefixer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CfnApiProps } from "@aws-cdk/aws-apigatewayv2"; | ||
|
||
export const CfnApiProperties: CfnApiProps = { | ||
apiKeySelectionExpression: "apiKeySelectionExpression", | ||
basePath: "basePath", | ||
body: {}, | ||
bodyS3Location: { | ||
bucket: "bucket", | ||
etag: "etag", | ||
key: "key", | ||
version: "version", | ||
}, | ||
corsConfiguration: { | ||
allowCredentials: false, | ||
allowHeaders: ["allowHeaders"], | ||
allowMethods: ["allowMethods"], | ||
allowOrigins: ["allowOrigins"], | ||
exposeHeaders: ["exposeHeaders"], | ||
maxAge: 123, | ||
}, | ||
credentialsArn: "credentialsArn", | ||
description: "description", | ||
disableExecuteApiEndpoint: false, | ||
disableSchemaValidation: false, | ||
failOnWarnings: false, | ||
name: "apiName", | ||
protocolType: "protocolType", | ||
routeKey: "routeKey", | ||
routeSelectionExpression: "routeSelectionExpression", | ||
tags: {}, | ||
target: "target", | ||
version: "version", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { CfnStageProps } from "@aws-cdk/aws-apigatewayv2"; | ||
|
||
export const CfnStageProperties: CfnStageProps = { | ||
apiId: "apiId", | ||
stageName: "stageName", | ||
|
||
// the properties below are optional | ||
accessLogSettings: { | ||
destinationArn: "destinationArn", | ||
format: "format", | ||
}, | ||
accessPolicyId: "accessPolicyId", | ||
autoDeploy: false, | ||
clientCertificateId: "clientCertificateId", | ||
defaultRouteSettings: { | ||
dataTraceEnabled: false, | ||
detailedMetricsEnabled: false, | ||
loggingLevel: "loggingLevel", | ||
throttlingBurstLimit: 123, | ||
throttlingRateLimit: 123, | ||
}, | ||
deploymentId: "deploymentId", | ||
description: "description", | ||
routeSettings: {}, | ||
stageVariables: {}, | ||
tags: {}, | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/infra/aspects/prefixers/apigatewayv2_cfn_api_prefixer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as apigatewayv2 from "@aws-cdk/aws-apigatewayv2"; | ||
import * as cdk from "@aws-cdk/core"; | ||
|
||
import { expect as expectCDK, haveResource } from "@aws-cdk/assert"; | ||
import { Apigatewayv2CfnApiPrefixer } from "../../../../lib"; | ||
import { CfnApiProperties } from "../../../fixtures/infra/aws-apigatewayv2/cfn_api"; | ||
import { EmptyResource } from "../../../fixtures/infra/empty_resource"; | ||
|
||
describe("Apigatewayv2 CfnApi Prefixer", () => { | ||
let app: cdk.App; | ||
let stack: cdk.Stack; | ||
let cfnApi: apigatewayv2.CfnApi; | ||
let prefixer: Apigatewayv2CfnApiPrefixer; | ||
let emptyPrefixer: Apigatewayv2CfnApiPrefixer; | ||
|
||
beforeEach(() => { | ||
app = new cdk.App(); | ||
stack = new cdk.Stack(app, "AspectTestStack", {}); | ||
cfnApi = new apigatewayv2.CfnApi(stack, "api1", CfnApiProperties); | ||
prefixer = new Apigatewayv2CfnApiPrefixer(cfnApi, "test-prefix-"); | ||
emptyPrefixer = new Apigatewayv2CfnApiPrefixer(cfnApi, ""); | ||
}); | ||
|
||
describe("Empty Prefix", () => { | ||
test("Keeps api name the same", () => { | ||
emptyPrefixer.prefix(); | ||
|
||
expectCDK(stack).to( | ||
haveResource("AWS::ApiGatewayV2::Api", { | ||
Name: "apiName", | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe("With Prefix", () => { | ||
test("Adds prefix to the start of the api name", () => { | ||
prefixer.prefix(); | ||
|
||
expectCDK(stack).to( | ||
haveResource("AWS::ApiGatewayV2::Api", { | ||
Name: "test-prefix-apiName", | ||
}) | ||
); | ||
}); | ||
test.todo("Truncates the api name if too long"); | ||
}); | ||
|
||
describe("Undefined Resource", () => { | ||
test("Raises error if no prefixer defined for resource", () => { | ||
const unknownResource = new EmptyResource(stack, "empty", { | ||
type: "EmptyResource", | ||
}); | ||
|
||
expect(() => { | ||
new Apigatewayv2CfnApiPrefixer(unknownResource, "prefix"); | ||
}).toThrowError( | ||
"Specified node is not an instance of CfnApi and cannot be prefixed using this prefixer" | ||
); | ||
}); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
test/infra/aspects/prefixers/apigatewayv2_cfn_stage_prefixer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as apigatewayv2 from "@aws-cdk/aws-apigatewayv2"; | ||
import * as cdk from "@aws-cdk/core"; | ||
|
||
import { expect as expectCDK, haveResource } from "@aws-cdk/assert"; | ||
import { Apigatewayv2CfnStagePrefixer } from "../../../../lib"; | ||
import { CfnStageProperties } from "../../../fixtures/infra/aws-apigatewayv2/cfn_stage"; | ||
import { EmptyResource } from "../../../fixtures/infra/empty_resource"; | ||
|
||
describe("Apigatewayv2 CfnStage Prefixer", () => { | ||
let app: cdk.App; | ||
let stack: cdk.Stack; | ||
let cfnStage: apigatewayv2.CfnStage; | ||
let prefixer: Apigatewayv2CfnStagePrefixer; | ||
let emptyPrefixer: Apigatewayv2CfnStagePrefixer; | ||
|
||
beforeEach(() => { | ||
app = new cdk.App(); | ||
stack = new cdk.Stack(app, "AspectTestStack", {}); | ||
cfnStage = new apigatewayv2.CfnStage(stack, "api1", CfnStageProperties); | ||
prefixer = new Apigatewayv2CfnStagePrefixer(cfnStage, "test-prefix-"); | ||
emptyPrefixer = new Apigatewayv2CfnStagePrefixer(cfnStage, ""); | ||
}); | ||
|
||
describe("Empty Prefix", () => { | ||
test("Keeps api name the same", () => { | ||
emptyPrefixer.prefix(); | ||
|
||
expectCDK(stack).to( | ||
haveResource("AWS::ApiGatewayV2::Stage", { | ||
StageName: "stageName", | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe("With Prefix", () => { | ||
test("Keeps api stage name the same", () => { | ||
prefixer.prefix(); | ||
|
||
expectCDK(stack).to( | ||
haveResource("AWS::ApiGatewayV2::Stage", { | ||
StageName: "stageName", | ||
}) | ||
); | ||
}); | ||
test.todo("Truncates the api name if too long"); | ||
}); | ||
|
||
describe("Undefined Resource", () => { | ||
test("Raises error if no prefixer defined for resource", () => { | ||
const unknownResource = new EmptyResource(stack, "empty", { | ||
type: "EmptyResource", | ||
}); | ||
|
||
expect(() => { | ||
new Apigatewayv2CfnStagePrefixer(unknownResource, "prefix"); | ||
}).toThrowError( | ||
"Specified node is not an instance of CfnStage and cannot be prefixed using this prefixer" | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters