@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 176 lines 6.09 KB
Raw
1 import * as cip from "@distilled.cloud/aws/cognito-identity-provider";
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 type { Providers } from "../Providers.ts";
8
9 export interface GroupProps {
10 /**
11 * The ID of the user pool the group belongs to. Changing this triggers a
12 * replacement.
13 */
14 userPoolId: string;
15 /**
16 * Name of the group. If omitted, a deterministic physical name is
17 * generated from the app, stage, and logical ID. Changing this triggers a
18 * replacement.
19 */
20 groupName?: string;
21 /**
22 * Description of the group (up to 2048 characters).
23 */
24 description?: string;
25 /**
26 * ARN of an IAM role associated with the group. Users in the group can
27 * assume this role via an identity pool (`cognito:roles` /
28 * `cognito:preferred_role` claims).
29 */
30 roleArn?: string;
31 /**
32 * Non-negative precedence; lower values take priority when a user belongs
33 * to multiple groups with role ARNs.
34 */
35 precedence?: number;
36 }
37
38 export interface Group extends Resource<
39 "AWS.Cognito.Group",
40 GroupProps,
41 {
42 /** The name of the group. */
43 groupName: string;
44 /** The ID of the user pool the group belongs to. */
45 userPoolId: string;
46 },
47 never,
48 Providers
49 > {}
50
51 /**
52 * A group within an Amazon Cognito user pool. Groups organize users, appear
53 * in the `cognito:groups` token claim, and can carry an IAM role for
54 * identity-pool federation.
55 * ### Creating Groups
56 * **Example:** Basic Group
57 * ```typescript
58 * import * as Cognito from "@alchemy.run/aws/Cognito";
59 *
60 * const pool = yield* Cognito.UserPool("Users", {});
61 * const admins = yield* Cognito.Group("Admins", {
62 * userPoolId: pool.userPoolId,
63 * description: "Administrators",
64 * });
65 * ```
66 *
67 * **Example:** Group with Role and Precedence
68 * ```typescript
69 * const admins = yield* Cognito.Group("Admins", {
70 * userPoolId: pool.userPoolId,
71 * roleArn: role.roleArn,
72 * precedence: 1,
73 * });
74 * ```
75 *
76 * @resource
77 */
78 export const Group = Resource<Group>("AWS.Cognito.Group");
79
80 export const GroupProvider = () =>
81 Provider.effect(
82 Group,
83 Effect.gen(function* () {
84 const createName = Effect.fn(function* (id: string, props: Pick<GroupProps, "groupName">) {
85 return props.groupName ?? (yield* createPhysicalName({ id, maxLength: 128 }));
86 });
87
88 const getGroup = Effect.fn(function* (userPoolId: string, groupName: string) {
89 return yield* cip.getGroup({ UserPoolId: userPoolId, GroupName: groupName }).pipe(
90 Effect.map((r) => r.Group),
91 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
92 );
93 });
94
95 return Group.Provider.of({
96 stables: ["groupName", "userPoolId"],
97
98 // Sub-resource keyed entirely by its user pool (userPoolId) with no global
99 // enumeration API of its own — nuke reaches it through the parent's
100 // deletion, so enumeration returns empty per the ProviderService
101 // doctrine.
102 list: () => Effect.succeed([]),
103
104 read: Effect.fn(function* ({ id, olds, output }) {
105 const userPoolId = output?.userPoolId ?? olds?.userPoolId;
106 if (userPoolId === undefined) return undefined;
107 const name = output?.groupName ?? (yield* createName(id, olds ?? {}));
108 const observed = yield* getGroup(userPoolId, name);
109 return observed === undefined ? undefined : { groupName: name, userPoolId };
110 }),
111
112 diff: Effect.fn(function* ({ id, news, olds }) {
113 if (!isResolved(news)) return undefined;
114 const oldName = yield* createName(id, olds ?? {});
115 const newName = yield* createName(id, news ?? {});
116 if (oldName !== newName || olds?.userPoolId !== news?.userPoolId) {
117 return { action: "replace" } as const;
118 }
119 }),
120
121 reconcile: Effect.fn(function* ({ id, news, output, session }) {
122 const groupName = output?.groupName ?? (yield* createName(id, news));
123 const userPoolId = news.userPoolId;
124
125 // 1. OBSERVE
126 let observed = yield* getGroup(userPoolId, groupName);
127
128 // 2. ENSURE — tolerate the create race.
129 if (observed === undefined) {
130 observed = yield* cip
131 .createGroup({
132 UserPoolId: userPoolId,
133 GroupName: groupName,
134 Description: news.description,
135 RoleArn: news.roleArn,
136 Precedence: news.precedence,
137 })
138 .pipe(
139 Effect.map((r) => r.Group),
140 Effect.catchTag("GroupExistsException", () => getGroup(userPoolId, groupName)),
141 );
142 } else {
143 // 3. SYNC — description/role/precedence are mutable in place.
144 const drift =
145 (news.description !== undefined && observed.Description !== news.description) ||
146 (news.roleArn !== undefined && observed.RoleArn !== news.roleArn) ||
147 (news.precedence !== undefined && observed.Precedence !== news.precedence) ||
148 (news.description === undefined && observed.Description !== undefined) ||
149 (news.roleArn === undefined && observed.RoleArn !== undefined) ||
150 (news.precedence === undefined && observed.Precedence !== undefined);
151 if (drift) {
152 yield* cip.updateGroup({
153 UserPoolId: userPoolId,
154 GroupName: groupName,
155 Description: news.description,
156 RoleArn: news.roleArn,
157 Precedence: news.precedence,
158 });
159 }
160 }
161
162 yield* session.note(groupName);
163 return { groupName, userPoolId };
164 }),
165
166 delete: Effect.fn(function* ({ output }) {
167 yield* cip
168 .deleteGroup({
169 UserPoolId: output.userPoolId,
170 GroupName: output.groupName,
171 })
172 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
173 }),
174 });
175 }),
176 );