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

main
ts 214 lines 7.27 KB
Raw
1 import * as databrew from "@distilled.cloud/aws/databrew";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import { AWSEnvironment } from "../Environment.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 cleanMap,
14 databrewArn,
15 fetchObservedTags,
16 retryWhileConflict,
17 retryWhileRoleNotAssumable,
18 syncTags,
19 } from "./internal.ts";
20
21 /** How DataBrew samples the dataset in the interactive project view. */
22 export interface ProjectSample {
23 /** Number of rows in the sample (default 500). */
24 size?: number;
25 /** Sampling strategy. */
26 type: "FIRST_N" | "LAST_N" | "RANDOM" | (string & {});
27 }
28
29 export interface ProjectProps {
30 /**
31 * Name of the project. If omitted, a unique name is generated. Changing
32 * the name replaces the project.
33 * @default a generated physical name
34 */
35 projectName?: string;
36 /**
37 * The dataset the project explores. Changing it replaces the project.
38 */
39 datasetName: string;
40 /**
41 * The recipe the project edits (its `LATEST_WORKING` version). Changing
42 * it replaces the project.
43 */
44 recipeName: string;
45 /**
46 * The sample shown in the interactive session.
47 * @default 500 rows, FIRST_N
48 */
49 sample?: ProjectSample;
50 /**
51 * The IAM role ARN DataBrew assumes for the project's interactive
52 * sessions (needs read access to the dataset's source).
53 */
54 role: string;
55 /**
56 * Tags to apply to the project. Merged with internal Alchemy tags.
57 */
58 tags?: Record<string, string>;
59 }
60
61 export interface Project extends Resource<
62 "AWS.DataBrew.Project",
63 ProjectProps,
64 {
65 /** Name of the project. */
66 projectName: string;
67 /** ARN of the project. */
68 projectArn: string;
69 },
70 {},
71 Providers
72 > {}
73
74 /**
75 * An AWS Glue DataBrew project — the interactive workspace binding a dataset
76 * to a recipe's working version. The project definition is free; costs only
77 * accrue when an interactive session is started in the console.
78 * ### Creating Projects
79 * **Example:** Dataset + Recipe Project
80 * ```typescript
81 * import * as AWS from "@alchemy.run/aws";
82 *
83 * const project = yield* AWS.DataBrew.Project("Explore", {
84 * datasetName: dataset.datasetName,
85 * recipeName: recipe.recipeName,
86 * role: role.roleArn,
87 * });
88 * ```
89 *
90 * **Example:** Custom Sample
91 * ```typescript
92 * const project = yield* AWS.DataBrew.Project("Explore", {
93 * datasetName: dataset.datasetName,
94 * recipeName: recipe.recipeName,
95 * sample: { type: "RANDOM", size: 250 },
96 * role: role.roleArn,
97 * });
98 * ```
99 *
100 * @resource
101 */
102 export const Project = Resource<Project>("AWS.DataBrew.Project");
103
104 const buildSample = (sample: ProjectSample | undefined) =>
105 sample ? { Size: sample.size, Type: sample.type } : undefined;
106
107 export const ProjectProvider = () =>
108 Provider.effect(
109 Project,
110 Effect.gen(function* () {
111 const createName = Effect.fn(function* (
112 id: string,
113 props: { projectName?: string | undefined },
114 ) {
115 return props.projectName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
116 });
117
118 const observe = Effect.fn(function* (name: string) {
119 return yield* databrew
120 .describeProject({ Name: name })
121 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
122 });
123
124 return Project.Provider.of({
125 stables: ["projectName", "projectArn"],
126
127 list: () =>
128 Effect.gen(function* () {
129 const { accountId, region } = yield* AWSEnvironment.current;
130 const pages = yield* databrew.listProjects.pages({}).pipe(Stream.runCollect);
131 return Array.from(pages)
132 .flatMap((page) => page.Projects ?? [])
133 .map((p) => ({
134 projectName: p.Name,
135 projectArn: p.ResourceArn ?? databrewArn(region, accountId, "project", p.Name),
136 }));
137 }),
138
139 read: Effect.fn(function* ({ id, olds, output }) {
140 const { accountId, region } = yield* AWSEnvironment.current;
141 const name = output?.projectName ?? (yield* createName(id, olds ?? {}));
142 const project = yield* observe(name);
143 if (project === undefined) return undefined;
144 const arn = project.ResourceArn ?? databrewArn(region, accountId, "project", name);
145 const attrs = { projectName: name, projectArn: arn };
146 const tags = cleanMap(project.Tags);
147 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
148 }),
149
150 diff: Effect.fn(function* ({ id, news, olds }) {
151 if (!isResolved(news)) return undefined;
152 const oldName = yield* createName(id, olds);
153 const newName = yield* createName(id, news);
154 if (oldName !== newName) return { action: "replace" } as const;
155 // UpdateProject only accepts Sample + RoleArn — the dataset and
156 // recipe associations are immutable.
157 if (olds.datasetName !== news.datasetName) {
158 return { action: "replace" } as const;
159 }
160 if (olds.recipeName !== news.recipeName) {
161 return { action: "replace" } as const;
162 }
163 }),
164
165 reconcile: Effect.fn(function* ({ id, news, output, session }) {
166 const { accountId, region } = yield* AWSEnvironment.current;
167 const name = output?.projectName ?? (yield* createName(id, news));
168 const internalTags = yield* createInternalTags(id);
169 const desiredTags = { ...news.tags, ...internalTags };
170
171 // 1. OBSERVE
172 const project = yield* observe(name);
173
174 // 2. ENSURE / 3. SYNC (Sample + RoleArn are the mutable aspects)
175 if (project === undefined) {
176 yield* retryWhileRoleNotAssumable(
177 databrew.createProject({
178 Name: name,
179 DatasetName: news.datasetName,
180 RecipeName: news.recipeName,
181 Sample: buildSample(news.sample),
182 RoleArn: news.role,
183 Tags: desiredTags,
184 }),
185 ).pipe(Effect.catchTag("ConflictException", () => Effect.void));
186 } else {
187 yield* retryWhileRoleNotAssumable(
188 databrew.updateProject({
189 Name: name,
190 Sample: buildSample(news.sample),
191 RoleArn: news.role,
192 }),
193 );
194 }
195
196 const arn = project?.ResourceArn ?? databrewArn(region, accountId, "project", name);
197
198 // 3b. SYNC TAGS against observed cloud tags
199 const observedTags = yield* fetchObservedTags(arn);
200 yield* syncTags(arn, observedTags, desiredTags);
201
202 yield* session.note(name);
203 return { projectName: name, projectArn: arn };
204 }),
205
206 delete: Effect.fn(function* ({ output }) {
207 // ConflictException while an interactive session is winding down.
208 yield* retryWhileConflict(databrew.deleteProject({ Name: output.projectName })).pipe(
209 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
210 );
211 }),
212 });
213 }),
214 );