|
| 1 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 2 | +import * as kms from 'aws-cdk-lib/aws-kms'; |
| 3 | +import { ArnFormat, IResource, Lazy, Resource, Stack, Token } from 'aws-cdk-lib/core'; |
| 4 | +import { Construct } from 'constructs'; |
| 5 | +import { CfnGeofenceCollection } from 'aws-cdk-lib/aws-location'; |
| 6 | +import { generateUniqueId } from './util'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A Geofence Collection |
| 10 | + */ |
| 11 | +export interface IGeofenceCollection extends IResource { |
| 12 | + /** |
| 13 | + * The name of the geofence collection |
| 14 | + * |
| 15 | + * @attribute |
| 16 | + */ |
| 17 | + readonly geofenceCollectionName: string; |
| 18 | + |
| 19 | + /** |
| 20 | + * The Amazon Resource Name (ARN) of the geofence collection resource |
| 21 | + * |
| 22 | + * @attribute Arn, CollectionArn |
| 23 | + */ |
| 24 | + readonly geofenceCollectionArn: string; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Properties for a geofence collection |
| 29 | + */ |
| 30 | +export interface GeofenceCollectionProps { |
| 31 | + /** |
| 32 | + * A name for the geofence collection |
| 33 | + * |
| 34 | + * @default - A name is automatically generated |
| 35 | + */ |
| 36 | + readonly geofenceCollectionName?: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * A description for the geofence collection |
| 40 | + * |
| 41 | + * @default - no description |
| 42 | + */ |
| 43 | + readonly description?: string; |
| 44 | + |
| 45 | + /** |
| 46 | + * The customer managed to encrypt your data. |
| 47 | + * |
| 48 | + * @default - Use an AWS managed key |
| 49 | + * @see https://docs.aws.amazon.com/location/latest/developerguide/encryption-at-rest.html |
| 50 | + */ |
| 51 | + readonly kmsKey?: kms.IKey; |
| 52 | +} |
| 53 | + |
| 54 | +abstract class GeofenceCollectionBase extends Resource implements IGeofenceCollection { |
| 55 | + public abstract readonly geofenceCollectionName: string; |
| 56 | + public abstract readonly geofenceCollectionArn: string; |
| 57 | + |
| 58 | + /** |
| 59 | + * Grant the given principal identity permissions to perform the actions on this geofence collection. |
| 60 | + */ |
| 61 | + public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant { |
| 62 | + return iam.Grant.addToPrincipal({ |
| 63 | + grantee: grantee, |
| 64 | + actions: actions, |
| 65 | + resourceArns: [this.geofenceCollectionArn], |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Grant the given identity permissions to read this geofence collection |
| 71 | + * |
| 72 | + * See https://docs.aws.amazon.com/location/latest/developerguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-read-only-geofences |
| 73 | + */ |
| 74 | + public grantRead(grantee: iam.IGrantable): iam.Grant { |
| 75 | + return this.grant(grantee, |
| 76 | + 'geo:ListGeofences', |
| 77 | + 'geo:GetGeofence', |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A Geofence Collection |
| 84 | + * |
| 85 | + * @see https://docs.aws.amazon.com/location/latest/developerguide/geofence-tracker-concepts.html#geofence-overview |
| 86 | + */ |
| 87 | +export class GeofenceCollection extends GeofenceCollectionBase { |
| 88 | + /** |
| 89 | + * Use an existing geofence collection by name |
| 90 | + */ |
| 91 | + public static fromGeofenceCollectionName(scope: Construct, id: string, geofenceCollectionName: string): IGeofenceCollection { |
| 92 | + const geofenceCollectionArn = Stack.of(scope).formatArn({ |
| 93 | + service: 'geo', |
| 94 | + resource: 'geofence-collection', |
| 95 | + resourceName: geofenceCollectionName, |
| 96 | + }); |
| 97 | + |
| 98 | + return GeofenceCollection.fromGeofenceCollectionArn(scope, id, geofenceCollectionArn); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Use an existing geofence collection by ARN |
| 103 | + */ |
| 104 | + public static fromGeofenceCollectionArn(scope: Construct, id: string, geofenceCollectionArn: string): IGeofenceCollection { |
| 105 | + const parsedArn = Stack.of(scope).splitArn(geofenceCollectionArn, ArnFormat.SLASH_RESOURCE_NAME); |
| 106 | + |
| 107 | + if (!parsedArn.resourceName) { |
| 108 | + throw new Error(`Geofence Collection Arn ${geofenceCollectionArn} does not have a resource name.`); |
| 109 | + } |
| 110 | + |
| 111 | + class Import extends GeofenceCollectionBase { |
| 112 | + public readonly geofenceCollectionName = parsedArn.resourceName!; |
| 113 | + public readonly geofenceCollectionArn = geofenceCollectionArn; |
| 114 | + } |
| 115 | + |
| 116 | + return new Import(scope, id, { |
| 117 | + account: parsedArn.account, |
| 118 | + region: parsedArn.region, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + public readonly geofenceCollectionName: string; |
| 123 | + |
| 124 | + public readonly geofenceCollectionArn: string; |
| 125 | + |
| 126 | + /** |
| 127 | + * The timestamp for when the geofence collection resource was created in ISO 8601 forma |
| 128 | + * |
| 129 | + * @attribute |
| 130 | + */ |
| 131 | + public readonly geofenceCollectionCreateTime: string; |
| 132 | + |
| 133 | + /** |
| 134 | + * The timestamp for when the geofence collection resource was last updated in ISO 8601 format |
| 135 | + * |
| 136 | + * @attribute |
| 137 | + */ |
| 138 | + public readonly geofenceCollectionUpdateTime: string; |
| 139 | + |
| 140 | + constructor(scope: Construct, id: string, props: GeofenceCollectionProps = {}) { |
| 141 | + if (props.geofenceCollectionName && !Token.isUnresolved(props.geofenceCollectionName) && !/^[-.\w]{1,100}$/.test(props.geofenceCollectionName)) { |
| 142 | + 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}`); |
| 143 | + } |
| 144 | + |
| 145 | + super(scope, id, { |
| 146 | + physicalName: props.geofenceCollectionName ?? Lazy.string({ produce: () => generateUniqueId(this) }), |
| 147 | + }); |
| 148 | + |
| 149 | + const geofenceCollection = new CfnGeofenceCollection(this, 'Resource', { |
| 150 | + collectionName: this.physicalName, |
| 151 | + description: props.description, |
| 152 | + kmsKeyId: props.kmsKey?.keyArn, |
| 153 | + }); |
| 154 | + |
| 155 | + this.geofenceCollectionName = geofenceCollection.ref; |
| 156 | + this.geofenceCollectionArn = geofenceCollection.attrArn; |
| 157 | + this.geofenceCollectionCreateTime = geofenceCollection.attrCreateTime; |
| 158 | + this.geofenceCollectionUpdateTime = geofenceCollection.attrUpdateTime; |
| 159 | + } |
| 160 | +} |
0 commit comments