The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ds from "@distilled.cloud/aws/directory-service"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | |
| 9 | export class EventTopicUnexpectedResponse extends Data.TaggedError("EventTopicUnexpectedResponse")<{ |
| 10 | readonly message: string; |
| 11 | readonly cause?: unknown; |
| 12 | }> {} |
| 13 | |
| 14 | export interface EventTopicProps { |
| 15 | /** |
| 16 | * Id of the {@link Directory} whose status notifications are published. |
| 17 | * Changing the directory replaces the association. |
| 18 | */ |
| 19 | directoryId: string; |
| 20 | /** |
| 21 | * Name of the Amazon SNS topic (same account and region) the directory |
| 22 | * publishes status messages to. Changing the topic replaces the |
| 23 | * association. |
| 24 | */ |
| 25 | topicName: string; |
| 26 | } |
| 27 | |
| 28 | export interface EventTopic extends Resource< |
| 29 | "AWS.DirectoryService.EventTopic", |
| 30 | EventTopicProps, |
| 31 | { |
| 32 | /** The ID of the directory publishing status notifications. */ |
| 33 | directoryId: string; |
| 34 | /** The name of the SNS topic receiving status notifications. */ |
| 35 | topicName: string; |
| 36 | /** The ARN of the SNS topic receiving status notifications. */ |
| 37 | topicArn: string | undefined; |
| 38 | /** The status of the association, e.g. `Registered`. */ |
| 39 | status: string | undefined; |
| 40 | }, |
| 41 | never, |
| 42 | Providers |
| 43 | > {} |
| 44 | |
| 45 | /** |
| 46 | * An association between an AWS Directory Service {@link Directory} and an |
| 47 | * Amazon SNS topic. The directory publishes a status message to the topic |
| 48 | * whenever it changes stage — e.g. from `Active` to `Impaired` or |
| 49 | * `Inoperable`, and back to `Active` — which is Directory Service's native |
| 50 | * event mechanism. |
| 51 | * |
| 52 | * To consume the notifications from a Lambda function, use |
| 53 | * {@link consumeDirectoryStatus}, which creates this association and |
| 54 | * subscribes the function to the topic. |
| 55 | * ### Publishing Directory Status Notifications |
| 56 | * **Example:** Publish Status Changes to an SNS Topic |
| 57 | * ```typescript |
| 58 | * const topic = yield* SNS.Topic("DirectoryStatus", {}); |
| 59 | * const eventTopic = yield* DirectoryService.EventTopic("Status", { |
| 60 | * directoryId: directory.directoryId, |
| 61 | * topicName: topic.topicName, |
| 62 | * }); |
| 63 | * ``` |
| 64 | * |
| 65 | * @resource |
| 66 | */ |
| 67 | export const EventTopic = Resource<EventTopic>("AWS.DirectoryService.EventTopic"); |
| 68 | |
| 69 | export const EventTopicProvider = () => |
| 70 | Provider.effect( |
| 71 | EventTopic, |
| 72 | Effect.gen(function* () { |
| 73 | const readEventTopic = Effect.fn(function* (directoryId: string, topicName: string) { |
| 74 | const response = yield* ds |
| 75 | .describeEventTopics({ |
| 76 | DirectoryId: directoryId, |
| 77 | TopicNames: [topicName], |
| 78 | }) |
| 79 | .pipe(Effect.catchTag("EntityDoesNotExistException", () => Effect.undefined)); |
| 80 | return response?.EventTopics?.find((topic) => topic.TopicName === topicName); |
| 81 | }); |
| 82 | |
| 83 | const toAttrs = (directoryId: string, topicName: string, topic: ds.EventTopic) => ({ |
| 84 | directoryId, |
| 85 | topicName, |
| 86 | topicArn: topic.TopicArn, |
| 87 | status: topic.Status, |
| 88 | }); |
| 89 | |
| 90 | return { |
| 91 | stables: ["directoryId", "topicName"], |
| 92 | |
| 93 | diff: Effect.fn(function* ({ olds, news }) { |
| 94 | if (!isResolved(news)) return undefined; |
| 95 | if (olds === undefined || news === undefined) return undefined; |
| 96 | if (news.directoryId !== olds.directoryId) { |
| 97 | return { action: "replace" } as const; |
| 98 | } |
| 99 | if (news.topicName !== olds.topicName) { |
| 100 | return { action: "replace" } as const; |
| 101 | } |
| 102 | }), |
| 103 | |
| 104 | read: Effect.fn(function* ({ olds, output }) { |
| 105 | const directoryId = output?.directoryId ?? olds?.directoryId; |
| 106 | const topicName = output?.topicName ?? olds?.topicName; |
| 107 | if (directoryId === undefined || topicName === undefined) { |
| 108 | return undefined; |
| 109 | } |
| 110 | const topic = yield* readEventTopic(directoryId, topicName); |
| 111 | if (topic === undefined) return undefined; |
| 112 | return toAttrs(directoryId, topicName, topic); |
| 113 | }), |
| 114 | |
| 115 | // Existence-only resource: the association has no mutable aspects, |
| 116 | // so reconcile is observe → ensure. |
| 117 | reconcile: Effect.fn(function* ({ news, session }) { |
| 118 | const props = news!; |
| 119 | |
| 120 | // 1. Observe — cloud state is authoritative. |
| 121 | let observed = yield* readEventTopic(props.directoryId, props.topicName); |
| 122 | |
| 123 | // 2. Ensure — register if missing. Re-registering an existing |
| 124 | // association is treated as a race and tolerated by |
| 125 | // re-observing below. |
| 126 | if (observed === undefined) { |
| 127 | yield* ds.registerEventTopic({ |
| 128 | DirectoryId: props.directoryId, |
| 129 | TopicName: props.topicName, |
| 130 | }); |
| 131 | observed = yield* readEventTopic(props.directoryId, props.topicName); |
| 132 | } |
| 133 | if (observed === undefined) { |
| 134 | return yield* new EventTopicUnexpectedResponse({ |
| 135 | message: `event topic '${props.topicName}' on '${props.directoryId}' not visible after register`, |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | yield* session.note(`${props.directoryId}/${props.topicName}`); |
| 140 | return toAttrs(props.directoryId, props.topicName, observed); |
| 141 | }), |
| 142 | |
| 143 | delete: Effect.fn(function* ({ output }) { |
| 144 | yield* ds |
| 145 | .deregisterEventTopic({ |
| 146 | DirectoryId: output.directoryId, |
| 147 | TopicName: output.topicName, |
| 148 | }) |
| 149 | .pipe( |
| 150 | // The association — or the whole parent directory — being |
| 151 | // gone is success. |
| 152 | Effect.catchTag("EntityDoesNotExistException", () => Effect.void), |
| 153 | ); |
| 154 | }), |
| 155 | |
| 156 | // Event topics are keyed by their parent directory; there is no |
| 157 | // account-wide enumeration. |
| 158 | list: () => Effect.succeed([]), |
| 159 | }; |
| 160 | }), |
| 161 | ); |