The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as aoss from "@distilled.cloud/aws/opensearchserverless"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { canonicalizePolicy, retryWhileConflict, stringifyPolicy } from "./internal.ts"; |
| 10 | import type { SecurityPolicyDocument } from "./SecurityPolicy.ts"; |
| 11 | |
| 12 | /** The kind of lifecycle policy. OpenSearch Serverless only defines `retention`. */ |
| 13 | export type LifecyclePolicyType = "retention"; |
| 14 | |
| 15 | export interface LifecyclePolicyProps { |
| 16 | /** |
| 17 | * Name of the lifecycle policy (3-32 characters, lowercase). Changing the |
| 18 | * name replaces the policy. |
| 19 | * @default a generated physical name |
| 20 | */ |
| 21 | policyName?: string; |
| 22 | /** |
| 23 | * The policy kind. OpenSearch Serverless currently only supports |
| 24 | * `retention` policies. Changing the type replaces the policy. |
| 25 | * @default "retention" |
| 26 | */ |
| 27 | type?: LifecyclePolicyType; |
| 28 | /** |
| 29 | * The lifecycle policy document — a JSON object with `Rules` matching index |
| 30 | * patterns to a retention window, e.g. |
| 31 | * `{ Rules: [{ ResourceType: "index", Resource: ["index/my-collection/*"], MinIndexRetention: "30d" }] }`. |
| 32 | * Use `NoMinIndexRetention: true` on a rule to retain matched indexes |
| 33 | * indefinitely. Supply an object or a pre-serialized string. |
| 34 | */ |
| 35 | policy: SecurityPolicyDocument; |
| 36 | /** |
| 37 | * A human-readable description of the policy. |
| 38 | */ |
| 39 | description?: string; |
| 40 | } |
| 41 | |
| 42 | export interface LifecyclePolicy extends Resource< |
| 43 | "AWS.OpenSearchServerless.LifecyclePolicy", |
| 44 | LifecyclePolicyProps, |
| 45 | { |
| 46 | /** |
| 47 | * Name of the lifecycle policy. |
| 48 | */ |
| 49 | policyName: string; |
| 50 | /** |
| 51 | * Policy type (`retention`). |
| 52 | */ |
| 53 | type: string; |
| 54 | /** |
| 55 | * Version of the policy, used for optimistic-concurrency updates. |
| 56 | */ |
| 57 | policyVersion: string; |
| 58 | /** |
| 59 | * Description of the lifecycle policy. |
| 60 | */ |
| 61 | description?: string; |
| 62 | }, |
| 63 | {}, |
| 64 | Providers |
| 65 | > {} |
| 66 | |
| 67 | /** |
| 68 | * An Amazon OpenSearch Serverless data lifecycle policy. Retention lifecycle |
| 69 | * policies control how long documents are retained in the indexes matched by |
| 70 | * the policy's resource patterns — OpenSearch Serverless automatically deletes |
| 71 | * documents older than the configured `MinIndexRetention`. |
| 72 | * |
| 73 | * Lifecycle policies are free, provision instantly, and are matched to |
| 74 | * indexes by resource pattern (e.g. `index/my-collection/*`) — the collection |
| 75 | * does not need to exist when the policy is created. |
| 76 | * |
| 77 | * ### Creating Lifecycle Policies |
| 78 | * **Example:** Retain Log Indexes for 30 Days |
| 79 | * ```typescript |
| 80 | * import * as AWS from "@alchemy.run/aws"; |
| 81 | * |
| 82 | * const retention = yield* AWS.OpenSearchServerless.LifecyclePolicy("Retention", { |
| 83 | * policyName: "logs-retention", |
| 84 | * policy: { |
| 85 | * Rules: [ |
| 86 | * { |
| 87 | * ResourceType: "index", |
| 88 | * Resource: ["index/logs/*"], |
| 89 | * MinIndexRetention: "30d", |
| 90 | * }, |
| 91 | * ], |
| 92 | * }, |
| 93 | * }); |
| 94 | * ``` |
| 95 | * |
| 96 | * **Example:** Unlimited Retention for Specific Indexes |
| 97 | * ```typescript |
| 98 | * const keepForever = yield* AWS.OpenSearchServerless.LifecyclePolicy("KeepForever", { |
| 99 | * policyName: "audit-retention", |
| 100 | * policy: { |
| 101 | * Rules: [ |
| 102 | * { |
| 103 | * ResourceType: "index", |
| 104 | * Resource: ["index/audit/*"], |
| 105 | * NoMinIndexRetention: true, |
| 106 | * }, |
| 107 | * ], |
| 108 | * }, |
| 109 | * }); |
| 110 | * ``` |
| 111 | * |
| 112 | * @resource |
| 113 | */ |
| 114 | export const LifecyclePolicy = Resource<LifecyclePolicy>( |
| 115 | "AWS.OpenSearchServerless.LifecyclePolicy", |
| 116 | ); |
| 117 | |
| 118 | const LIFECYCLE_POLICY_TYPE = "retention"; |
| 119 | |
| 120 | export const LifecyclePolicyProvider = () => |
| 121 | Provider.effect( |
| 122 | LifecyclePolicy, |
| 123 | Effect.gen(function* () { |
| 124 | const createName = Effect.fn(function* ( |
| 125 | id: string, |
| 126 | props: { policyName?: string | undefined }, |
| 127 | ) { |
| 128 | return ( |
| 129 | props.policyName ?? (yield* createPhysicalName({ id, maxLength: 32, lowercase: true })) |
| 130 | ); |
| 131 | }); |
| 132 | |
| 133 | const toAttributes = (detail: aoss.LifecyclePolicyDetail) => ({ |
| 134 | policyName: detail.name!, |
| 135 | type: detail.type!, |
| 136 | policyVersion: detail.policyVersion!, |
| 137 | description: detail.description, |
| 138 | }); |
| 139 | |
| 140 | // batchGetLifecyclePolicy reports a missing policy in |
| 141 | // lifecyclePolicyErrorDetails instead of failing, which observe treats |
| 142 | // as "not present". |
| 143 | const observe = Effect.fn(function* (type: string, name: string) { |
| 144 | const response = yield* aoss.batchGetLifecyclePolicy({ |
| 145 | identifiers: [{ type, name }], |
| 146 | }); |
| 147 | return response.lifecyclePolicyDetails?.[0]; |
| 148 | }); |
| 149 | |
| 150 | return LifecyclePolicy.Provider.of({ |
| 151 | stables: ["policyName", "type"], |
| 152 | |
| 153 | list: () => |
| 154 | Effect.gen(function* () { |
| 155 | const pages = yield* aoss.listLifecyclePolicies |
| 156 | .pages({ type: LIFECYCLE_POLICY_TYPE }) |
| 157 | .pipe(Stream.runCollect); |
| 158 | return Array.from(pages) |
| 159 | .flatMap((page) => page.lifecyclePolicySummaries ?? []) |
| 160 | .filter( |
| 161 | (s) => |
| 162 | s.name !== undefined && s.type !== undefined && s.policyVersion !== undefined, |
| 163 | ) |
| 164 | .map((s) => ({ |
| 165 | policyName: s.name!, |
| 166 | type: s.type!, |
| 167 | policyVersion: s.policyVersion!, |
| 168 | description: s.description, |
| 169 | })); |
| 170 | }), |
| 171 | |
| 172 | read: Effect.fn(function* ({ id, olds, output }) { |
| 173 | const name = output?.policyName ?? (yield* createName(id, olds ?? {})); |
| 174 | const detail = yield* observe(LIFECYCLE_POLICY_TYPE, name); |
| 175 | if (detail?.name === undefined) { |
| 176 | return undefined; |
| 177 | } |
| 178 | // Lifecycle policies carry no tags, so an existing same-name policy |
| 179 | // is adopted. |
| 180 | return toAttributes(detail); |
| 181 | }), |
| 182 | |
| 183 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 184 | if (!isResolved(news)) return undefined; |
| 185 | if ((olds.type ?? LIFECYCLE_POLICY_TYPE) !== (news.type ?? LIFECYCLE_POLICY_TYPE)) { |
| 186 | return { action: "replace" } as const; |
| 187 | } |
| 188 | const oldName = yield* createName(id, olds); |
| 189 | const newName = yield* createName(id, news); |
| 190 | if (oldName !== newName) { |
| 191 | return { action: "replace" } as const; |
| 192 | } |
| 193 | // description/policy fall through to the default update path |
| 194 | }), |
| 195 | |
| 196 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 197 | const type = news.type ?? LIFECYCLE_POLICY_TYPE; |
| 198 | const name = output?.policyName ?? (yield* createName(id, news)); |
| 199 | const policy = stringifyPolicy(news.policy); |
| 200 | |
| 201 | // 1. OBSERVE |
| 202 | let detail = yield* observe(type, name); |
| 203 | |
| 204 | // 2. ENSURE — create if missing; tolerate a concurrent create race |
| 205 | if (detail?.name === undefined) { |
| 206 | detail = yield* aoss |
| 207 | .createLifecyclePolicy({ |
| 208 | type, |
| 209 | name, |
| 210 | policy, |
| 211 | description: news.description, |
| 212 | }) |
| 213 | .pipe( |
| 214 | Effect.map((r) => r.lifecyclePolicyDetail), |
| 215 | Effect.catchTag("ConflictException", () => observe(type, name)), |
| 216 | ); |
| 217 | } else { |
| 218 | // 3. SYNC — update policy/description when observed drifts from desired |
| 219 | const policyDrift = canonicalizePolicy(detail.policy) !== canonicalizePolicy(policy); |
| 220 | const descriptionDrift = |
| 221 | news.description !== undefined && news.description !== detail.description; |
| 222 | if (policyDrift || descriptionDrift) { |
| 223 | detail = yield* aoss |
| 224 | .updateLifecyclePolicy({ |
| 225 | type, |
| 226 | name, |
| 227 | policyVersion: detail.policyVersion!, |
| 228 | policy: policyDrift ? policy : undefined, |
| 229 | description: descriptionDrift ? news.description : undefined, |
| 230 | }) |
| 231 | .pipe(Effect.map((r) => r.lifecyclePolicyDetail)); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (detail?.name === undefined) { |
| 236 | return yield* aoss.ResourceNotFoundException.make({ |
| 237 | message: `lifecycle policy ${type}/${name} not visible after reconcile`, |
| 238 | }); |
| 239 | } |
| 240 | yield* session.note(`${type}/${name}`); |
| 241 | return toAttributes(detail); |
| 242 | }), |
| 243 | |
| 244 | delete: Effect.fn(function* ({ output }) { |
| 245 | yield* retryWhileConflict( |
| 246 | aoss.deleteLifecyclePolicy({ |
| 247 | type: output.type, |
| 248 | name: output.policyName, |
| 249 | }), |
| 250 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 251 | }), |
| 252 | }); |
| 253 | }), |
| 254 | ); |