forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(location):
PlaceIndex
(aws#22853)
Add a L2 for the place index resource. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
16 changed files
with
736 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './place-index'; | ||
|
||
// AWS::Location CloudFormation Resources: | ||
export * from './location.generated'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { ArnFormat, IResource, Lazy, Names, Resource, Stack, Token } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CfnPlaceIndex } from './location.generated'; | ||
|
||
/** | ||
* A Place Index | ||
*/ | ||
export interface IPlaceIndex extends IResource { | ||
/** | ||
* The name of the place index | ||
* | ||
* @attribute | ||
*/ | ||
readonly placeIndexName: string; | ||
|
||
/** | ||
* The Amazon Resource Name (ARN) of the place index resource | ||
* | ||
* @attribute Arn,IndexArn | ||
*/ | ||
readonly placeIndexArn: string; | ||
} | ||
|
||
/** | ||
* Properties for a place index | ||
*/ | ||
export interface PlaceIndexProps { | ||
/** | ||
* A name for the place index | ||
* | ||
* @default - A name is automatically generated | ||
*/ | ||
readonly placeIndexName?: string; | ||
|
||
/** | ||
* Data source for the place index | ||
* | ||
* @default DataSource.ESRI | ||
*/ | ||
readonly dataSource?: DataSource; | ||
|
||
/** | ||
* Intend use for the results of an operation | ||
* | ||
* @default IntendedUse.SINGLE_USE | ||
*/ | ||
readonly intendedUse?: IntendedUse; | ||
|
||
/** | ||
* A description for the place index | ||
* | ||
* @default - no description | ||
*/ | ||
readonly description?: string; | ||
} | ||
|
||
/** | ||
* Data source for a place index | ||
*/ | ||
export enum DataSource { | ||
/** | ||
* Esri | ||
* | ||
* @see https://docs.aws.amazon.com/location/latest/developerguide/esri.html | ||
*/ | ||
ESRI = 'Esri', | ||
|
||
/** | ||
* HERE | ||
* | ||
* @see https://docs.aws.amazon.com/location/latest/developerguide/HERE.html | ||
*/ | ||
HERE = 'Here' | ||
} | ||
|
||
/** | ||
* Intend use for the results of an operation | ||
*/ | ||
export enum IntendedUse { | ||
/** | ||
* The results won't be stored | ||
*/ | ||
SINGLE_USE = 'SingleUse', | ||
|
||
/** | ||
* The result can be cached or stored in a database | ||
*/ | ||
STORAGE = 'Storage' | ||
} | ||
|
||
abstract class PlaceIndexBase extends Resource implements IPlaceIndex { | ||
public abstract readonly placeIndexName: string; | ||
public abstract readonly placeIndexArn: string; | ||
|
||
/** | ||
* Grant the given principal identity permissions to perform the actions on this place index. | ||
*/ | ||
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant { | ||
return iam.Grant.addToPrincipal({ | ||
grantee: grantee, | ||
actions: actions, | ||
resourceArns: [this.placeIndexArn], | ||
}); | ||
} | ||
|
||
/** | ||
* Grant the given identity permissions to search using this index | ||
*/ | ||
public grantSearch(grantee: iam.IGrantable): iam.Grant { | ||
return this.grant(grantee, | ||
'geo:SearchPlaceIndexForPosition', | ||
'geo:SearchPlaceIndexForSuggestions', | ||
'geo:SearchPlaceIndexForText', | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* A Place Index | ||
* | ||
* @see https://docs.aws.amazon.com/location/latest/developerguide/places-concepts.html | ||
*/ | ||
export class PlaceIndex extends PlaceIndexBase { | ||
/** | ||
* Use an existing place index by name | ||
*/ | ||
public static fromPlaceIndexName(scope: Construct, id: string, placeIndexName: string): IPlaceIndex { | ||
const placeIndexArn = Stack.of(scope).formatArn({ | ||
service: 'geo', | ||
resource: 'place-index', | ||
resourceName: placeIndexName, | ||
}); | ||
|
||
return PlaceIndex.fromPlaceIndexArn(scope, id, placeIndexArn); | ||
} | ||
|
||
/** | ||
* Use an existing place index by ARN | ||
*/ | ||
public static fromPlaceIndexArn(scope: Construct, id: string, placeIndexArn: string): IPlaceIndex { | ||
const parsedArn = Stack.of(scope).splitArn(placeIndexArn, ArnFormat.SLASH_RESOURCE_NAME); | ||
|
||
if (!parsedArn.resourceName) { | ||
throw new Error(`Place Index Arn ${placeIndexArn} does not have a resource name.`); | ||
} | ||
|
||
class Import extends PlaceIndexBase { | ||
public readonly placeIndexName = parsedArn.resourceName!; | ||
public readonly placeIndexArn = placeIndexArn; | ||
} | ||
|
||
return new Import(scope, id, { | ||
account: parsedArn.account, | ||
region: parsedArn.region, | ||
}); | ||
} | ||
|
||
public readonly placeIndexName: string; | ||
|
||
public readonly placeIndexArn: string; | ||
|
||
/** | ||
* The timestamp for when the place index resource was created in ISO 8601 forma | ||
* | ||
* @attribute | ||
*/ | ||
public readonly placeIndexCreateTime: string; | ||
|
||
/** | ||
* The timestamp for when the place index resource was last updated in ISO 8601 format | ||
* | ||
* @attribute | ||
*/ | ||
public readonly placeIndexUpdateTime: string; | ||
|
||
|
||
constructor(scope: Construct, id: string, props: PlaceIndexProps = {}) { | ||
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}`); | ||
} | ||
|
||
super(scope, id, { | ||
physicalName: props.placeIndexName ?? Lazy.string({ produce: () => this.generateUniqueId() }), | ||
}); | ||
|
||
const placeIndex = new CfnPlaceIndex(this, 'Resource', { | ||
indexName: this.physicalName, | ||
dataSource: props.dataSource ?? DataSource.ESRI, | ||
dataSourceConfiguration: props.intendedUse | ||
? { intendedUse: props.intendedUse } | ||
: undefined, | ||
description: props.description, | ||
}); | ||
|
||
this.placeIndexName = placeIndex.ref; | ||
this.placeIndexArn = placeIndex.attrArn; | ||
this.placeIndexCreateTime = placeIndex.attrCreateTime; | ||
this.placeIndexUpdateTime = placeIndex.attrUpdateTime; | ||
} | ||
|
||
private generateUniqueId(): string { | ||
const name = Names.uniqueId(this); | ||
if (name.length > 100) { | ||
return name.substring(0, 50) + name.substring(name.length - 50); | ||
} | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Fixture with packages imported, but nothing else | ||
import { Stack } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import * as location from '@aws-cdk/aws-location'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
|
||
class Fixture extends Stack { | ||
constructor(scope: Construct, id: string) { | ||
super(scope, id); | ||
|
||
/// here | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...t/integ.place-index.js.snapshot/PlaceIndexTestDefaultTestDeployAssert3F96C508.assets.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"version": "21.0.0", | ||
"files": { | ||
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { | ||
"source": { | ||
"path": "PlaceIndexTestDefaultTestDeployAssert3F96C508.template.json", | ||
"packaging": "file" | ||
}, | ||
"destinations": { | ||
"current_account-current_region": { | ||
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", | ||
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", | ||
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" | ||
} | ||
} | ||
} | ||
}, | ||
"dockerImages": {} | ||
} |
36 changes: 36 additions & 0 deletions
36
...integ.place-index.js.snapshot/PlaceIndexTestDefaultTestDeployAssert3F96C508.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"Parameters": { | ||
"BootstrapVersion": { | ||
"Type": "AWS::SSM::Parameter::Value<String>", | ||
"Default": "/cdk-bootstrap/hnb659fds/version", | ||
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" | ||
} | ||
}, | ||
"Rules": { | ||
"CheckBootstrapVersion": { | ||
"Assertions": [ | ||
{ | ||
"Assert": { | ||
"Fn::Not": [ | ||
{ | ||
"Fn::Contains": [ | ||
[ | ||
"1", | ||
"2", | ||
"3", | ||
"4", | ||
"5" | ||
], | ||
{ | ||
"Ref": "BootstrapVersion" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." | ||
} | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.