Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
k70suK3-k06a7ash1 committed Apr 9, 2024
1 parent 9c1b8b1 commit b5ca933
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 148 deletions.
141 changes: 7 additions & 134 deletions cdk/lib/utils/corn.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,16 @@
type CronFieldBase = string | number | "*" | "-" | ",";

type Minute = `${CronFieldBase}` | "/";
type Hour = `${CronFieldBase}` | "/";
type DayOfMonth = `${CronFieldBase}` | "?" | "L" | "W" | "/";
type Month =
| `${CronFieldBase}`
| "JAN"
| "FEB"
| "MAR"
| "APR"
| "MAY"
| "JUN"
| "JUL"
| "AUG"
| "SEP"
| "OCT"
| "NOV"
| "DEC"
| "/";
type DayOfWeek =
| `${CronFieldBase}`
| "?"
| "L"
| "#"
| "SUN"
| "MON"
| "TUE"
| "WED"
| "THU"
| "FRI"
| "SAT"
| "/";
type Year = `${1970 | number}-${2199 | number}` | "*" | "-" | "/";

type CronSchedule = {
minute: Minute;
hour: Hour;
dayOfMonth: DayOfMonth;
month: Month;
dayOfWeek: DayOfWeek;
year: Year;
};
import { CronOptions } from "aws-cdk-lib/aws-events";

export type RdsSchedules = {
stop?: CronSchedule;
restored?: CronSchedule;
};

const isCronFieldBase = (value: unknown): value is CronFieldBase =>
typeof value === "string" ||
typeof value === "number" ||
value === "*" ||
value === "-" ||
value === ",";

const isMinute = (value: unknown): value is Minute =>
isCronFieldBase(value) || value === "/";

const isHour = (value: unknown): value is Hour =>
isCronFieldBase(value) || value === "/";

const isDayOfMonth = (value: unknown): value is DayOfMonth =>
isCronFieldBase(value) ||
value === "?" ||
value === "L" ||
value === "W" ||
value === "/";

const isMonth = (value: unknown): value is Month => {
const months = [
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC",
];
return (
isCronFieldBase(value) ||
(typeof value === "string" && months.includes(value)) ||
value === "/"
);
};

const isDayOfWeek = (value: unknown): value is DayOfWeek => {
const days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
return (
isCronFieldBase(value) ||
value === "?" ||
value === "L" ||
value === "#" ||
(typeof value === "string" && days.includes(value)) ||
value === "/"
);
};

const isYear = (value: unknown): value is Year => {
if (typeof value !== "string") return false;
if (value === "*" || value === "-" || value === "/") return true;

const yearRange = value.split("-");
if (yearRange.length !== 2) return false;

const startYear = parseInt(yearRange[0], 10);
const endYear = parseInt(yearRange[1], 10);

return (
!isNaN(startYear) && !isNaN(endYear) && startYear >= 1970 && endYear <= 2199
);
};

const hasCornTypeGuard = (
rdsSchedules: unknown
): rdsSchedules is RdsSchedules => {
const args = rdsSchedules as RdsSchedules;
return isCornSchedule(args.restored) && isCornSchedule(args.stop);
stop: CronOptions;
restored: CronOptions;
};

const isCornSchedule = (cornObject: unknown): cornObject is CronSchedule => {
const args = cornObject as CronSchedule;

return (
isYear(args.year) &&
isDayOfWeek(args.dayOfWeek) &&
isDayOfMonth(args.dayOfMonth) &&
isMonth(args.month) &&
isHour(args.hour) &&
isMinute(args.minute)
);
};
const hasCronObject = (cornObject: CronOptions) =>
Object.keys(cornObject).length > 0;

export const createRdsScheduler = (rdsSchedules: RdsSchedules) => {
const hasCorn = () => hasCornTypeGuard(rdsSchedules);
const hasCorn = () =>
hasCronObject(rdsSchedules.stop) && hasCronObject(rdsSchedules.restored);

return {
hasCorn,
Expand Down
40 changes: 26 additions & 14 deletions cdk/test/cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ describe("Fine-grained Assertions Test", () => {
dbEncryption: false,
publishedApiAllowedIpV4AddressRanges: [""],
publishedApiAllowedIpV6AddressRanges: [""],
rdsSchedules: {},
rdsSchedules: {
stop: {},
restored: {},
},
}
);
const hasGoogleProviderTemplate = Template.fromStack(
Expand Down Expand Up @@ -75,7 +78,10 @@ describe("Fine-grained Assertions Test", () => {
dbEncryption: false,
publishedApiAllowedIpV4AddressRanges: [""],
publishedApiAllowedIpV6AddressRanges: [""],
rdsSchedules: {},
rdsSchedules: {
stop: {},
restored: {},
},
}
);
const hasOidcProviderTemplate = Template.fromStack(hasOidcProviderStack);
Expand Down Expand Up @@ -114,7 +120,10 @@ describe("Fine-grained Assertions Test", () => {
dbEncryption: false,
publishedApiAllowedIpV4AddressRanges: [""],
publishedApiAllowedIpV6AddressRanges: [""],
rdsSchedules: {},
rdsSchedules: {
stop: {},
restored: {},
},
});
const template = Template.fromStack(stack);

Expand All @@ -137,20 +146,20 @@ describe("Scheduler Test", () => {
publishedApiAllowedIpV6AddressRanges: [""],
rdsSchedules: {
stop: {
minute: "",
hour: "",
dayOfMonth: "",
month: "",
dayOfWeek: "",
minute: "00",
hour: "22",
day: "*",
month: "*",
year: "*",
weekDay: "*",
},
restored: {
minute: "",
hour: "",
dayOfMonth: "",
month: "",
dayOfWeek: "",
minute: "00",
hour: "7",
day: "*",
month: "*",
year: "*",
weekDay: "*",
},
},
});
Expand All @@ -167,7 +176,10 @@ describe("Scheduler Test", () => {
dbEncryption: false,
publishedApiAllowedIpV4AddressRanges: [""],
publishedApiAllowedIpV6AddressRanges: [""],
rdsSchedules: {},
rdsSchedules: {
stop: {},
restored: {},
},
});
});
});

0 comments on commit b5ca933

Please sign in to comment.