1import * as Effect from "effect/Effect";
2import * as Output from "../../Output.ts";
3import type { Cluster } from "./Cluster.ts";
4import type { Instance } from "./Instance.ts";
5import type { Table } from "./Table.ts";
6import { bindGcpHost } from "../Host.ts";
7import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts";
8
9/**
10 * Shared HTTP scaffolding for Bigtable instance, cluster, and table
11 * bindings. NOT exported from index.ts.
12 */
13export const makeBigtableInstanceHttpBinding = <
14 I extends { name?: string },
15 A,
16 E,
17>(options: {
18 tag: string;
19 iam: BindingIam;
20 operation: GcpHttpOp<I, A, E>;
21}) =>
22 Effect.gen(function* () {
23 const run = yield* options.operation;
24 return Effect.fn(function* (instance: Instance) {
25 yield* bindGcpHost({
26 tag: options.tag,
27 resource: instance,
28 iam: [grantFor(options.iam, instance.name)],
29 });
30 const name = yield* instance.name;
31 return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* (
32 request?: Omit<I, "name">,
33 ) {
34 return yield* run({
35 ...(request as I),
36 name: yield* name,
37 } as I);
38 });
39 });
40 });
41
42export const makeBigtableClusterHttpBinding = <
43 I extends { name?: string },
44 A,
45 E,
46>(options: {
47 tag: string;
48 iam: BindingIam;
49 operation: GcpHttpOp<I, A, E>;
50}) =>
51 Effect.gen(function* () {
52 const run = yield* options.operation;
53 return Effect.fn(function* (cluster: Cluster) {
54 yield* bindGcpHost({
55 tag: options.tag,
56 resource: cluster,
57 iam: [
58 grantFor(
59 options.iam,
60 Output.interpolate`projects/${cluster.project}/instances/${cluster.instanceId}`,
61 ),
62 ],
63 });
64 const name = yield* cluster.name;
65 return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* (
66 request?: Omit<I, "name">,
67 ) {
68 return yield* run({
69 ...(request as I),
70 name: yield* name,
71 } as I);
72 });
73 });
74 });
75
76export const makeBigtableTableHttpBinding = <
77 I extends { name?: string },
78 A,
79 E,
80>(options: {
81 tag: string;
82 iam: BindingIam;
83 operation: GcpHttpOp<I, A, E>;
84}) =>
85 Effect.gen(function* () {
86 const run = yield* options.operation;
87 return Effect.fn(function* (table: Table) {
88 yield* bindGcpHost({
89 tag: options.tag,
90 resource: table,
91 iam: [grantFor(options.iam, table.name)],
92 });
93 const name = yield* table.name;
94 return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (
95 request?: Omit<I, "name">,
96 ) {
97 return yield* run({
98 ...(request as I),
99 name: yield* name,
100 } as I);
101 });
102 });
103 });