1import * as Effect from "effect/Effect";
2import type { Cluster } from "./Cluster.ts";
3import type { ClustersNodePool } from "./ClustersNodePool.ts";
4import type { NodePool } from "./NodePool.ts";
5import { bindGcpHost } from "../Host.ts";
6import { type BindingIam, type GcpHttpOp, grantFor } from "../HttpBinding.ts";
7
8/**
9 * Shared HTTP scaffolding for GKE cluster and node pool bindings.
10 * NOT exported from index.ts.
11 */
12export const makeContainerClusterHttpBinding = <
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: Cluster) {
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
41export const makeContainerNodePoolHttpBinding = <
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* (nodePool: NodePool) {
53 yield* bindGcpHost({
54 tag: options.tag,
55 resource: nodePool,
56 iam: [grantFor(options.iam, nodePool.name)],
57 });
58 const name = yield* nodePool.name;
59 return Effect.fn(`${options.tag}(${nodePool.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 makeContainerClustersNodePoolHttpBinding = <
71 I extends {
72 projectId: string;
73 zone: string;
74 clusterId: string;
75 nodePoolId: string;
76 name?: string;
77 },
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* (nodePool: ClustersNodePool) {
88 yield* bindGcpHost({
89 tag: options.tag,
90 resource: nodePool,
91 iam: [grantFor(options.iam, nodePool.name)],
92 });
93 const name = yield* nodePool.name;
94 const project = yield* nodePool.project;
95 const zone = yield* nodePool.zone;
96 const clusterId = yield* nodePool.clusterId;
97 const nodePoolId = yield* nodePool.nodePoolId;
98 return Effect.fn(`${options.tag}(${nodePool.LogicalId})`)(function* (
99 request?: Omit<
100 I,
101 "projectId" | "zone" | "clusterId" | "nodePoolId" | "name"
102 >,
103 ) {
104 return yield* run({
105 ...(request as I),
106 projectId: yield* project,
107 zone: yield* zone,
108 clusterId: yield* clusterId,
109 nodePoolId: yield* nodePoolId,
110 name: yield* name,
111 } as I);
112 });
113 });
114 });