The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as Organizations from "@alchemy.run/aws/Organizations"; |
| 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 * as Stream from "effect/Stream"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | // A well-formed-but-nonexistent handshake id — drives the typed error paths |
| 13 | // for the handshake bindings (proving the IAM grant + typed union; an IAM gap |
| 14 | // would surface AccessDeniedException instead). |
| 15 | const NONEXISTENT_HANDSHAKE_ID = "h-abcd1234efgh5678"; |
| 16 | |
| 17 | // A well-formed-but-nonexistent create-account request id. |
| 18 | const NONEXISTENT_CAR_ID = "car-abcd1234abcd1234abcd1234abcd1234"; |
| 19 | |
| 20 | export class OrganizationsTestFunction extends Lambda.Function<Lambda.Function>()( |
| 21 | "OrganizationsTestFunction", |
| 22 | ) {} |
| 23 | |
| 24 | export default OrganizationsTestFunction.make( |
| 25 | { |
| 26 | main, |
| 27 | functionUrl: true, |
| 28 | }, |
| 29 | Effect.gen(function* () { |
| 30 | // --- account-level bindings (Organizations is a management-account |
| 31 | // singleton; these are the organization-tree, policy, delegation, |
| 32 | // vending, and handshake APIs an org-automation function uses) --- |
| 33 | const describeOrganization = yield* Organizations.DescribeOrganization(); |
| 34 | const listRoots = yield* Organizations.ListRoots(); |
| 35 | const listAccounts = yield* Organizations.ListAccounts(); |
| 36 | const listAccountsForParent = yield* Organizations.ListAccountsForParent(); |
| 37 | const listOrganizationalUnitsForParent = |
| 38 | yield* Organizations.ListOrganizationalUnitsForParent(); |
| 39 | const listChildren = yield* Organizations.ListChildren(); |
| 40 | const listParents = yield* Organizations.ListParents(); |
| 41 | const listPolicies = yield* Organizations.ListPolicies(); |
| 42 | const listPoliciesForTarget = yield* Organizations.ListPoliciesForTarget(); |
| 43 | const listTargetsForPolicy = yield* Organizations.ListTargetsForPolicy(); |
| 44 | const describeEffectivePolicy = yield* Organizations.DescribeEffectivePolicy(); |
| 45 | const listAccountsWithInvalidEffectivePolicy = |
| 46 | yield* Organizations.ListAccountsWithInvalidEffectivePolicy(); |
| 47 | const listEffectivePolicyValidationErrors = |
| 48 | yield* Organizations.ListEffectivePolicyValidationErrors(); |
| 49 | const listDelegatedAdministrators = yield* Organizations.ListDelegatedAdministrators(); |
| 50 | const listDelegatedServicesForAccount = yield* Organizations.ListDelegatedServicesForAccount(); |
| 51 | const listAWSServiceAccessForOrganization = |
| 52 | yield* Organizations.ListAWSServiceAccessForOrganization(); |
| 53 | const listTagsForResource = yield* Organizations.ListTagsForResource(); |
| 54 | const describeCreateAccountStatus = yield* Organizations.DescribeCreateAccountStatus(); |
| 55 | const listCreateAccountStatus = yield* Organizations.ListCreateAccountStatus(); |
| 56 | const inviteAccountToOrganization = yield* Organizations.InviteAccountToOrganization(); |
| 57 | const acceptHandshake = yield* Organizations.AcceptHandshake(); |
| 58 | const declineHandshake = yield* Organizations.DeclineHandshake(); |
| 59 | const cancelHandshake = yield* Organizations.CancelHandshake(); |
| 60 | const describeHandshake = yield* Organizations.DescribeHandshake(); |
| 61 | const listHandshakesForAccount = yield* Organizations.ListHandshakesForAccount(); |
| 62 | const listHandshakesForOrganization = yield* Organizations.ListHandshakesForOrganization(); |
| 63 | |
| 64 | // --- event source --- |
| 65 | // Deploy-time: creates the EventBridge rule (default bus, source |
| 66 | // aws.organizations) targeting this Function. Runtime firing needs a |
| 67 | // real organization change in us-east-1 of the management account, so |
| 68 | // the test only verifies the subscription deploys. |
| 69 | yield* Organizations.consumeOrganizationsEvents( |
| 70 | { events: ["CreateAccountResult", "MoveAccount"] }, |
| 71 | (events) => |
| 72 | Stream.runForEach(events, (event) => |
| 73 | Effect.log(`organizations event: ${event.detail.eventName}`), |
| 74 | ), |
| 75 | ); |
| 76 | |
| 77 | const bound = { |
| 78 | describeOrganization, |
| 79 | listRoots, |
| 80 | listAccounts, |
| 81 | listAccountsForParent, |
| 82 | listOrganizationalUnitsForParent, |
| 83 | listChildren, |
| 84 | listParents, |
| 85 | listPolicies, |
| 86 | listPoliciesForTarget, |
| 87 | listTargetsForPolicy, |
| 88 | describeEffectivePolicy, |
| 89 | listAccountsWithInvalidEffectivePolicy, |
| 90 | listEffectivePolicyValidationErrors, |
| 91 | listDelegatedAdministrators, |
| 92 | listDelegatedServicesForAccount, |
| 93 | listAWSServiceAccessForOrganization, |
| 94 | listTagsForResource, |
| 95 | describeCreateAccountStatus, |
| 96 | listCreateAccountStatus, |
| 97 | inviteAccountToOrganization, |
| 98 | acceptHandshake, |
| 99 | declineHandshake, |
| 100 | cancelHandshake, |
| 101 | describeHandshake, |
| 102 | listHandshakesForAccount, |
| 103 | listHandshakesForOrganization, |
| 104 | }; |
| 105 | |
| 106 | // The testing account is the management account of a standing |
| 107 | // organization, so the read paths return live data; every route still |
| 108 | // tolerates the typed not-in-organization / access-denied tags so the |
| 109 | // fixture stays correct on any account. |
| 110 | const rootId = describeOrganization().pipe( |
| 111 | Effect.flatMap(() => listRoots()), |
| 112 | Effect.map((r) => r.Roots?.[0]?.Id), |
| 113 | ); |
| 114 | |
| 115 | const managementAccountId = describeOrganization().pipe( |
| 116 | Effect.map((r) => r.Organization?.MasterAccountId), |
| 117 | ); |
| 118 | |
| 119 | return { |
| 120 | fetch: Effect.gen(function* () { |
| 121 | const request = yield* HttpServerRequest; |
| 122 | const url = new URL(request.originalUrl); |
| 123 | const pathname = url.pathname; |
| 124 | |
| 125 | if (request.method === "GET" && pathname === "/bindings") { |
| 126 | return yield* HttpServerResponse.json({ |
| 127 | bound: Object.keys(bound), |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | if (request.method === "GET" && pathname === "/organization") { |
| 132 | const result = yield* describeOrganization().pipe( |
| 133 | Effect.map((r) => ({ |
| 134 | ok: true as const, |
| 135 | id: r.Organization?.Id ?? null, |
| 136 | managementAccountId: r.Organization?.MasterAccountId ?? null, |
| 137 | })), |
| 138 | Effect.catchTag(["AccessDeniedException", "AWSOrganizationsNotInUseException"], (e) => |
| 139 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 140 | ), |
| 141 | ); |
| 142 | return yield* HttpServerResponse.json(result); |
| 143 | } |
| 144 | |
| 145 | if (request.method === "GET" && pathname === "/roots") { |
| 146 | const result = yield* listRoots().pipe( |
| 147 | Effect.map((r) => ({ |
| 148 | ok: true as const, |
| 149 | count: r.Roots?.length ?? 0, |
| 150 | rootId: r.Roots?.[0]?.Id ?? null, |
| 151 | })), |
| 152 | Effect.catchTag(["AccessDeniedException", "AWSOrganizationsNotInUseException"], (e) => |
| 153 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 154 | ), |
| 155 | ); |
| 156 | return yield* HttpServerResponse.json(result); |
| 157 | } |
| 158 | |
| 159 | if (request.method === "GET" && pathname === "/accounts") { |
| 160 | const result = yield* listAccounts().pipe( |
| 161 | Effect.map((r) => ({ |
| 162 | ok: true as const, |
| 163 | count: r.Accounts?.length ?? 0, |
| 164 | })), |
| 165 | Effect.catchTag(["AccessDeniedException", "AWSOrganizationsNotInUseException"], (e) => |
| 166 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 167 | ), |
| 168 | ); |
| 169 | return yield* HttpServerResponse.json(result); |
| 170 | } |
| 171 | |
| 172 | if (request.method === "GET" && pathname === "/accounts-for-parent") { |
| 173 | const result = yield* Effect.gen(function* () { |
| 174 | const parent = yield* rootId; |
| 175 | if (parent === undefined) { |
| 176 | return { ok: false as const, tag: "NoRoot" }; |
| 177 | } |
| 178 | const r = yield* listAccountsForParent({ ParentId: parent }); |
| 179 | return { ok: true as const, count: r.Accounts?.length ?? 0 }; |
| 180 | }).pipe( |
| 181 | Effect.catchTag( |
| 182 | [ |
| 183 | "AccessDeniedException", |
| 184 | "AWSOrganizationsNotInUseException", |
| 185 | "ParentNotFoundException", |
| 186 | ], |
| 187 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 188 | ), |
| 189 | ); |
| 190 | return yield* HttpServerResponse.json(result); |
| 191 | } |
| 192 | |
| 193 | if (request.method === "GET" && pathname === "/ous-for-parent") { |
| 194 | const result = yield* Effect.gen(function* () { |
| 195 | const parent = yield* rootId; |
| 196 | if (parent === undefined) { |
| 197 | return { ok: false as const, tag: "NoRoot" }; |
| 198 | } |
| 199 | const r = yield* listOrganizationalUnitsForParent({ |
| 200 | ParentId: parent, |
| 201 | }); |
| 202 | return { |
| 203 | ok: true as const, |
| 204 | count: r.OrganizationalUnits?.length ?? 0, |
| 205 | }; |
| 206 | }).pipe( |
| 207 | Effect.catchTag( |
| 208 | [ |
| 209 | "AccessDeniedException", |
| 210 | "AWSOrganizationsNotInUseException", |
| 211 | "ParentNotFoundException", |
| 212 | ], |
| 213 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 214 | ), |
| 215 | ); |
| 216 | return yield* HttpServerResponse.json(result); |
| 217 | } |
| 218 | |
| 219 | if (request.method === "GET" && pathname === "/children") { |
| 220 | const result = yield* Effect.gen(function* () { |
| 221 | const parent = yield* rootId; |
| 222 | if (parent === undefined) { |
| 223 | return { ok: false as const, tag: "NoRoot" }; |
| 224 | } |
| 225 | const r = yield* listChildren({ |
| 226 | ParentId: parent, |
| 227 | ChildType: "ACCOUNT", |
| 228 | }); |
| 229 | return { ok: true as const, count: r.Children?.length ?? 0 }; |
| 230 | }).pipe( |
| 231 | Effect.catchTag( |
| 232 | [ |
| 233 | "AccessDeniedException", |
| 234 | "AWSOrganizationsNotInUseException", |
| 235 | "ParentNotFoundException", |
| 236 | ], |
| 237 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 238 | ), |
| 239 | ); |
| 240 | return yield* HttpServerResponse.json(result); |
| 241 | } |
| 242 | |
| 243 | if (request.method === "GET" && pathname === "/parents") { |
| 244 | const result = yield* Effect.gen(function* () { |
| 245 | const accountId = yield* managementAccountId; |
| 246 | if (accountId === undefined) { |
| 247 | return { ok: false as const, tag: "NoOrganization" }; |
| 248 | } |
| 249 | const r = yield* listParents({ ChildId: accountId }); |
| 250 | return { ok: true as const, count: r.Parents?.length ?? 0 }; |
| 251 | }).pipe( |
| 252 | Effect.catchTag( |
| 253 | [ |
| 254 | "AccessDeniedException", |
| 255 | "AWSOrganizationsNotInUseException", |
| 256 | "ChildNotFoundException", |
| 257 | ], |
| 258 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 259 | ), |
| 260 | ); |
| 261 | return yield* HttpServerResponse.json(result); |
| 262 | } |
| 263 | |
| 264 | if (request.method === "GET" && pathname === "/policies") { |
| 265 | const result = yield* listPolicies({ |
| 266 | Filter: "SERVICE_CONTROL_POLICY", |
| 267 | }).pipe( |
| 268 | Effect.map((r) => ({ |
| 269 | ok: true as const, |
| 270 | count: r.Policies?.length ?? 0, |
| 271 | })), |
| 272 | Effect.catchTag(["AccessDeniedException", "AWSOrganizationsNotInUseException"], (e) => |
| 273 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 274 | ), |
| 275 | ); |
| 276 | return yield* HttpServerResponse.json(result); |
| 277 | } |
| 278 | |
| 279 | if (request.method === "GET" && pathname === "/policies-for-target") { |
| 280 | const result = yield* Effect.gen(function* () { |
| 281 | const target = yield* rootId; |
| 282 | if (target === undefined) { |
| 283 | return { ok: false as const, tag: "NoRoot" }; |
| 284 | } |
| 285 | const r = yield* listPoliciesForTarget({ |
| 286 | TargetId: target, |
| 287 | Filter: "SERVICE_CONTROL_POLICY", |
| 288 | }); |
| 289 | return { ok: true as const, count: r.Policies?.length ?? 0 }; |
| 290 | }).pipe( |
| 291 | Effect.catchTag( |
| 292 | [ |
| 293 | "AccessDeniedException", |
| 294 | "AWSOrganizationsNotInUseException", |
| 295 | "TargetNotFoundException", |
| 296 | ], |
| 297 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 298 | ), |
| 299 | ); |
| 300 | return yield* HttpServerResponse.json(result); |
| 301 | } |
| 302 | |
| 303 | if (request.method === "GET" && pathname === "/targets-for-policy") { |
| 304 | // Discover a policy id from the SCP list (p-FullAWSAccess exists in |
| 305 | // every organization with SCPs available), then read its targets — |
| 306 | // proves ListTargetsForPolicy's grant end-to-end. |
| 307 | const result = yield* Effect.gen(function* () { |
| 308 | const r = yield* listPolicies({ |
| 309 | Filter: "SERVICE_CONTROL_POLICY", |
| 310 | }); |
| 311 | const policyId = r.Policies?.[0]?.Id; |
| 312 | if (policyId === undefined) { |
| 313 | return { ok: false as const, tag: "NoPolicy" }; |
| 314 | } |
| 315 | const t = yield* listTargetsForPolicy({ PolicyId: policyId }); |
| 316 | return { ok: true as const, count: t.Targets?.length ?? 0 }; |
| 317 | }).pipe( |
| 318 | Effect.catchTag( |
| 319 | [ |
| 320 | "AccessDeniedException", |
| 321 | "AWSOrganizationsNotInUseException", |
| 322 | "PolicyNotFoundException", |
| 323 | ], |
| 324 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 325 | ), |
| 326 | ); |
| 327 | return yield* HttpServerResponse.json(result); |
| 328 | } |
| 329 | |
| 330 | if (request.method === "GET" && pathname === "/effective-policy") { |
| 331 | // The standing organization has no tag policies enabled, so the |
| 332 | // grant is proved by the typed EffectivePolicyNotFoundException / |
| 333 | // ConstraintViolationException rather than live content. |
| 334 | const result = yield* describeEffectivePolicy({ |
| 335 | PolicyType: "TAG_POLICY", |
| 336 | }).pipe( |
| 337 | Effect.map((r) => ({ |
| 338 | ok: true as const, |
| 339 | policyType: r.EffectivePolicy?.PolicyType ?? null, |
| 340 | })), |
| 341 | Effect.catchTag( |
| 342 | [ |
| 343 | "AccessDeniedException", |
| 344 | "AWSOrganizationsNotInUseException", |
| 345 | "ConstraintViolationException", |
| 346 | "EffectivePolicyNotFoundException", |
| 347 | "TargetNotFoundException", |
| 348 | ], |
| 349 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 350 | ), |
| 351 | ); |
| 352 | return yield* HttpServerResponse.json(result); |
| 353 | } |
| 354 | |
| 355 | if (request.method === "GET" && pathname === "/invalid-effective-policy-accounts") { |
| 356 | const result = yield* listAccountsWithInvalidEffectivePolicy({ |
| 357 | PolicyType: "TAG_POLICY", |
| 358 | }).pipe( |
| 359 | Effect.map((r) => ({ |
| 360 | ok: true as const, |
| 361 | count: r.Accounts?.length ?? 0, |
| 362 | })), |
| 363 | Effect.catchTag( |
| 364 | [ |
| 365 | "AccessDeniedException", |
| 366 | "AWSOrganizationsNotInUseException", |
| 367 | "ConstraintViolationException", |
| 368 | "EffectivePolicyNotFoundException", |
| 369 | "InvalidInputException", |
| 370 | "UnsupportedAPIEndpointException", |
| 371 | ], |
| 372 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 373 | ), |
| 374 | ); |
| 375 | return yield* HttpServerResponse.json(result); |
| 376 | } |
| 377 | |
| 378 | if (request.method === "GET" && pathname === "/effective-policy-validation-errors") { |
| 379 | const result = yield* Effect.gen(function* () { |
| 380 | const accountId = yield* managementAccountId; |
| 381 | if (accountId === undefined) { |
| 382 | return { ok: false as const, tag: "NoOrganization" }; |
| 383 | } |
| 384 | const r = yield* listEffectivePolicyValidationErrors({ |
| 385 | AccountId: accountId, |
| 386 | PolicyType: "TAG_POLICY", |
| 387 | }); |
| 388 | return { |
| 389 | ok: true as const, |
| 390 | count: r.EffectivePolicyValidationErrors?.length ?? 0, |
| 391 | }; |
| 392 | }).pipe( |
| 393 | Effect.catchTag( |
| 394 | [ |
| 395 | "AccessDeniedException", |
| 396 | "AccountNotFoundException", |
| 397 | "AWSOrganizationsNotInUseException", |
| 398 | "ConstraintViolationException", |
| 399 | "EffectivePolicyNotFoundException", |
| 400 | "InvalidInputException", |
| 401 | "UnsupportedAPIEndpointException", |
| 402 | ], |
| 403 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 404 | ), |
| 405 | ); |
| 406 | return yield* HttpServerResponse.json(result); |
| 407 | } |
| 408 | |
| 409 | if (request.method === "GET" && pathname === "/delegated-administrators") { |
| 410 | const result = yield* listDelegatedAdministrators().pipe( |
| 411 | Effect.map((r) => ({ |
| 412 | ok: true as const, |
| 413 | count: r.DelegatedAdministrators?.length ?? 0, |
| 414 | })), |
| 415 | Effect.catchTag( |
| 416 | [ |
| 417 | "AccessDeniedException", |
| 418 | "AWSOrganizationsNotInUseException", |
| 419 | "ConstraintViolationException", |
| 420 | ], |
| 421 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 422 | ), |
| 423 | ); |
| 424 | return yield* HttpServerResponse.json(result); |
| 425 | } |
| 426 | |
| 427 | if (request.method === "GET" && pathname === "/delegated-services") { |
| 428 | // The management account is never a delegated administrator, so |
| 429 | // the typed AccountNotRegisteredException proves the grant. |
| 430 | const result = yield* Effect.gen(function* () { |
| 431 | const accountId = yield* managementAccountId; |
| 432 | if (accountId === undefined) { |
| 433 | return { ok: false as const, tag: "NoOrganization" }; |
| 434 | } |
| 435 | const r = yield* listDelegatedServicesForAccount({ |
| 436 | AccountId: accountId, |
| 437 | }); |
| 438 | return { |
| 439 | ok: true as const, |
| 440 | count: r.DelegatedServices?.length ?? 0, |
| 441 | }; |
| 442 | }).pipe( |
| 443 | Effect.catchTag( |
| 444 | [ |
| 445 | "AccessDeniedException", |
| 446 | "AccountNotFoundException", |
| 447 | "AccountNotRegisteredException", |
| 448 | "AWSOrganizationsNotInUseException", |
| 449 | "ConstraintViolationException", |
| 450 | ], |
| 451 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 452 | ), |
| 453 | ); |
| 454 | return yield* HttpServerResponse.json(result); |
| 455 | } |
| 456 | |
| 457 | if (request.method === "GET" && pathname === "/service-access") { |
| 458 | const result = yield* listAWSServiceAccessForOrganization().pipe( |
| 459 | Effect.map((r) => ({ |
| 460 | ok: true as const, |
| 461 | count: r.EnabledServicePrincipals?.length ?? 0, |
| 462 | })), |
| 463 | Effect.catchTag( |
| 464 | [ |
| 465 | "AccessDeniedException", |
| 466 | "AWSOrganizationsNotInUseException", |
| 467 | "ConstraintViolationException", |
| 468 | ], |
| 469 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 470 | ), |
| 471 | ); |
| 472 | return yield* HttpServerResponse.json(result); |
| 473 | } |
| 474 | |
| 475 | if (request.method === "GET" && pathname === "/tags") { |
| 476 | const result = yield* Effect.gen(function* () { |
| 477 | const accountId = yield* managementAccountId; |
| 478 | if (accountId === undefined) { |
| 479 | return { ok: false as const, tag: "NoOrganization" }; |
| 480 | } |
| 481 | const r = yield* listTagsForResource({ ResourceId: accountId }); |
| 482 | return { ok: true as const, count: r.Tags?.length ?? 0 }; |
| 483 | }).pipe( |
| 484 | Effect.catchTag( |
| 485 | [ |
| 486 | "AccessDeniedException", |
| 487 | "AWSOrganizationsNotInUseException", |
| 488 | "TargetNotFoundException", |
| 489 | ], |
| 490 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 491 | ), |
| 492 | ); |
| 493 | return yield* HttpServerResponse.json(result); |
| 494 | } |
| 495 | |
| 496 | if (request.method === "GET" && pathname === "/create-account-statuses") { |
| 497 | const result = yield* listCreateAccountStatus().pipe( |
| 498 | Effect.map((r) => ({ |
| 499 | ok: true as const, |
| 500 | count: r.CreateAccountStatuses?.length ?? 0, |
| 501 | })), |
| 502 | Effect.catchTag( |
| 503 | [ |
| 504 | "AccessDeniedException", |
| 505 | "AWSOrganizationsNotInUseException", |
| 506 | "UnsupportedAPIEndpointException", |
| 507 | ], |
| 508 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 509 | ), |
| 510 | ); |
| 511 | return yield* HttpServerResponse.json(result); |
| 512 | } |
| 513 | |
| 514 | if (request.method === "GET" && pathname === "/create-account-status-not-found") { |
| 515 | const tag = yield* describeCreateAccountStatus({ |
| 516 | CreateAccountRequestId: NONEXISTENT_CAR_ID, |
| 517 | }).pipe( |
| 518 | Effect.map(() => "Found"), |
| 519 | Effect.catchTag( |
| 520 | [ |
| 521 | "AccessDeniedException", |
| 522 | "AWSOrganizationsNotInUseException", |
| 523 | "CreateAccountStatusNotFoundException", |
| 524 | "InvalidInputException", |
| 525 | "UnsupportedAPIEndpointException", |
| 526 | ], |
| 527 | (e) => Effect.succeed(e._tag), |
| 528 | ), |
| 529 | ); |
| 530 | return yield* HttpServerResponse.json({ tag }); |
| 531 | } |
| 532 | |
| 533 | if (request.method === "GET" && pathname === "/handshakes-account") { |
| 534 | const result = yield* listHandshakesForAccount().pipe( |
| 535 | Effect.map((r) => ({ |
| 536 | ok: true as const, |
| 537 | count: r.Handshakes?.length ?? 0, |
| 538 | })), |
| 539 | Effect.catchTag(["AccessDeniedException", "ConcurrentModificationException"], (e) => |
| 540 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 541 | ), |
| 542 | ); |
| 543 | return yield* HttpServerResponse.json(result); |
| 544 | } |
| 545 | |
| 546 | if (request.method === "GET" && pathname === "/handshakes-org") { |
| 547 | const result = yield* listHandshakesForOrganization().pipe( |
| 548 | Effect.map((r) => ({ |
| 549 | ok: true as const, |
| 550 | count: r.Handshakes?.length ?? 0, |
| 551 | })), |
| 552 | Effect.catchTag( |
| 553 | [ |
| 554 | "AccessDeniedException", |
| 555 | "AWSOrganizationsNotInUseException", |
| 556 | "ConcurrentModificationException", |
| 557 | ], |
| 558 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 559 | ), |
| 560 | ); |
| 561 | return yield* HttpServerResponse.json(result); |
| 562 | } |
| 563 | |
| 564 | if (request.method === "GET" && pathname === "/handshake-not-found") { |
| 565 | const tag = yield* describeHandshake({ |
| 566 | HandshakeId: NONEXISTENT_HANDSHAKE_ID, |
| 567 | }).pipe( |
| 568 | Effect.map(() => "Found"), |
| 569 | Effect.catchTag( |
| 570 | ["AccessDeniedException", "HandshakeNotFoundException", "InvalidInputException"], |
| 571 | (e) => Effect.succeed(e._tag), |
| 572 | ), |
| 573 | ); |
| 574 | return yield* HttpServerResponse.json({ tag }); |
| 575 | } |
| 576 | |
| 577 | if (request.method === "GET" && pathname === "/accept-handshake-not-found") { |
| 578 | const tag = yield* acceptHandshake({ |
| 579 | HandshakeId: NONEXISTENT_HANDSHAKE_ID, |
| 580 | }).pipe( |
| 581 | Effect.map(() => "Found"), |
| 582 | Effect.catchTag( |
| 583 | [ |
| 584 | "AccessDeniedException", |
| 585 | "AWSOrganizationsNotInUseException", |
| 586 | "HandshakeNotFoundException", |
| 587 | "InvalidInputException", |
| 588 | ], |
| 589 | (e) => Effect.succeed(e._tag), |
| 590 | ), |
| 591 | ); |
| 592 | return yield* HttpServerResponse.json({ tag }); |
| 593 | } |
| 594 | |
| 595 | if (request.method === "GET" && pathname === "/decline-handshake-not-found") { |
| 596 | const tag = yield* declineHandshake({ |
| 597 | HandshakeId: NONEXISTENT_HANDSHAKE_ID, |
| 598 | }).pipe( |
| 599 | Effect.map(() => "Found"), |
| 600 | Effect.catchTag( |
| 601 | ["AccessDeniedException", "HandshakeNotFoundException", "InvalidInputException"], |
| 602 | (e) => Effect.succeed(e._tag), |
| 603 | ), |
| 604 | ); |
| 605 | return yield* HttpServerResponse.json({ tag }); |
| 606 | } |
| 607 | |
| 608 | if (request.method === "GET" && pathname === "/cancel-handshake-not-found") { |
| 609 | const tag = yield* cancelHandshake({ |
| 610 | HandshakeId: NONEXISTENT_HANDSHAKE_ID, |
| 611 | }).pipe( |
| 612 | Effect.map(() => "Found"), |
| 613 | Effect.catchTag( |
| 614 | ["AccessDeniedException", "HandshakeNotFoundException", "InvalidInputException"], |
| 615 | (e) => Effect.succeed(e._tag), |
| 616 | ), |
| 617 | ); |
| 618 | return yield* HttpServerResponse.json({ tag }); |
| 619 | } |
| 620 | |
| 621 | if (request.method === "GET" && pathname === "/invite-invalid") { |
| 622 | // A syntactically invalid target id — proves the grant via the |
| 623 | // typed InvalidInputException without ever creating a real |
| 624 | // handshake. |
| 625 | const tag = yield* inviteAccountToOrganization({ |
| 626 | Target: { Id: "not-a-valid-account-id", Type: "ACCOUNT" }, |
| 627 | }).pipe( |
| 628 | Effect.map(() => "Invited"), |
| 629 | Effect.catchTag( |
| 630 | [ |
| 631 | "AccessDeniedException", |
| 632 | "AWSOrganizationsNotInUseException", |
| 633 | "ConstraintViolationException", |
| 634 | "HandshakeConstraintViolationException", |
| 635 | "InvalidInputException", |
| 636 | ], |
| 637 | (e) => Effect.succeed(e._tag), |
| 638 | ), |
| 639 | ); |
| 640 | return yield* HttpServerResponse.json({ tag }); |
| 641 | } |
| 642 | |
| 643 | return yield* HttpServerResponse.json( |
| 644 | { error: "Not found", method: request.method, pathname }, |
| 645 | { status: 404 }, |
| 646 | ); |
| 647 | }).pipe(Effect.orDie), |
| 648 | }; |
| 649 | }).pipe( |
| 650 | Effect.provide( |
| 651 | Layer.mergeAll( |
| 652 | Lambda.EventSource, |
| 653 | Organizations.DescribeOrganizationHttp, |
| 654 | Organizations.ListRootsHttp, |
| 655 | Organizations.ListAccountsHttp, |
| 656 | Organizations.ListAccountsForParentHttp, |
| 657 | Organizations.ListOrganizationalUnitsForParentHttp, |
| 658 | Organizations.ListChildrenHttp, |
| 659 | Organizations.ListParentsHttp, |
| 660 | Organizations.ListPoliciesHttp, |
| 661 | Organizations.ListPoliciesForTargetHttp, |
| 662 | Organizations.ListTargetsForPolicyHttp, |
| 663 | Organizations.DescribeEffectivePolicyHttp, |
| 664 | Organizations.ListAccountsWithInvalidEffectivePolicyHttp, |
| 665 | Organizations.ListEffectivePolicyValidationErrorsHttp, |
| 666 | Organizations.ListDelegatedAdministratorsHttp, |
| 667 | Organizations.ListDelegatedServicesForAccountHttp, |
| 668 | Organizations.ListAWSServiceAccessForOrganizationHttp, |
| 669 | Organizations.ListTagsForResourceHttp, |
| 670 | Organizations.DescribeCreateAccountStatusHttp, |
| 671 | Organizations.ListCreateAccountStatusHttp, |
| 672 | Organizations.InviteAccountToOrganizationHttp, |
| 673 | Organizations.AcceptHandshakeHttp, |
| 674 | Organizations.DeclineHandshakeHttp, |
| 675 | Organizations.CancelHandshakeHttp, |
| 676 | Organizations.DescribeHandshakeHttp, |
| 677 | Organizations.ListHandshakesForAccountHttp, |
| 678 | Organizations.ListHandshakesForOrganizationHttp, |
| 679 | ), |
| 680 | ), |
| 681 | ), |
| 682 | ); |