Skip to content

Commit f6ad9c9

Browse files
authored
fix(location): underscores are not allowed in the name (#32046)
### 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*
1 parent 27babe6 commit f6ad9c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+19536
-21352
lines changed

packages/@aws-cdk/aws-location-alpha/lib/geofence-collection.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,14 @@ export class GeofenceCollection extends Resource implements IGeofenceCollection
118118
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
119119
}
120120

121-
if (props.geofenceCollectionName && !Token.isUnresolved(props.geofenceCollectionName) && !/^[-.\w]{1,100}$/.test(props.geofenceCollectionName)) {
122-
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}`);
121+
if (props.geofenceCollectionName !== undefined && !Token.isUnresolved(props.geofenceCollectionName)) {
122+
if (props.geofenceCollectionName.length < 1 || props.geofenceCollectionName.length > 100) {
123+
throw new Error(`\`geofenceCollectionName\` must be between 1 and 100 characters, got: ${props.geofenceCollectionName.length} characters.`);
124+
}
125+
126+
if (!/^[-._\w]+$/.test(props.geofenceCollectionName)) {
127+
throw new Error(`\`geofenceCollectionName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.geofenceCollectionName}.`);
128+
}
123129
}
124130

125131
super(scope, id, {

packages/@aws-cdk/aws-location-alpha/lib/place-index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,14 @@ export class PlaceIndex extends Resource implements IPlaceIndex {
137137
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
138138
}
139139

140-
if (props.placeIndexName && !Token.isUnresolved(props.placeIndexName) && !/^[-.\w]{1,100}$/.test(props.placeIndexName)) {
141-
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}`);
140+
if (props.placeIndexName !== undefined && !Token.isUnresolved(props.placeIndexName)) {
141+
if (props.placeIndexName.length < 1 || props.placeIndexName.length > 100) {
142+
throw new Error(`\`placeIndexName\` must be between 1 and 100 characters, got: ${props.placeIndexName.length} characters.`);
143+
}
144+
145+
if (!/^[-._\w]+$/.test(props.placeIndexName)) {
146+
throw new Error(`\`placeIndexName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.placeIndexName}.`);
147+
}
142148
}
143149

144150
super(scope, id, {

packages/@aws-cdk/aws-location-alpha/lib/route-calculator.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,14 @@ export class RouteCalculator extends Resource implements IRouteCalculator {
114114
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
115115
}
116116

117-
if (props.routeCalculatorName && !Token.isUnresolved(props.routeCalculatorName) && !/^[-.\w]{1,100}$/.test(props.routeCalculatorName)) {
118-
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}`);
117+
if (props.routeCalculatorName !== undefined && !Token.isUnresolved(props.routeCalculatorName)) {
118+
if (props.routeCalculatorName.length < 1 || props.routeCalculatorName.length > 100) {
119+
throw new Error(`\`routeCalculatorName\` must be between 1 and 100 characters, got: ${props.routeCalculatorName.length} characters.`);
120+
}
121+
122+
if (!/^[-._\w]+$/.test(props.routeCalculatorName)) {
123+
throw new Error(`\`routeCalculatorName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.routeCalculatorName}.`);
124+
}
119125
}
120126

121127
super(scope, id, {

packages/@aws-cdk/aws-location-alpha/lib/tracker.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,14 @@ export class Tracker extends Resource implements ITracker {
177177
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
178178
}
179179

180-
if (props.trackerName && !Token.isUnresolved(props.trackerName) && !/^[-.\w]{1,100}$/.test(props.trackerName)) {
181-
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}`);
180+
if (props.trackerName !== undefined && !Token.isUnresolved(props.trackerName)) {
181+
if (props.trackerName.length < 1 || props.trackerName.length > 100) {
182+
throw new Error(`\`trackerName\` must be between 1 and 100 characters, got: ${props.trackerName.length} characters.`);
183+
}
184+
185+
if (!/^[-._\w]+$/.test(props.trackerName)) {
186+
throw new Error(`\`trackerName\` must contain only alphanumeric characters, hyphens, periods and underscores, got: ${props.trackerName}.`);
187+
}
182188
}
183189

184190
if (!Token.isUnresolved(props.kmsKey)

packages/@aws-cdk/aws-location-alpha/test/geofence-collection.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,26 @@ test('throws with invalid description', () => {
3232
})).toThrow('`description` must be between 0 and 1000 characters. Received: 1001 characters');
3333
});
3434

35+
test('create a geofence collection with name', () => {
36+
new GeofenceCollection(stack, 'GeofenceCollection', {
37+
geofenceCollectionName: 'my_geofence_collection',
38+
});
39+
40+
Template.fromStack(stack).hasResourceProperties('AWS::Location::GeofenceCollection', {
41+
CollectionName: 'my_geofence_collection',
42+
});
43+
});
44+
45+
test.each(['', 'a'.repeat(101)])('throws with invalid name, got: %s', (geofenceCollectionName) => {
46+
expect(() => new GeofenceCollection(stack, 'GeofenceCollection', {
47+
geofenceCollectionName,
48+
})).toThrow(`\`geofenceCollectionName\` must be between 1 and 100 characters, got: ${geofenceCollectionName.length} characters.`);
49+
});
50+
3551
test('throws with invalid name', () => {
3652
expect(() => new GeofenceCollection(stack, 'GeofenceCollection', {
3753
geofenceCollectionName: 'inv@lid',
38-
})).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');
54+
})).toThrow('`geofenceCollectionName` must contain only alphanumeric characters, hyphens, periods and underscores, got: inv@lid.');
3955
});
4056

4157
test('grant read actions', () => {

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/GeofenceCollectionTestDefaultTestDeployAssert44609017.assets.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/cdk-integ-location-geofence-collection.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/cdk-integ-location-geofence-collection.template.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"GeofenceCollection6FAC681F": {
3939
"Type": "AWS::Location::GeofenceCollection",
4040
"Properties": {
41-
"CollectionName": "MyGeofenceCollection",
41+
"CollectionName": "my_geofence_collection",
4242
"Description": "test",
4343
"KmsKeyId": {
4444
"Fn::GetAtt": [

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/manifest.json

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.js.snapshot/tree.json

+25-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.geofence-collection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestStack extends Stack {
1313
});
1414

1515
new GeofenceCollection(this, 'GeofenceCollection', {
16-
geofenceCollectionName: 'MyGeofenceCollection',
16+
geofenceCollectionName: 'my_geofence_collection',
1717
description: 'test',
1818
kmsKey,
1919
});

packages/@aws-cdk/aws-location-alpha/test/integ.place-index.js.snapshot/PlaceIndexTestDefaultTestDeployAssert3F96C508.assets.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.place-index.js.snapshot/cdk-integ-location-place-index.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.place-index.js.snapshot/cdk-integ-location-place-index.template.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Type": "AWS::Location::PlaceIndex",
55
"Properties": {
66
"DataSource": "Esri",
7-
"IndexName": "cdkinteglocationplaceindexPlaceIndex21A03EAC"
7+
"IndexName": "my_place_index"
88
}
99
}
1010
},

packages/@aws-cdk/aws-location-alpha/test/integ.place-index.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-location-alpha/test/integ.place-index.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)