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

main
ts 341 lines 11.1 KB
Raw
1 import * as oracle from "@distilled.cloud/gcp/oracledatabase_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 { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 OracleDatabaseNotResolved,
13 collectPages,
14 hasAlchemyLabelMap,
15 listAtLocation,
16 normalizeLocation,
17 parentOf,
18 parseName,
19 replaceOnIdentity,
20 resourceNameOf,
21 retryConflict,
22 retryQuota,
23 toPhysicalId,
24 userLabels,
25 waitUntilExists,
26 waitUntilGone,
27 waitUntilReady,
28 } from "./internal.ts";
29 import { waitForOperation } from "./operations.ts";
30
31 const COLLECTION = "goldengateConnectionAssignments";
32 const FALLBACK_ID = "ggassign";
33
34 export type GoldengateConnectionAssignmentPropertiesInput = {
35 /**
36 * GoldenGate connection
37 * (`projects/{project}/locations/{location}/goldengateConnections/{id}`).
38 * Required on create. Immutable.
39 */
40 goldengateConnection?: string;
41 /**
42 * GoldenGate deployment
43 * (`projects/{project}/locations/{location}/goldengateDeployments/{id}`).
44 * Required on create. Immutable.
45 */
46 goldengateDeployment?: string;
47 };
48
49 export type GoldengateConnectionAssignmentProps = {
50 /**
51 * Assignment id. If omitted, a unique RFC1035 name is generated.
52 * Immutable.
53 */
54 goldengateConnectionAssignmentId?: string;
55 /**
56 * Region. Immutable.
57 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
58 */
59 location?: string;
60 /**
61 * Display name.
62 */
63 displayName?: string;
64 /**
65 * User labels. Alchemy ownership labels are merged in automatically.
66 */
67 labels?: Record<string, string>;
68 /**
69 * Assignment properties.
70 */
71 properties?: GoldengateConnectionAssignmentPropertiesInput;
72 /** Connection name. Convenience alias. */
73 goldengateConnection?: string;
74 /** Deployment name. Convenience alias. */
75 goldengateDeployment?: string;
76 };
77
78 export type GoldengateConnectionAssignment = Resource<
79 "GCP.OracleDatabase.GoldengateConnectionAssignment",
80 GoldengateConnectionAssignmentProps,
81 {
82 /** Full resource name. */
83 name: string;
84 /** Assignment id. */
85 goldengateConnectionAssignmentId: string;
86 /** Project id. */
87 project: string;
88 /** Location id. */
89 location: string;
90 /** Display name. */
91 displayName: string | undefined;
92 /** User labels (Alchemy ownership labels stripped). */
93 labels: Record<string, string>;
94 /** Entitlement id. */
95 entitlementId: string | undefined;
96 /** Assigned connection. */
97 goldengateConnection: string | undefined;
98 /** Assigned deployment. */
99 goldengateDeployment: string | undefined;
100 /** Lifecycle state. */
101 state: string | undefined;
102 /** Credential store alias. */
103 alias: string | undefined;
104 /** OCID. */
105 ocid: string | undefined;
106 /** RFC3339 creation timestamp. */
107 createTime: string | undefined;
108 },
109 never,
110 Providers
111 >;
112
113 /**
114 * An Oracle GoldenGate connection assignment on Google Cloud.
115 *
116 * Changing `goldengateConnectionAssignmentId`, `location`, the
117 * connection, or the deployment replaces the assignment. There is no
118 * patch API in the distilled SDK, so labels are applied at create.
119 *
120 * ### Creating an assignment
121 * **Example:** Bind a connection to a deployment
122 * ```typescript
123 * const assignment = yield* GCP.OracleDatabase.GoldengateConnectionAssignment(
124 * "Assign",
125 * {
126 * goldengateConnection: connection.name,
127 * goldengateDeployment: deployment.name,
128 * displayName: "assign",
129 * },
130 * );
131 * ```
132 *
133 * @resource
134 * @category OracleDatabase
135 */
136 export const GoldengateConnectionAssignment = Resource<GoldengateConnectionAssignment>(
137 "GCP.OracleDatabase.GoldengateConnectionAssignment",
138 );
139
140 const mergedProperties = (
141 news: GoldengateConnectionAssignmentProps,
142 ): GoldengateConnectionAssignmentPropertiesInput => ({
143 ...news.properties,
144 goldengateConnection: news.goldengateConnection ?? news.properties?.goldengateConnection,
145 goldengateDeployment: news.goldengateDeployment ?? news.properties?.goldengateDeployment,
146 });
147
148 const toCreateBody = (
149 news: GoldengateConnectionAssignmentProps,
150 desiredLabels: Record<string, string>,
151 ): oracle.GoldengateConnectionAssignment => {
152 const props = mergedProperties(news);
153 const properties: oracle.GoldengateConnectionAssignmentProperties = {};
154 if (props.goldengateConnection !== undefined) {
155 properties.goldengateConnection = props.goldengateConnection;
156 }
157 if (props.goldengateDeployment !== undefined) {
158 properties.goldengateDeployment = props.goldengateDeployment;
159 }
160 const body: oracle.GoldengateConnectionAssignment = {
161 labels: desiredLabels,
162 properties,
163 };
164 if (news.displayName !== undefined) body.displayName = news.displayName;
165 return body;
166 };
167
168 const toAttrs = (assignment: oracle.GoldengateConnectionAssignment, project: string) => {
169 const name = assignment.name ?? "";
170 const parsed = parseName(name, COLLECTION);
171 return {
172 name,
173 goldengateConnectionAssignmentId: parsed.id,
174 project: parsed.project || project,
175 location: parsed.location,
176 displayName: assignment.displayName,
177 labels: userLabels(assignment.labels),
178 entitlementId: assignment.entitlementId,
179 goldengateConnection: assignment.properties?.goldengateConnection,
180 goldengateDeployment: assignment.properties?.goldengateDeployment,
181 state: assignment.properties?.state,
182 alias: assignment.properties?.alias,
183 ocid: assignment.properties?.ocid,
184 createTime: assignment.createTime,
185 };
186 };
187
188 const getByName = (name: string) =>
189 retryQuota(oracle.getProjectsLocationsGoldengateConnectionAssignments({ name })).pipe(
190 Effect.catchTag("NotFound", () => Effect.undefined),
191 );
192
193 const listAssignments = (project: string, region: string) => {
194 const collect = (parent: string) =>
195 collectPages(
196 oracle.listProjectsLocationsGoldengateConnectionAssignments.pages({
197 parent,
198 pageSize: 1000,
199 }),
200 (page) => page.goldengateConnectionAssignments,
201 ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels))));
202 return listAtLocation(project, region, collect).pipe(
203 Effect.catchTag("NotFound", () => Effect.succeed([])),
204 );
205 };
206
207 export const GoldengateConnectionAssignmentProvider = () =>
208 Provider.succeed(GoldengateConnectionAssignment, {
209 stables: [
210 "name",
211 "goldengateConnectionAssignmentId",
212 "project",
213 "location",
214 "createTime",
215 "ocid",
216 ],
217
218 diff: Effect.fn(function* ({ news, olds, output }) {
219 if (!isResolved(news)) return undefined;
220 const env = yield* GcpEnvironment.current;
221 const previousConn =
222 olds?.goldengateConnection ??
223 olds?.properties?.goldengateConnection ??
224 output?.goldengateConnection ??
225 "";
226 const nextConn =
227 news.goldengateConnection ?? news.properties?.goldengateConnection ?? previousConn;
228 const previousDep =
229 olds?.goldengateDeployment ??
230 olds?.properties?.goldengateDeployment ??
231 output?.goldengateDeployment ??
232 "";
233 const nextDep =
234 news.goldengateDeployment ?? news.properties?.goldengateDeployment ?? previousDep;
235 return replaceOnIdentity({
236 previousId:
237 olds?.goldengateConnectionAssignmentId ?? output?.goldengateConnectionAssignmentId,
238 nextId:
239 news.goldengateConnectionAssignmentId ??
240 olds?.goldengateConnectionAssignmentId ??
241 output?.goldengateConnectionAssignmentId,
242 previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region),
243 nextLocation: normalizeLocation(
244 news.location ?? olds?.location ?? output?.location ?? env.region,
245 ),
246 extra: nextConn !== previousConn || nextDep !== previousDep,
247 });
248 }),
249
250 read: Effect.fn(function* ({ id, olds, output }) {
251 const env = yield* GcpEnvironment.current;
252 const goldengateConnectionAssignmentId = yield* toPhysicalId(
253 id,
254 olds?.goldengateConnectionAssignmentId,
255 output?.goldengateConnectionAssignmentId,
256 FALLBACK_ID,
257 );
258 const location = normalizeLocation(olds?.location ?? output?.location ?? env.region);
259 const name =
260 output?.name ??
261 resourceNameOf(env.project, location, COLLECTION, goldengateConnectionAssignmentId);
262 const existing = yield* getByName(name);
263 if (existing === undefined) return undefined;
264 const attrs = toAttrs(existing, env.project);
265 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
266 }),
267
268 list: () =>
269 Effect.gen(function* () {
270 const env = yield* GcpEnvironment.current;
271 const items = yield* listAssignments(env.project, env.region);
272 return items.map((item) => toAttrs(item, env.project));
273 }),
274
275 reconcile: Effect.fn(function* ({ id, news, output }) {
276 const env = yield* GcpEnvironment.current;
277 const goldengateConnectionAssignmentId = yield* toPhysicalId(
278 id,
279 news.goldengateConnectionAssignmentId,
280 output?.goldengateConnectionAssignmentId,
281 FALLBACK_ID,
282 );
283 const location = normalizeLocation(news.location ?? output?.location ?? env.region);
284 const name = resourceNameOf(
285 env.project,
286 location,
287 COLLECTION,
288 goldengateConnectionAssignmentId,
289 );
290 const desiredLabels = {
291 ...toLabels(news.labels),
292 ...(yield* createInternalLabels(id)),
293 };
294
295 let current = yield* getByName(output?.name ?? name);
296
297 if (current === undefined) {
298 const created = yield* oracle
299 .createProjectsLocationsGoldengateConnectionAssignments({
300 parent: parentOf(env.project, location),
301 goldengateConnectionAssignmentId,
302 body: toCreateBody(news, desiredLabels),
303 })
304 .pipe(
305 retryQuota,
306 Effect.catchTag("Conflict", () => Effect.undefined),
307 );
308 if (created !== undefined) {
309 yield* waitForOperation(created);
310 }
311 current = yield* waitUntilExists(getByName(name), name);
312 }
313
314 if (current === undefined) {
315 return yield* new OracleDatabaseNotResolved({ resourceName: name });
316 }
317
318 const ready = yield* waitUntilReady(
319 getByName(current.name ?? name),
320 current.name ?? name,
321 (value) => value.properties?.state,
322 );
323
324 return toAttrs(ready, env.project);
325 }),
326
327 delete: Effect.fn(function* ({ output }) {
328 const operation = yield* oracle
329 .deleteProjectsLocationsGoldengateConnectionAssignments({
330 name: output.name,
331 })
332 .pipe(
333 retryConflict,
334 Effect.catchTag("NotFound", () => Effect.undefined),
335 );
336 if (operation !== undefined) {
337 yield* waitForOperation(operation, { notFoundOk: true });
338 }
339 yield* waitUntilGone(getByName(output.name), output.name);
340 }),
341 });