| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { WorkstationCluster } from "./WorkstationCluster.ts"; |
| 3 | import type { WorkstationClustersWorkstationConfig } from "./WorkstationClustersWorkstationConfig.ts"; |
| 4 | import type { WorkstationClustersWorkstationConfigsWorkstation } from "./WorkstationClustersWorkstationConfigsWorkstation.ts"; |
| 5 | import { bindGcpHost } from "../Host.ts"; |
| 6 | import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Shared HTTP scaffolding for Cloud Workstations bindings. |
| 10 | * NOT exported from index.ts. |
| 11 | */ |
| 12 | export const makeClusterHttpBinding = < |
| 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* (cluster: WorkstationCluster) { |
| 24 | yield* bindGcpHost({ |
| 25 | tag: options.tag, |
| 26 | resource: cluster, |
| 27 | iam: [grantFor(options.iam, cluster.name)], |
| 28 | }); |
| 29 | const name = yield* cluster.name; |
| 30 | return Effect.fn(`${options.tag}(${cluster.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 | |
| 41 | export const makeConfigHttpBinding = < |
| 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* (config: WorkstationClustersWorkstationConfig) { |
| 53 | yield* bindGcpHost({ |
| 54 | tag: options.tag, |
| 55 | resource: config, |
| 56 | iam: [grantFor(options.iam, config.name)], |
| 57 | }); |
| 58 | const name = yield* config.name; |
| 59 | return Effect.fn(`${options.tag}(${config.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 | |
| 70 | export const makeWorkstationHttpBinding = < |
| 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* ( |
| 82 | workstation: WorkstationClustersWorkstationConfigsWorkstation, |
| 83 | ) { |
| 84 | yield* bindGcpHost({ |
| 85 | tag: options.tag, |
| 86 | resource: workstation, |
| 87 | iam: [grantFor(options.iam, workstation.name)], |
| 88 | }); |
| 89 | const name = yield* workstation.name; |
| 90 | return Effect.fn(`${options.tag}(${workstation.LogicalId})`)(function* ( |
| 91 | request?: Omit<I, "name">, |
| 92 | ) { |
| 93 | return yield* run({ |
| 94 | ...(request as I), |
| 95 | name: yield* name, |
| 96 | } as I); |
| 97 | }); |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | export const makeGenerateAccessTokenHttpBinding = < |
| 102 | I extends { workstation?: string }, |
| 103 | A, |
| 104 | E, |
| 105 | >(options: { |
| 106 | tag: string; |
| 107 | iam: BindingIam; |
| 108 | operation: GcpHttpOp<I, A, E>; |
| 109 | }) => |
| 110 | Effect.gen(function* () { |
| 111 | const run = yield* options.operation; |
| 112 | return Effect.fn(function* ( |
| 113 | workstation: WorkstationClustersWorkstationConfigsWorkstation, |
| 114 | ) { |
| 115 | yield* bindGcpHost({ |
| 116 | tag: options.tag, |
| 117 | resource: workstation, |
| 118 | iam: [grantFor(options.iam, workstation.name)], |
| 119 | }); |
| 120 | const name = yield* workstation.name; |
| 121 | return Effect.fn(`${options.tag}(${workstation.LogicalId})`)(function* ( |
| 122 | request?: Omit<I, "workstation">, |
| 123 | ) { |
| 124 | return yield* run({ |
| 125 | ...(request as I), |
| 126 | workstation: yield* name, |
| 127 | } as I); |
| 128 | }); |
| 129 | }); |
| 130 | }); |