The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as FSx from "@alchemy.run/aws/FSx"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | |
| 9 | const main = path.resolve(import.meta.dirname, "bindings-handler.ts"); |
| 10 | |
| 11 | // Syntactically valid but nonexistent ids: each probe must round-trip to FSx |
| 12 | // and come back as the typed *NotFound — an IAM gap would surface |
| 13 | // AccessDeniedException (a 500 via Effect.orDie) instead. |
| 14 | const MISSING_BACKUP_ID = "backup-00000000000000000"; |
| 15 | const MISSING_SNAPSHOT_ID = "fsvolsnap-00000000000000000"; |
| 16 | const MISSING_VOLUME_ID = "fsvol-00000000000000000"; |
| 17 | const MISSING_TASK_ID = "task-00000000000000000"; |
| 18 | // Well-formed FSx snapshot ARN pointing at a nonexistent snapshot. FSx |
| 19 | // reports the missing source snapshot as "SourceSnapshotARN provided is not |
| 20 | // a valid ARN" — the distilled patch carves the typed SourceSnapshotNotFound |
| 21 | // out of that BadRequest by message predicate. |
| 22 | const MISSING_SNAPSHOT_ARN = |
| 23 | "arn:aws:fsx:us-west-2:000000000000:snapshot/fsvolsnap-00000000000000000"; |
| 24 | |
| 25 | export class FSxBindingsFunction extends Lambda.Function<Lambda.Function>()( |
| 26 | "FSxBindingsFunction", |
| 27 | ) {} |
| 28 | |
| 29 | export default FSxBindingsFunction.make( |
| 30 | { |
| 31 | main, |
| 32 | functionUrl: true, |
| 33 | }, |
| 34 | Effect.gen(function* () { |
| 35 | // Account-level bindings only — the file-system-scoped bindings |
| 36 | // (DescribeFileSystem, CreateBackup, CreateDataRepositoryTask, |
| 37 | // ReleaseFileSystemNfsV3Locks) require a live FSx file system, which |
| 38 | // takes ~8 minutes to provision and bills hourly; their runtime path is |
| 39 | // identical scaffolding and is exercised by the account-level routes. |
| 40 | const describeBackups = yield* FSx.DescribeBackups(); |
| 41 | const deleteBackup = yield* FSx.DeleteBackup(); |
| 42 | const copyBackup = yield* FSx.CopyBackup(); |
| 43 | const updateSnapshot = yield* FSx.UpdateSnapshot(); |
| 44 | const describeSnapshots = yield* FSx.DescribeSnapshots(); |
| 45 | const createSnapshot = yield* FSx.CreateSnapshot(); |
| 46 | const deleteSnapshot = yield* FSx.DeleteSnapshot(); |
| 47 | const restoreVolumeFromSnapshot = yield* FSx.RestoreVolumeFromSnapshot(); |
| 48 | const copySnapshotAndUpdateVolume = yield* FSx.CopySnapshotAndUpdateVolume(); |
| 49 | const describeVolumes = yield* FSx.DescribeVolumes(); |
| 50 | const describeStorageVirtualMachines = yield* FSx.DescribeStorageVirtualMachines(); |
| 51 | const describeDataRepositoryTasks = yield* FSx.DescribeDataRepositoryTasks(); |
| 52 | const describeDataRepositoryAssociations = yield* FSx.DescribeDataRepositoryAssociations(); |
| 53 | const cancelDataRepositoryTask = yield* FSx.CancelDataRepositoryTask(); |
| 54 | |
| 55 | const bound = { |
| 56 | describeBackups, |
| 57 | deleteBackup, |
| 58 | copyBackup, |
| 59 | updateSnapshot, |
| 60 | describeSnapshots, |
| 61 | createSnapshot, |
| 62 | deleteSnapshot, |
| 63 | restoreVolumeFromSnapshot, |
| 64 | copySnapshotAndUpdateVolume, |
| 65 | describeVolumes, |
| 66 | describeStorageVirtualMachines, |
| 67 | describeDataRepositoryTasks, |
| 68 | describeDataRepositoryAssociations, |
| 69 | cancelDataRepositoryTask, |
| 70 | }; |
| 71 | |
| 72 | // Run a probe expected to fail with a typed tag; return the tag so the |
| 73 | // test can assert the exact typed error (proving both the IAM grant and |
| 74 | // the typed error union end-to-end). |
| 75 | const probeTag = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) => |
| 76 | effect.pipe( |
| 77 | Effect.result, |
| 78 | Effect.map((result) => (result._tag === "Failure" ? result.failure._tag : "Success")), |
| 79 | ); |
| 80 | |
| 81 | return { |
| 82 | fetch: Effect.gen(function* () { |
| 83 | const request = yield* HttpServerRequest; |
| 84 | const url = new URL(request.originalUrl); |
| 85 | const pathname = url.pathname; |
| 86 | |
| 87 | if (request.method === "GET" && pathname === "/bindings") { |
| 88 | return yield* HttpServerResponse.json({ |
| 89 | bound: Object.keys(bound), |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | if (request.method === "GET" && pathname === "/backups") { |
| 94 | const response = yield* describeBackups(); |
| 95 | return yield* HttpServerResponse.json({ |
| 96 | count: (response.Backups ?? []).length, |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | if (request.method === "GET" && pathname === "/snapshots") { |
| 101 | const response = yield* describeSnapshots(); |
| 102 | return yield* HttpServerResponse.json({ |
| 103 | count: (response.Snapshots ?? []).length, |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | if (request.method === "GET" && pathname === "/volumes") { |
| 108 | const response = yield* describeVolumes(); |
| 109 | return yield* HttpServerResponse.json({ |
| 110 | count: (response.Volumes ?? []).length, |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | if (request.method === "GET" && pathname === "/svms") { |
| 115 | const response = yield* describeStorageVirtualMachines(); |
| 116 | return yield* HttpServerResponse.json({ |
| 117 | count: (response.StorageVirtualMachines ?? []).length, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/dr-tasks") { |
| 122 | const response = yield* describeDataRepositoryTasks(); |
| 123 | return yield* HttpServerResponse.json({ |
| 124 | count: (response.DataRepositoryTasks ?? []).length, |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | if (request.method === "GET" && pathname === "/dr-associations") { |
| 129 | const response = yield* describeDataRepositoryAssociations(); |
| 130 | return yield* HttpServerResponse.json({ |
| 131 | count: (response.Associations ?? []).length, |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | if (request.method === "POST" && pathname === "/backup/delete-missing") { |
| 136 | const tag = yield* probeTag(deleteBackup({ BackupId: MISSING_BACKUP_ID })); |
| 137 | return yield* HttpServerResponse.json({ tag }); |
| 138 | } |
| 139 | |
| 140 | if (request.method === "POST" && pathname === "/backup/copy-missing") { |
| 141 | const tag = yield* probeTag(copyBackup({ SourceBackupId: MISSING_BACKUP_ID })); |
| 142 | return yield* HttpServerResponse.json({ tag }); |
| 143 | } |
| 144 | |
| 145 | if (request.method === "POST" && pathname === "/snapshot/update-missing") { |
| 146 | const tag = yield* probeTag( |
| 147 | updateSnapshot({ |
| 148 | SnapshotId: MISSING_SNAPSHOT_ID, |
| 149 | Name: "alchemy-fsx-bindings-probe", |
| 150 | }), |
| 151 | ); |
| 152 | return yield* HttpServerResponse.json({ tag }); |
| 153 | } |
| 154 | |
| 155 | if (request.method === "POST" && pathname === "/snapshot/delete-missing") { |
| 156 | const tag = yield* probeTag(deleteSnapshot({ SnapshotId: MISSING_SNAPSHOT_ID })); |
| 157 | return yield* HttpServerResponse.json({ tag }); |
| 158 | } |
| 159 | |
| 160 | if (request.method === "POST" && pathname === "/snapshot/create-missing-volume") { |
| 161 | const tag = yield* probeTag( |
| 162 | createSnapshot({ |
| 163 | Name: "alchemy-fsx-bindings-probe", |
| 164 | VolumeId: MISSING_VOLUME_ID, |
| 165 | }), |
| 166 | ); |
| 167 | return yield* HttpServerResponse.json({ tag }); |
| 168 | } |
| 169 | |
| 170 | if (request.method === "POST" && pathname === "/volume/restore-missing") { |
| 171 | const tag = yield* probeTag( |
| 172 | restoreVolumeFromSnapshot({ |
| 173 | VolumeId: MISSING_VOLUME_ID, |
| 174 | SnapshotId: MISSING_SNAPSHOT_ID, |
| 175 | }), |
| 176 | ); |
| 177 | return yield* HttpServerResponse.json({ tag }); |
| 178 | } |
| 179 | |
| 180 | if (request.method === "POST" && pathname === "/volume/copy-snapshot-missing") { |
| 181 | const tag = yield* probeTag( |
| 182 | copySnapshotAndUpdateVolume({ |
| 183 | VolumeId: MISSING_VOLUME_ID, |
| 184 | SourceSnapshotARN: MISSING_SNAPSHOT_ARN, |
| 185 | }), |
| 186 | ); |
| 187 | return yield* HttpServerResponse.json({ tag }); |
| 188 | } |
| 189 | |
| 190 | if (request.method === "POST" && pathname === "/dr-task/cancel-missing") { |
| 191 | const tag = yield* probeTag(cancelDataRepositoryTask({ TaskId: MISSING_TASK_ID })); |
| 192 | return yield* HttpServerResponse.json({ tag }); |
| 193 | } |
| 194 | |
| 195 | return yield* HttpServerResponse.json( |
| 196 | { error: "Not found", method: request.method, pathname }, |
| 197 | { status: 404 }, |
| 198 | ); |
| 199 | }).pipe(Effect.orDie), |
| 200 | }; |
| 201 | }).pipe( |
| 202 | Effect.provide( |
| 203 | Layer.mergeAll( |
| 204 | FSx.DescribeBackupsHttp, |
| 205 | FSx.DeleteBackupHttp, |
| 206 | FSx.CopyBackupHttp, |
| 207 | FSx.UpdateSnapshotHttp, |
| 208 | FSx.DescribeSnapshotsHttp, |
| 209 | FSx.CreateSnapshotHttp, |
| 210 | FSx.DeleteSnapshotHttp, |
| 211 | FSx.RestoreVolumeFromSnapshotHttp, |
| 212 | FSx.CopySnapshotAndUpdateVolumeHttp, |
| 213 | FSx.DescribeVolumesHttp, |
| 214 | FSx.DescribeStorageVirtualMachinesHttp, |
| 215 | FSx.DescribeDataRepositoryTasksHttp, |
| 216 | FSx.DescribeDataRepositoryAssociationsHttp, |
| 217 | FSx.CancelDataRepositoryTaskHttp, |
| 218 | ), |
| 219 | ), |
| 220 | ), |
| 221 | ); |