|
| 1 | +import { EnrichmentParametersConfig, IEnrichment, IPipe, InputTransformation } from '@aws-cdk/aws-pipes-alpha'; |
| 2 | +import { IRestApi } from 'aws-cdk-lib/aws-apigateway'; |
| 3 | +import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam'; |
| 4 | +import { CfnPipe } from 'aws-cdk-lib/aws-pipes'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Properties for a ApiGatewayEnrichment |
| 8 | + */ |
| 9 | +export interface ApiGatewayEnrichmentProps { |
| 10 | + /** |
| 11 | + * The input transformation for the enrichment |
| 12 | + * @see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-input-transformation.html |
| 13 | + * @default - None |
| 14 | + */ |
| 15 | + readonly inputTransformation?: InputTransformation; |
| 16 | + |
| 17 | + /** |
| 18 | + * The method for API Gateway resource. |
| 19 | + * |
| 20 | + * @default '*' - ANY |
| 21 | + */ |
| 22 | + readonly method?: string; |
| 23 | + |
| 24 | + /** |
| 25 | + * The path for the API Gateway resource. |
| 26 | + * |
| 27 | + * @default '/' |
| 28 | + */ |
| 29 | + readonly path?: string; |
| 30 | + |
| 31 | + /** |
| 32 | + * The deployment stage for the API Gateway resource. |
| 33 | + * |
| 34 | + * @default - the value of `deploymentStage.stageName` of target API Gateway resource. |
| 35 | + */ |
| 36 | + readonly stage?: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * The headers that need to be sent as part of request invoking the API Gateway REST API. |
| 40 | + * |
| 41 | + * @default - none |
| 42 | + */ |
| 43 | + readonly headerParameters?: Record<string, string>; |
| 44 | + |
| 45 | + /** |
| 46 | + * The path parameter values used to populate the API Gateway REST API path wildcards ("*"). |
| 47 | + * |
| 48 | + * @default - none |
| 49 | + */ |
| 50 | + readonly pathParameterValues?: string[]; |
| 51 | + |
| 52 | + /** |
| 53 | + * The query string keys/values that need to be sent as part of request invoking the EventBridge API destination. |
| 54 | + * |
| 55 | + * @default - none |
| 56 | + */ |
| 57 | + readonly queryStringParameters?: Record<string, string>; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * An API Gateway enrichment for a pipe |
| 62 | + */ |
| 63 | +export class ApiGatewayEnrichment implements IEnrichment { |
| 64 | + public readonly enrichmentArn: string; |
| 65 | + |
| 66 | + private readonly inputTransformation?: InputTransformation; |
| 67 | + private readonly headerParameters?: Record<string, string>; |
| 68 | + private readonly pathParameterValues?: string[]; |
| 69 | + private readonly queryStringParameters?: Record<string, string>; |
| 70 | + |
| 71 | + constructor(private readonly restApi: IRestApi, props?: ApiGatewayEnrichmentProps) { |
| 72 | + this.enrichmentArn = restApi.arnForExecuteApi( |
| 73 | + props?.method, |
| 74 | + props?.path || '/', |
| 75 | + props?.stage || this.restApi.deploymentStage.stageName, |
| 76 | + ); |
| 77 | + this.inputTransformation = props?.inputTransformation; |
| 78 | + this.headerParameters = props?.headerParameters; |
| 79 | + this.queryStringParameters = props?.queryStringParameters; |
| 80 | + this.pathParameterValues = props?.pathParameterValues; |
| 81 | + } |
| 82 | + |
| 83 | + bind(pipe: IPipe): EnrichmentParametersConfig { |
| 84 | + |
| 85 | + const httpParameters: CfnPipe.PipeEnrichmentHttpParametersProperty | undefined = |
| 86 | + this.headerParameters ?? |
| 87 | + this.pathParameterValues ?? |
| 88 | + this.queryStringParameters |
| 89 | + ? { |
| 90 | + headerParameters: this.headerParameters, |
| 91 | + pathParameterValues: this.pathParameterValues, |
| 92 | + queryStringParameters: this.queryStringParameters, |
| 93 | + } |
| 94 | + : undefined; |
| 95 | + |
| 96 | + return { |
| 97 | + enrichmentParameters: { |
| 98 | + inputTemplate: this.inputTransformation?.bind(pipe).inputTemplate, |
| 99 | + httpParameters, |
| 100 | + }, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + grantInvoke(pipeRole: IRole): void { |
| 105 | + pipeRole.addToPrincipalPolicy(new PolicyStatement({ |
| 106 | + resources: [this.enrichmentArn], |
| 107 | + actions: ['execute-api:Invoke'], |
| 108 | + })); |
| 109 | + } |
| 110 | +} |
| 111 | + |
0 commit comments