Skip to content

Commit

Permalink
remove base class
Browse files Browse the repository at this point in the history
  • Loading branch information
mazyu36 committed Aug 29, 2024
1 parent e10a710 commit 6c8dff4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
59 changes: 26 additions & 33 deletions packages/@aws-cdk/aws-location-alpha/lib/route-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ export interface RouteCalculatorProps {

/**
* Data source for the route calculator
*
* @default DataSource.ESRI
*/
readonly dataSource?: DataSource;
readonly dataSource: DataSource;

/**
* A description for the route calculator
Expand All @@ -53,39 +51,12 @@ export interface RouteCalculatorProps {
readonly description?: string;
}

abstract class RouteCalculatorBase extends Resource implements IRouteCalculator {
public abstract readonly routeCalculatorName: string;
public abstract readonly routeCalculatorArn: string;

/**
* Grant the given principal identity permissions to perform the actions on this route calculator.
*/
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipal({
grantee: grantee,
actions: actions,
resourceArns: [this.routeCalculatorArn],
});
}

/**
* Grant the given identity permissions to access to a route calculator resource to calculate a route.
*
* @see https://docs.aws.amazon.com/location/latest/developerguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-calculate-route
*/
public grantRead(grantee: iam.IGrantable): iam.Grant {
return this.grant(grantee,
'geo:CalculateRoute',
);
}
}

/**
* A Route Calculator
*
* @see https://docs.aws.amazon.com/location/latest/developerguide/places-concepts.html
*/
export class RouteCalculator extends RouteCalculatorBase {
export class RouteCalculator extends Resource implements IRouteCalculator {
/**
* Use an existing route calculator by name
*/
Expand All @@ -109,7 +80,7 @@ export class RouteCalculator extends RouteCalculatorBase {
throw new Error(`Route Calculator Arn ${routeCalculatorArn} does not have a resource name.`);
}

class Import extends RouteCalculatorBase {
class Import extends Resource implements IRouteCalculator {
public readonly routeCalculatorName = parsedArn.resourceName!;
public readonly routeCalculatorArn = routeCalculatorArn;
}
Expand Down Expand Up @@ -138,7 +109,7 @@ export class RouteCalculator extends RouteCalculatorBase {
*/
public readonly routeCalculatorUpdateTime: string;

constructor(scope: Construct, id: string, props: RouteCalculatorProps = {}) {
constructor(scope: Construct, id: string, props: RouteCalculatorProps) {

if (props.description && !Token.isUnresolved(props.description) && props.description.length > 1000) {
throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`);
Expand All @@ -163,4 +134,26 @@ export class RouteCalculator extends RouteCalculatorBase {
this.routeCalculatorCreateTime = routeCalculator.attrCreateTime;
this.routeCalculatorUpdateTime = routeCalculator.attrUpdateTime;
}

/**
* Grant the given principal identity permissions to perform the actions on this route calculator.
*/
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipal({
grantee: grantee,
actions: actions,
resourceArns: [this.routeCalculatorArn],
});
}

/**
* Grant the given identity permissions to access to a route calculator resource to calculate a route.
*
* @see https://docs.aws.amazon.com/location/latest/developerguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-calculate-route
*/
public grantRead(grantee: iam.IGrantable): iam.Grant {
return this.grant(grantee,
'geo:CalculateRoute',
);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { App, Stack } from 'aws-cdk-lib';
import * as integ from '@aws-cdk/integ-tests-alpha';
import { Construct } from 'constructs';
import { RouteCalculator } from '../lib';
import { DataSource } from '../lib';
import { RouteCalculator } from '../lib/route-calculator';

class TestStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);

new RouteCalculator(this, 'RouteCalculator');
new RouteCalculator(this, 'RouteCalculator', {
dataSource: DataSource.ESRI,
});
}
}

Expand Down
13 changes: 10 additions & 3 deletions packages/@aws-cdk/aws-location-alpha/test/route-calculator.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Match, Template } from 'aws-cdk-lib/assertions';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Stack } from 'aws-cdk-lib';
import { DataSource, RouteCalculator } from '../lib';
import { DataSource } from '../lib';
import { RouteCalculator } from '../lib/route-calculator';

let stack: Stack;
beforeEach(() => {
stack = new Stack();
});

test('create a route calculator', () => {
new RouteCalculator(stack, 'RouteCalculator');
new RouteCalculator(stack, 'RouteCalculator', {
dataSource: DataSource.ESRI,
});

Template.fromStack(stack).hasResourceProperties('AWS::Location::RouteCalculator', {
DataSource: 'Esri',
Expand All @@ -18,7 +21,10 @@ test('create a route calculator', () => {
});

test('creates a route calculator with empty description', () => {
new RouteCalculator(stack, 'RouteCalculator', { description: '' });
new RouteCalculator(stack, 'RouteCalculator', {
description: '',
dataSource: DataSource.ESRI,
});

Template.fromStack(stack).hasResourceProperties('AWS::Location::RouteCalculator', {
Description: '',
Expand All @@ -28,6 +34,7 @@ test('creates a route calculator with empty description', () => {
test('throws with invalid description', () => {
expect(() => new RouteCalculator(stack, 'RouteCalculator', {
description: 'a'.repeat(1001),
dataSource: DataSource.ESRI,
})).toThrow('`description` must be between 0 and 1000 characters. Received: 1001 characters');
});

Expand Down

0 comments on commit 6c8dff4

Please sign in to comment.