From 9968af056b39095e16f952c2d3d656edc5f109ec Mon Sep 17 00:00:00 2001 From: go-to-k <24818752+go-to-k@users.noreply.github.com> Date: Thu, 8 Jun 2023 00:34:42 +0900 Subject: [PATCH] chore: change types for of options --- .../aws-apprunner-alpha/lib/service.ts | 42 +++------ .../aws-apprunner-alpha/test/service.test.ts | 88 ++++++------------- 2 files changed, 39 insertions(+), 91 deletions(-) diff --git a/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts b/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts index a6c4f918d5c01..8b687efc7a7da 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts @@ -24,6 +24,11 @@ export enum ImageRepositoryType { ECR = 'ECR', } +/** + * CPU input patterns for App Runner service + */ +export type CPUPatternType = '256' | '512' | '1024' | '2048' | '4096' | '0.25 vCPU' | '0.5 vCPU' | '1 vCPU' | '2 vCPU' | '4 vCPU' + /** * The number of CPU units reserved for each instance of your App Runner service. * @@ -61,27 +66,20 @@ export class Cpu { * * @param unit custom CPU unit */ - public static of(unit: string): Cpu { - const numericPatterns = ['256', '512', '1024', '2048', '4096']; - const unitPatterns = ['0.25 vCPU', '0.5 vCPU', '1 vCPU', '2 vCPU', '4 vCPU']; - const allowedPatterns = numericPatterns.concat(unitPatterns); - const isValidValue = allowedPatterns.some( - (pattern) => pattern === unit, - ); - if (!isValidValue) { - throw new Error('CPU value is invalid'); - }; - - return new Cpu(unit); - } + public static of(unit: CPUPatternType): Cpu { return new Cpu(unit); } /** * * @param unit The unit of CPU. */ - private constructor(public readonly unit: string) {} + private constructor(public readonly unit: CPUPatternType) {} } +/** + * Memory input patterns for App Runner service + */ +export type MemoryPatternType = '512' | '1024' | '2048' | '3072' | '4096' | '6144' | '8192' | '10240' | '12288' | '0.5 GB' | '1 GB' | '2 GB' | '3 GB' | '4 GB' | '6 GB' | '8 GB' | '10 GB' | '12 GB'; + /** * The amount of memory reserved for each instance of your App Runner service. */ @@ -138,25 +136,13 @@ export class Memory { * * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apprunner-service-instanceconfiguration.html#cfn-apprunner-service-instanceconfiguration-memory */ - public static of(unit: string): Memory { - const numericPatterns = ['512', '1024', '2048', '3072', '4096', '6144', '8192', '10240', '12288']; - const unitPatterns = ['0.5 GB', '1 GB', '2 GB', '3 GB', '4 GB', '6 GB', '8 GB', '10 GB', '12 GB']; - const allowedPatterns = numericPatterns.concat(unitPatterns); - const isValidValue = allowedPatterns.some( - (pattern) => pattern === unit, - ); - if (!isValidValue) { - throw new Error('Memory value is invalid'); - }; - - return new Memory(unit); - } + public static of(unit: MemoryPatternType): Memory { return new Memory(unit); } /** * * @param unit The unit of memory. */ - private constructor(public readonly unit: string) { } + private constructor(public readonly unit: MemoryPatternType) { } } /** diff --git a/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts b/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts index b660aee025916..875851431ad16 100644 --- a/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts +++ b/packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts @@ -925,7 +925,7 @@ test('cpu and memory properties as unit values are allowed', () => { }); }); -test('cpu and memory properties as numeric values are allowed', () => { +test('cpu and memory properties as unit values by an of() method are allowed', () => { // GIVEN const app = new cdk.App(); const stack = new cdk.Stack(app, 'demo-stack'); @@ -934,14 +934,14 @@ test('cpu and memory properties as numeric values are allowed', () => { source: apprunner.Source.fromEcrPublic({ imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', }), - cpu: apprunner.Cpu.of('1024'), - memory: apprunner.Memory.of('3072'), + cpu: apprunner.Cpu.of('1 vCPU'), + memory: apprunner.Memory.of('3 GB'), }); // THEN Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::Service', { InstanceConfiguration: { - Cpu: '1024', - Memory: '3072', + Cpu: '1 vCPU', + Memory: '3 GB', }, NetworkConfiguration: { EgressConfiguration: { @@ -951,68 +951,30 @@ test('cpu and memory properties as numeric values are allowed', () => { }); }); -test('invalid cpu property as unit value is not allowed', () => { - // GIVEN - const app = new cdk.App(); - const stack = new cdk.Stack(app, 'demo-stack'); - // WHEN - expect(() => { - new apprunner.Service(stack, 'DemoService', { - source: apprunner.Source.fromEcrPublic({ - imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', - }), - cpu: apprunner.Cpu.of('1000 vCPU'), - memory: apprunner.Memory.of('3 GB'), - }); - }).toThrow('CPU value is invalid'); -}); - -test('invalid cpu property as numeric value is not allowed', () => { +test('cpu and memory properties as numeric values by an of() method are allowed', () => { // GIVEN const app = new cdk.App(); const stack = new cdk.Stack(app, 'demo-stack'); // WHEN - expect(() => { - new apprunner.Service(stack, 'DemoService', { - source: apprunner.Source.fromEcrPublic({ - imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', - }), - cpu: apprunner.Cpu.of('1'), - memory: apprunner.Memory.of('3 GB'), - }); - }).toThrow('CPU value is invalid'); -}); - -test('invalid memory property as unit value is not allowed', () => { - // GIVEN - const app = new cdk.App(); - const stack = new cdk.Stack(app, 'demo-stack'); - // WHEN - expect(() => { - new apprunner.Service(stack, 'DemoService', { - source: apprunner.Source.fromEcrPublic({ - imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', - }), - cpu: apprunner.Cpu.of('1 vCPU'), - memory: apprunner.Memory.of('3000 GB'), - }); - }).toThrow('Memory value is invalid'); -}); - -test('invalid memory property as numeric value is not allowed', () => { - // GIVEN - const app = new cdk.App(); - const stack = new cdk.Stack(app, 'demo-stack'); - // WHEN - expect(() => { - new apprunner.Service(stack, 'DemoService', { - source: apprunner.Source.fromEcrPublic({ - imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', - }), - cpu: apprunner.Cpu.of('1 vCPU'), - memory: apprunner.Memory.of('3'), - }); - }).toThrow('Memory value is invalid'); + new apprunner.Service(stack, 'DemoService', { + source: apprunner.Source.fromEcrPublic({ + imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest', + }), + cpu: apprunner.Cpu.of('1024'), + memory: apprunner.Memory.of('3072'), + }); + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AppRunner::Service', { + InstanceConfiguration: { + Cpu: '1024', + Memory: '3072', + }, + NetworkConfiguration: { + EgressConfiguration: { + EgressType: 'DEFAULT', + }, + }, + }); }); test('environment variable with a prefix of AWSAPPRUNNER should throw an error', () => {