@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 273 lines 9.03 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 * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 collectPages,
13 expandParent,
14 fieldMask,
15 fingerprint,
16 getTopic,
17 listAlchemyClusters,
18 normalizeLocation,
19 parseName,
20 replaceOnIdentity,
21 retryTransient,
22 rfc1035,
23 stringMapOf,
24 toPhysicalId,
25 } from "./internal.ts";
26
27 const DEFAULT_PARTITION_COUNT = 1;
28 const DEFAULT_REPLICATION_FACTOR = 3;
29
30 export type ClustersTopicProps = {
31 /**
32 * Parent cluster. Full name
33 * `projects/{project}/locations/{location}/clusters/{cluster}` or the
34 * cluster id (combined with `location`). Immutable — changing it
35 * replaces the topic.
36 */
37 cluster: string;
38 /**
39 * Region used when `cluster` is a bare id.
40 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
41 */
42 location?: string;
43 /**
44 * Topic id (the `{topic}` segment of `.../clusters/{cluster}/topics/{topic}`).
45 * If omitted, a unique RFC1035 name is generated. Immutable — changing
46 * it replaces the topic.
47 */
48 topicId?: string;
49 /**
50 * Partition count. Can only be increased, not decreased.
51 * @default 1
52 */
53 partitionCount?: number;
54 /**
55 * Replication factor. Immutable — changing it replaces the topic.
56 * @default 3
57 */
58 replicationFactor?: number;
59 /**
60 * Topic configs that override cluster defaults (`cleanup.policy`,
61 * `compression.type`, …).
62 */
63 configs?: Record<string, string>;
64 };
65
66 export type ClustersTopic = Resource<
67 "GCP.ManagedKafka.ClustersTopic",
68 ClustersTopicProps,
69 {
70 /** Full resource name `.../clusters/{cluster}/topics/{topic}`. */
71 name: string;
72 /** Topic id. */
73 topicId: string;
74 /** Parent cluster resource name. */
75 cluster: string;
76 /** Project id. */
77 project: string;
78 /** Location id. */
79 location: string;
80 /** Partition count. */
81 partitionCount: number;
82 /** Replication factor. */
83 replicationFactor: number;
84 /** Topic config overrides. */
85 configs: Record<string, string>;
86 },
87 never,
88 Providers
89 >;
90
91 /**
92 * A Kafka topic on a Managed Service for Apache Kafka cluster.
93 *
94 * Changing `topicId`, `cluster`, `location`, or `replicationFactor`
95 * replaces the topic. `partitionCount` can only increase. Configs update
96 * in place.
97 *
98 * ### Creating a Topic
99 * **Example:** Generated name
100 * ```typescript
101 * const topic = yield* GCP.ManagedKafka.ClustersTopic("Events", {
102 * cluster: cluster.name,
103 * });
104 * ```
105 *
106 * **Example:** Explicit id, partitions, and configs
107 * ```typescript
108 * const topic = yield* GCP.ManagedKafka.ClustersTopic("Events", {
109 * cluster: cluster.name,
110 * topicId: "order-events",
111 * partitionCount: 3,
112 * replicationFactor: 3,
113 * configs: { "cleanup.policy": "compact" },
114 * });
115 * ```
116 *
117 * @resource
118 * @category ManagedKafka
119 */
120 export const ClustersTopic = Resource<ClustersTopic>("GCP.ManagedKafka.ClustersTopic");
121
122 export class ClustersTopicNotResolved extends Data.TaggedError(
123 "GCP.ManagedKafka.ClustersTopicNotResolved",
124 )<{
125 resourceName: string;
126 }> {}
127
128 const clusterOf = (cluster: string, project: string, location: string) =>
129 expandParent(cluster, project, location, "clusters");
130
131 const resourceName = (cluster: string, topicId: string) => `${cluster}/topics/${topicId}`;
132
133 const toAttrs = (topic: kafka.Topic, project: string) => {
134 const name = topic.name ?? "";
135 const parsed = parseName(name, "topics");
136 return {
137 name,
138 topicId: parsed.id,
139 cluster: parsed.parent,
140 project: parsed.project || project,
141 location: parsed.location,
142 partitionCount: topic.partitionCount ?? DEFAULT_PARTITION_COUNT,
143 replicationFactor: topic.replicationFactor ?? DEFAULT_REPLICATION_FACTOR,
144 configs: stringMapOf(topic.configs),
145 };
146 };
147
148 export const ClustersTopicProvider = () =>
149 Provider.succeed(ClustersTopic, {
150 stables: ["name", "topicId", "cluster", "project", "location"],
151
152 diff: Effect.fn(function* ({ news, olds, output }) {
153 if (!isResolved(news)) return undefined;
154 const env = yield* GcpEnvironment.current;
155 const location = normalizeLocation(news.location ?? output?.location, env.region);
156 const previousFactor = olds?.replicationFactor ?? output?.replicationFactor;
157 const nextFactor = news.replicationFactor ?? previousFactor;
158 return replaceOnIdentity({
159 previousId: olds?.topicId ?? output?.topicId,
160 nextId: news.topicId ? rfc1035(news.topicId, "topic") : (olds?.topicId ?? output?.topicId),
161 previousParent: olds?.cluster ?? output?.cluster,
162 nextParent: clusterOf(news.cluster, env.project, location),
163 extra:
164 previousFactor !== undefined && nextFactor !== undefined && previousFactor !== nextFactor,
165 });
166 }),
167
168 read: Effect.fn(function* ({ id, olds, output }) {
169 const env = yield* GcpEnvironment.current;
170 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
171 const topicId = yield* toPhysicalId(id, olds?.topicId, output?.topicId, "topic");
172 const cluster =
173 olds?.cluster !== undefined
174 ? clusterOf(olds.cluster, env.project, location)
175 : (output?.cluster ?? "");
176 const name = output?.name ?? (cluster.length > 0 ? resourceName(cluster, topicId) : "");
177 const existing = yield* getTopic(name);
178 if (existing === undefined) return undefined;
179 const attrs = toAttrs(existing, env.project);
180 return output !== undefined || olds !== undefined ? attrs : Unowned(attrs);
181 }),
182
183 list: () =>
184 Effect.gen(function* () {
185 const env = yield* GcpEnvironment.current;
186 const clusters = yield* listAlchemyClusters(env.project, env.region);
187 const topics = yield* Effect.forEach(
188 clusters.filter((cluster) => (cluster.name ?? "").length > 0),
189 (cluster: kafka.Cluster) =>
190 collectPages(
191 kafka.listProjectsLocationsClustersTopics.pages({
192 parent: cluster.name!,
193 pageSize: 1000,
194 }),
195 (page) => page.topics,
196 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as kafka.Topic[]))),
197 { concurrency: 4 },
198 );
199 return topics.flat().map((topic) => toAttrs(topic, env.project));
200 }),
201
202 reconcile: Effect.fn(function* ({ id, news, output }) {
203 const env = yield* GcpEnvironment.current;
204 const location = normalizeLocation(news.location ?? output?.location, env.region);
205 const cluster = clusterOf(news.cluster, env.project, location);
206 const topicId = yield* toPhysicalId(id, news.topicId, output?.topicId, "topic");
207 const name = output?.name ?? resourceName(cluster, topicId);
208 const partitionCount = news.partitionCount ?? DEFAULT_PARTITION_COUNT;
209 const replicationFactor = news.replicationFactor ?? DEFAULT_REPLICATION_FACTOR;
210 const configs = news.configs;
211
212 let current = yield* getTopic(name);
213
214 if (current === undefined) {
215 const created = yield* retryTransient(
216 kafka
217 .createProjectsLocationsClustersTopics({
218 parent: cluster,
219 topicId,
220 body: {
221 partitionCount,
222 replicationFactor,
223 configs,
224 },
225 })
226 .pipe(Effect.catchTag("Conflict", () => getTopic(name))),
227 );
228 current = created ?? undefined;
229 // A new topic reads back NotFound (UnknownTopicOrPartition) until its
230 // partitions are assigned; block until it is readable.
231 if (current !== undefined) {
232 current = yield* getTopic(name).pipe(
233 Effect.repeat({
234 schedule: Schedule.spaced("5 seconds"),
235 until: (topic) => topic !== undefined,
236 times: 24,
237 }),
238 );
239 }
240 }
241
242 if (current === undefined) {
243 return yield* new ClustersTopicNotResolved({ resourceName: name });
244 }
245
246 const partitionsChanged =
247 (current.partitionCount ?? DEFAULT_PARTITION_COUNT) !== partitionCount;
248 const configsChanged =
249 fingerprint(stringMapOf(current.configs)) !== fingerprint(stringMapOf(configs));
250
251 if (partitionsChanged || configsChanged) {
252 current = yield* kafka.patchProjectsLocationsClustersTopics({
253 name,
254 updateMask: fieldMask([
255 partitionsChanged && "partition_count",
256 configsChanged && "configs",
257 ]),
258 body: {
259 partitionCount,
260 configs,
261 },
262 });
263 }
264
265 return toAttrs(current, env.project);
266 }),
267
268 delete: Effect.fn(function* ({ output }) {
269 yield* kafka
270 .deleteProjectsLocationsClustersTopics({ name: output.name })
271 .pipe(Effect.catchTag("NotFound", () => Effect.void));
272 }),
273 });