The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as sitewise from "@distilled.cloud/aws/iotsitewise"; |
| 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, 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 { fetchSiteWiseTags, matchesDesired, syncSiteWiseTags } from "./internal.ts"; |
| 13 | |
| 14 | export class IoTSiteWiseGatewayConflict extends Data.TaggedError("IoTSiteWiseGatewayConflict")<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export class IoTSiteWiseGatewayInvalidProps extends Data.TaggedError( |
| 20 | "IoTSiteWiseGatewayInvalidProps", |
| 21 | )<{ |
| 22 | readonly message: string; |
| 23 | readonly cause?: unknown; |
| 24 | }> {} |
| 25 | |
| 26 | export class IoTSiteWiseGatewayUnexpectedResponse extends Data.TaggedError( |
| 27 | "IoTSiteWiseGatewayUnexpectedResponse", |
| 28 | )<{ |
| 29 | readonly message: string; |
| 30 | readonly cause?: unknown; |
| 31 | }> {} |
| 32 | |
| 33 | export type GatewayPlatform = sitewise.GatewayPlatform; |
| 34 | |
| 35 | export interface GatewayProps { |
| 36 | /** |
| 37 | * A unique name for the gateway. |
| 38 | * @default ${app}-${stage}-${id} |
| 39 | */ |
| 40 | gatewayName?: string; |
| 41 | /** |
| 42 | * The gateway's platform: exactly one of `greengrassV2` (an IoT |
| 43 | * Greengrass V2 core device), `siemensIE` (a Siemens Industrial Edge |
| 44 | * device), or the legacy `greengrass` group. Changing the platform |
| 45 | * replaces the gateway. |
| 46 | */ |
| 47 | gatewayPlatform: GatewayPlatform; |
| 48 | /** |
| 49 | * The version of the gateway to create (`"2.0"` or `"3.0"` for |
| 50 | * Greengrass V2 platforms). Changing the version replaces the gateway. |
| 51 | */ |
| 52 | gatewayVersion?: string; |
| 53 | /** |
| 54 | * Tags to associate with the gateway. |
| 55 | */ |
| 56 | tags?: Record<string, string>; |
| 57 | } |
| 58 | |
| 59 | export interface Gateway extends Resource< |
| 60 | "AWS.IoTSiteWise.Gateway", |
| 61 | GatewayProps, |
| 62 | { |
| 63 | /** |
| 64 | * Service-assigned UUID of the gateway. |
| 65 | */ |
| 66 | gatewayId: string; |
| 67 | /** |
| 68 | * ARN of the gateway. |
| 69 | */ |
| 70 | gatewayArn: string; |
| 71 | /** |
| 72 | * The gateway's name. |
| 73 | */ |
| 74 | gatewayName: string; |
| 75 | /** |
| 76 | * The gateway's platform, as reported by the service. |
| 77 | */ |
| 78 | gatewayPlatform: GatewayPlatform | undefined; |
| 79 | /** |
| 80 | * The gateway's version, as reported by the service. |
| 81 | */ |
| 82 | gatewayVersion: string | undefined; |
| 83 | /** |
| 84 | * Current tags reported for the gateway. |
| 85 | */ |
| 86 | tags: Record<string, string>; |
| 87 | }, |
| 88 | never, |
| 89 | Providers |
| 90 | > {} |
| 91 | |
| 92 | /** |
| 93 | * An AWS IoT SiteWise gateway — the ingestion point that connects |
| 94 | * on-premises industrial data sources (e.g. OPC UA servers) to IoT |
| 95 | * SiteWise, hosted on an IoT Greengrass V2 core device or a Siemens |
| 96 | * Industrial Edge device. |
| 97 | * |
| 98 | * The cloud-side gateway resource registers immediately; the edge |
| 99 | * software syncs to it asynchronously once the referenced core device is |
| 100 | * online (the device does not need to exist to create the gateway). |
| 101 | * |
| 102 | * ### Creating Gateways |
| 103 | * **Example:** Greengrass V2 Gateway |
| 104 | * ```typescript |
| 105 | * import * as AWS from "@alchemy.run/aws"; |
| 106 | * |
| 107 | * const gateway = yield* AWS.IoTSiteWise.Gateway("FactoryGateway", { |
| 108 | * gatewayPlatform: { |
| 109 | * greengrassV2: { coreDeviceThingName: "FactoryCoreDevice" }, |
| 110 | * }, |
| 111 | * }); |
| 112 | * ``` |
| 113 | * |
| 114 | * @resource |
| 115 | */ |
| 116 | export const Gateway = Resource<Gateway>("AWS.IoTSiteWise.Gateway"); |
| 117 | |
| 118 | const createGatewayName = (id: string, props: { gatewayName?: string | undefined }) => |
| 119 | props.gatewayName |
| 120 | ? Effect.succeed(props.gatewayName) |
| 121 | : createPhysicalName({ id, maxLength: 256 }); |
| 122 | |
| 123 | interface GatewayState { |
| 124 | attrs: Gateway["Attributes"]; |
| 125 | described: sitewise.DescribeGatewayResponse; |
| 126 | } |
| 127 | |
| 128 | const readGatewayById = Effect.fn(function* (gatewayId: string) { |
| 129 | const described = yield* sitewise |
| 130 | .describeGateway({ gatewayId }) |
| 131 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 132 | if (!described) return undefined; |
| 133 | const state: GatewayState = { |
| 134 | described, |
| 135 | attrs: { |
| 136 | gatewayId: described.gatewayId, |
| 137 | gatewayArn: described.gatewayArn, |
| 138 | gatewayName: described.gatewayName, |
| 139 | gatewayPlatform: described.gatewayPlatform, |
| 140 | gatewayVersion: described.gatewayVersion, |
| 141 | tags: yield* fetchSiteWiseTags(described.gatewayArn), |
| 142 | }, |
| 143 | }; |
| 144 | return state; |
| 145 | }); |
| 146 | |
| 147 | const findGatewayByName = Effect.fn(function* (name: string) { |
| 148 | const summaries = yield* sitewise.listGateways.pages({}).pipe( |
| 149 | EffectStream.runCollect, |
| 150 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.gatewaySummaries)), |
| 151 | ); |
| 152 | const match = summaries.find((summary) => summary.gatewayName === name); |
| 153 | if (!match) return undefined; |
| 154 | return yield* readGatewayById(match.gatewayId); |
| 155 | }); |
| 156 | |
| 157 | export const GatewayProvider = () => |
| 158 | Provider.effect( |
| 159 | Gateway, |
| 160 | Effect.gen(function* () { |
| 161 | return { |
| 162 | stables: ["gatewayId", "gatewayArn"], |
| 163 | list: () => |
| 164 | Effect.gen(function* () { |
| 165 | const summaries = yield* sitewise.listGateways.pages({}).pipe( |
| 166 | EffectStream.runCollect, |
| 167 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.gatewaySummaries)), |
| 168 | ); |
| 169 | const hydrated = yield* Effect.forEach( |
| 170 | summaries.map((summary) => summary.gatewayId), |
| 171 | (gatewayId) => readGatewayById(gatewayId), |
| 172 | { concurrency: 5 }, |
| 173 | ); |
| 174 | return hydrated.flatMap((state) => (state === undefined ? [] : [state.attrs])); |
| 175 | }), |
| 176 | read: Effect.fn(function* ({ id, olds, output }) { |
| 177 | const state = output?.gatewayId |
| 178 | ? yield* readGatewayById(output.gatewayId) |
| 179 | : yield* findGatewayByName(yield* createGatewayName(id, olds ?? {})); |
| 180 | if (!state) return undefined; |
| 181 | return (yield* hasAlchemyTags(id, state.attrs.tags as Tags)) |
| 182 | ? state.attrs |
| 183 | : Unowned(state.attrs); |
| 184 | }), |
| 185 | diff: Effect.fn(function* ({ news, olds }) { |
| 186 | if (!isResolved(news)) return; |
| 187 | if (olds === undefined) return; |
| 188 | // Platform and version are fixed at creation; only the name is |
| 189 | // mutable via UpdateGateway. |
| 190 | if ( |
| 191 | !matchesDesired(news.gatewayPlatform, olds.gatewayPlatform) || |
| 192 | !matchesDesired(olds.gatewayPlatform, news.gatewayPlatform) || |
| 193 | (news.gatewayVersion !== undefined && news.gatewayVersion !== olds.gatewayVersion) |
| 194 | ) { |
| 195 | return { action: "replace" } as const; |
| 196 | } |
| 197 | }), |
| 198 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 199 | if (!news) { |
| 200 | return yield* new IoTSiteWiseGatewayInvalidProps({ |
| 201 | message: "IoT SiteWise Gateway requires props", |
| 202 | }); |
| 203 | } |
| 204 | const name = yield* createGatewayName(id, news); |
| 205 | const internalTags = yield* createInternalTags(id); |
| 206 | const desiredTags = { ...internalTags, ...news.tags }; |
| 207 | |
| 208 | // Observe — prefer the cached id; fall back to a name lookup. |
| 209 | let state = output?.gatewayId |
| 210 | ? yield* readGatewayById(output.gatewayId) |
| 211 | : yield* findGatewayByName(name); |
| 212 | |
| 213 | // Ensure — create if missing (gateway creation is synchronous). |
| 214 | if (state === undefined) { |
| 215 | const created = yield* sitewise |
| 216 | .createGateway({ |
| 217 | gatewayName: name, |
| 218 | gatewayPlatform: news.gatewayPlatform, |
| 219 | gatewayVersion: news.gatewayVersion, |
| 220 | tags: desiredTags, |
| 221 | }) |
| 222 | .pipe( |
| 223 | // A concurrent create of the same name is a race — adopt it. |
| 224 | Effect.catchTag("ResourceAlreadyExistsException", () => |
| 225 | Effect.gen(function* () { |
| 226 | const existing = yield* findGatewayByName(name); |
| 227 | if (existing === undefined) { |
| 228 | return yield* new IoTSiteWiseGatewayConflict({ |
| 229 | message: `gateway '${name}' already exists but was not found by name`, |
| 230 | }); |
| 231 | } |
| 232 | return { |
| 233 | gatewayId: existing.attrs.gatewayId, |
| 234 | gatewayArn: existing.attrs.gatewayArn, |
| 235 | }; |
| 236 | }), |
| 237 | ), |
| 238 | ); |
| 239 | yield* session.note(`Created gateway ${name} (${created.gatewayId})`); |
| 240 | state = yield* readGatewayById(created.gatewayId); |
| 241 | if (state === undefined) { |
| 242 | return yield* new IoTSiteWiseGatewayUnexpectedResponse({ |
| 243 | message: `failed to read created gateway ${name}`, |
| 244 | }); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Sync — only the name is mutable. |
| 249 | if (name !== state.described.gatewayName) { |
| 250 | yield* sitewise.updateGateway({ |
| 251 | gatewayId: state.attrs.gatewayId, |
| 252 | gatewayName: name, |
| 253 | }); |
| 254 | yield* session.note(`Renamed gateway to ${name}`); |
| 255 | } |
| 256 | |
| 257 | // Sync tags — diff against observed cloud tags. |
| 258 | yield* syncSiteWiseTags(state.attrs.gatewayArn, state.attrs.tags, desiredTags); |
| 259 | |
| 260 | yield* session.note(state.attrs.gatewayArn); |
| 261 | |
| 262 | const final = yield* readGatewayById(state.attrs.gatewayId); |
| 263 | if (!final) { |
| 264 | return yield* new IoTSiteWiseGatewayUnexpectedResponse({ |
| 265 | message: `failed to read reconciled gateway ${name}`, |
| 266 | }); |
| 267 | } |
| 268 | return final.attrs; |
| 269 | }), |
| 270 | delete: Effect.fn(function* ({ output }) { |
| 271 | yield* sitewise |
| 272 | .deleteGateway({ gatewayId: output.gatewayId }) |
| 273 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 274 | }), |
| 275 | }; |
| 276 | }), |
| 277 | ); |