The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as opensearch from "@distilled.cloud/aws/opensearch"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schedule from "effect/Schedule"; |
| 4 | |
| 5 | /** |
| 6 | * Convert OpenSearch's `[{ Key, Value }]` tag list into a plain record, |
| 7 | * dropping any entry missing a key or value. |
| 8 | */ |
| 9 | export const toTagRecord = ( |
| 10 | tags: ReadonlyArray<{ Key?: string; Value?: string }> | undefined, |
| 11 | ): Record<string, string> => |
| 12 | Object.fromEntries( |
| 13 | (tags ?? []) |
| 14 | .filter( |
| 15 | (tag): tag is { Key: string; Value: string } => |
| 16 | typeof tag.Key === "string" && typeof tag.Value === "string", |
| 17 | ) |
| 18 | .map((tag) => [tag.Key, tag.Value]), |
| 19 | ); |
| 20 | |
| 21 | /** |
| 22 | * Read the observed tags for an OpenSearch domain by ARN. A domain that has |
| 23 | * just been created (or is mid-transition) can transiently reject `listTags`; |
| 24 | * treat any failure as "no observed tags" so tag reconciliation still runs. |
| 25 | */ |
| 26 | export const readDomainTags = Effect.fn(function* (arn: string) { |
| 27 | const response = yield* opensearch |
| 28 | .listTags({ ARN: arn }) |
| 29 | .pipe(Effect.orElseSucceed(() => undefined)); |
| 30 | return toTagRecord(response?.TagList); |
| 31 | }); |
| 32 | |
| 33 | /** |
| 34 | * Poll an OpenSearch domain until `done` observes a settled state (bounded: |
| 35 | * 15s x 120 = ~30 minutes; domain provisioning and blue/green config changes |
| 36 | * typically take 15-25 minutes). If the schedule exhausts first, the last |
| 37 | * observation is returned as-is so the caller sees the real (still |
| 38 | * converging) domain rather than a spurious failure. |
| 39 | * |
| 40 | * The explicit `Effect.Effect<A, E, R>` return annotation is load-bearing: |
| 41 | * inlining repeat/retry combinators in provider lifecycle code lets their |
| 42 | * conditional return types survive into declaration emit and widen the |
| 43 | * provider layer to `unknown` R, poisoning `AWS.providers()` downstream. |
| 44 | */ |
| 45 | export const repeatUntilDomainState = <E extends { readonly _tag: string }, R>( |
| 46 | read: Effect.Effect<opensearch.DomainStatus | undefined, E, R>, |
| 47 | done: (domain: opensearch.DomainStatus | undefined) => boolean, |
| 48 | ): Effect.Effect<opensearch.DomainStatus | undefined, E, R> => |
| 49 | Effect.repeat(read, { |
| 50 | schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(120)]), |
| 51 | until: done, |
| 52 | }); |
| 53 | |
| 54 | /** |
| 55 | * The domain's search/HTTP endpoint: the public `Endpoint` (IPv4), else the |
| 56 | * dual-stack `EndpointV2`, else the VPC endpoint from the `Endpoints` map. |
| 57 | */ |
| 58 | export const domainEndpoint = (domain: opensearch.DomainStatus): string | undefined => |
| 59 | domain.Endpoint ?? |
| 60 | domain.EndpointV2 ?? |
| 61 | domain.Endpoints?.vpc ?? |
| 62 | Object.values(domain.Endpoints ?? {}).find((value) => value !== undefined); |
| 63 | |
| 64 | /** |
| 65 | * A domain is active once created, no config change is being applied, and |
| 66 | * its endpoint has been published. `Processing` can flip to `false` before |
| 67 | * creation has fully finished (`DomainProcessingStatus` still `"Creating"`, |
| 68 | * no `Endpoint` yet), so the processing status and the endpoint are both |
| 69 | * required — otherwise the reconciler returns `endpoint: undefined` and every |
| 70 | * data-plane binding resolves an empty endpoint. |
| 71 | */ |
| 72 | export const isDomainActive = (domain: opensearch.DomainStatus | undefined): boolean => |
| 73 | domain === undefined || |
| 74 | (domain.Created === true && |
| 75 | domain.Processing !== true && |
| 76 | domain.Deleted !== true && |
| 77 | (domain.DomainProcessingStatus === undefined || domain.DomainProcessingStatus === "Active") && |
| 78 | domainEndpoint(domain) !== undefined); |
| 79 | |
| 80 | /** A domain can be deleted once it is no longer applying a config change. */ |
| 81 | export const isDomainDeletable = (domain: opensearch.DomainStatus | undefined): boolean => |
| 82 | domain === undefined || domain.Deleted === true || domain.Processing !== true; |