From 0b1ea768d4ecbc429e6971cb2062279de09df61e Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 12 Jul 2019 10:48:02 +0200 Subject: [PATCH] fix(logs): fix infinite retention for jsii users (#3250) The retention period was still accepting the value `Infinity` for "infinite retention", even though the property has been changed to an enum instead of `number`. Even though this apparently works for TypeScript customers, jsii customers will not be able to pass in a number where an enum value is expected, and so won't be able to configure infinite retention. Add an `INFINITE` value to the enum. --- packages/@aws-cdk/aws-logs/lib/log-group.ts | 13 +++++++--- .../@aws-cdk/aws-logs/test/test.loggroup.ts | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-logs/lib/log-group.ts b/packages/@aws-cdk/aws-logs/lib/log-group.ts index c3095413f35d0..d57d47b22e653 100644 --- a/packages/@aws-cdk/aws-logs/lib/log-group.ts +++ b/packages/@aws-cdk/aws-logs/lib/log-group.ts @@ -259,7 +259,12 @@ export enum RetentionDays { /** * 10 years */ - TEN_YEARS = 3653 + TEN_YEARS = 3653, + + /** + * Retain logs forever + */ + INFINITE = 9999, } /** @@ -276,9 +281,9 @@ export interface LogGroupProps { /** * How long, in days, the log contents will be retained. * - * To retain all logs, set this value to Infinity. + * To retain all logs, set this value to RetentionDays.INFINITE. * - * @default RetentionDays.TwoYears + * @default RetentionDays.TWO_YEARS */ readonly retention?: RetentionDays; @@ -328,7 +333,7 @@ export class LogGroup extends LogGroupBase { let retentionInDays = props.retention; if (retentionInDays === undefined) { retentionInDays = RetentionDays.TWO_YEARS; } - if (retentionInDays === Infinity) { retentionInDays = undefined; } + if (retentionInDays === Infinity || retentionInDays === RetentionDays.INFINITE) { retentionInDays = undefined; } if (retentionInDays !== undefined && retentionInDays <= 0) { throw new Error(`retentionInDays must be positive, got ${retentionInDays}`); diff --git a/packages/@aws-cdk/aws-logs/test/test.loggroup.ts b/packages/@aws-cdk/aws-logs/test/test.loggroup.ts index b6c22b183d6cd..86f55e8794171 100644 --- a/packages/@aws-cdk/aws-logs/test/test.loggroup.ts +++ b/packages/@aws-cdk/aws-logs/test/test.loggroup.ts @@ -43,6 +43,32 @@ export = { // WHEN new LogGroup(stack, 'LogGroup', { + retention: RetentionDays.INFINITE, + }); + + // THEN + expect(stack).to(matchTemplate({ + Resources: { + LogGroupF5B46931: { + Type: "AWS::Logs::LogGroup", + DeletionPolicy: "Retain", + UpdateReplacePolicy: "Retain" + } + } + })); + + test.done(); + }, + + 'infinite retention via legacy method'(test: Test) { + // GIVEN + const stack = new Stack(); + + // WHEN + new LogGroup(stack, 'LogGroup', { + // Don't know why TypeScript doesn't complain about passing Infinity to + // something where an enum is expected, but better keep this behavior for + // existing clients. retention: Infinity });