From 394d6d4aa8c87c477062c00913fdea440959a55e Mon Sep 17 00:00:00 2001 From: "k.goto" <24818752+go-to-k@users.noreply.github.com> Date: Tue, 11 Jul 2023 07:58:01 +0900 Subject: [PATCH] feat(glue): validate maxCapacity, workerCount, and workerType (#26241) This PR adds the following validations, allows errors to be output in the synth phase instead of the deploy phase. - The case that maxCapacity and workerType (and workerCount) are specified. - In this [doc](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-jobs-job.html#aws-glue-api-jobs-job-Job), `Do not set MaxCapacity if using WorkerType and NumberOfWorkers.` - The case for maxCapacity with GlueVersion 2.0 or later. - In this [doc](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-jobs-job.html#aws-glue-api-jobs-job-Job), `For Glue version 2.0 or later jobs, you cannot specify a Maximum capacity` - The case that only either workerType or workerCount is specified. - Then an error occurs in CloudFormation, `Please set both Worker Type and Number of Workers.` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-glue-alpha/lib/job.ts | 10 ++++ .../@aws-cdk/aws-glue-alpha/test/job.test.ts | 48 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/packages/@aws-cdk/aws-glue-alpha/lib/job.ts b/packages/@aws-cdk/aws-glue-alpha/lib/job.ts index dd6cc425e23e0..7b5c6a1cdf7c4 100644 --- a/packages/@aws-cdk/aws-glue-alpha/lib/job.ts +++ b/packages/@aws-cdk/aws-glue-alpha/lib/job.ts @@ -719,6 +719,16 @@ export class Job extends JobBase { } } + if (props.maxCapacity !== undefined && (props.workerType && props.workerCount !== undefined)) { + throw new Error('maxCapacity cannot be used when setting workerType and workerCount'); + } + if (props.maxCapacity !== undefined && ![GlueVersion.V0_9, GlueVersion.V1_0].includes(executable.glueVersion)) { + throw new Error('maxCapacity cannot be used when GlueVersion 2.0 or later'); + } + if ((!props.workerType && props.workerCount !== undefined) || (props.workerType && props.workerCount === undefined)) { + throw new Error('Both workerType and workerCount must be set'); + } + const jobResource = new CfnJob(this, 'Resource', { name: props.jobName, description: props.description, diff --git a/packages/@aws-cdk/aws-glue-alpha/test/job.test.ts b/packages/@aws-cdk/aws-glue-alpha/test/job.test.ts index 59316670b0410..cdb5a838fba09 100644 --- a/packages/@aws-cdk/aws-glue-alpha/test/job.test.ts +++ b/packages/@aws-cdk/aws-glue-alpha/test/job.test.ts @@ -1049,5 +1049,53 @@ describe('Job', () => { })); }); }); + + describe('validation for maxCapacity and workerType', () => { + test('maxCapacity with workerType and workerCount should throw', () => { + expect(() => new glue.Job(stack, 'Job', { + executable: glue.JobExecutable.pythonEtl({ + glueVersion: glue.GlueVersion.V1_0, + pythonVersion: glue.PythonVersion.THREE, + script, + }), + maxCapacity: 10, + workerType: glue.WorkerType.G_1X, + workerCount: 10, + })).toThrow('maxCapacity cannot be used when setting workerType and workerCount'); + }); + + test('maxCapacity with GlueVersion 2.0 or later should throw', () => { + expect(() => new glue.Job(stack, 'Job', { + executable: glue.JobExecutable.pythonEtl({ + glueVersion: glue.GlueVersion.V2_0, + pythonVersion: glue.PythonVersion.THREE, + script, + }), + maxCapacity: 10, + })).toThrow('maxCapacity cannot be used when GlueVersion 2.0 or later'); + }); + + test('workerType without workerCount should throw', () => { + expect(() => new glue.Job(stack, 'Job', { + executable: glue.JobExecutable.pythonEtl({ + glueVersion: glue.GlueVersion.V2_0, + pythonVersion: glue.PythonVersion.THREE, + script, + }), + workerType: glue.WorkerType.G_1X, + })).toThrow('Both workerType and workerCount must be set'); + }); + + test('workerCount without workerType should throw', () => { + expect(() => new glue.Job(stack, 'Job', { + executable: glue.JobExecutable.pythonEtl({ + glueVersion: glue.GlueVersion.V2_0, + pythonVersion: glue.PythonVersion.THREE, + script, + }), + workerCount: 10, + })).toThrow('Both workerType and workerCount must be set'); + }); + }); }); });