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 fis from "@distilled.cloud/aws/fis"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | |
| 9 | export interface TargetAccountConfigurationProps { |
| 10 | /** |
| 11 | * The ID of the experiment template the target account belongs to. The |
| 12 | * template must use `accountTargeting: "multi-account"`. Changing it |
| 13 | * replaces the configuration. |
| 14 | */ |
| 15 | experimentTemplateId: string; |
| 16 | /** |
| 17 | * The AWS account ID of the target account. Changing it replaces the |
| 18 | * configuration. |
| 19 | */ |
| 20 | accountId: string; |
| 21 | /** |
| 22 | * The ARN of an IAM role in the target account that grants FIS permission |
| 23 | * to perform the experiment's actions there. The role must trust |
| 24 | * `fis.amazonaws.com`. |
| 25 | */ |
| 26 | roleArn: string; |
| 27 | /** |
| 28 | * A description of the target account. Once set, it cannot be fully |
| 29 | * removed via the API — only changed. |
| 30 | */ |
| 31 | description?: string; |
| 32 | } |
| 33 | |
| 34 | export interface TargetAccountConfiguration extends Resource< |
| 35 | "AWS.FIS.TargetAccountConfiguration", |
| 36 | TargetAccountConfigurationProps, |
| 37 | { |
| 38 | /** |
| 39 | * The ID of the experiment template the target account belongs to. |
| 40 | */ |
| 41 | experimentTemplateId: string; |
| 42 | /** |
| 43 | * The AWS account ID of the target account. |
| 44 | */ |
| 45 | accountId: string; |
| 46 | /** |
| 47 | * The ARN of the IAM role FIS assumes in the target account. |
| 48 | */ |
| 49 | roleArn: string; |
| 50 | }, |
| 51 | never, |
| 52 | Providers |
| 53 | > {} |
| 54 | |
| 55 | /** |
| 56 | * A target account configuration for a multi-account AWS Fault Injection |
| 57 | * Service (FIS) experiment template — registers an AWS account (and the IAM |
| 58 | * role FIS assumes there) as a target of the experiment, so a single |
| 59 | * experiment can inject faults into resources across accounts. |
| 60 | * |
| 61 | * The parent {@link ExperimentTemplate} must declare |
| 62 | * `experimentOptions: { accountTargeting: "multi-account" }`. |
| 63 | * ### Registering Target Accounts |
| 64 | * **Example:** Add a Target Account to a Multi-Account Template |
| 65 | * ```typescript |
| 66 | * import * as AWS from "@alchemy.run/aws"; |
| 67 | * |
| 68 | * const template = yield* AWS.FIS.ExperimentTemplate("CrossAccount", { |
| 69 | * roleArn: orchestratorRole.roleArn, |
| 70 | * experimentOptions: { accountTargeting: "multi-account" }, |
| 71 | * actions: { |
| 72 | * Wait: { actionId: "aws:fis:wait", parameters: { duration: "PT1M" } }, |
| 73 | * }, |
| 74 | * }); |
| 75 | * |
| 76 | * const target = yield* AWS.FIS.TargetAccountConfiguration("WorkloadAccount", { |
| 77 | * experimentTemplateId: template.id, |
| 78 | * accountId: "111122223333", |
| 79 | * roleArn: "arn:aws:iam::111122223333:role/FisTargetRole", |
| 80 | * description: "the workload account faults are injected into", |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * @resource |
| 85 | */ |
| 86 | export const TargetAccountConfiguration = Resource<TargetAccountConfiguration>( |
| 87 | "AWS.FIS.TargetAccountConfiguration", |
| 88 | ); |
| 89 | |
| 90 | export const TargetAccountConfigurationProvider = () => |
| 91 | Provider.effect( |
| 92 | TargetAccountConfiguration, |
| 93 | Effect.gen(function* () { |
| 94 | // A missing template and a missing configuration both surface as |
| 95 | // ResourceNotFoundException — either way the configuration does not |
| 96 | // exist. |
| 97 | const observe = (experimentTemplateId: string, accountId: string) => |
| 98 | fis.getTargetAccountConfiguration({ experimentTemplateId, accountId }).pipe( |
| 99 | Effect.map((r) => r.targetAccountConfiguration), |
| 100 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 101 | ); |
| 102 | |
| 103 | const toAttrs = (experimentTemplateId: string, config: fis.TargetAccountConfiguration) => ({ |
| 104 | experimentTemplateId, |
| 105 | accountId: config.accountId!, |
| 106 | roleArn: config.roleArn!, |
| 107 | }); |
| 108 | |
| 109 | return TargetAccountConfiguration.Provider.of({ |
| 110 | stables: ["experimentTemplateId", "accountId"], |
| 111 | |
| 112 | // The (template, account) pair IS the configuration's identity — |
| 113 | // changing either side replaces it. roleArn/description sync in |
| 114 | // place. |
| 115 | diff: Effect.fn(function* ({ news, olds }) { |
| 116 | if (!isResolved(news)) return undefined; |
| 117 | if ( |
| 118 | olds !== undefined && |
| 119 | (olds.experimentTemplateId !== news.experimentTemplateId || |
| 120 | olds.accountId !== news.accountId) |
| 121 | ) { |
| 122 | return { action: "replace" } as const; |
| 123 | } |
| 124 | }), |
| 125 | |
| 126 | read: Effect.fn(function* ({ olds, output }) { |
| 127 | const experimentTemplateId = output?.experimentTemplateId ?? olds?.experimentTemplateId; |
| 128 | const accountId = output?.accountId ?? olds?.accountId; |
| 129 | if (experimentTemplateId === undefined || accountId === undefined) { |
| 130 | return undefined; |
| 131 | } |
| 132 | const config = yield* observe(experimentTemplateId, accountId); |
| 133 | if (config === undefined) return undefined; |
| 134 | // Target account configurations cannot carry tags; existence under |
| 135 | // the parent template is the ownership signal. |
| 136 | return toAttrs(experimentTemplateId, config); |
| 137 | }), |
| 138 | |
| 139 | reconcile: Effect.fn(function* ({ news, session }) { |
| 140 | // 1. Observe — the (template, account) pair is the deterministic |
| 141 | // identity; cloud state is authoritative. |
| 142 | let observed = yield* observe(news.experimentTemplateId, news.accountId); |
| 143 | |
| 144 | if (observed === undefined) { |
| 145 | // 2. Ensure — create when missing; a concurrent create race |
| 146 | // surfaces as ConflictException and falls through to observe. |
| 147 | observed = yield* fis |
| 148 | .createTargetAccountConfiguration({ |
| 149 | experimentTemplateId: news.experimentTemplateId, |
| 150 | accountId: news.accountId, |
| 151 | roleArn: news.roleArn, |
| 152 | description: news.description, |
| 153 | }) |
| 154 | .pipe( |
| 155 | Effect.map((r) => r.targetAccountConfiguration), |
| 156 | Effect.catchTag("ConflictException", () => |
| 157 | observe(news.experimentTemplateId, news.accountId), |
| 158 | ), |
| 159 | ); |
| 160 | } else if ( |
| 161 | // 3. Sync — diff observed against desired and apply the single |
| 162 | // update on any delta. `description` is settable-but-not-removable, |
| 163 | // so it only participates while declared. |
| 164 | observed.roleArn !== news.roleArn || |
| 165 | (news.description !== undefined && observed.description !== news.description) |
| 166 | ) { |
| 167 | observed = yield* fis |
| 168 | .updateTargetAccountConfiguration({ |
| 169 | experimentTemplateId: news.experimentTemplateId, |
| 170 | accountId: news.accountId, |
| 171 | roleArn: news.roleArn, |
| 172 | description: news.description, |
| 173 | }) |
| 174 | .pipe(Effect.map((r) => r.targetAccountConfiguration)); |
| 175 | } |
| 176 | |
| 177 | yield* session.note(`${news.experimentTemplateId}/${news.accountId}`); |
| 178 | return toAttrs(news.experimentTemplateId, observed!); |
| 179 | }), |
| 180 | |
| 181 | delete: Effect.fn(function* ({ output }) { |
| 182 | yield* fis |
| 183 | .deleteTargetAccountConfiguration({ |
| 184 | experimentTemplateId: output.experimentTemplateId, |
| 185 | accountId: output.accountId, |
| 186 | }) |
| 187 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 188 | }), |
| 189 | |
| 190 | // Enumerate every template's target account configurations. |
| 191 | // Single-account templates simply have none. |
| 192 | list: () => |
| 193 | Effect.gen(function* () { |
| 194 | const templates = yield* fis.listExperimentTemplates.items({}).pipe( |
| 195 | Stream.runCollect, |
| 196 | Effect.map((chunk) => Array.from(chunk)), |
| 197 | ); |
| 198 | const results: TargetAccountConfiguration["Attributes"][] = []; |
| 199 | for (const template of templates) { |
| 200 | if (template.id === undefined) continue; |
| 201 | const configs = yield* fis.listTargetAccountConfigurations |
| 202 | .items({ experimentTemplateId: template.id }) |
| 203 | .pipe( |
| 204 | Stream.runCollect, |
| 205 | Effect.map((chunk) => Array.from(chunk)), |
| 206 | // A template can vanish between enumeration and listing. |
| 207 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed([])), |
| 208 | ); |
| 209 | for (const config of configs) { |
| 210 | if (config.accountId === undefined || config.roleArn === undefined) { |
| 211 | continue; |
| 212 | } |
| 213 | results.push({ |
| 214 | experimentTemplateId: template.id, |
| 215 | accountId: config.accountId, |
| 216 | roleArn: config.roleArn, |
| 217 | }); |
| 218 | } |
| 219 | } |
| 220 | return results; |
| 221 | }), |
| 222 | }); |
| 223 | }), |
| 224 | ); |