1import * as dsql from "@distilled.cloud/aws/dsql";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import { isBindingHost } from "../Lambda/Function.ts";
6import type { Cluster } from "./Cluster.ts";
7import { GetVpcEndpointServiceName } from "./GetVpcEndpointServiceName.ts";
8
9/**
10 * HTTP implementation of {@link GetVpcEndpointServiceName}. At deploy time
11 * it grants `dsql:GetVpcEndpointServiceName` on the cluster to the host
12 * Function; at runtime it calls the DSQL control plane with the Function's
13 * own credentials.
14 */
15export const GetVpcEndpointServiceNameHttp = Layer.effect(
16 GetVpcEndpointServiceName,
17 Effect.gen(function* () {
18 const op = yield* dsql.getVpcEndpointServiceName;
19
20 return Effect.fn(function* <R extends Cluster>(cluster: R) {
21 const Identifier = yield* cluster.clusterId;
22 if (!globalThis.__ALCHEMY_RUNTIME__) {
23 const host = yield* Binding.Host;
24 if (isBindingHost(host)) {
25 yield* host.bind`Allow(${host}, AWS.DSQL.GetVpcEndpointServiceName(${cluster}))`(
26 {
27 policyStatements: [
28 {
29 Effect: "Allow",
30 Action: ["dsql:GetVpcEndpointServiceName"],
31 Resource: [cluster.clusterArn],
32 },
33 ],
34 },
35 );
36 }
37 }
38 return Effect.fn(
39 `AWS.DSQL.GetVpcEndpointServiceName(${cluster.LogicalId})`,
40 )(function* () {
41 return yield* op({ identifier: yield* Identifier });
42 });
43 });
44 }),
45);