| 1 | import * as TSQ from "@distilled.cloud/aws/timestream-query"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import * as Output from "../../Output.ts"; |
| 6 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 7 | import { |
| 8 | ExecuteScheduledQuery, |
| 9 | type ExecuteScheduledQueryRequest, |
| 10 | } from "./ExecuteScheduledQuery.ts"; |
| 11 | import type { ScheduledQuery } from "./ScheduledQuery.ts"; |
| 12 | import { discover, withEndpoint } from "./internal.ts"; |
| 13 | |
| 14 | // Bespoke (not on the BindingHttp scaffold): the only Timestream binding |
| 15 | // scoped to a ScheduledQuery rather than a Table or the account. |
| 16 | export const ExecuteScheduledQueryHttp = Layer.effect( |
| 17 | ExecuteScheduledQuery, |
| 18 | Effect.gen(function* () { |
| 19 | // Yield-first captures the operations' services (Credentials/Region/ |
| 20 | // HttpClient) at layer init so the runtime callable is requirement-free. |
| 21 | const executeScheduledQuery = yield* TSQ.executeScheduledQuery; |
| 22 | const describeEndpoints = yield* TSQ.describeEndpoints; |
| 23 | const withQueryEndpoint = withEndpoint( |
| 24 | discover("query", describeEndpoints({})), |
| 25 | ); |
| 26 | return Effect.fn(function* (scheduledQuery: ScheduledQuery) { |
| 27 | const ScheduledQueryArn = yield* scheduledQuery.scheduledQueryArn; |
| 28 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 29 | const host = yield* Binding.Host; |
| 30 | if (isBindingHost(host)) { |
| 31 | yield* host.bind`Allow(${host}, AWS.Timestream.ExecuteScheduledQuery(${scheduledQuery}))`( |
| 32 | { |
| 33 | policyStatements: [ |
| 34 | { |
| 35 | Effect: "Allow", |
| 36 | Action: ["timestream:ExecuteScheduledQuery"], |
| 37 | Resource: [ |
| 38 | Output.interpolate`${scheduledQuery.scheduledQueryArn}`, |
| 39 | ], |
| 40 | }, |
| 41 | // Endpoint discovery is required and is not scoped to a |
| 42 | // resource. |
| 43 | { |
| 44 | Effect: "Allow", |
| 45 | Action: ["timestream:DescribeEndpoints"], |
| 46 | Resource: ["*"], |
| 47 | }, |
| 48 | ], |
| 49 | }, |
| 50 | ); |
| 51 | } |
| 52 | } |
| 53 | return Effect.fn( |
| 54 | `AWS.Timestream.ExecuteScheduledQuery(${scheduledQuery.LogicalId})`, |
| 55 | )(function* (request: ExecuteScheduledQueryRequest) { |
| 56 | return yield* withQueryEndpoint( |
| 57 | executeScheduledQuery({ |
| 58 | ...request, |
| 59 | ScheduledQueryArn: yield* ScheduledQueryArn, |
| 60 | }), |
| 61 | ); |
| 62 | }); |
| 63 | }); |
| 64 | }), |
| 65 | ); |