The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as paymentcryptography from "@distilled.cloud/aws/payment-cryptography"; |
| 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 | |
| 10 | export interface AliasProps { |
| 11 | /** |
| 12 | * The alias name. Must begin with `alias/` and may contain alphanumeric |
| 13 | * characters, forward slashes, underscores and hyphens. Changing it |
| 14 | * replaces the alias. |
| 15 | * @default alias/${app}-${stage}-${id} |
| 16 | */ |
| 17 | aliasName?: string; |
| 18 | /** |
| 19 | * The ARN of the key the alias points to. Omit to create an unattached |
| 20 | * alias; set later to point it at a key. Updated in place. |
| 21 | */ |
| 22 | keyArn?: string; |
| 23 | } |
| 24 | |
| 25 | export interface Alias extends Resource< |
| 26 | "AWS.PaymentCryptography.Alias", |
| 27 | AliasProps, |
| 28 | { |
| 29 | /** |
| 30 | * Name of the alias (starts with `alias/`). |
| 31 | */ |
| 32 | aliasName: string; |
| 33 | /** |
| 34 | * ARN of the key the alias points to, if attached. |
| 35 | */ |
| 36 | keyArn: string | undefined; |
| 37 | }, |
| 38 | never, |
| 39 | Providers |
| 40 | > {} |
| 41 | |
| 42 | /** |
| 43 | * A friendly name for an AWS Payment Cryptography {@link Key}. Aliases give |
| 44 | * keys a stable, human-readable identifier that survives key rotation — the |
| 45 | * alias can be repointed to a new key without touching consumers. |
| 46 | * ### Creating Aliases |
| 47 | * **Example:** Alias attached to a key |
| 48 | * ```typescript |
| 49 | * import * as PaymentCryptography from "@alchemy.run/aws/PaymentCryptography"; |
| 50 | * |
| 51 | * const key = yield* PaymentCryptography.Key("DataKey", { keyAttributes: { ... } }); |
| 52 | * const alias = yield* PaymentCryptography.Alias("DataKeyAlias", { |
| 53 | * keyArn: key.keyArn, |
| 54 | * }); |
| 55 | * ``` |
| 56 | * |
| 57 | * **Example:** Alias with an explicit name |
| 58 | * ```typescript |
| 59 | * const alias = yield* PaymentCryptography.Alias("DataKeyAlias", { |
| 60 | * aliasName: "alias/payments/data-encryption", |
| 61 | * keyArn: key.keyArn, |
| 62 | * }); |
| 63 | * ``` |
| 64 | * |
| 65 | * @resource |
| 66 | */ |
| 67 | export const Alias = Resource<Alias>("AWS.PaymentCryptography.Alias"); |
| 68 | |
| 69 | const toAttrs = (alias: paymentcryptography.Alias): Alias["Attributes"] => ({ |
| 70 | aliasName: alias.AliasName, |
| 71 | keyArn: alias.KeyArn, |
| 72 | }); |
| 73 | |
| 74 | export const AliasProvider = () => |
| 75 | Provider.effect( |
| 76 | Alias, |
| 77 | Effect.gen(function* () { |
| 78 | const createAliasName = Effect.fn(function* (id: string, props: AliasProps) { |
| 79 | if (props.aliasName) { |
| 80 | return props.aliasName; |
| 81 | } |
| 82 | const baseName = yield* createPhysicalName({ |
| 83 | id, |
| 84 | maxLength: 256 - "alias/".length, |
| 85 | }); |
| 86 | return `alias/${baseName}`; |
| 87 | }); |
| 88 | |
| 89 | const observeAlias = Effect.fn(function* (aliasName: string) { |
| 90 | return yield* paymentcryptography.getAlias({ AliasName: aliasName }).pipe( |
| 91 | Effect.map((r) => r.Alias), |
| 92 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 93 | ); |
| 94 | }); |
| 95 | |
| 96 | return Alias.Provider.of({ |
| 97 | stables: ["aliasName"], |
| 98 | list: () => |
| 99 | paymentcryptography.listAliases.items({}).pipe( |
| 100 | Stream.runCollect, |
| 101 | Effect.map((chunk) => Array.from(chunk).map(toAttrs)), |
| 102 | ), |
| 103 | read: Effect.fn(function* ({ id, olds, output }) { |
| 104 | const aliasName = output?.aliasName ?? (yield* createAliasName(id, olds ?? {})); |
| 105 | const alias = yield* observeAlias(aliasName); |
| 106 | if (alias === undefined) return undefined; |
| 107 | return toAttrs(alias); |
| 108 | }), |
| 109 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 110 | if (!isResolved(news)) return undefined; |
| 111 | const oldName = yield* createAliasName(id, olds); |
| 112 | const newName = yield* createAliasName(id, news); |
| 113 | if (oldName !== newName) { |
| 114 | return { action: "replace" } as const; |
| 115 | } |
| 116 | // fall through: undefined → default update (keyArn repoint) |
| 117 | }), |
| 118 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 119 | const aliasName = output?.aliasName ?? (yield* createAliasName(id, news)); |
| 120 | |
| 121 | // 1. Observe — cloud state is authoritative. |
| 122 | let alias = yield* observeAlias(aliasName); |
| 123 | |
| 124 | // 2. Ensure — create when missing; a concurrent create surfaces as |
| 125 | // ConflictException, which resolves by re-observing. |
| 126 | if (alias === undefined) { |
| 127 | alias = yield* paymentcryptography |
| 128 | .createAlias({ AliasName: aliasName, KeyArn: news.keyArn }) |
| 129 | .pipe( |
| 130 | Effect.map((r) => r.Alias), |
| 131 | Effect.catchTag("ConflictException", () => |
| 132 | paymentcryptography |
| 133 | .getAlias({ AliasName: aliasName }) |
| 134 | .pipe(Effect.map((r) => r.Alias)), |
| 135 | ), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | // 3. Sync — repoint the alias when the observed target differs |
| 140 | // from the desired one (including detaching). |
| 141 | if (alias.KeyArn !== news.keyArn) { |
| 142 | alias = yield* paymentcryptography |
| 143 | .updateAlias({ AliasName: aliasName, KeyArn: news.keyArn }) |
| 144 | .pipe(Effect.map((r) => r.Alias)); |
| 145 | } |
| 146 | |
| 147 | yield* session.note(aliasName); |
| 148 | return toAttrs(alias); |
| 149 | }), |
| 150 | delete: Effect.fn(function* ({ output }) { |
| 151 | yield* paymentcryptography |
| 152 | .deleteAlias({ AliasName: output.aliasName }) |
| 153 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 154 | }), |
| 155 | }); |
| 156 | }), |
| 157 | ); |