Skip to content

Commit

Permalink
fix(location): underscores are not allowed in the name (#32046)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

N/A

### Reason for this change
According to the [CFn documentation](https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-location-tracker.html#cfn-location-tracker-trackername), underscores should be allowed in the name of the following resources. 

However, the Regex pattern is incorrect, and underscores are not permitted.

* [geofenceCollectionName](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-location-alpha.GeofenceCollection.html#geofencecollectionnamespan-classapi-icon-api-icon-experimental-titlethis-api-element-is-experimental-it-may-change-without-noticespan)
* [placeIndexName](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-location-alpha.PlaceIndex.html#placeindexnamespan-classapi-icon-api-icon-experimental-titlethis-api-element-is-experimental-it-may-change-without-noticespan)
* [routeCalculatorName](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-location-alpha.RouteCalculator.html#routecalculatornamespan-classapi-icon-api-icon-experimental-titlethis-api-element-is-experimental-it-may-change-without-noticespan)
* [trackerName](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-location-alpha.Tracker.html#trackernamespan-classapi-icon-api-icon-experimental-titlethis-api-element-is-experimental-it-may-change-without-noticespan)


### Description of changes
Fix regex pattern in validations.
Additionally, I separated the Regex pattern validation and the character count validation to make it more user-friendly.



### Description of how you validated changes
Add unit tests and integ tests.



### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mazyu36 authored Nov 21, 2024
1 parent 27babe6 commit f6ad9c9
Show file tree
Hide file tree
Showing 42 changed files with 19,536 additions and 21,352 deletions.
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-location-alpha/lib/geofence-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ export class GeofenceCollection extends Resource implements IGeofenceCollection
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
}

if (props.geofenceCollectionName && !Token.isUnresolved(props.geofenceCollectionName) && !/^[-.\w]{1,100}$/.test(props.geofenceCollectionName)) {
throw new Error(`Invalid geofence collection name. The geofence collection name must be between 1 and 100 characters and contain only alphanumeric characters, hyphens, periods and underscores. Received: ${props.geofenceCollectionName}`);
if (props.geofenceCollectionName !== undefined && !Token.isUnresolved(props.geofenceCollectionName)) {
if (props.geofenceCollectionName.length < 1 || props.geofenceCollectionName.length > 100) {
throw new Error(`\`geofenceCollectionName\` must be between 1 and 100 characters, got: ${props.geofenceCollectionName.length} characters.`);
}

if (!/^[-._\w]+$/.test(props.geofenceCollectionName)) {
throw new Error(`\`geofenceCollectionName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.geofenceCollectionName}.`);
}
}

super(scope, id, {
Expand Down
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-location-alpha/lib/place-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,14 @@ export class PlaceIndex extends Resource implements IPlaceIndex {
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
}

if (props.placeIndexName && !Token.isUnresolved(props.placeIndexName) && !/^[-.\w]{1,100}$/.test(props.placeIndexName)) {
throw new Error(`Invalid place index name. The place index name must be between 1 and 100 characters and contain only alphanumeric characters, hyphens, periods and underscores. Received: ${props.placeIndexName}`);
if (props.placeIndexName !== undefined && !Token.isUnresolved(props.placeIndexName)) {
if (props.placeIndexName.length < 1 || props.placeIndexName.length > 100) {
throw new Error(`\`placeIndexName\` must be between 1 and 100 characters, got: ${props.placeIndexName.length} characters.`);
}

if (!/^[-._\w]+$/.test(props.placeIndexName)) {
throw new Error(`\`placeIndexName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.placeIndexName}.`);
}
}

super(scope, id, {
Expand Down
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-location-alpha/lib/route-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ export class RouteCalculator extends Resource implements IRouteCalculator {
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
}

if (props.routeCalculatorName && !Token.isUnresolved(props.routeCalculatorName) && !/^[-.\w]{1,100}$/.test(props.routeCalculatorName)) {
throw new Error(`Invalid route calculator name. The route calculator name must be between 1 and 100 characters and contain only alphanumeric characters, hyphens, periods and underscores. Received: ${props.routeCalculatorName}`);
if (props.routeCalculatorName !== undefined && !Token.isUnresolved(props.routeCalculatorName)) {
if (props.routeCalculatorName.length < 1 || props.routeCalculatorName.length > 100) {
throw new Error(`\`routeCalculatorName\` must be between 1 and 100 characters, got: ${props.routeCalculatorName.length} characters.`);
}

if (!/^[-._\w]+$/.test(props.routeCalculatorName)) {
throw new Error(`\`routeCalculatorName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.routeCalculatorName}.`);
}
}

super(scope, id, {
Expand Down
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-location-alpha/lib/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,14 @@ export class Tracker extends Resource implements ITracker {
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
}

if (props.trackerName && !Token.isUnresolved(props.trackerName) && !/^[-.\w]{1,100}$/.test(props.trackerName)) {
throw new Error(`Invalid tracker name. The tracker name must be between 1 and 100 characters and contain only alphanumeric characters, hyphens, periods and underscores. Received: ${props.trackerName}`);
if (props.trackerName !== undefined && !Token.isUnresolved(props.trackerName)) {
if (props.trackerName.length < 1 || props.trackerName.length > 100) {
throw new Error(`\`trackerName\` must be between 1 and 100 characters, got: ${props.trackerName.length} characters.`);
}

if (!/^[-._\w]+$/.test(props.trackerName)) {
throw new Error(`\`trackerName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.trackerName}.`);
}
}

if (!Token.isUnresolved(props.kmsKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@ test('throws with invalid description', () => {
})).toThrow('`description` must be between 0 and 1000 characters. Received: 1001 characters');
});

test('create a geofence collection with name', () => {
new GeofenceCollection(stack, 'GeofenceCollection', {
geofenceCollectionName: 'my_geofence_collection',
});

Template.fromStack(stack).hasResourceProperties('AWS::Location::GeofenceCollection', {
CollectionName: 'my_geofence_collection',
});
});

test.each(['', 'a'.repeat(101)])('throws with invalid name, got: %s', (geofenceCollectionName) => {
expect(() => new GeofenceCollection(stack, 'GeofenceCollection', {
geofenceCollectionName,
})).toThrow(`\`geofenceCollectionName\` must be between 1 and 100 characters, got: ${geofenceCollectionName.length} characters.`);
});

test('throws with invalid name', () => {
expect(() => new GeofenceCollection(stack, 'GeofenceCollection', {
geofenceCollectionName: 'inv@lid',
})).toThrow('Invalid geofence collection name. The geofence collection name must be between 1 and 100 characters and contain only alphanumeric characters, hyphens, periods and underscores. Received: inv@lid');
})).toThrow('`geofenceCollectionName` must contain only alphanumeric characters, hyphens, periods and underscores, got: inv@lid.');
});

test('grant read actions', () => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"GeofenceCollection6FAC681F": {
"Type": "AWS::Location::GeofenceCollection",
"Properties": {
"CollectionName": "MyGeofenceCollection",
"CollectionName": "my_geofence_collection",
"Description": "test",
"KmsKeyId": {
"Fn::GetAtt": [
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestStack extends Stack {
});

new GeofenceCollection(this, 'GeofenceCollection', {
geofenceCollectionName: 'MyGeofenceCollection',
geofenceCollectionName: 'my_geofence_collection',
description: 'test',
kmsKey,
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Type": "AWS::Location::PlaceIndex",
"Properties": {
"DataSource": "Esri",
"IndexName": "cdkinteglocationplaceindexPlaceIndex21A03EAC"
"IndexName": "my_place_index"
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f6ad9c9

Please sign in to comment.