Skip to content

Commit

Permalink
[SLS-2426] Improve cdk token support (#121)
Browse files Browse the repository at this point in the history
* check if site is a cdk token and do not throw error

* check string instead of isUnresolved to avoid v1 package issues

* tests
SLS-2426
  • Loading branch information
lizapressman authored Sep 15, 2022
1 parent 4863571 commit ebefde3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
6 changes: 5 additions & 1 deletion common/datadogSharedLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export function validateProps(props: DatadogProps) {
"us5.datadoghq.com",
"ddog-gov.com",
];
if (props.site !== undefined && !siteList.includes(props.site.toLowerCase())) {
if (
props.site !== undefined &&
!siteList.includes(props.site.toLowerCase()) &&
!(props.site.startsWith("${Token[") && props.site.endsWith("]}"))
) {
throw new Error(
"Warning: Invalid site URL. Must be either datadoghq.com, datadoghq.eu, us3.datadoghq.com, us5.datadoghq.com, or ddog-gov.com.",
);
Expand Down
36 changes: 36 additions & 0 deletions v1/test/datadog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@aws-cdk/assert/jest";
import * as lambda from "@aws-cdk/aws-lambda";
import { LogGroup } from "@aws-cdk/aws-logs";
import * as cdk from "@aws-cdk/core";
import { Token } from "@aws-cdk/core";
import { addCdkConstructVersionTag, checkForMultipleApiKeys, Datadog } from "../src/index";
const versionJson = require("../version.json");
const EXTENSION_LAYER_VERSION = 5;
Expand Down Expand Up @@ -44,6 +45,41 @@ describe("validateProps", () => {
);
});

it("doesn't throw an error when the site is set as a cdk token", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "stack", {
env: {
region: "sa-east-1",
},
});
const hello = new lambda.Function(stack, "HelloHandler", {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});
let threwError = false;
let thrownError: Error | undefined;
const datadogSite = "${Token[TOKEN.100]}" as Token;
try {
const datadogCdk = new Datadog(stack, "Datadog", {
nodeLayerVersion: NODE_LAYER_VERSION,
extensionLayerVersion: EXTENSION_LAYER_VERSION,
apiKey: "1234",
enableDatadogTracing: false,
flushMetricsToLogs: false,
site: `${datadogSite}`,
});
datadogCdk.addLambdaFunctions([hello]);
} catch (e) {
threwError = true;
if (e instanceof Error) {
thrownError = e;
}
}
expect(threwError).toBe(false);
expect(thrownError?.message).toEqual(undefined);
});

it("throws error if flushMetricsToLogs is false and both API key and KMS API key are undefined", () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, "stack", {
Expand Down
2 changes: 1 addition & 1 deletion v1/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ABSENT } from "@aws-cdk/assert/lib/assertions/have-resource";
import * as lambda from "@aws-cdk/aws-lambda";
import * as cdk from "@aws-cdk/core";
import "@aws-cdk/assert/jest";
import { ABSENT } from "@aws-cdk/assert/lib/assertions/have-resource";
import {
Datadog,
DD_ACCOUNT_ID,
Expand Down
2 changes: 1 addition & 1 deletion v1/yarn.lock

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

37 changes: 36 additions & 1 deletion v2/test/datadog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Stack } from "aws-cdk-lib";
import { App, Stack, Token } from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { LogGroup } from "aws-cdk-lib/aws-logs";
Expand Down Expand Up @@ -44,6 +44,41 @@ describe("validateProps", () => {
);
});

it("doesn't throw an error when the site is set as a cdk token", () => {
const app = new App();
const stack = new Stack(app, "stack", {
env: {
region: "sa-east-1",
},
});
const hello = new lambda.Function(stack, "HelloHandler", {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});
let threwError = false;
let thrownError: Error | undefined;
const datadogSite = "${Token[TOKEN.100]}" as Token;
try {
const datadogCdk = new Datadog(stack, "Datadog", {
nodeLayerVersion: NODE_LAYER_VERSION,
extensionLayerVersion: EXTENSION_LAYER_VERSION,
apiKey: "1234",
enableDatadogTracing: false,
flushMetricsToLogs: false,
site: `${datadogSite}`,
});
datadogCdk.addLambdaFunctions([hello]);
} catch (e) {
threwError = true;
if (e instanceof Error) {
thrownError = e;
}
}
expect(threwError).toBe(false);
expect(thrownError?.message).toEqual(undefined);
});

it("throws error if flushMetricsToLogs is false and both API key and KMS API key are undefined", () => {
const app = new App();
const stack = new Stack(app, "stack", {
Expand Down

0 comments on commit ebefde3

Please sign in to comment.