@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, 04:45 UTC

main
ts 196 lines 6.71 KB
Raw
1 import * as backupsearch from "@distilled.cloud/aws/backupsearch";
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 { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Stream from "effect/Stream";
9 import type { Providers } from "../Providers.ts";
10 import { readBackupSearchTags, syncBackupSearchTags } from "./internal.ts";
11
12 export interface S3ExportSpecification {
13 /** Name of the destination S3 bucket that receives the results `.csv`. */
14 destinationBucket: string;
15 /** Key prefix under which the results are written. */
16 destinationPrefix?: string;
17 }
18
19 export interface ExportSpecification {
20 /** Export the search results to an S3 bucket. */
21 s3ExportSpecification: S3ExportSpecification;
22 }
23
24 export interface ExportJobProps {
25 /**
26 * Identifier of the search job whose results are exported. Immutable —
27 * changing it replaces the export job.
28 */
29 searchJobIdentifier: string;
30 /**
31 * Where the results are exported to. Immutable — changing it replaces the
32 * export job.
33 */
34 exportSpecification: ExportSpecification;
35 /**
36 * ARN of the IAM role BackupSearch assumes to write the export to the
37 * destination bucket. Immutable — changing it replaces the export job.
38 */
39 roleArn?: string;
40 /**
41 * User-defined tags for the export job.
42 */
43 tags?: Record<string, string>;
44 }
45
46 export interface ExportJob extends Resource<
47 "AWS.BackupSearch.ExportJob",
48 ExportJobProps,
49 {
50 /**
51 * Service-assigned unique ID of the export job.
52 */
53 exportJobIdentifier: string;
54 /**
55 * ARN of the export job.
56 */
57 exportJobArn: string;
58 /**
59 * ARN of the search job whose results are exported.
60 */
61 searchJobArn: string | undefined;
62 /**
63 * Current status of the export job (e.g. `RUNNING`, `COMPLETED`).
64 */
65 status: string;
66 },
67 never,
68 Providers
69 > {}
70
71 /**
72 * An AWS Backup Search export job — transmits the results of a completed
73 * search job to a designated S3 bucket as a `.csv` file, retaining them
74 * beyond the search job's seven-day retention. An export job is immutable
75 * once started and cannot be stopped or deleted; its record ages out
76 * server-side.
77 *
78 * ### Exporting Search Results
79 * **Example:** Export to an S3 Bucket
80 * ```typescript
81 * const exportJob = yield* BackupSearch.ExportJob("Results", {
82 * searchJobIdentifier: search.searchJobIdentifier,
83 * exportSpecification: {
84 * s3ExportSpecification: {
85 * destinationBucket: bucket.bucketName,
86 * destinationPrefix: "backup-search-results/",
87 * },
88 * },
89 * roleArn: role.roleArn,
90 * });
91 * ```
92 *
93 * @resource
94 */
95 export const ExportJob = Resource<ExportJob>("AWS.BackupSearch.ExportJob");
96
97 export const ExportJobProvider = () =>
98 Provider.effect(
99 ExportJob,
100 Effect.gen(function* () {
101 const get = Effect.fn(function* (exportJobIdentifier: string) {
102 return yield* backupsearch
103 .getSearchResultExportJob({
104 ExportJobIdentifier: exportJobIdentifier,
105 })
106 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
107 });
108
109 const toAttrs = (
110 job: backupsearch.GetSearchResultExportJobOutput | backupsearch.ExportJobSummary,
111 ) => ({
112 exportJobIdentifier: job.ExportJobIdentifier,
113 exportJobArn: job.ExportJobArn!,
114 searchJobArn: job.SearchJobArn,
115 status: (job.Status ?? "UNKNOWN") as string,
116 });
117
118 return {
119 stables: ["exportJobIdentifier", "exportJobArn"],
120
121 diff: Effect.fn(function* ({ olds, news }) {
122 if (!isResolved(news)) return undefined;
123 if (
124 (olds.searchJobIdentifier ?? undefined) !== (news.searchJobIdentifier ?? undefined) ||
125 (olds.roleArn ?? undefined) !== (news.roleArn ?? undefined) ||
126 JSON.stringify(olds.exportSpecification ?? null) !==
127 JSON.stringify(news.exportSpecification ?? null)
128 ) {
129 return { action: "replace" } as const;
130 }
131 }),
132
133 read: Effect.fn(function* ({ id, output }) {
134 if (!output?.exportJobIdentifier) return undefined;
135 const job = yield* get(output.exportJobIdentifier);
136 if (job === undefined) return undefined;
137 const attrs = toAttrs(job);
138 const tags =
139 job.ExportJobArn !== undefined ? yield* readBackupSearchTags(job.ExportJobArn) : {};
140 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
141 }),
142
143 reconcile: Effect.fn(function* ({ id, news, output, session }) {
144 const internalTags = yield* createInternalTags(id);
145 const desiredTags = { ...internalTags, ...news.tags };
146
147 // OBSERVE — output is only an identifier cache; the job may be gone.
148 let job =
149 output?.exportJobIdentifier !== undefined
150 ? yield* get(output.exportJobIdentifier)
151 : undefined;
152
153 // ENSURE — an export job is immutable once started; start it if
154 // missing. Any change to its inputs is a replacement (diff).
155 if (job === undefined) {
156 const started = yield* backupsearch.startSearchResultExportJob({
157 SearchJobIdentifier: news.searchJobIdentifier,
158 ExportSpecification: {
159 s3ExportSpecification: {
160 DestinationBucket:
161 news.exportSpecification.s3ExportSpecification.destinationBucket,
162 DestinationPrefix:
163 news.exportSpecification.s3ExportSpecification.destinationPrefix,
164 },
165 },
166 RoleArn: news.roleArn,
167 Tags: desiredTags,
168 });
169 job = yield* backupsearch.getSearchResultExportJob({
170 ExportJobIdentifier: started.ExportJobIdentifier,
171 });
172 } else if (job.ExportJobArn !== undefined) {
173 // SYNC — tags are the only mutable aspect; diff against OBSERVED.
174 yield* syncBackupSearchTags(job.ExportJobArn, desiredTags);
175 }
176
177 yield* session.note(job.ExportJobIdentifier);
178 return toAttrs(job);
179 }),
180
181 // An export job cannot be stopped or deleted — its record ages out
182 // server-side and the exported .csv lives in the destination bucket.
183 delete: () => Effect.void,
184
185 list: () =>
186 backupsearch.listSearchResultExportJobs.pages({}).pipe(
187 Stream.runCollect,
188 Effect.map((chunk) =>
189 Array.from(chunk)
190 .flatMap((page) => page.ExportJobs ?? [])
191 .map(toAttrs),
192 ),
193 ),
194 };
195 }),
196 );