Skip to content

Commit

Permalink
fix(region-info): facts only returned from constant region list (#27506)
Browse files Browse the repository at this point in the history
Facts are only being returned from the regions in a constant list.
This PR returns facts for all Regions in `AWS_REGIONS` + `this.database`.

Closes #27260.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
msambol authored and mrgrain committed Nov 1, 2023
1 parent 72f478f commit 605368c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/aws-cdk-lib/region-info/lib/fact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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
* may not be an exhaustive list of all available AWS regions.
* @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[] {
// Return by copy to ensure no modifications can be made to the undelying constant.
return Array.from(AWS_REGIONS);
// Return the union of regions in AWS_REGIONS and custom defined regions.
return [...new Set([...AWS_REGIONS, ...Object.keys(this.database)])];
}

/**
Expand All @@ -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}.`);
}

return foundFact;
Expand Down
19 changes: 19 additions & 0 deletions packages/aws-cdk-lib/region-info/test/fact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,23 @@ describe('register', () => {
// Cleanup
Fact.unregister(region, name);
});

test('registering a fact with a new region adds the region', () => {
// GIVEN
const region = 'my-custom-region';
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('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();
});
});
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/region-info/test/region-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ test.each([
['us-iso-west-1', false],
])('%p should be opt-in: %p', (region, expected) => {
expect(RegionInfo.get(region).isOptInRegion).toEqual(expected);
});
});

0 comments on commit 605368c

Please sign in to comment.