The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as quicksight from "@distilled.cloud/aws/quicksight"; |
| 2 | import { diffTags } from "alchemy/Tags"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | |
| 7 | /** |
| 8 | * A QuickSight resource whose asynchronous create/update has not converged |
| 9 | * to a terminal status yet. |
| 10 | */ |
| 11 | export class QuickSightNotSettled extends Data.TaggedError("QuickSightNotSettled")<{ |
| 12 | readonly resourceId: string; |
| 13 | readonly status: string | undefined; |
| 14 | }> {} |
| 15 | |
| 16 | /** |
| 17 | * A QuickSight resource whose asynchronous create/update converged to a |
| 18 | * `*_FAILED` status. |
| 19 | */ |
| 20 | export class QuickSightOperationFailed extends Data.TaggedError("QuickSightOperationFailed")<{ |
| 21 | readonly resourceId: string; |
| 22 | readonly status: string; |
| 23 | readonly reason: string | undefined; |
| 24 | }> {} |
| 25 | |
| 26 | const isInProgress = (status: string | undefined): boolean => |
| 27 | status === "CREATION_IN_PROGRESS" || status === "UPDATE_IN_PROGRESS"; |
| 28 | |
| 29 | const isFailed = (status: string | undefined): boolean => |
| 30 | status === "CREATION_FAILED" || status === "UPDATE_FAILED"; |
| 31 | |
| 32 | /** |
| 33 | * Poll a QuickSight resource until its `ResourceStatus` reaches a terminal |
| 34 | * value. `*_IN_PROGRESS` repeats (bounded); `*_FAILED` fails immediately. |
| 35 | * Creation/update of most QuickSight resources settles within a few seconds |
| 36 | * (data-source connectivity validation can take longer) — budget ~2.5 min. |
| 37 | * |
| 38 | * The explicit `Effect.Effect<A, E, R>` return annotation is load-bearing: |
| 39 | * inlining retry/repeat combinators in provider lifecycle code lets their |
| 40 | * conditional return types survive into declaration emit and widen the |
| 41 | * provider layer to `unknown` (see `../VpcLattice/internal.ts`). |
| 42 | * |
| 43 | * If the bounded schedule is exhausted while the resource is still |
| 44 | * `*_IN_PROGRESS`, the last observation is returned as-is so the caller sees |
| 45 | * the real (still-converging) resource rather than a spurious failure. |
| 46 | */ |
| 47 | export const waitForSettled = < |
| 48 | A extends { status?: string }, |
| 49 | E extends { readonly _tag: string }, |
| 50 | R, |
| 51 | >( |
| 52 | resourceId: string, |
| 53 | read: Effect.Effect<A | undefined, E, R>, |
| 54 | ): Effect.Effect<A | undefined, E | QuickSightOperationFailed, R> => |
| 55 | Effect.flatMap( |
| 56 | Effect.repeat(read, { |
| 57 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(30)]), |
| 58 | until: (observed) => observed === undefined || !isInProgress(observed.status), |
| 59 | }), |
| 60 | (observed): Effect.Effect<A | undefined, QuickSightOperationFailed> => |
| 61 | observed !== undefined && isFailed(observed.status) |
| 62 | ? Effect.fail( |
| 63 | new QuickSightOperationFailed({ |
| 64 | resourceId, |
| 65 | status: observed.status!, |
| 66 | reason: undefined, |
| 67 | }), |
| 68 | ) |
| 69 | : Effect.succeed(observed), |
| 70 | ); |
| 71 | |
| 72 | /** |
| 73 | * Convert a QuickSight wire tag list into a plain record. |
| 74 | */ |
| 75 | export const toTagRecord = ( |
| 76 | tags: ReadonlyArray<{ Key?: string; Value?: string }> | undefined, |
| 77 | ): Record<string, string> => |
| 78 | Object.fromEntries( |
| 79 | (tags ?? []) |
| 80 | .filter( |
| 81 | (tag): tag is { Key: string; Value: string } => |
| 82 | typeof tag.Key === "string" && typeof tag.Value === "string", |
| 83 | ) |
| 84 | .map((tag) => [tag.Key, tag.Value]), |
| 85 | ); |
| 86 | |
| 87 | /** |
| 88 | * Read the observed tags of a QuickSight resource by ARN. Best-effort — a |
| 89 | * failure (e.g. a race with deletion) reports no tags. |
| 90 | */ |
| 91 | export const readQuickSightTags = Effect.fn(function* (arn: string) { |
| 92 | const response = yield* quicksight |
| 93 | .listTagsForResource({ ResourceArn: arn }) |
| 94 | .pipe(Effect.orElseSucceed(() => undefined)); |
| 95 | return toTagRecord(response?.Tags); |
| 96 | }); |
| 97 | |
| 98 | /** |
| 99 | * Sync tags on a QuickSight resource: diff OBSERVED cloud tags against the |
| 100 | * desired set and apply only the delta. |
| 101 | */ |
| 102 | export const syncQuickSightTags = Effect.fn(function* ( |
| 103 | arn: string, |
| 104 | desiredTags: Record<string, string>, |
| 105 | ) { |
| 106 | const observedTags = yield* readQuickSightTags(arn); |
| 107 | const { removed, upsert } = diffTags(observedTags, desiredTags); |
| 108 | if (upsert.length > 0) { |
| 109 | yield* quicksight.tagResource({ ResourceArn: arn, Tags: upsert }); |
| 110 | } |
| 111 | if (removed.length > 0) { |
| 112 | yield* quicksight.untagResource({ ResourceArn: arn, TagKeys: removed }); |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | /** |
| 117 | * Convert a desired tag record into the wire tag list for create calls. |
| 118 | * QuickSight requires a non-empty `Tags` list when supplied, so an empty |
| 119 | * record maps to `undefined`. |
| 120 | */ |
| 121 | export const toWireTags = (tags: Record<string, string>): quicksight.Tag[] | undefined => { |
| 122 | const entries = Object.entries(tags); |
| 123 | return entries.length > 0 ? entries.map(([Key, Value]) => ({ Key, Value })) : undefined; |
| 124 | }; |