The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as RedshiftServerless from "@alchemy.run/aws/RedshiftServerless"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "snapshot-handler.ts"); |
| 11 | |
| 12 | export class SnapshotFunction extends Lambda.Function<Lambda.Function>()("SnapshotFunction") {} |
| 13 | |
| 14 | /** |
| 15 | * Exercises the snapshot/recovery-point bindings against a namespace. The |
| 16 | * namespace needs a workgroup: CreateSnapshot on a bare namespace fails with |
| 17 | * `ResourceNotFoundException: You can't create a snapshot because there isn't |
| 18 | * a workgroup created for the namespace.` The workgroup runs at the minimum |
| 19 | * base capacity and never executes a query. |
| 20 | */ |
| 21 | export default SnapshotFunction.make( |
| 22 | { |
| 23 | main, |
| 24 | functionUrl: true, |
| 25 | timeout: Duration.seconds(60), |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | const namespace = yield* RedshiftServerless.Namespace("SnapshotNamespace", { |
| 29 | namespaceName: "alchemy-test-rssnap-ns", |
| 30 | dbName: "dev", |
| 31 | adminUsername: "alchemyadmin", |
| 32 | manageAdminPassword: true, |
| 33 | }); |
| 34 | yield* RedshiftServerless.Workgroup("SnapshotWorkgroup", { |
| 35 | workgroupName: "alchemy-test-rssnap-wg", |
| 36 | namespaceName: namespace.namespaceName, |
| 37 | baseCapacity: 8, |
| 38 | publiclyAccessible: false, |
| 39 | }); |
| 40 | |
| 41 | const NamespaceName = yield* namespace.namespaceName; |
| 42 | const createSnapshot = yield* RedshiftServerless.CreateSnapshot(namespace); |
| 43 | const getSnapshot = yield* RedshiftServerless.GetSnapshot(); |
| 44 | const listSnapshots = yield* RedshiftServerless.ListSnapshots(); |
| 45 | const updateSnapshot = yield* RedshiftServerless.UpdateSnapshot(); |
| 46 | const deleteSnapshot = yield* RedshiftServerless.DeleteSnapshot(); |
| 47 | const listRecoveryPoints = yield* RedshiftServerless.ListRecoveryPoints(); |
| 48 | const listTableRestoreStatus = yield* RedshiftServerless.ListTableRestoreStatus(); |
| 49 | |
| 50 | return { |
| 51 | fetch: Effect.gen(function* () { |
| 52 | const request = yield* HttpServerRequest; |
| 53 | const url = new URL(request.originalUrl); |
| 54 | const pathname = url.pathname; |
| 55 | const name = url.searchParams.get("name") ?? ""; |
| 56 | |
| 57 | if (request.method === "POST" && pathname === "/snapshot") { |
| 58 | const { snapshot } = yield* createSnapshot({ |
| 59 | snapshotName: name, |
| 60 | retentionPeriod: 1, |
| 61 | }); |
| 62 | return yield* HttpServerResponse.json({ |
| 63 | snapshotName: snapshot?.snapshotName, |
| 64 | status: snapshot?.status, |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | if (request.method === "GET" && pathname === "/snapshot") { |
| 69 | const { snapshot } = yield* getSnapshot({ snapshotName: name }); |
| 70 | return yield* HttpServerResponse.json({ |
| 71 | snapshotName: snapshot?.snapshotName, |
| 72 | namespaceName: snapshot?.namespaceName, |
| 73 | retentionPeriod: snapshot?.snapshotRetentionPeriod, |
| 74 | status: snapshot?.status, |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | if (request.method === "GET" && pathname === "/snapshots") { |
| 79 | const namespaceName = yield* NamespaceName; |
| 80 | const { snapshots } = yield* listSnapshots({ namespaceName }); |
| 81 | return yield* HttpServerResponse.json({ |
| 82 | names: (snapshots ?? []).map((s) => s.snapshotName), |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | if (request.method === "POST" && pathname === "/snapshot/retention") { |
| 87 | const { snapshot } = yield* updateSnapshot({ |
| 88 | snapshotName: name, |
| 89 | retentionPeriod: 2, |
| 90 | }); |
| 91 | return yield* HttpServerResponse.json({ |
| 92 | retentionPeriod: snapshot?.snapshotRetentionPeriod, |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | if (request.method === "DELETE" && pathname === "/snapshot") { |
| 97 | yield* deleteSnapshot({ snapshotName: name }); |
| 98 | return yield* HttpServerResponse.json({ deleted: true }); |
| 99 | } |
| 100 | |
| 101 | if (request.method === "GET" && pathname === "/recovery-points") { |
| 102 | const namespaceName = yield* NamespaceName; |
| 103 | const { recoveryPoints } = yield* listRecoveryPoints({ |
| 104 | namespaceName, |
| 105 | }); |
| 106 | return yield* HttpServerResponse.json({ |
| 107 | count: (recoveryPoints ?? []).length, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | if (request.method === "GET" && pathname === "/table-restores") { |
| 112 | const namespaceName = yield* NamespaceName; |
| 113 | const { tableRestoreStatuses } = yield* listTableRestoreStatus({ |
| 114 | namespaceName, |
| 115 | }); |
| 116 | return yield* HttpServerResponse.json({ |
| 117 | count: (tableRestoreStatuses ?? []).length, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | return yield* HttpServerResponse.json( |
| 122 | { error: "Not found", method: request.method, pathname }, |
| 123 | { status: 404 }, |
| 124 | ); |
| 125 | }).pipe(Effect.orDie), |
| 126 | }; |
| 127 | }).pipe( |
| 128 | Effect.provide( |
| 129 | Layer.mergeAll( |
| 130 | RedshiftServerless.CreateSnapshotHttp, |
| 131 | RedshiftServerless.GetSnapshotHttp, |
| 132 | RedshiftServerless.ListSnapshotsHttp, |
| 133 | RedshiftServerless.UpdateSnapshotHttp, |
| 134 | RedshiftServerless.DeleteSnapshotHttp, |
| 135 | RedshiftServerless.ListRecoveryPointsHttp, |
| 136 | RedshiftServerless.ListTableRestoreStatusHttp, |
| 137 | ), |
| 138 | ), |
| 139 | ), |
| 140 | ); |