|
| 1 | +import {LambdaIntegration, PassthroughBehavior, StepFunctionsIntegration} from "aws-cdk-lib/aws-apigateway" |
| 2 | +import {IManagedPolicy} from "aws-cdk-lib/aws-iam" |
| 3 | +import {HttpMethod} from "aws-cdk-lib/aws-lambda" |
| 4 | +import {Construct} from "constructs" |
| 5 | +import { |
| 6 | + ExpressStateMachine, |
| 7 | + LambdaEndpoint, |
| 8 | + RestApiGateway, |
| 9 | + StateMachineEndpoint, |
| 10 | + TypescriptLambdaFunction |
| 11 | +} from "@nhsdigital/eps-cdk-constructs" |
| 12 | + |
| 13 | +export interface ApisProps { |
| 14 | + readonly stackName: string |
| 15 | + readonly logRetentionInDays: number |
| 16 | + readonly mutualTlsTrustStoreKey: string | undefined |
| 17 | + readonly forwardCsocLogs: boolean |
| 18 | + readonly csocApiGatewayDestination: string |
| 19 | + readonly deployCheckPrescriptionStatusUpdate: boolean |
| 20 | + readonly exposeGetStatusUpdates: boolean |
| 21 | + functions: {[key: string]: TypescriptLambdaFunction} |
| 22 | + stateMachines: {[key: string]: ExpressStateMachine} |
| 23 | +} |
| 24 | + |
| 25 | +export class Apis extends Construct { |
| 26 | + public constructor(scope: Construct, id: string, props: ApisProps) { |
| 27 | + super(scope, id) |
| 28 | + |
| 29 | + // Collect execution policies for all resources that need API Gateway access |
| 30 | + const executionPolicies: Array<IManagedPolicy> = [ |
| 31 | + props.stateMachines.updatePrescriptionStatus.executionPolicy, |
| 32 | + props.stateMachines.format1UpdatePrescriptionsStatus.executionPolicy, |
| 33 | + props.functions.status.executionPolicy, |
| 34 | + props.functions.capabilityStatement.executionPolicy, |
| 35 | + props.functions.nhsNotifyUpdateCallback.executionPolicy |
| 36 | + ] |
| 37 | + |
| 38 | + if (props.exposeGetStatusUpdates) { |
| 39 | + executionPolicies.push(props.functions.getStatusUpdates.executionPolicy) |
| 40 | + } |
| 41 | + |
| 42 | + if (props.deployCheckPrescriptionStatusUpdate) { |
| 43 | + executionPolicies.push(props.functions.checkPrescriptionStatusUpdates.executionPolicy) |
| 44 | + } |
| 45 | + |
| 46 | + const apiGateway = new RestApiGateway(this, "RestApiGateway", { |
| 47 | + stackName: props.stackName, |
| 48 | + logRetentionInDays: props.logRetentionInDays, |
| 49 | + mutualTlsTrustStoreKey: props.mutualTlsTrustStoreKey, |
| 50 | + forwardCsocLogs: props.forwardCsocLogs, |
| 51 | + csocApiGatewayDestination: props.csocApiGatewayDestination, |
| 52 | + executionPolicies |
| 53 | + }) |
| 54 | + |
| 55 | + const rootResource = apiGateway.api.root |
| 56 | + |
| 57 | + // POST / — UpdatePrescriptionStatus state machine integration (root resource) |
| 58 | + rootResource.addMethod( |
| 59 | + HttpMethod.POST, |
| 60 | + StepFunctionsIntegration.startExecution( |
| 61 | + props.stateMachines.updatePrescriptionStatus.stateMachine, |
| 62 | + { |
| 63 | + credentialsRole: apiGateway.role, |
| 64 | + passthroughBehavior: PassthroughBehavior.WHEN_NO_MATCH |
| 65 | + } |
| 66 | + ), |
| 67 | + { |
| 68 | + methodResponses: [ |
| 69 | + {statusCode: "200"}, |
| 70 | + {statusCode: "400"}, |
| 71 | + {statusCode: "500"} |
| 72 | + ] |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + // POST /format-1 — Format1 state machine integration |
| 77 | + new StateMachineEndpoint(this, "Format1UpdatePrescriptionStatusEndpoint", { |
| 78 | + parentResource: rootResource, |
| 79 | + resourceName: "format-1", |
| 80 | + method: HttpMethod.POST, |
| 81 | + restApiGatewayRole: apiGateway.role, |
| 82 | + stateMachine: props.stateMachines.format1UpdatePrescriptionsStatus |
| 83 | + }) |
| 84 | + |
| 85 | + // POST /notification-delivery-status-callback — Lambda proxy integration |
| 86 | + new LambdaEndpoint(this, "NotificationDeliveryStatusCallbackEndpoint", { |
| 87 | + parentResource: rootResource, |
| 88 | + resourceName: "notification-delivery-status-callback", |
| 89 | + method: HttpMethod.POST, |
| 90 | + restApiGatewayRole: apiGateway.role, |
| 91 | + lambdaFunction: props.functions.nhsNotifyUpdateCallback |
| 92 | + }) |
| 93 | + |
| 94 | + // GET /_status — Lambda proxy integration |
| 95 | + new LambdaEndpoint(this, "StatusEndpoint", { |
| 96 | + parentResource: rootResource, |
| 97 | + resourceName: "_status", |
| 98 | + method: HttpMethod.GET, |
| 99 | + restApiGatewayRole: apiGateway.role, |
| 100 | + lambdaFunction: props.functions.status |
| 101 | + }) |
| 102 | + |
| 103 | + // GET /metadata — Lambda proxy integration |
| 104 | + new LambdaEndpoint(this, "CapabilityStatementEndpoint", { |
| 105 | + parentResource: rootResource, |
| 106 | + resourceName: "metadata", |
| 107 | + method: HttpMethod.GET, |
| 108 | + restApiGatewayRole: apiGateway.role, |
| 109 | + lambdaFunction: props.functions.capabilityStatement |
| 110 | + }) |
| 111 | + |
| 112 | + // GET /checkprescriptionstatusupdates — conditional Lambda proxy integration |
| 113 | + if (props.deployCheckPrescriptionStatusUpdate) { |
| 114 | + new LambdaEndpoint(this, "CheckPrescriptionStatusUpdatesEndpoint", { |
| 115 | + parentResource: rootResource, |
| 116 | + resourceName: "checkprescriptionstatusupdates", |
| 117 | + method: HttpMethod.GET, |
| 118 | + restApiGatewayRole: apiGateway.role, |
| 119 | + lambdaFunction: props.functions.checkPrescriptionStatusUpdates |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + // POST /get-status-updates — conditional Lambda integration (non-proxy) |
| 124 | + if (props.exposeGetStatusUpdates) { |
| 125 | + const getStatusUpdatesResource = rootResource.addResource("get-status-updates") |
| 126 | + getStatusUpdatesResource.addMethod( |
| 127 | + HttpMethod.POST, |
| 128 | + new LambdaIntegration(props.functions.getStatusUpdates.function, { |
| 129 | + credentialsRole: apiGateway.role, |
| 130 | + proxy: false, |
| 131 | + requestTemplates: { |
| 132 | + "application/json": "$input.json('$')" |
| 133 | + }, |
| 134 | + integrationResponses: [ |
| 135 | + { |
| 136 | + statusCode: "200", |
| 137 | + responseTemplates: { |
| 138 | + "application/json": "$input.body" |
| 139 | + } |
| 140 | + } |
| 141 | + ] |
| 142 | + }), |
| 143 | + { |
| 144 | + methodResponses: [ |
| 145 | + {statusCode: "200"} |
| 146 | + ] |
| 147 | + } |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments