The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as iotfleetwise from "@distilled.cloud/aws/iotfleetwise"; |
| 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 Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | inFleetWiseRegion, |
| 14 | readFleetWiseTags, |
| 15 | retryObservation, |
| 16 | syncFleetWiseTags, |
| 17 | toFleetWiseTagList, |
| 18 | } from "./internal.ts"; |
| 19 | |
| 20 | export class IoTFleetWiseFleetUnexpectedResponse extends Data.TaggedError( |
| 21 | "IoTFleetWiseFleetUnexpectedResponse", |
| 22 | )<{ |
| 23 | readonly message: string; |
| 24 | readonly cause?: unknown; |
| 25 | }> {} |
| 26 | |
| 27 | export interface FleetProps { |
| 28 | /** |
| 29 | * ID of the fleet. Must be 1-100 characters of `[a-zA-Z0-9:_-]`. If |
| 30 | * omitted, a deterministic physical name is generated. Changing the ID |
| 31 | * replaces the fleet. |
| 32 | */ |
| 33 | fleetId?: string; |
| 34 | /** |
| 35 | * ARN of the {@link SignalCatalog} the fleet is associated with. |
| 36 | * Changing the signal catalog replaces the fleet. |
| 37 | */ |
| 38 | signalCatalogArn: string; |
| 39 | /** |
| 40 | * Human-readable description of the fleet. |
| 41 | */ |
| 42 | description?: string; |
| 43 | /** |
| 44 | * User-defined tags for the fleet. |
| 45 | */ |
| 46 | tags?: Record<string, string>; |
| 47 | } |
| 48 | |
| 49 | export interface Fleet extends Resource< |
| 50 | "AWS.IoTFleetWise.Fleet", |
| 51 | FleetProps, |
| 52 | { |
| 53 | /** The unique ID of the fleet. */ |
| 54 | fleetId: string; |
| 55 | /** The ARN of the fleet. */ |
| 56 | fleetArn: string; |
| 57 | /** The signal catalog associated with the fleet. */ |
| 58 | signalCatalogArn: string; |
| 59 | }, |
| 60 | never, |
| 61 | Providers |
| 62 | > {} |
| 63 | |
| 64 | /** |
| 65 | * An AWS IoT FleetWise fleet — a group of vehicles that campaigns can |
| 66 | * target collectively. |
| 67 | * |
| 68 | * Fleets are free, provisioned near-instantly, and only carry a |
| 69 | * description besides their signal-catalog association. AWS IoT FleetWise |
| 70 | * is allowlist-gated and offered in `us-east-1`/`eu-central-1` only. |
| 71 | * ### Creating a Fleet |
| 72 | * **Example:** Basic Fleet |
| 73 | * ```typescript |
| 74 | * const fleet = yield* Fleet("TestFleet", { |
| 75 | * signalCatalogArn: catalog.signalCatalogArn, |
| 76 | * description: "west-coast pilot vehicles", |
| 77 | * }); |
| 78 | * ``` |
| 79 | * |
| 80 | * @resource |
| 81 | */ |
| 82 | export const Fleet = Resource<Fleet>("AWS.IoTFleetWise.Fleet"); |
| 83 | |
| 84 | export const FleetProvider = () => |
| 85 | Provider.effect( |
| 86 | Fleet, |
| 87 | Effect.gen(function* () { |
| 88 | const toId = (id: string, props: { fleetId?: string }) => |
| 89 | props.fleetId ? Effect.succeed(props.fleetId) : createPhysicalName({ id, maxLength: 100 }); |
| 90 | |
| 91 | const readFleet = Effect.fn(function* (fleetId: string) { |
| 92 | return yield* iotfleetwise.getFleet({ fleetId }).pipe( |
| 93 | inFleetWiseRegion, |
| 94 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 95 | ); |
| 96 | }); |
| 97 | |
| 98 | const toAttrs = (fleet: iotfleetwise.GetFleetResponse) => ({ |
| 99 | fleetId: fleet.id, |
| 100 | fleetArn: fleet.arn, |
| 101 | signalCatalogArn: fleet.signalCatalogArn, |
| 102 | }); |
| 103 | |
| 104 | return { |
| 105 | stables: ["fleetId", "fleetArn"], |
| 106 | |
| 107 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 108 | if (!isResolved(news)) return undefined; |
| 109 | if ((yield* toId(id, olds)) !== (yield* toId(id, news))) { |
| 110 | return { action: "replace" } as const; |
| 111 | } |
| 112 | // The update API can only change the description. |
| 113 | if (news.signalCatalogArn !== olds.signalCatalogArn) { |
| 114 | return { action: "replace" } as const; |
| 115 | } |
| 116 | }), |
| 117 | |
| 118 | read: Effect.fn(function* ({ id, olds, output }) { |
| 119 | const fleetId = output?.fleetId ?? (yield* toId(id, olds ?? {})); |
| 120 | const found = yield* readFleet(fleetId); |
| 121 | if (found === undefined) return undefined; |
| 122 | const attrs = toAttrs(found); |
| 123 | const tags = yield* readFleetWiseTags(found.arn); |
| 124 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 125 | }), |
| 126 | |
| 127 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 128 | const fleetId = output?.fleetId ?? (yield* toId(id, news)); |
| 129 | const internalTags = yield* createInternalTags(id); |
| 130 | const desiredTags = { ...internalTags, ...news.tags }; |
| 131 | |
| 132 | // 1. Observe — cloud state is authoritative. |
| 133 | let observed = yield* readFleet(fleetId); |
| 134 | |
| 135 | // 2. Ensure — create if missing; tolerate the AlreadyExists race. |
| 136 | if (observed === undefined) { |
| 137 | yield* iotfleetwise |
| 138 | .createFleet({ |
| 139 | fleetId, |
| 140 | signalCatalogArn: news.signalCatalogArn, |
| 141 | description: news.description, |
| 142 | tags: toFleetWiseTagList(desiredTags), |
| 143 | }) |
| 144 | .pipe( |
| 145 | inFleetWiseRegion, |
| 146 | Effect.catchTag("ConflictException", () => Effect.void), |
| 147 | ); |
| 148 | observed = yield* readFleet(fleetId).pipe( |
| 149 | Effect.flatMap((fleet) => |
| 150 | fleet === undefined |
| 151 | ? Effect.fail( |
| 152 | new IoTFleetWiseFleetUnexpectedResponse({ |
| 153 | message: `Fleet '${fleetId}' not found`, |
| 154 | }), |
| 155 | ) |
| 156 | : Effect.succeed(fleet), |
| 157 | ), |
| 158 | retryObservation, |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | // 3. Sync description — the only mutable aspect. |
| 163 | if (news.description !== undefined && news.description !== observed.description) { |
| 164 | yield* iotfleetwise |
| 165 | .updateFleet({ fleetId, description: news.description }) |
| 166 | .pipe(inFleetWiseRegion); |
| 167 | } |
| 168 | |
| 169 | // 3b. Sync tags against OBSERVED cloud tags. |
| 170 | yield* syncFleetWiseTags(observed.arn, desiredTags); |
| 171 | |
| 172 | yield* session.note(fleetId); |
| 173 | return toAttrs(observed); |
| 174 | }), |
| 175 | |
| 176 | delete: Effect.fn(function* ({ output }) { |
| 177 | // Idempotent: deleting a missing fleet succeeds (vehicles are |
| 178 | // detached automatically before deletion). |
| 179 | yield* iotfleetwise.deleteFleet({ fleetId: output.fleetId }).pipe(inFleetWiseRegion); |
| 180 | }), |
| 181 | |
| 182 | list: () => |
| 183 | iotfleetwise.listFleets.items({}).pipe( |
| 184 | Stream.runCollect, |
| 185 | Effect.map((chunk) => |
| 186 | Array.from(chunk).map((summary) => ({ |
| 187 | fleetId: summary.id, |
| 188 | fleetArn: summary.arn, |
| 189 | signalCatalogArn: summary.signalCatalogArn, |
| 190 | })), |
| 191 | ), |
| 192 | inFleetWiseRegion, |
| 193 | ), |
| 194 | }; |
| 195 | }), |
| 196 | ); |