@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 147 lines 5.16 KB
Raw
1 import * as ag from "@distilled.cloud/aws/api-gateway";
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 type { Providers } from "../Providers.ts";
7 import { retryOnApiStatusUpdating } from "./common.ts";
8
9 export interface AccountProps {
10 /**
11 * IAM role ARN for API Gateway to push logs to CloudWatch.
12 */
13 cloudwatchRoleArn?: string;
14 }
15
16 /** @resource */
17 export interface Account extends Resource<
18 "AWS.ApiGateway.Account",
19 AccountProps,
20 {
21 cloudwatchRoleArn: string | undefined;
22 /**
23 * True when this stack last applied a desired `cloudwatchRoleArn` (including clearing it).
24 * Used so destroy does not remove a role the stack never configured.
25 */
26 managesCloudwatchRoleArn: boolean;
27 },
28 never,
29 Providers
30 > {}
31
32 /**
33 * Account-level settings for Amazon API Gateway in the current region
34 * (CloudWatch logging role, etc.).
35 *
36 * ### Account settings
37 * **Example:** Set logging role
38 * ```typescript
39 * yield* ApiGateway.Account("Account", {
40 * cloudwatchRoleArn: role.roleArn,
41 * });
42 * ```
43 */
44 const AccountResource = Resource<Account>("AWS.ApiGateway.Account");
45
46 export { AccountResource as Account };
47
48 export const AccountProvider = () =>
49 Provider.effect(
50 AccountResource,
51 Effect.gen(function* () {
52 return {
53 diff: Effect.fn(function* ({ news: newsIn, olds, output }) {
54 if (!isResolved(newsIn)) return;
55 const news = newsIn as AccountProps;
56 const prevManages = output?.managesCloudwatchRoleArn ?? false;
57 const nextManages = news.cloudwatchRoleArn !== undefined;
58 if (nextManages) {
59 if (news.cloudwatchRoleArn !== olds.cloudwatchRoleArn) {
60 return { action: "update" } as const;
61 }
62 } else if (prevManages) {
63 return { action: "update" } as const;
64 }
65 }),
66 read: Effect.fn(function* ({ output }) {
67 const a = yield* ag
68 .getAccount({})
69 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
70 return {
71 cloudwatchRoleArn: a?.cloudwatchRoleArn,
72 managesCloudwatchRoleArn: output?.managesCloudwatchRoleArn ?? false,
73 };
74 }),
75 // Account settings are an account/region singleton — there is no
76 // collection API, only `getAccount`. Return the single instance as a
77 // one-element array (account singleton pattern).
78 list: () =>
79 ag.getAccount({}).pipe(
80 Effect.map((a) => [
81 {
82 cloudwatchRoleArn: a.cloudwatchRoleArn,
83 managesCloudwatchRoleArn: false,
84 },
85 ]),
86 Effect.catchTag("NotFoundException", () => Effect.succeed([])),
87 ),
88 reconcile: Effect.fn(function* ({ news: newsIn, session }) {
89 if (!isResolved(newsIn)) {
90 return yield* Effect.die("Account props were not resolved");
91 }
92 const news = newsIn as AccountProps;
93
94 // Observe — read the current account-level CloudWatch role from
95 // API Gateway. The account settings resource is a singleton per
96 // region, so there is no `ensure` step: we always sync.
97 const observed = yield* ag
98 .getAccount({})
99 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
100 const observedRoleArn = observed?.cloudwatchRoleArn;
101
102 // Sync the cloudwatchRoleArn — observed ↔ desired. We treat
103 // `undefined` desired as "do not manage the field" so the user
104 // can opt out without us clobbering whatever was already set
105 // out of band.
106 const manages = news.cloudwatchRoleArn !== undefined;
107 if (manages) {
108 const desired = news.cloudwatchRoleArn;
109 if (desired !== observedRoleArn) {
110 yield* retryOnApiStatusUpdating(
111 ag.updateAccount({
112 patchOperations: desired
113 ? [
114 {
115 op: "replace",
116 path: "/cloudwatchRoleArn",
117 value: desired,
118 },
119 ]
120 : [{ op: "remove", path: "/cloudwatchRoleArn" }],
121 }),
122 );
123 yield* session.note("Updated API Gateway account settings");
124 }
125 }
126
127 const final = yield* ag.getAccount({});
128 return {
129 cloudwatchRoleArn: final.cloudwatchRoleArn,
130 managesCloudwatchRoleArn: manages,
131 };
132 }),
133 delete: Effect.fn(function* ({ output, session }) {
134 if (output.managesCloudwatchRoleArn) {
135 yield* retryOnApiStatusUpdating(
136 ag
137 .updateAccount({
138 patchOperations: [{ op: "remove", path: "/cloudwatchRoleArn" }],
139 })
140 .pipe(Effect.catchTag("BadRequestException", () => Effect.void)),
141 );
142 yield* session.note("Cleared API Gateway account CloudWatch role");
143 }
144 }),
145 };
146 }),
147 );