|
| 1 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 2 | +import { ArnFormat, IResource, Lazy, Resource, Stack, Token } from 'aws-cdk-lib/core'; |
| 3 | +import { Construct } from 'constructs'; |
| 4 | +import { CfnRouteCalculator } from 'aws-cdk-lib/aws-location'; |
| 5 | +import { generateUniqueId, DataSource } from './util'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A Route Calculator |
| 9 | + */ |
| 10 | +export interface IRouteCalculator extends IResource { |
| 11 | + /** |
| 12 | + * The name of the route calculator |
| 13 | + * |
| 14 | + * @attribute |
| 15 | + */ |
| 16 | + readonly routeCalculatorName: string; |
| 17 | + |
| 18 | + /** |
| 19 | + * The Amazon Resource Name (ARN) of the route calculator resource |
| 20 | + * |
| 21 | + * @attribute Arn,CalculatorArn |
| 22 | + */ |
| 23 | + readonly routeCalculatorArn: string; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Properties for a route calculator |
| 28 | + */ |
| 29 | +export interface RouteCalculatorProps { |
| 30 | + /** |
| 31 | + * A name for the route calculator |
| 32 | + * |
| 33 | + * Must be between 1 and 100 characters and contain only alphanumeric characters, |
| 34 | + * hyphens, periods and underscores. |
| 35 | + * |
| 36 | + * @default - A name is automatically generated |
| 37 | + */ |
| 38 | + readonly routeCalculatorName?: string; |
| 39 | + |
| 40 | + /** |
| 41 | + * Data source for the route calculator |
| 42 | + */ |
| 43 | + readonly dataSource: DataSource; |
| 44 | + |
| 45 | + /** |
| 46 | + * A description for the route calculator |
| 47 | + * |
| 48 | + * @default - no description |
| 49 | + */ |
| 50 | + readonly description?: string; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * A Route Calculator |
| 55 | + * |
| 56 | + * @see https://docs.aws.amazon.com/location/latest/developerguide/places-concepts.html |
| 57 | + */ |
| 58 | +export class RouteCalculator extends Resource implements IRouteCalculator { |
| 59 | + /** |
| 60 | + * Use an existing route calculator by name |
| 61 | + */ |
| 62 | + public static fromRouteCalculatorName(scope: Construct, id: string, routeCalculatorName: string): IRouteCalculator { |
| 63 | + const routeCalculatorArn = Stack.of(scope).formatArn({ |
| 64 | + service: 'geo', |
| 65 | + resource: 'route-calculator', |
| 66 | + resourceName: routeCalculatorName, |
| 67 | + }); |
| 68 | + |
| 69 | + return RouteCalculator.fromRouteCalculatorArn(scope, id, routeCalculatorArn); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Use an existing route calculator by ARN |
| 74 | + */ |
| 75 | + public static fromRouteCalculatorArn(scope: Construct, id: string, routeCalculatorArn: string): IRouteCalculator { |
| 76 | + const parsedArn = Stack.of(scope).splitArn(routeCalculatorArn, ArnFormat.SLASH_RESOURCE_NAME); |
| 77 | + |
| 78 | + if (!parsedArn.resourceName) { |
| 79 | + throw new Error(`Route Calculator Arn ${routeCalculatorArn} does not have a resource name.`); |
| 80 | + } |
| 81 | + |
| 82 | + class Import extends Resource implements IRouteCalculator { |
| 83 | + public readonly routeCalculatorName = parsedArn.resourceName!; |
| 84 | + public readonly routeCalculatorArn = routeCalculatorArn; |
| 85 | + } |
| 86 | + |
| 87 | + return new Import(scope, id, { |
| 88 | + account: parsedArn.account, |
| 89 | + region: parsedArn.region, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public readonly routeCalculatorName: string; |
| 94 | + |
| 95 | + public readonly routeCalculatorArn: string; |
| 96 | + |
| 97 | + /** |
| 98 | + * The timestamp for when the route calculator resource was created in ISO 8601 format |
| 99 | + * |
| 100 | + * @attribute |
| 101 | + */ |
| 102 | + public readonly routeCalculatorCreateTime: string; |
| 103 | + |
| 104 | + /** |
| 105 | + * The timestamp for when the route calculator resource was last updated in ISO 8601 format |
| 106 | + * |
| 107 | + * @attribute |
| 108 | + */ |
| 109 | + public readonly routeCalculatorUpdateTime: string; |
| 110 | + |
| 111 | + constructor(scope: Construct, id: string, props: RouteCalculatorProps) { |
| 112 | + |
| 113 | + if (props.description && !Token.isUnresolved(props.description) && props.description.length > 1000) { |
| 114 | + throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`); |
| 115 | + } |
| 116 | + |
| 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}`); |
| 119 | + } |
| 120 | + |
| 121 | + super(scope, id, { |
| 122 | + physicalName: props.routeCalculatorName ?? Lazy.string({ produce: () => generateUniqueId(this) }), |
| 123 | + }); |
| 124 | + |
| 125 | + const routeCalculator = new CfnRouteCalculator(this, 'Resource', { |
| 126 | + calculatorName: this.physicalName, |
| 127 | + dataSource: props.dataSource ?? DataSource.ESRI, |
| 128 | + description: props.description, |
| 129 | + }); |
| 130 | + |
| 131 | + this.routeCalculatorName = routeCalculator.ref; |
| 132 | + this.routeCalculatorArn = routeCalculator.attrArn; |
| 133 | + this.routeCalculatorCreateTime = routeCalculator.attrCreateTime; |
| 134 | + this.routeCalculatorUpdateTime = routeCalculator.attrUpdateTime; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Grant the given principal identity permissions to perform the actions on this route calculator. |
| 139 | + */ |
| 140 | + public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant { |
| 141 | + return iam.Grant.addToPrincipal({ |
| 142 | + grantee: grantee, |
| 143 | + actions: actions, |
| 144 | + resourceArns: [this.routeCalculatorArn], |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Grant the given identity permissions to access to a route calculator resource to calculate a route. |
| 150 | + * |
| 151 | + * @see https://docs.aws.amazon.com/location/latest/developerguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-calculate-route |
| 152 | + */ |
| 153 | + public grantRead(grantee: iam.IGrantable): iam.Grant { |
| 154 | + return this.grant(grantee, |
| 155 | + 'geo:CalculateRoute', |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
0 commit comments