Skip to content

Commit

Permalink
fix(api-gatewayv2): use API name in fallback name when possible
Browse files Browse the repository at this point in the history
Avoids potentially referring to an API ID that ends up being a token.
  • Loading branch information
echeung-amzn committed Sep 12, 2024
1 parent b0cc9b3 commit e64032f
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HttpApi } from "aws-cdk-lib/aws-apigatewayv2";
import {
GraphWidget,
HorizontalAnnotation,
Expand Down Expand Up @@ -158,7 +159,11 @@ export class ApiGatewayV2HttpApiMonitoring extends Monitoring {
super(scope, props);

// used when humanReadableName is not provided by user
const fallbackNameArray = [props.api.apiId];
const fallbackNameArray = [
(props.api as HttpApi)?.httpApiName ??
(props.api as HttpApi)?.httpApiId ??
props.api.apiId,
];
fallbackNameArray.push(props.apiStage ?? "$default");
if (props.apiMethod) {
fallbackNameArray.push(props.apiMethod);
Expand Down
12 changes: 2 additions & 10 deletions test/facade/__snapshots__/MonitoringAspect.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,43 @@ import {
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
import { TestMonitoringScope } from "../TestMonitoringScope";

test("snapshot test: no alarms", () => {
test("snapshot test: no alarms with explicit API name", () => {
const stack = new Stack();

const scope = new TestMonitoringScope(stack, "Scope");

const testHttpApi = new HttpApi(stack, "testHttpApi", {
apiName: "testHttpApi",
const monitoring = new ApiGatewayV2HttpApiMonitoring(scope, {
api: new HttpApi(stack, "testHttpApi", {
apiName: "testHttpApiName",
}),
});

addMonitoringDashboardsToStack(stack, monitoring);
expect(Template.fromStack(stack)).toMatchSnapshot();
});

test("snapshot test: no alarms with no explicit API name", () => {
const stack = new Stack();

const scope = new TestMonitoringScope(stack, "Scope");

const monitoring = new ApiGatewayV2HttpApiMonitoring(scope, {
api: testHttpApi,
humanReadableName: "Dummy API Gateway for testing",
alarmFriendlyName: "DummyApi",
api: new HttpApi(stack, "testHttpApi"),
});

addMonitoringDashboardsToStack(stack, monitoring);
expect(Template.fromStack(stack)).toMatchSnapshot();
});

test("snapshot test: no alarms with imported IHttpApi", () => {
const stack = new Stack();

const scope = new TestMonitoringScope(stack, "Scope");

const monitoring = new ApiGatewayV2HttpApiMonitoring(scope, {
api: HttpApi.fromHttpApiAttributes(scope, "Imported", {
httpApiId: "imported-id",
}),
});

addMonitoringDashboardsToStack(stack, monitoring);
Expand Down
Loading

0 comments on commit e64032f

Please sign in to comment.