The best way to control Claude Code, Codex and more from any computer or phone

main
ts 294 lines 9.42 KB
Raw
1 import * as kafka from "@distilled.cloud/gcp/managedkafka_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 collectPages,
12 expandParent,
13 fieldMask,
14 fingerprint,
15 getConnector,
16 listAlchemyConnectClusters,
17 normalizeLocation,
18 parseName,
19 replaceOnIdentity,
20 retryTransient,
21 rfc1035,
22 stringMapOf,
23 toPhysicalId,
24 waitUntilGone,
25 waitUntilReady,
26 } from "./internal.ts";
27
28 export type TaskRetryPolicy = {
29 /** Minimum backoff before retrying a failed task (e.g. `"60s"`). */
30 minimumBackoff?: string;
31 /** Maximum backoff before retrying a failed task (e.g. `"43200s"`). */
32 maximumBackoff?: string;
33 /**
34 * Disable task retry.
35 * @default false
36 */
37 taskRetryDisabled?: boolean;
38 };
39
40 export type ConnectClustersConnectorProps = {
41 /**
42 * Parent Connect cluster. Full name
43 * `projects/{project}/locations/{location}/connectClusters/{id}` or
44 * the Connect cluster id (combined with `location`). Immutable —
45 * changing it replaces the connector.
46 */
47 connectCluster: string;
48 /**
49 * Region used when `connectCluster` is a bare id.
50 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
51 */
52 location?: string;
53 /**
54 * Connector id. If omitted, a unique RFC1035 name is generated.
55 * Immutable — changing it replaces the connector.
56 */
57 connectorId?: string;
58 /**
59 * Connector config (`connector.class`, `tasks.max`, converters, …).
60 */
61 configs?: Record<string, string>;
62 /**
63 * Task restart policy.
64 */
65 taskRestartPolicy?: TaskRetryPolicy;
66 };
67
68 export type ConnectClustersConnector = Resource<
69 "GCP.ManagedKafka.ConnectClustersConnector",
70 ConnectClustersConnectorProps,
71 {
72 /** Full resource name `.../connectClusters/{id}/connectors/{connector}`. */
73 name: string;
74 /** Connector id. */
75 connectorId: string;
76 /** Parent Connect cluster resource name. */
77 connectCluster: string;
78 /** Project id. */
79 project: string;
80 /** Location id. */
81 location: string;
82 /** Connector configs. */
83 configs: Record<string, string>;
84 /** Server-reported state. */
85 state: string | undefined;
86 /** Task restart policy currently applied. */
87 taskRestartPolicy: TaskRetryPolicy | undefined;
88 },
89 never,
90 Providers
91 >;
92
93 /**
94 * A Kafka Connect connector on a Managed Kafka Connect cluster.
95 *
96 * Changing `connectorId`, `connectCluster`, or `location` replaces the
97 * connector. Configs and the task restart policy update in place.
98 *
99 * ### Creating a Connector
100 * **Example:** Pub/Sub sink
101 * ```typescript
102 * const connector = yield* GCP.ManagedKafka.ConnectClustersConnector(
103 * "Sink",
104 * {
105 * connectCluster: connect.name,
106 * configs: {
107 * "connector.class":
108 * "com.google.pubsub.kafka.sink.CloudPubSubSinkConnector",
109 * "tasks.max": "1",
110 * topics: "orders",
111 * "cps.project": "my-project",
112 * "cps.topic": "orders-sink",
113 * },
114 * },
115 * );
116 * ```
117 *
118 * @resource
119 * @category ManagedKafka
120 */
121 export const ConnectClustersConnector = Resource<ConnectClustersConnector>(
122 "GCP.ManagedKafka.ConnectClustersConnector",
123 );
124
125 export class ConnectClustersConnectorNotResolved extends Data.TaggedError(
126 "GCP.ManagedKafka.ConnectClustersConnectorNotResolved",
127 )<{
128 resourceName: string;
129 }> {}
130
131 const connectClusterOf = (value: string, project: string, location: string) =>
132 expandParent(value, project, location, "connectClusters");
133
134 const resourceName = (connectCluster: string, connectorId: string) =>
135 `${connectCluster}/connectors/${connectorId}`;
136
137 const policyOf = (
138 policy: kafka.TaskRetryPolicy | TaskRetryPolicy | undefined,
139 ): TaskRetryPolicy | undefined => {
140 if (policy === undefined) return undefined;
141 return {
142 minimumBackoff: policy.minimumBackoff,
143 maximumBackoff: policy.maximumBackoff,
144 taskRetryDisabled: policy.taskRetryDisabled === true,
145 };
146 };
147
148 const toAttrs = (connector: kafka.Connector, project: string) => {
149 const name = connector.name ?? "";
150 const parsed = parseName(name, "connectors");
151 return {
152 name,
153 connectorId: parsed.id,
154 connectCluster: parsed.parent,
155 project: parsed.project || project,
156 location: parsed.location,
157 configs: stringMapOf(connector.configs),
158 state: connector.state,
159 taskRestartPolicy: policyOf(connector.taskRestartPolicy),
160 };
161 };
162
163 export const ConnectClustersConnectorProvider = () =>
164 Provider.succeed(ConnectClustersConnector, {
165 stables: ["name", "connectorId", "connectCluster", "project", "location"],
166
167 diff: Effect.fn(function* ({ news, olds, output }) {
168 if (!isResolved(news)) return undefined;
169 const env = yield* GcpEnvironment.current;
170 const location = normalizeLocation(news.location ?? output?.location, env.region);
171 return replaceOnIdentity({
172 previousId: olds?.connectorId ?? output?.connectorId,
173 nextId: news.connectorId
174 ? rfc1035(news.connectorId, "connector")
175 : (olds?.connectorId ?? output?.connectorId),
176 previousParent: olds?.connectCluster ?? output?.connectCluster,
177 nextParent: connectClusterOf(news.connectCluster, env.project, location),
178 });
179 }),
180
181 read: Effect.fn(function* ({ id, olds, output }) {
182 const env = yield* GcpEnvironment.current;
183 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
184 const connectorId = yield* toPhysicalId(
185 id,
186 olds?.connectorId,
187 output?.connectorId,
188 "connector",
189 );
190 const connectCluster =
191 olds?.connectCluster !== undefined
192 ? connectClusterOf(olds.connectCluster, env.project, location)
193 : (output?.connectCluster ?? "");
194 const name =
195 output?.name ??
196 (connectCluster.length > 0 ? resourceName(connectCluster, connectorId) : "");
197 const existing = yield* getConnector(name);
198 if (existing === undefined) return undefined;
199 const attrs = toAttrs(existing, env.project);
200 return output !== undefined || olds !== undefined ? attrs : Unowned(attrs);
201 }),
202
203 list: () =>
204 Effect.gen(function* () {
205 const env = yield* GcpEnvironment.current;
206 const clusters = yield* listAlchemyConnectClusters(env.project, env.region);
207 const connectors = yield* Effect.forEach(
208 clusters.filter((cluster) => (cluster.name ?? "").length > 0),
209 (cluster: kafka.ConnectCluster) =>
210 collectPages(
211 kafka.listProjectsLocationsConnectClustersConnectors.pages({
212 parent: cluster.name!,
213 pageSize: 1000,
214 }),
215 (page) => page.connectors,
216 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as kafka.Connector[]))),
217 { concurrency: 4 },
218 );
219 return connectors.flat().map((connector) => toAttrs(connector, env.project));
220 }),
221
222 reconcile: Effect.fn(function* ({ id, news, output }) {
223 const env = yield* GcpEnvironment.current;
224 const location = normalizeLocation(news.location ?? output?.location, env.region);
225 const connectCluster = connectClusterOf(news.connectCluster, env.project, location);
226 const connectorId = yield* toPhysicalId(
227 id,
228 news.connectorId,
229 output?.connectorId,
230 "connector",
231 );
232 const name = output?.name ?? resourceName(connectCluster, connectorId);
233 const configs = news.configs;
234 const taskRestartPolicy = news.taskRestartPolicy;
235
236 let current = yield* getConnector(name);
237
238 if (current === undefined) {
239 const created = yield* retryTransient(
240 kafka
241 .createProjectsLocationsConnectClustersConnectors({
242 parent: connectCluster,
243 connectorId,
244 body: {
245 configs,
246 taskRestartPolicy,
247 },
248 })
249 .pipe(Effect.catchTag("Conflict", () => getConnector(name))),
250 );
251 current = created ?? undefined;
252 }
253
254 if (current === undefined) {
255 return yield* new ConnectClustersConnectorNotResolved({ resourceName: name });
256 }
257
258 const configsChanged =
259 fingerprint(stringMapOf(current.configs)) !== fingerprint(stringMapOf(configs));
260 const policyChanged =
261 fingerprint(policyOf(current.taskRestartPolicy)) !==
262 fingerprint(policyOf(taskRestartPolicy));
263
264 if (configsChanged || policyChanged) {
265 current = yield* kafka.patchProjectsLocationsConnectClustersConnectors({
266 name,
267 updateMask: fieldMask([
268 configsChanged && "configs",
269 policyChanged && "task_restart_policy",
270 ]),
271 body: {
272 configs,
273 taskRestartPolicy,
274 },
275 });
276 }
277
278 current = yield* waitUntilReady(
279 getConnector(current.name ?? name),
280 current.name ?? name,
281 (connector) => connector.state,
282 );
283 return toAttrs(current, env.project);
284 }),
285
286 delete: Effect.fn(function* ({ output }) {
287 yield* kafka
288 .deleteProjectsLocationsConnectClustersConnectors({
289 name: output.name,
290 })
291 .pipe(Effect.catchTag("NotFound", () => Effect.void));
292 yield* waitUntilGone(getConnector(output.name), output.name);
293 }),
294 });