The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as organizations from "@distilled.cloud/aws/organizations"; |
| 2 | import * as Provider from "alchemy/Provider"; |
| 3 | import { Resource } from "alchemy/Resource"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import type { Providers } from "../Providers.ts"; |
| 7 | import { retryOrganizations, unredact } from "./common.ts"; |
| 8 | |
| 9 | export class OrganizationInvalidProps extends Data.TaggedError("OrganizationInvalidProps")<{ |
| 10 | readonly message: string; |
| 11 | readonly cause?: unknown; |
| 12 | }> {} |
| 13 | |
| 14 | export class OrganizationOperationFailed extends Data.TaggedError("OrganizationOperationFailed")<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export class OrganizationUnexpectedResponse extends Data.TaggedError( |
| 20 | "OrganizationUnexpectedResponse", |
| 21 | )<{ |
| 22 | readonly message: string; |
| 23 | readonly cause?: unknown; |
| 24 | }> {} |
| 25 | |
| 26 | export type OrganizationId = string; |
| 27 | export type OrganizationArn = string; |
| 28 | |
| 29 | export interface OrganizationProps { |
| 30 | /** |
| 31 | * Organization feature set. |
| 32 | * `ALL` unlocks the full AWS Organizations feature set. |
| 33 | * @default "ALL" |
| 34 | */ |
| 35 | featureSet?: organizations.OrganizationFeatureSet; |
| 36 | } |
| 37 | |
| 38 | export interface Organization extends Resource< |
| 39 | "AWS.Organizations.Organization", |
| 40 | OrganizationProps, |
| 41 | { |
| 42 | /** |
| 43 | * ID of the organization (e.g. `o-exampleorgid`). |
| 44 | */ |
| 45 | organizationId: OrganizationId; |
| 46 | /** |
| 47 | * ARN of the organization. |
| 48 | */ |
| 49 | organizationArn: OrganizationArn; |
| 50 | /** |
| 51 | * Feature set enabled on the organization (`ALL` or |
| 52 | * `CONSOLIDATED_BILLING`). |
| 53 | */ |
| 54 | featureSet: organizations.OrganizationFeatureSet | undefined; |
| 55 | /** |
| 56 | * ARN of the management account. |
| 57 | */ |
| 58 | managementAccountArn: string | undefined; |
| 59 | /** |
| 60 | * 12-digit ID of the management account. |
| 61 | */ |
| 62 | managementAccountId: string | undefined; |
| 63 | /** |
| 64 | * Email address of the management account. |
| 65 | */ |
| 66 | managementAccountEmail: string | undefined; |
| 67 | /** |
| 68 | * Policy types available to the organization. |
| 69 | */ |
| 70 | availablePolicyTypes: organizations.PolicyTypeSummary[]; |
| 71 | }, |
| 72 | never, |
| 73 | Providers |
| 74 | > {} |
| 75 | |
| 76 | /** |
| 77 | * The AWS Organization for the current management account. |
| 78 | * |
| 79 | * This is a singleton-style resource. If an organization already exists, |
| 80 | * Alchemy adopts and reconciles it instead of creating a second one. |
| 81 | * ### Creating An Organization |
| 82 | * **Example:** Full Features Organization |
| 83 | * ```typescript |
| 84 | * const organization = yield* Organization("Org", { |
| 85 | * featureSet: "ALL", |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * @resource |
| 90 | */ |
| 91 | export const Organization = Resource<Organization>("AWS.Organizations.Organization"); |
| 92 | |
| 93 | export const OrganizationProvider = () => |
| 94 | Provider.effect( |
| 95 | Organization, |
| 96 | Effect.gen(function* () { |
| 97 | return { |
| 98 | stables: ["organizationId", "organizationArn", "managementAccountId"], |
| 99 | diff: Effect.fn(function* () {}), |
| 100 | read: Effect.fn(function* () { |
| 101 | const org = yield* readOrganization(); |
| 102 | return org?.Id && org.Arn ? toAttrs(org) : undefined; |
| 103 | }), |
| 104 | // Account singleton: there is at most one organization per management |
| 105 | // account and no list API. `describeOrganization` (via `readOrganization`) |
| 106 | // returns the single org, or the typed `AWSOrganizationsNotInUseException` |
| 107 | // is caught to `undefined` when the account isn't a management account. |
| 108 | list: () => |
| 109 | Effect.gen(function* () { |
| 110 | const org = yield* readOrganization(); |
| 111 | return org?.Id && org.Arn ? [toAttrs(org)] : []; |
| 112 | }), |
| 113 | reconcile: Effect.fn(function* ({ news, session }) { |
| 114 | const desiredFeatureSet = news.featureSet ?? "ALL"; |
| 115 | |
| 116 | // Observe — fetch the live singleton organization. There's at most |
| 117 | // one organization per management account, and `read` may have |
| 118 | // surfaced an existing one for adoption. |
| 119 | let org = yield* readOrganization(); |
| 120 | |
| 121 | // Ensure — create the organization if it's missing. Tolerate |
| 122 | // `AlreadyInOrganizationException` as a race with a concurrent |
| 123 | // creator (e.g. another tool, or a peer reconciler). |
| 124 | if (!org) { |
| 125 | org = yield* retryOrganizations( |
| 126 | organizations.createOrganization({ |
| 127 | FeatureSet: desiredFeatureSet, |
| 128 | }), |
| 129 | ).pipe( |
| 130 | Effect.map((response) => response.Organization), |
| 131 | Effect.catchTag("AlreadyInOrganizationException", () => readOrganization()), |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | if (!org?.Id || !org.Arn) { |
| 136 | return yield* new OrganizationUnexpectedResponse({ |
| 137 | message: "failed to resolve organization after reconcile", |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | const orgArn = org.Arn; |
| 142 | |
| 143 | // Sync feature set — observed ↔ desired. `ensureFeatureSet` |
| 144 | // handles the only legal transition (CONSOLIDATED_BILLING -> ALL) |
| 145 | // and rejects anything else. |
| 146 | org = yield* ensureFeatureSet({ |
| 147 | desired: desiredFeatureSet, |
| 148 | current: org, |
| 149 | }); |
| 150 | |
| 151 | yield* session.note(orgArn); |
| 152 | return toAttrs(org); |
| 153 | }), |
| 154 | delete: Effect.fn(function* () { |
| 155 | yield* retryOrganizations( |
| 156 | organizations |
| 157 | .deleteOrganization({}) |
| 158 | .pipe(Effect.catchTag("AWSOrganizationsNotInUseException", () => Effect.void)), |
| 159 | ); |
| 160 | }), |
| 161 | }; |
| 162 | }), |
| 163 | ); |
| 164 | |
| 165 | const toAttrs = (org: organizations.Organization): Organization["Attributes"] => ({ |
| 166 | organizationId: org.Id ?? "", |
| 167 | organizationArn: org.Arn ?? "", |
| 168 | featureSet: org.FeatureSet, |
| 169 | managementAccountArn: org.MasterAccountArn, |
| 170 | managementAccountId: org.MasterAccountId, |
| 171 | managementAccountEmail: unredact(org.MasterAccountEmail), |
| 172 | availablePolicyTypes: org.AvailablePolicyTypes ?? [], |
| 173 | }); |
| 174 | |
| 175 | const readOrganization = () => |
| 176 | retryOrganizations( |
| 177 | organizations.describeOrganization({}).pipe( |
| 178 | Effect.map((response) => response.Organization), |
| 179 | Effect.catchTag("AWSOrganizationsNotInUseException", () => Effect.undefined), |
| 180 | ), |
| 181 | ); |
| 182 | |
| 183 | const ensureFeatureSet = Effect.fn(function* ({ |
| 184 | desired, |
| 185 | current, |
| 186 | }: { |
| 187 | desired: organizations.OrganizationFeatureSet | undefined; |
| 188 | current: organizations.Organization; |
| 189 | }) { |
| 190 | const desiredFeatureSet = desired ?? "ALL"; |
| 191 | if (current.FeatureSet === desiredFeatureSet) { |
| 192 | return current; |
| 193 | } |
| 194 | |
| 195 | if (desiredFeatureSet === "ALL" && current.FeatureSet === "CONSOLIDATED_BILLING") { |
| 196 | yield* retryOrganizations(organizations.enableAllFeatures({})); |
| 197 | |
| 198 | const updated = yield* readOrganization(); |
| 199 | if (updated?.FeatureSet === "ALL") { |
| 200 | return updated; |
| 201 | } |
| 202 | |
| 203 | return yield* new OrganizationOperationFailed({ |
| 204 | message: |
| 205 | "Organization upgrade to ALL features requires handshake completion before deployment can converge", |
| 206 | }); |
| 207 | } |
| 208 | |
| 209 | return yield* new OrganizationInvalidProps({ |
| 210 | message: `Organization feature set cannot be changed from '${current.FeatureSet}' to '${desiredFeatureSet}'`, |
| 211 | }); |
| 212 | }); |