From 51ec1d3e1852c44eb55111d1e43ca289c612bbdd Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Wed, 11 Oct 2023 17:30:02 -0600 Subject: [PATCH 1/8] chore(region-info): update fact not found error message --- packages/aws-cdk-lib/region-info/lib/fact.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/region-info/lib/fact.ts b/packages/aws-cdk-lib/region-info/lib/fact.ts index 366141721c693..1fdf17251beae 100644 --- a/packages/aws-cdk-lib/region-info/lib/fact.ts +++ b/packages/aws-cdk-lib/region-info/lib/fact.ts @@ -37,7 +37,7 @@ export class Fact { const foundFact = this.find(region, name); if (!foundFact) { - throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}`); + throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}. Regions is a fixed list and defined in aws-entities.ts as AWS_REGIONS.`); } return foundFact; From 877334c28e292a64c4319c4ac628677aedbd07c1 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Thu, 12 Oct 2023 12:00:01 -0600 Subject: [PATCH 2/8] Add feedback from Luca --- packages/aws-cdk-lib/region-info/lib/fact.ts | 6 ++---- .../aws-cdk-lib/region-info/test/fact.test.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/region-info/lib/fact.ts b/packages/aws-cdk-lib/region-info/lib/fact.ts index 1fdf17251beae..eb75040bf4e82 100644 --- a/packages/aws-cdk-lib/region-info/lib/fact.ts +++ b/packages/aws-cdk-lib/region-info/lib/fact.ts @@ -1,5 +1,3 @@ -import { AWS_REGIONS } from './aws-entities'; - /** * A database of regional information. */ @@ -10,7 +8,7 @@ export class Fact { */ public static get regions(): string[] { // Return by copy to ensure no modifications can be made to the undelying constant. - return Array.from(AWS_REGIONS); + return Array.from(Object.keys(this.database)); } /** @@ -37,7 +35,7 @@ export class Fact { const foundFact = this.find(region, name); if (!foundFact) { - throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}. Regions is a fixed list and defined in aws-entities.ts as AWS_REGIONS.`); + throw new Error(`No fact ${name} could be found for region: ${region} and name: ${name}.`); } return foundFact; diff --git a/packages/aws-cdk-lib/region-info/test/fact.test.ts b/packages/aws-cdk-lib/region-info/test/fact.test.ts index b3e3bab7fdab7..2e403f37cf7aa 100644 --- a/packages/aws-cdk-lib/region-info/test/fact.test.ts +++ b/packages/aws-cdk-lib/region-info/test/fact.test.ts @@ -89,4 +89,19 @@ describe('register', () => { // Cleanup Fact.unregister(region, name); }); + + test('registering a fact with a new region adds the region', () => { + // GIVEN + const region = 'us-unreleased-1'; + const name = FactName.PARTITION; + const value = 'nebraska'; + + // WHEN + expect(Fact.find(region, name)).not.toBe(value); + expect(() => Fact.register({ region, name, value })).not.toThrowError(); + + // THEN + expect(Fact.regions.includes('us-unreleased-1')).toBeTruthy(); + expect(Fact.find(region, name)).toBe('nebraska'); + }); }); From f2164c916affa0486ae58b764845410160d42fd6 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Thu, 12 Oct 2023 13:02:27 -0600 Subject: [PATCH 3/8] Add int test --- .../region-info/test/region-info.test.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/region-info/test/region-info.test.ts b/packages/aws-cdk-lib/region-info/test/region-info.test.ts index f014405a8b145..5d195634e956c 100644 --- a/packages/aws-cdk-lib/region-info/test/region-info.test.ts +++ b/packages/aws-cdk-lib/region-info/test/region-info.test.ts @@ -1,5 +1,5 @@ import { APPCONFIG_LAMBDA_LAYER_ARNS, CLOUDWATCH_LAMBDA_INSIGHTS_ARNS } from '../build-tools/fact-tables'; -import { FactName, RegionInfo } from '../lib'; +import { Fact, FactName, RegionInfo } from '../lib'; import { AWS_REGIONS, AWS_SERVICES } from '../lib/aws-entities'; test('built-in data is correct', () => { @@ -66,10 +66,22 @@ test('limitedRegionMap only returns information for certain regions', () => { expect(map2['cn-north-1']).toBeDefined(); }); +test('registering a fact with a new region adds the region', () => { + const region = 'us-unreleased-1'; + const name = FactName.PARTITION; + const value = 'nebraska'; + + expect(Fact.find(region, name)).not.toBe(value); + expect(() => Fact.register({ region, name, value })).not.toThrowError(); + + expect(Fact.regions.includes('us-unreleased-1')).toBeTruthy(); + expect(Fact.find(region, name)).toBe('nebraska'); +}); + test.each([ ['us-east-1', false], ['me-south-1', true], ['us-iso-west-1', false], ])('%p should be opt-in: %p', (region, expected) => { expect(RegionInfo.get(region).isOptInRegion).toEqual(expected); -}); \ No newline at end of file +}); From a127b92350469a55fdeb64480bbc9c279dbaf4b8 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Thu, 12 Oct 2023 13:06:17 -0600 Subject: [PATCH 4/8] thats not actually an int test --- .../aws-cdk-lib/region-info/test/region-info.test.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packages/aws-cdk-lib/region-info/test/region-info.test.ts b/packages/aws-cdk-lib/region-info/test/region-info.test.ts index 5d195634e956c..76819bc0baf79 100644 --- a/packages/aws-cdk-lib/region-info/test/region-info.test.ts +++ b/packages/aws-cdk-lib/region-info/test/region-info.test.ts @@ -66,18 +66,6 @@ test('limitedRegionMap only returns information for certain regions', () => { expect(map2['cn-north-1']).toBeDefined(); }); -test('registering a fact with a new region adds the region', () => { - const region = 'us-unreleased-1'; - const name = FactName.PARTITION; - const value = 'nebraska'; - - expect(Fact.find(region, name)).not.toBe(value); - expect(() => Fact.register({ region, name, value })).not.toThrowError(); - - expect(Fact.regions.includes('us-unreleased-1')).toBeTruthy(); - expect(Fact.find(region, name)).toBe('nebraska'); -}); - test.each([ ['us-east-1', false], ['me-south-1', true], From 17654c3a4e62c332dee68fcae559363370970290 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Mon, 23 Oct 2023 18:58:29 -0600 Subject: [PATCH 5/8] add feedback from Momo --- packages/aws-cdk-lib/region-info/lib/fact.ts | 6 ++++-- packages/aws-cdk-lib/region-info/test/fact.test.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/region-info/lib/fact.ts b/packages/aws-cdk-lib/region-info/lib/fact.ts index eb75040bf4e82..38df4573f31db 100644 --- a/packages/aws-cdk-lib/region-info/lib/fact.ts +++ b/packages/aws-cdk-lib/region-info/lib/fact.ts @@ -1,14 +1,16 @@ +import { AWS_REGIONS } from './aws-entities'; + /** * A database of regional information. */ export class Fact { /** * @returns the list of names of AWS regions for which there is at least one registered fact. This - * may not be an exhaustive list of all available AWS regions. + * includes Regions defined in AWS_REGIONS plus custom defined regions. */ public static get regions(): string[] { // Return by copy to ensure no modifications can be made to the undelying constant. - return Array.from(Object.keys(this.database)); + return Array.from(AWS_REGIONS.concat(Object.keys(this.database))); } /** diff --git a/packages/aws-cdk-lib/region-info/test/fact.test.ts b/packages/aws-cdk-lib/region-info/test/fact.test.ts index 2e403f37cf7aa..46abed8a371b6 100644 --- a/packages/aws-cdk-lib/region-info/test/fact.test.ts +++ b/packages/aws-cdk-lib/region-info/test/fact.test.ts @@ -92,7 +92,7 @@ describe('register', () => { test('registering a fact with a new region adds the region', () => { // GIVEN - const region = 'us-unreleased-1'; + const region = 'my-custom-region'; const name = FactName.PARTITION; const value = 'nebraska'; @@ -101,7 +101,7 @@ describe('register', () => { expect(() => Fact.register({ region, name, value })).not.toThrowError(); // THEN - expect(Fact.regions.includes('us-unreleased-1')).toBeTruthy(); + expect(Fact.regions.includes('my-custom-region')).toBeTruthy(); expect(Fact.find(region, name)).toBe('nebraska'); }); }); From 04e22bf04842f4494326b141ec3f943465ed096e Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Mon, 23 Oct 2023 18:59:24 -0600 Subject: [PATCH 6/8] capital R --- packages/aws-cdk-lib/region-info/lib/fact.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/region-info/lib/fact.ts b/packages/aws-cdk-lib/region-info/lib/fact.ts index 38df4573f31db..9671de701a5c8 100644 --- a/packages/aws-cdk-lib/region-info/lib/fact.ts +++ b/packages/aws-cdk-lib/region-info/lib/fact.ts @@ -5,7 +5,7 @@ import { AWS_REGIONS } from './aws-entities'; */ export class Fact { /** - * @returns the list of names of AWS regions for which there is at least one registered fact. This + * @returns the list of names of AWS Regions for which there is at least one registered fact. This * includes Regions defined in AWS_REGIONS plus custom defined regions. */ public static get regions(): string[] { From 714db32f0e2117a718ac2c067bbeea929f52fef3 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Mon, 23 Oct 2023 19:00:08 -0600 Subject: [PATCH 7/8] remove Fact --- packages/aws-cdk-lib/region-info/test/region-info.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/region-info/test/region-info.test.ts b/packages/aws-cdk-lib/region-info/test/region-info.test.ts index 76819bc0baf79..76130465d6567 100644 --- a/packages/aws-cdk-lib/region-info/test/region-info.test.ts +++ b/packages/aws-cdk-lib/region-info/test/region-info.test.ts @@ -1,5 +1,5 @@ import { APPCONFIG_LAMBDA_LAYER_ARNS, CLOUDWATCH_LAMBDA_INSIGHTS_ARNS } from '../build-tools/fact-tables'; -import { Fact, FactName, RegionInfo } from '../lib'; +import { FactName, RegionInfo } from '../lib'; import { AWS_REGIONS, AWS_SERVICES } from '../lib/aws-entities'; test('built-in data is correct', () => { From b5b907aa6baee5a479d672a86abc5eba71d1319c Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Mon, 23 Oct 2023 19:19:52 -0600 Subject: [PATCH 8/8] make sure regions are unique --- packages/aws-cdk-lib/region-info/lib/fact.ts | 4 ++-- packages/aws-cdk-lib/region-info/test/fact.test.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/region-info/lib/fact.ts b/packages/aws-cdk-lib/region-info/lib/fact.ts index 9671de701a5c8..67bbe9ce42d52 100644 --- a/packages/aws-cdk-lib/region-info/lib/fact.ts +++ b/packages/aws-cdk-lib/region-info/lib/fact.ts @@ -9,8 +9,8 @@ export class Fact { * includes Regions defined in AWS_REGIONS plus custom defined regions. */ public static get regions(): string[] { - // Return by copy to ensure no modifications can be made to the undelying constant. - return Array.from(AWS_REGIONS.concat(Object.keys(this.database))); + // Return the union of regions in AWS_REGIONS and custom defined regions. + return [...new Set([...AWS_REGIONS, ...Object.keys(this.database)])]; } /** diff --git a/packages/aws-cdk-lib/region-info/test/fact.test.ts b/packages/aws-cdk-lib/region-info/test/fact.test.ts index 46abed8a371b6..a77e34bade5d1 100644 --- a/packages/aws-cdk-lib/region-info/test/fact.test.ts +++ b/packages/aws-cdk-lib/region-info/test/fact.test.ts @@ -104,4 +104,8 @@ describe('register', () => { expect(Fact.regions.includes('my-custom-region')).toBeTruthy(); expect(Fact.find(region, name)).toBe('nebraska'); }); + + test('regions does not return duplicate regions', () => { + expect(new Set(Fact.regions).size == Fact.regions.length).toBeTruthy(); + }); });