| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { Backup } from "./Backup.ts"; |
| 3 | import type { Cluster } from "./Cluster.ts"; |
| 4 | import type { ClustersUser } from "./ClustersUser.ts"; |
| 5 | import type { Instance } from "./Instance.ts"; |
| 6 | import { bindGcpHost } from "../Host.ts"; |
| 7 | import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Shared HTTP scaffolding for AlloyDB cluster, instance, backup, and |
| 11 | * user bindings. NOT exported from index.ts. |
| 12 | */ |
| 13 | export const makeAlloyDbClusterHttpBinding = < |
| 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* (cluster: Cluster) { |
| 25 | yield* bindGcpHost({ |
| 26 | tag: options.tag, |
| 27 | resource: cluster, |
| 28 | iam: [grantFor(options.iam, cluster.name)], |
| 29 | }); |
| 30 | const name = yield* cluster.name; |
| 31 | return Effect.fn(`${options.tag}(${cluster.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 | |
| 42 | export const makeAlloyDbInstanceHttpBinding = < |
| 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* (instance: Instance) { |
| 54 | yield* bindGcpHost({ |
| 55 | tag: options.tag, |
| 56 | resource: instance, |
| 57 | iam: [grantFor(options.iam, instance.name)], |
| 58 | }); |
| 59 | const name = yield* instance.name; |
| 60 | return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* ( |
| 61 | request?: Omit<I, "name">, |
| 62 | ) { |
| 63 | return yield* run({ |
| 64 | ...(request as I), |
| 65 | name: yield* name, |
| 66 | } as I); |
| 67 | }); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | export const makeAlloyDbBackupHttpBinding = < |
| 72 | I extends { name?: string }, |
| 73 | A, |
| 74 | E, |
| 75 | >(options: { |
| 76 | tag: string; |
| 77 | iam: BindingIam; |
| 78 | operation: GcpHttpOp<I, A, E>; |
| 79 | }) => |
| 80 | Effect.gen(function* () { |
| 81 | const run = yield* options.operation; |
| 82 | return Effect.fn(function* (backup: Backup) { |
| 83 | yield* bindGcpHost({ |
| 84 | tag: options.tag, |
| 85 | resource: backup, |
| 86 | iam: [grantFor(options.iam, backup.name)], |
| 87 | }); |
| 88 | const name = yield* backup.name; |
| 89 | return Effect.fn(`${options.tag}(${backup.LogicalId})`)(function* ( |
| 90 | request?: Omit<I, "name">, |
| 91 | ) { |
| 92 | return yield* run({ |
| 93 | ...(request as I), |
| 94 | name: yield* name, |
| 95 | } as I); |
| 96 | }); |
| 97 | }); |
| 98 | }); |
| 99 | |
| 100 | export const makeAlloyDbUserHttpBinding = < |
| 101 | I extends { name?: string }, |
| 102 | A, |
| 103 | E, |
| 104 | >(options: { |
| 105 | tag: string; |
| 106 | iam: BindingIam; |
| 107 | operation: GcpHttpOp<I, A, E>; |
| 108 | }) => |
| 109 | Effect.gen(function* () { |
| 110 | const run = yield* options.operation; |
| 111 | return Effect.fn(function* (user: ClustersUser) { |
| 112 | yield* bindGcpHost({ |
| 113 | tag: options.tag, |
| 114 | resource: user, |
| 115 | iam: [grantFor(options.iam, user.name)], |
| 116 | }); |
| 117 | const name = yield* user.name; |
| 118 | return Effect.fn(`${options.tag}(${user.LogicalId})`)(function* ( |
| 119 | request?: Omit<I, "name">, |
| 120 | ) { |
| 121 | return yield* run({ |
| 122 | ...(request as I), |
| 123 | name: yield* name, |
| 124 | } as I); |
| 125 | }); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | export const makeAlloyDbConnectionInfoHttpBinding = < |
| 130 | I extends { parent?: string }, |
| 131 | A, |
| 132 | E, |
| 133 | >(options: { |
| 134 | tag: string; |
| 135 | iam: BindingIam; |
| 136 | operation: GcpHttpOp<I, A, E>; |
| 137 | }) => |
| 138 | Effect.gen(function* () { |
| 139 | const run = yield* options.operation; |
| 140 | return Effect.fn(function* (instance: Instance) { |
| 141 | yield* bindGcpHost({ |
| 142 | tag: options.tag, |
| 143 | resource: instance, |
| 144 | iam: [grantFor(options.iam, instance.name)], |
| 145 | }); |
| 146 | const name = yield* instance.name; |
| 147 | return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* ( |
| 148 | request?: Omit<I, "parent">, |
| 149 | ) { |
| 150 | return yield* run({ |
| 151 | ...(request as I), |
| 152 | parent: yield* name, |
| 153 | } as I); |
| 154 | }); |
| 155 | }); |
| 156 | }); |