The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as location from "@distilled.cloud/aws/location"; |
| 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, type Tags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as EffectStream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { toTagRecord } from "./internal.ts"; |
| 13 | |
| 14 | export class GeofenceCollectionUnexpectedResponse extends Data.TaggedError( |
| 15 | "GeofenceCollectionUnexpectedResponse", |
| 16 | )<{ |
| 17 | readonly message: string; |
| 18 | readonly cause?: unknown; |
| 19 | }> {} |
| 20 | |
| 21 | export interface GeofenceCollectionProps { |
| 22 | /** |
| 23 | * Name of the geofence collection. Immutable — changing it replaces the |
| 24 | * collection. |
| 25 | * @default ${app}-${stage}-${id} |
| 26 | */ |
| 27 | collectionName?: string; |
| 28 | /** |
| 29 | * KMS key ID used to encrypt the collection's geofence data. Immutable — |
| 30 | * changing it replaces the collection. |
| 31 | */ |
| 32 | kmsKeyId?: string; |
| 33 | /** |
| 34 | * Optional description of the geofence collection. |
| 35 | */ |
| 36 | description?: string; |
| 37 | /** |
| 38 | * Tags to associate with the geofence collection. |
| 39 | */ |
| 40 | tags?: Record<string, string>; |
| 41 | } |
| 42 | |
| 43 | export interface GeofenceCollection extends Resource< |
| 44 | "AWS.Location.GeofenceCollection", |
| 45 | GeofenceCollectionProps, |
| 46 | { |
| 47 | /** Physical name of the geofence collection. */ |
| 48 | collectionName: string; |
| 49 | /** ARN of the geofence collection. */ |
| 50 | collectionArn: string; |
| 51 | /** KMS key ID backing the collection, if configured. */ |
| 52 | kmsKeyId: string | undefined; |
| 53 | /** Description of the collection. */ |
| 54 | description: string | undefined; |
| 55 | /** Tags currently associated with the collection. */ |
| 56 | tags: Record<string, string>; |
| 57 | }, |
| 58 | never, |
| 59 | Providers |
| 60 | > {} |
| 61 | |
| 62 | /** |
| 63 | * An Amazon Location Service geofence collection. A geofence collection stores |
| 64 | * geofences and evaluates device positions against them. The KMS key is |
| 65 | * immutable; the description can be updated in place. |
| 66 | * |
| 67 | * ### Creating Geofence Collections |
| 68 | * **Example:** Basic Geofence Collection |
| 69 | * ```typescript |
| 70 | * import * as Location from "@alchemy.run/aws/Location"; |
| 71 | * |
| 72 | * const collection = yield* Location.GeofenceCollection("Fences", {}); |
| 73 | * ``` |
| 74 | * |
| 75 | * **Example:** Encrypted Geofence Collection |
| 76 | * ```typescript |
| 77 | * const collection = yield* Location.GeofenceCollection("SecureFences", { |
| 78 | * kmsKeyId: "alias/my-key", |
| 79 | * description: "Encrypted geofence collection", |
| 80 | * }); |
| 81 | * ``` |
| 82 | * |
| 83 | * @resource |
| 84 | */ |
| 85 | export const GeofenceCollection = Resource<GeofenceCollection>("AWS.Location.GeofenceCollection"); |
| 86 | |
| 87 | const createCollectionName = (id: string, props: { collectionName?: string | undefined }) => |
| 88 | Effect.gen(function* () { |
| 89 | if (props.collectionName) return props.collectionName; |
| 90 | return yield* createPhysicalName({ id, maxLength: 100 }); |
| 91 | }); |
| 92 | |
| 93 | const readCollection = Effect.fn(function* (collectionName: string) { |
| 94 | const found = yield* location |
| 95 | .describeGeofenceCollection({ CollectionName: collectionName }) |
| 96 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 97 | if (!found) return undefined; |
| 98 | return { |
| 99 | collectionName: found.CollectionName, |
| 100 | collectionArn: found.CollectionArn, |
| 101 | kmsKeyId: found.KmsKeyId, |
| 102 | description: found.Description ? found.Description : undefined, |
| 103 | tags: toTagRecord(found.Tags), |
| 104 | } satisfies GeofenceCollection["Attributes"]; |
| 105 | }); |
| 106 | |
| 107 | export const GeofenceCollectionProvider = () => |
| 108 | Provider.effect( |
| 109 | GeofenceCollection, |
| 110 | Effect.gen(function* () { |
| 111 | return { |
| 112 | stables: ["collectionName", "collectionArn"], |
| 113 | list: () => |
| 114 | Effect.gen(function* () { |
| 115 | const names = yield* location.listGeofenceCollections.pages({}).pipe( |
| 116 | EffectStream.runCollect, |
| 117 | Effect.map((chunk) => |
| 118 | Array.from(chunk).flatMap((page) => |
| 119 | (page.Entries ?? []).map((entry) => entry.CollectionName), |
| 120 | ), |
| 121 | ), |
| 122 | ); |
| 123 | const hydrated = yield* Effect.forEach(names, (name) => readCollection(name), { |
| 124 | concurrency: 10, |
| 125 | }); |
| 126 | return hydrated.filter( |
| 127 | (attrs): attrs is GeofenceCollection["Attributes"] => attrs !== undefined, |
| 128 | ); |
| 129 | }), |
| 130 | read: Effect.fn(function* ({ id, olds, output }) { |
| 131 | const collectionName = |
| 132 | output?.collectionName ?? (yield* createCollectionName(id, olds ?? {})); |
| 133 | const state = yield* readCollection(collectionName); |
| 134 | if (!state) return undefined; |
| 135 | return (yield* hasAlchemyTags(id, state.tags as Tags)) ? state : Unowned(state); |
| 136 | }), |
| 137 | diff: Effect.fn(function* ({ id, news = {}, olds = {} }) { |
| 138 | if (!isResolved(news)) return; |
| 139 | const oldName = yield* createCollectionName(id, olds); |
| 140 | const newName = yield* createCollectionName(id, news); |
| 141 | if (oldName !== newName || olds.kmsKeyId !== news.kmsKeyId) { |
| 142 | return { action: "replace" } as const; |
| 143 | } |
| 144 | }), |
| 145 | reconcile: Effect.fn(function* ({ id, news = {}, output, session }) { |
| 146 | const collectionName = output?.collectionName ?? (yield* createCollectionName(id, news)); |
| 147 | const internalTags = yield* createInternalTags(id); |
| 148 | const desiredTags = { ...internalTags, ...news.tags }; |
| 149 | |
| 150 | let state = yield* readCollection(collectionName); |
| 151 | |
| 152 | if (state === undefined) { |
| 153 | yield* location |
| 154 | .createGeofenceCollection({ |
| 155 | CollectionName: collectionName, |
| 156 | KmsKeyId: news.kmsKeyId, |
| 157 | Description: news.description, |
| 158 | Tags: desiredTags, |
| 159 | }) |
| 160 | .pipe(Effect.catchTag("ConflictException", () => Effect.void)); |
| 161 | state = yield* readCollection(collectionName); |
| 162 | if (state === undefined) { |
| 163 | return yield* new GeofenceCollectionUnexpectedResponse({ |
| 164 | message: `failed to read created geofence collection ${collectionName}`, |
| 165 | }); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (state.description !== (news.description ?? undefined)) { |
| 170 | yield* location.updateGeofenceCollection({ |
| 171 | CollectionName: collectionName, |
| 172 | Description: news.description, |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | const { removed, upsert } = diffTags(state.tags, desiredTags); |
| 177 | if (removed.length > 0) { |
| 178 | yield* location.untagResource({ |
| 179 | ResourceArn: state.collectionArn, |
| 180 | TagKeys: removed, |
| 181 | }); |
| 182 | } |
| 183 | if (upsert.length > 0) { |
| 184 | yield* location.tagResource({ |
| 185 | ResourceArn: state.collectionArn, |
| 186 | Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | yield* session.note(state.collectionArn); |
| 191 | |
| 192 | const final = yield* readCollection(collectionName); |
| 193 | if (!final) { |
| 194 | return yield* new GeofenceCollectionUnexpectedResponse({ |
| 195 | message: `failed to read reconciled geofence collection ${collectionName}`, |
| 196 | }); |
| 197 | } |
| 198 | return final; |
| 199 | }), |
| 200 | delete: Effect.fn(function* ({ output }) { |
| 201 | yield* location |
| 202 | .deleteGeofenceCollection({ |
| 203 | CollectionName: output.collectionName, |
| 204 | }) |
| 205 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 206 | }), |
| 207 | }; |
| 208 | }), |
| 209 | ); |