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
| 1 | import * as synthetics from "@distilled.cloud/aws/synthetics"; |
| 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, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | |
| 13 | export interface GroupProps { |
| 14 | /** |
| 15 | * Name of the group. Must match `^[0-9a-zA-Z_-]+$` and be at most 64 |
| 16 | * characters. Group names must be unique in the account (groups are |
| 17 | * global resources visible from every Region). |
| 18 | * @default ${app}-${id}-${stage}-${suffix} |
| 19 | */ |
| 20 | groupName?: string; |
| 21 | /** |
| 22 | * ARNs of the canaries associated with this group (as many as 10). The |
| 23 | * canaries must exist in the Region the group is managed from; membership |
| 24 | * is converged on every deploy (canaries added here are associated, |
| 25 | * canaries removed are disassociated). |
| 26 | */ |
| 27 | members?: string[]; |
| 28 | /** |
| 29 | * Tags to apply to the group. Merged with internal Alchemy tags. |
| 30 | */ |
| 31 | tags?: Record<string, string>; |
| 32 | } |
| 33 | |
| 34 | export interface Group extends Resource< |
| 35 | "AWS.Synthetics.Group", |
| 36 | GroupProps, |
| 37 | { |
| 38 | /** |
| 39 | * Physical name of the group. |
| 40 | */ |
| 41 | groupName: string; |
| 42 | /** |
| 43 | * ARN of the group. The ARN reflects the home Region the group was |
| 44 | * created in, but the group itself is a global resource. |
| 45 | */ |
| 46 | groupArn: string; |
| 47 | /** |
| 48 | * Service-assigned unique ID of the group. |
| 49 | */ |
| 50 | groupId: string; |
| 51 | }, |
| 52 | never, |
| 53 | Providers |
| 54 | > {} |
| 55 | |
| 56 | /** |
| 57 | * A CloudWatch Synthetics group — associates canaries (including |
| 58 | * cross-Region canaries) so you can view aggregated run results and manage |
| 59 | * them as a unit. A group can hold as many as 10 canaries, and an account |
| 60 | * can have as many as 20 groups. |
| 61 | * ### Creating Groups |
| 62 | * **Example:** Group of Canaries |
| 63 | * ```typescript |
| 64 | * import * as Synthetics from "@alchemy.run/aws/Synthetics"; |
| 65 | * |
| 66 | * const group = yield* Synthetics.Group("ApiCanaries", { |
| 67 | * members: [checkoutCanary.canaryArn, searchCanary.canaryArn], |
| 68 | * }); |
| 69 | * ``` |
| 70 | * |
| 71 | * **Example:** Empty Group with Tags |
| 72 | * ```typescript |
| 73 | * const group = yield* Synthetics.Group("Fleet", { |
| 74 | * tags: { team: "platform" }, |
| 75 | * }); |
| 76 | * ``` |
| 77 | * |
| 78 | * @resource |
| 79 | */ |
| 80 | export const Group = Resource<Group>("AWS.Synthetics.Group"); |
| 81 | |
| 82 | export const GroupProvider = () => |
| 83 | Provider.effect( |
| 84 | Group, |
| 85 | Effect.gen(function* () { |
| 86 | const createGroupName = Effect.fn(function* ( |
| 87 | id: string, |
| 88 | props: { groupName?: string | undefined }, |
| 89 | ) { |
| 90 | if (props.groupName) return props.groupName; |
| 91 | return yield* createPhysicalName({ id, maxLength: 64 }); |
| 92 | }); |
| 93 | |
| 94 | const getGroupOrUndefined = Effect.fn(function* (identifier: string) { |
| 95 | return yield* synthetics.getGroup({ GroupIdentifier: identifier }).pipe( |
| 96 | Effect.map((r) => r.Group), |
| 97 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | const listMembers = Effect.fn(function* (identifier: string) { |
| 102 | const pages = yield* synthetics.listGroupResources |
| 103 | .pages({ GroupIdentifier: identifier }) |
| 104 | .pipe(Stream.runCollect); |
| 105 | return Array.from(pages).flatMap((page) => page.Resources ?? []); |
| 106 | }); |
| 107 | |
| 108 | const filterTags = ( |
| 109 | tags: { [key: string]: string | undefined } | undefined, |
| 110 | ): Record<string, string> => |
| 111 | Object.fromEntries( |
| 112 | Object.entries(tags ?? {}).filter( |
| 113 | (entry): entry is [string, string] => entry[1] !== undefined, |
| 114 | ), |
| 115 | ); |
| 116 | |
| 117 | const toAttributes = (groupName: string, group: synthetics.Group): Group["Attributes"] => ({ |
| 118 | groupName, |
| 119 | groupArn: group.Arn ?? "", |
| 120 | groupId: group.Id ?? "", |
| 121 | }); |
| 122 | |
| 123 | return Group.Provider.of({ |
| 124 | stables: ["groupName", "groupArn", "groupId"], |
| 125 | |
| 126 | list: () => |
| 127 | Effect.gen(function* () { |
| 128 | const pages = yield* synthetics.listGroups.pages({}).pipe(Stream.runCollect); |
| 129 | return Array.from(pages) |
| 130 | .flatMap((page) => page.Groups ?? []) |
| 131 | .filter((group) => group.Name !== undefined) |
| 132 | .map((group) => ({ |
| 133 | groupName: group.Name!, |
| 134 | groupArn: group.Arn ?? "", |
| 135 | groupId: group.Id ?? "", |
| 136 | })); |
| 137 | }), |
| 138 | |
| 139 | read: Effect.fn(function* ({ id, olds, output }) { |
| 140 | const groupName = output?.groupName ?? (yield* createGroupName(id, olds ?? {})); |
| 141 | const group = yield* getGroupOrUndefined(groupName); |
| 142 | if (group === undefined) return undefined; |
| 143 | const attrs = toAttributes(groupName, group); |
| 144 | const tags = filterTags(group.Tags); |
| 145 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 146 | }), |
| 147 | |
| 148 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 149 | if (!isResolved(news)) return undefined; |
| 150 | const oldName = yield* createGroupName(id, olds); |
| 151 | const newName = yield* createGroupName(id, news); |
| 152 | if (oldName !== newName) { |
| 153 | return { action: "replace" } as const; |
| 154 | } |
| 155 | // members and tags converge through reconcile |
| 156 | }), |
| 157 | |
| 158 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 159 | const groupName = output?.groupName ?? (yield* createGroupName(id, news)); |
| 160 | const internalTags = yield* createInternalTags(id); |
| 161 | const desiredTags = { ...news.tags, ...internalTags }; |
| 162 | |
| 163 | // 1. OBSERVE — cloud state is authoritative. |
| 164 | let observed = yield* getGroupOrUndefined(groupName); |
| 165 | |
| 166 | // 2. ENSURE — create if missing; tolerate a concurrent-create race. |
| 167 | if (observed === undefined) { |
| 168 | yield* synthetics |
| 169 | .createGroup({ Name: groupName, Tags: desiredTags }) |
| 170 | .pipe(Effect.catchTag("ConflictException", () => Effect.void)); |
| 171 | observed = yield* getGroupOrUndefined(groupName); |
| 172 | yield* session.note(`created group ${groupName}`); |
| 173 | } |
| 174 | const groupArn = observed?.Arn ?? ""; |
| 175 | |
| 176 | // 3. SYNC members — diff observed canary associations against the |
| 177 | // desired members and apply only the delta. Association calls are |
| 178 | // idempotent per (group, canary) pair. |
| 179 | const observedMembers = yield* listMembers(groupName); |
| 180 | const desiredMembers = news.members ?? []; |
| 181 | const toAssociate = desiredMembers.filter((arn) => !observedMembers.includes(arn)); |
| 182 | const toDisassociate = observedMembers.filter((arn) => !desiredMembers.includes(arn)); |
| 183 | for (const ResourceArn of toAssociate) { |
| 184 | yield* synthetics.associateResource({ GroupIdentifier: groupName, ResourceArn }).pipe( |
| 185 | Effect.retry({ |
| 186 | while: (e): boolean => e._tag === "ConflictException", |
| 187 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]), |
| 188 | }), |
| 189 | ); |
| 190 | } |
| 191 | for (const ResourceArn of toDisassociate) { |
| 192 | yield* synthetics |
| 193 | .disassociateResource({ |
| 194 | GroupIdentifier: groupName, |
| 195 | ResourceArn, |
| 196 | }) |
| 197 | .pipe( |
| 198 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 199 | Effect.retry({ |
| 200 | while: (e): boolean => e._tag === "ConflictException", |
| 201 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]), |
| 202 | }), |
| 203 | ); |
| 204 | } |
| 205 | if (toAssociate.length > 0 || toDisassociate.length > 0) { |
| 206 | yield* session.note( |
| 207 | `synced group ${groupName} members (+${toAssociate.length}/-${toDisassociate.length})`, |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | // 4. SYNC tags — diff against OBSERVED cloud tags so adoption |
| 212 | // converges (create already applied them). |
| 213 | const currentTags = filterTags(observed?.Tags); |
| 214 | const { upsert, removed } = diffTags(currentTags, desiredTags); |
| 215 | if (upsert.length > 0) { |
| 216 | yield* synthetics.tagResource({ |
| 217 | ResourceArn: groupArn, |
| 218 | Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 219 | }); |
| 220 | } |
| 221 | if (removed.length > 0) { |
| 222 | yield* synthetics.untagResource({ |
| 223 | ResourceArn: groupArn, |
| 224 | TagKeys: removed, |
| 225 | }); |
| 226 | } |
| 227 | |
| 228 | yield* session.note(groupName); |
| 229 | return { |
| 230 | groupName, |
| 231 | groupArn, |
| 232 | groupId: observed?.Id ?? "", |
| 233 | }; |
| 234 | }), |
| 235 | |
| 236 | delete: Effect.fn(function* ({ output }) { |
| 237 | // The group does not need to be emptied first — deleting a group |
| 238 | // never deletes its canaries. |
| 239 | yield* synthetics.deleteGroup({ GroupIdentifier: output.groupName }).pipe( |
| 240 | Effect.retry({ |
| 241 | while: (e): boolean => e._tag === "ConflictException", |
| 242 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]), |
| 243 | }), |
| 244 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 245 | ); |
| 246 | }), |
| 247 | }); |
| 248 | }), |
| 249 | ); |