The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as keyspaces from "@distilled.cloud/aws/keyspaces"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import type { Keyspace } from "./Keyspace.ts"; |
| 7 | import { RestoreTable, type RestoreTableRequest } from "./RestoreTable.ts"; |
| 8 | import type { Table } from "./Table.ts"; |
| 9 | |
| 10 | /** |
| 11 | * HTTP implementation of {@link RestoreTable}: grants `cassandra:Select` on |
| 12 | * the source table plus `cassandra:Restore` (and the `Create`/`TagResource` |
| 13 | * actions the restore performs) on the target keyspace and its tables, and |
| 14 | * calls the Keyspaces HTTP API with the function's IAM credentials. |
| 15 | */ |
| 16 | export const RestoreTableHttp = Layer.effect( |
| 17 | RestoreTable, |
| 18 | Effect.gen(function* () { |
| 19 | const bind = yield* AWSBindings.Bind; |
| 20 | const restoreTable = yield* AWSBindings.withCredentials(keyspaces.restoreTable); |
| 21 | |
| 22 | return Effect.fn(function* <From extends Table, To extends Keyspace>( |
| 23 | source: From, |
| 24 | targetKeyspace: To, |
| 25 | ) { |
| 26 | const SourceKeyspaceName = yield* source.keyspaceName; |
| 27 | const SourceTableName = yield* source.tableName; |
| 28 | const TargetKeyspaceName = yield* targetKeyspace.keyspaceName; |
| 29 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 30 | yield* bind`AWS.Keyspaces.RestoreTable(${source}, ${targetKeyspace})`({ |
| 31 | policyStatements: [ |
| 32 | { |
| 33 | Effect: "Allow", |
| 34 | Action: ["cassandra:Select"], |
| 35 | Resource: [source.tableArn], |
| 36 | }, |
| 37 | { |
| 38 | // The keyspace ARN ends with a trailing slash |
| 39 | // (`.../keyspace/name/`), so its tables match `table/*`. |
| 40 | Effect: "Allow", |
| 41 | Action: ["cassandra:Restore", "cassandra:Create", "cassandra:TagResource"], |
| 42 | Resource: [ |
| 43 | targetKeyspace.keyspaceArn, |
| 44 | Output.interpolate`${targetKeyspace.keyspaceArn}table/*`, |
| 45 | ], |
| 46 | }, |
| 47 | ], |
| 48 | }); |
| 49 | } |
| 50 | return Effect.fn(`AWS.Keyspaces.RestoreTable(${source.LogicalId})`)(function* ( |
| 51 | request: RestoreTableRequest, |
| 52 | ) { |
| 53 | const sourceKeyspaceName = yield* SourceKeyspaceName; |
| 54 | const sourceTableName = yield* SourceTableName; |
| 55 | const targetKeyspaceName = yield* TargetKeyspaceName; |
| 56 | return yield* restoreTable({ |
| 57 | ...request, |
| 58 | sourceKeyspaceName, |
| 59 | sourceTableName, |
| 60 | targetKeyspaceName, |
| 61 | }); |
| 62 | }); |
| 63 | }); |
| 64 | }), |
| 65 | ); |