1import * as Effect from "effect/Effect";
2import type { Node } from "./Node.ts";
3import type { QueuedResource } from "./QueuedResource.ts";
4import { bindGcpHost } from "../Host.ts";
5import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts";
6
7/**
8 * Shared HTTP scaffolding for Cloud TPU node bindings.
9 * NOT exported from index.ts.
10 */
11export const makeTpuNodeHttpBinding = <
12 I extends { name?: string },
13 A,
14 E,
15>(options: {
16 tag: string;
17 iam: BindingIam;
18 operation: GcpHttpOp<I, A, E>;
19}) =>
20 Effect.gen(function* () {
21 const run = yield* options.operation;
22 return Effect.fn(function* (node: Node) {
23 yield* bindGcpHost({
24 tag: options.tag,
25 resource: node,
26 iam: [grantFor(options.iam, node.name)],
27 });
28 const name = yield* node.name;
29 return Effect.fn(`${options.tag}(${node.LogicalId})`)(function* (
30 request?: Omit<I, "name">,
31 ) {
32 return yield* run({
33 ...(request as I),
34 name: yield* name,
35 } as I);
36 });
37 });
38 });
39
40/**
41 * Shared HTTP scaffolding for Cloud TPU queued resource bindings.
42 * NOT exported from index.ts.
43 */
44export const makeTpuQueuedResourceHttpBinding = <
45 I extends { name?: string },
46 A,
47 E,
48>(options: {
49 tag: string;
50 iam: BindingIam;
51 operation: GcpHttpOp<I, A, E>;
52}) =>
53 Effect.gen(function* () {
54 const run = yield* options.operation;
55 return Effect.fn(function* (resource: QueuedResource) {
56 yield* bindGcpHost({
57 tag: options.tag,
58 resource: resource,
59 iam: [grantFor(options.iam, resource.name)],
60 });
61 const name = yield* resource.name;
62 return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (
63 request?: Omit<I, "name">,
64 ) {
65 return yield* run({
66 ...(request as I),
67 name: yield* name,
68 } as I);
69 });
70 });
71 });