|
| 1 | +import * as iam from '@aws-cdk/aws-iam'; |
| 2 | +import * as sfn from '@aws-cdk/aws-stepfunctions'; |
| 3 | +import { Construct } from 'constructs'; |
| 4 | +import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; |
| 5 | +import { AuthType, CallApiGatewayEndpointBaseProps } from './base-types'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Base CallApiGatewayEndpoint Task |
| 9 | + * @internal |
| 10 | + */ |
| 11 | +export abstract class CallApiGatewayEndpointBase extends sfn.TaskStateBase { |
| 12 | + private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [ |
| 13 | + sfn.IntegrationPattern.REQUEST_RESPONSE, |
| 14 | + sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN, |
| 15 | + ]; |
| 16 | + |
| 17 | + private readonly baseProps: CallApiGatewayEndpointBaseProps; |
| 18 | + private readonly integrationPattern: sfn.IntegrationPattern; |
| 19 | + |
| 20 | + protected abstract readonly apiEndpoint: string; |
| 21 | + protected abstract readonly arnForExecuteApi: string; |
| 22 | + protected abstract readonly stageName?: string; |
| 23 | + |
| 24 | + constructor(scope: Construct, id: string, props: CallApiGatewayEndpointBaseProps) { |
| 25 | + super(scope, id, props); |
| 26 | + |
| 27 | + this.baseProps = props; |
| 28 | + this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE; |
| 29 | + validatePatternSupported(this.integrationPattern, CallApiGatewayEndpointBase.SUPPORTED_INTEGRATION_PATTERNS); |
| 30 | + |
| 31 | + if (this.integrationPattern === sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN) { |
| 32 | + if (!sfn.FieldUtils.containsTaskToken(this.baseProps.headers)) { |
| 33 | + throw new Error('Task Token is required in `headers` for WAIT_FOR_TASK_TOKEN pattern. Use JsonPath.taskToken to set the token.'); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @internal |
| 40 | + */ |
| 41 | + protected _renderTask() { |
| 42 | + return { |
| 43 | + Resource: integrationResourceArn('apigateway', 'invoke', this.integrationPattern), |
| 44 | + Parameters: sfn.FieldUtils.renderObject({ |
| 45 | + ApiEndpoint: this.apiEndpoint, |
| 46 | + Method: this.baseProps.method, |
| 47 | + Headers: this.baseProps.headers?.value, |
| 48 | + Stage: this.stageName, |
| 49 | + Path: this.baseProps.apiPath, |
| 50 | + QueryParameters: this.baseProps.queryParameters?.value, |
| 51 | + RequestBody: this.baseProps.requestBody?.value, |
| 52 | + AuthType: this.baseProps.authType ? this.baseProps.authType : 'NO_AUTH', |
| 53 | + }), |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + protected createPolicyStatements(): iam.PolicyStatement[] { |
| 58 | + if (this.baseProps.authType === AuthType.NO_AUTH) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + return [ |
| 63 | + new iam.PolicyStatement({ |
| 64 | + resources: [this.arnForExecuteApi], |
| 65 | + actions: ['execute-api:Invoke'], |
| 66 | + }), |
| 67 | + ]; |
| 68 | + } |
| 69 | +} |
0 commit comments