The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as mq from "@distilled.cloud/aws/mq"; |
| 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 type { Providers } from "../Providers.ts"; |
| 11 | import { syncMqTags, toTagRecord } from "./internal.ts"; |
| 12 | |
| 13 | export class ConfigurationUnexpectedResponse extends Data.TaggedError( |
| 14 | "ConfigurationUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export interface ConfigurationProps { |
| 21 | /** |
| 22 | * Name of the configuration (1-150 chars, `a-z A-Z 0-9 . _ ~ -`). If |
| 23 | * omitted a deterministic physical name is generated. The name is |
| 24 | * immutable — changing it replaces the configuration. |
| 25 | */ |
| 26 | configurationName?: string; |
| 27 | /** |
| 28 | * Broker engine the configuration targets. Immutable — changing it |
| 29 | * replaces the configuration. |
| 30 | */ |
| 31 | engineType: "ACTIVEMQ" | "RABBITMQ"; |
| 32 | /** |
| 33 | * Broker engine version the configuration targets (e.g. `"5.18"` for |
| 34 | * ActiveMQ, `"3.13"` for RabbitMQ). Immutable — changing it replaces the |
| 35 | * configuration. |
| 36 | */ |
| 37 | engineVersion?: string; |
| 38 | /** |
| 39 | * Authentication strategy for the associated broker. Immutable — |
| 40 | * changing it replaces the configuration. |
| 41 | * @default "SIMPLE" |
| 42 | */ |
| 43 | authenticationStrategy?: "SIMPLE" | "LDAP"; |
| 44 | /** |
| 45 | * The broker configuration document as plain text (ActiveMQ XML or |
| 46 | * RabbitMQ Cuttlefish). Alchemy base64-encodes it for the API. Supplying |
| 47 | * (or changing) `data` publishes a new configuration revision. Omitting |
| 48 | * it keeps the engine's default revision. |
| 49 | */ |
| 50 | data?: string; |
| 51 | /** |
| 52 | * Description recorded on the published revision. Only applied when `data` |
| 53 | * is also supplied (the API updates data + description together). |
| 54 | */ |
| 55 | description?: string; |
| 56 | /** |
| 57 | * User-defined tags for the configuration. |
| 58 | */ |
| 59 | tags?: Record<string, string>; |
| 60 | } |
| 61 | |
| 62 | export interface Configuration extends Resource< |
| 63 | "AWS.MQ.Configuration", |
| 64 | ConfigurationProps, |
| 65 | { |
| 66 | /** Server-assigned unique id of the configuration (e.g. `c-1234...`). */ |
| 67 | configurationId: string; |
| 68 | /** ARN of the configuration. */ |
| 69 | configurationArn: string; |
| 70 | /** Name of the configuration. */ |
| 71 | configurationName: string; |
| 72 | /** Latest published revision number. */ |
| 73 | configurationRevision: number; |
| 74 | /** Broker engine the configuration targets (`ACTIVEMQ` or `RABBITMQ`). */ |
| 75 | engineType: string; |
| 76 | /** Engine version the configuration targets. */ |
| 77 | engineVersion: string | undefined; |
| 78 | }, |
| 79 | never, |
| 80 | Providers |
| 81 | > {} |
| 82 | |
| 83 | /** |
| 84 | * An Amazon MQ broker configuration — a versioned document (ActiveMQ XML or |
| 85 | * RabbitMQ Cuttlefish) that a {@link Broker} can reference to control |
| 86 | * engine-level settings. Each edit to `data` publishes a new immutable |
| 87 | * revision; a broker pins a specific `{ id, revision }` pair. |
| 88 | * |
| 89 | * ### Creating a Configuration |
| 90 | * **Example:** Default ActiveMQ Configuration |
| 91 | * ```typescript |
| 92 | * const config = yield* MQ.Configuration("BrokerConfig", { |
| 93 | * engineType: "ACTIVEMQ", |
| 94 | * engineVersion: "5.18", |
| 95 | * }); |
| 96 | * ``` |
| 97 | * |
| 98 | * **Example:** Custom ActiveMQ Configuration Document |
| 99 | * ```typescript |
| 100 | * const config = yield* MQ.Configuration("BrokerConfig", { |
| 101 | * engineType: "ACTIVEMQ", |
| 102 | * engineVersion: "5.18", |
| 103 | * description: "Enable statistics plugin", |
| 104 | * data: `<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 105 | * <broker xmlns="http://activemq.apache.org/schema/core"> |
| 106 | * <plugins> |
| 107 | * <statisticsBrokerPlugin/> |
| 108 | * </plugins> |
| 109 | * </broker>`, |
| 110 | * }); |
| 111 | * // config.configurationRevision -> 2 (the published revision) |
| 112 | * ``` |
| 113 | * |
| 114 | * ### Attaching to a Broker |
| 115 | * **Example:** Reference a Configuration from a Broker |
| 116 | * ```typescript |
| 117 | * const broker = yield* MQ.Broker("Orders", { |
| 118 | * engineType: "ACTIVEMQ", |
| 119 | * engineVersion: "5.18", |
| 120 | * hostInstanceType: "mq.t3.micro", |
| 121 | * users: [{ username: "admin", password: Redacted.make("SuperSecretPassw0rd") }], |
| 122 | * configuration: { |
| 123 | * id: config.configurationId, |
| 124 | * revision: config.configurationRevision, |
| 125 | * }, |
| 126 | * }); |
| 127 | * ``` |
| 128 | * |
| 129 | * @resource |
| 130 | */ |
| 131 | export const Configuration = Resource<Configuration>("AWS.MQ.Configuration"); |
| 132 | |
| 133 | const encodeData = (data: string): Effect.Effect<string> => |
| 134 | Effect.sync(() => Buffer.from(data, "utf8").toString("base64")); |
| 135 | |
| 136 | const decodeData = (data: string | undefined): Effect.Effect<string> => |
| 137 | Effect.sync(() => (data === undefined ? "" : Buffer.from(data, "base64").toString("utf8"))); |
| 138 | |
| 139 | export const ConfigurationProvider = () => |
| 140 | Provider.effect( |
| 141 | Configuration, |
| 142 | Effect.gen(function* () { |
| 143 | const toName = (id: string, props: Partial<ConfigurationProps>) => |
| 144 | props.configurationName |
| 145 | ? Effect.succeed(props.configurationName) |
| 146 | : createPhysicalName({ id, maxLength: 150 }); |
| 147 | |
| 148 | /** Read a configuration by id; a missing configuration reads as absent. */ |
| 149 | const readConfiguration = Effect.fn(function* (configurationId: string) { |
| 150 | return yield* mq |
| 151 | .describeConfiguration({ ConfigurationId: configurationId }) |
| 152 | .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined)); |
| 153 | }); |
| 154 | |
| 155 | /** Enumerate all configurations (listConfigurations is not paginated). */ |
| 156 | const listAllConfigurations = Effect.fn(function* () { |
| 157 | const configs: mq.Configuration[] = []; |
| 158 | let nextToken: string | undefined; |
| 159 | // Bounded page walk — MQ terminates on an absent NextToken. |
| 160 | for (let page = 0; page < 20; page++) { |
| 161 | const response = yield* mq.listConfigurations({ |
| 162 | MaxResults: 100, |
| 163 | NextToken: nextToken, |
| 164 | }); |
| 165 | configs.push(...(response.Configurations ?? [])); |
| 166 | nextToken = response.NextToken; |
| 167 | if (!nextToken) break; |
| 168 | } |
| 169 | return configs; |
| 170 | }); |
| 171 | |
| 172 | /** Find a live configuration by name. */ |
| 173 | const findByName = Effect.fn(function* (name: string) { |
| 174 | const configs = yield* listAllConfigurations(); |
| 175 | const summary = configs.find((c) => c.Name === name && c.Id); |
| 176 | if (!summary?.Id) return undefined; |
| 177 | return yield* readConfiguration(summary.Id); |
| 178 | }); |
| 179 | |
| 180 | const toAttrs = (config: mq.DescribeConfigurationResponse) => ({ |
| 181 | configurationId: config.Id!, |
| 182 | configurationArn: config.Arn!, |
| 183 | configurationName: config.Name!, |
| 184 | configurationRevision: config.LatestRevision?.Revision ?? 1, |
| 185 | engineType: config.EngineType!, |
| 186 | engineVersion: config.EngineVersion, |
| 187 | }); |
| 188 | |
| 189 | return { |
| 190 | stables: ["configurationId", "configurationArn", "configurationName"], |
| 191 | |
| 192 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 193 | if (!isResolved(news)) return undefined; |
| 194 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 195 | return { action: "replace" } as const; |
| 196 | } |
| 197 | // Engine type, version, and auth strategy are fixed at creation. |
| 198 | if ((news.engineType ?? undefined) !== (olds?.engineType ?? undefined)) { |
| 199 | return { action: "replace" } as const; |
| 200 | } |
| 201 | if ((news.engineVersion ?? undefined) !== (olds?.engineVersion ?? undefined)) { |
| 202 | return { action: "replace" } as const; |
| 203 | } |
| 204 | if ( |
| 205 | (news.authenticationStrategy ?? undefined) !== |
| 206 | (olds?.authenticationStrategy ?? undefined) |
| 207 | ) { |
| 208 | return { action: "replace" } as const; |
| 209 | } |
| 210 | }), |
| 211 | |
| 212 | read: Effect.fn(function* ({ id, olds, output }) { |
| 213 | const config = output?.configurationId |
| 214 | ? yield* readConfiguration(output.configurationId) |
| 215 | : yield* findByName(yield* toName(id, olds ?? {})); |
| 216 | if (config === undefined) return undefined; |
| 217 | const attrs = toAttrs(config); |
| 218 | const tags = toTagRecord(config.Tags); |
| 219 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 220 | }), |
| 221 | |
| 222 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 223 | const name = output?.configurationName ?? (yield* toName(id, news)); |
| 224 | const internalTags = yield* createInternalTags(id); |
| 225 | const desiredTags = { ...internalTags, ...news.tags }; |
| 226 | |
| 227 | // 1. Observe — cloud state is authoritative. |
| 228 | let observed = output?.configurationId |
| 229 | ? yield* readConfiguration(output.configurationId) |
| 230 | : undefined; |
| 231 | if (observed === undefined) { |
| 232 | observed = yield* findByName(name); |
| 233 | } |
| 234 | |
| 235 | // 2. Ensure — create if missing. createConfiguration seeds an |
| 236 | // initial default revision; `data` (if any) is published below. |
| 237 | if (observed === undefined) { |
| 238 | const created = yield* mq.createConfiguration({ |
| 239 | Name: name, |
| 240 | EngineType: news.engineType, |
| 241 | EngineVersion: news.engineVersion, |
| 242 | AuthenticationStrategy: news.authenticationStrategy, |
| 243 | Tags: desiredTags, |
| 244 | }); |
| 245 | observed = yield* readConfiguration(created.Id!); |
| 246 | if (observed === undefined) { |
| 247 | return yield* new ConfigurationUnexpectedResponse({ |
| 248 | message: `MQ configuration '${name}' disappeared immediately after create`, |
| 249 | }); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | const configurationId = observed.Id!; |
| 254 | |
| 255 | // 3. Sync configuration document — publish a new revision only when |
| 256 | // the desired `data` differs from the current latest revision. |
| 257 | if (news.data !== undefined) { |
| 258 | const revision = observed.LatestRevision?.Revision; |
| 259 | const current = |
| 260 | revision === undefined |
| 261 | ? undefined |
| 262 | : yield* mq.describeConfigurationRevision({ |
| 263 | ConfigurationId: configurationId, |
| 264 | ConfigurationRevision: String(revision), |
| 265 | }); |
| 266 | const currentData = yield* decodeData(current?.Data); |
| 267 | if (currentData !== news.data) { |
| 268 | yield* mq.updateConfiguration({ |
| 269 | ConfigurationId: configurationId, |
| 270 | Data: yield* encodeData(news.data), |
| 271 | Description: news.description, |
| 272 | }); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 277 | yield* syncMqTags(observed.Arn!, toTagRecord(observed.Tags), desiredTags); |
| 278 | |
| 279 | // 4. Re-read for the fresh latest revision + tags. |
| 280 | const final = yield* readConfiguration(configurationId); |
| 281 | if (final === undefined) { |
| 282 | return yield* new ConfigurationUnexpectedResponse({ |
| 283 | message: `MQ configuration '${name}' disappeared while reconciling`, |
| 284 | }); |
| 285 | } |
| 286 | yield* session.note(name); |
| 287 | return toAttrs(final); |
| 288 | }), |
| 289 | |
| 290 | delete: Effect.fn(function* ({ output }) { |
| 291 | yield* mq |
| 292 | .deleteConfiguration({ ConfigurationId: output.configurationId }) |
| 293 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)); |
| 294 | }), |
| 295 | |
| 296 | list: () => |
| 297 | Effect.gen(function* () { |
| 298 | const configs = yield* listAllConfigurations(); |
| 299 | return configs.flatMap((c) => |
| 300 | c.Id !== undefined && c.Arn !== undefined && c.Name !== undefined |
| 301 | ? [ |
| 302 | { |
| 303 | configurationId: c.Id, |
| 304 | configurationArn: c.Arn, |
| 305 | configurationName: c.Name, |
| 306 | configurationRevision: c.LatestRevision?.Revision ?? 1, |
| 307 | engineType: c.EngineType!, |
| 308 | engineVersion: c.EngineVersion, |
| 309 | }, |
| 310 | ] |
| 311 | : [], |
| 312 | ); |
| 313 | }), |
| 314 | }; |
| 315 | }), |
| 316 | ); |