Skip to content

Commit

Permalink
chore: change types for of options
Browse files Browse the repository at this point in the history
  • Loading branch information
go-to-k committed Jun 7, 2023
1 parent 5365375 commit 9968af0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 91 deletions.
42 changes: 14 additions & 28 deletions packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) { }
}

/**
Expand Down
88 changes: 25 additions & 63 deletions packages/@aws-cdk/aws-apprunner-alpha/test/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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: {
Expand All @@ -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', () => {
Expand Down

0 comments on commit 9968af0

Please sign in to comment.