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