The best way to control Claude Code, Codex and more from any computer or phone

main
ts 174 lines 5.66 KB
Raw
1 import * as iam from "@distilled.cloud/aws/iam";
2 import { isResolved } from "alchemy/Diff";
3 import type { Input } from "alchemy/Input";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9
10 export interface GroupMembershipProps {
11 /**
12 * Name of the IAM group to manage membership for.
13 */
14 groupName: Input<string>;
15 /**
16 * Exact set of user names that should be members of the group.
17 */
18 userNames: Input<string[]>;
19 }
20
21 export interface GroupMembership extends Resource<
22 "AWS.IAM.GroupMembership",
23 GroupMembershipProps,
24 {
25 /** The group whose membership is managed. */
26 groupName: string;
27 /** The user names currently in the group. */
28 userNames: string[];
29 },
30 never,
31 Providers
32 > {}
33
34 /**
35 * An explicit IAM group membership resource that owns a group's managed users.
36 *
37 * `GroupMembership` models the exact set of users in a group, making membership
38 * reconciliation explicit instead of spreading it across user or group resources.
39 * ### Managing Group Membership
40 * **Example:** Sync a Group's Members
41 * ```typescript
42 * const admins = yield* Group("Admins", {
43 * groupName: "admins",
44 * });
45 *
46 * const alice = yield* User("Alice", {
47 * userName: "alice",
48 * });
49 *
50 * const bob = yield* User("Bob", {
51 * userName: "bob",
52 * });
53 *
54 * const membership = yield* GroupMembership("AdminsMembership", {
55 * groupName: admins.groupName,
56 * userNames: [alice.userName, bob.userName],
57 * });
58 * ```
59 *
60 * @resource
61 */
62 export const GroupMembership = Resource<GroupMembership>("AWS.IAM.GroupMembership");
63
64 export const GroupMembershipProvider = () =>
65 Provider.succeed(GroupMembership, {
66 stables: ["groupName"],
67 diff: Effect.fn(function* ({ olds, news }) {
68 if (!isResolved(news)) return;
69 if (olds.groupName !== news.groupName) {
70 return { action: "replace" } as const;
71 }
72 }),
73 read: Effect.fn(function* ({ output }) {
74 if (!output) {
75 return undefined;
76 }
77 const response = yield* iam
78 .getGroup({
79 GroupName: output.groupName,
80 })
81 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.undefined));
82 if (!response?.Group?.GroupName) {
83 return undefined;
84 }
85 return {
86 groupName: response.Group.GroupName,
87 userNames: (response.Users ?? [])
88 .map((user) => user.UserName)
89 .filter((userName): userName is string => typeof userName === "string"),
90 };
91 }),
92 reconcile: Effect.fn(function* ({ news, session }) {
93 const groupName = news.groupName as string;
94 const desiredUsers = news.userNames as string[];
95
96 // Observe — read the actual membership of the group. `getGroup`
97 // both confirms the group exists and returns the current user list
98 // in a single call.
99 const response = yield* iam
100 .getGroup({ GroupName: groupName })
101 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.undefined));
102 const observedUsers = (response?.Users ?? [])
103 .map((user) => user.UserName)
104 .filter((userName): userName is string => typeof userName === "string");
105
106 // Sync — diff observed against desired and apply only the delta.
107 const observedSet = new Set(observedUsers);
108 const desiredSet = new Set(desiredUsers);
109 for (const userName of desiredUsers) {
110 if (!observedSet.has(userName)) {
111 yield* iam.addUserToGroup({
112 GroupName: groupName,
113 UserName: userName,
114 });
115 }
116 }
117 for (const userName of observedUsers) {
118 if (!desiredSet.has(userName)) {
119 yield* iam
120 .removeUserFromGroup({
121 GroupName: groupName,
122 UserName: userName,
123 })
124 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void));
125 }
126 }
127
128 yield* session.note(groupName);
129 return {
130 groupName,
131 userNames: desiredUsers,
132 };
133 }),
134 list: Effect.fn(function* () {
135 // The membership resource is keyed by groupName and its Attributes are
136 // the full set of users in that group, so enumerate every IAM group
137 // and read each group's user list — one item per group, matching the
138 // shape `read` produces.
139 const groups = yield* iam.listGroups.pages({}).pipe(
140 Stream.runCollect,
141 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Groups ?? [])),
142 );
143 const rows = yield* Effect.forEach(
144 groups,
145 (group) =>
146 iam.getGroup.pages({ GroupName: group.GroupName }).pipe(
147 Stream.runCollect,
148 Effect.map((chunk) => ({
149 groupName: group.GroupName,
150 userNames: Array.from(chunk)
151 .flatMap((page) => page.Users ?? [])
152 .map((user) => user.UserName)
153 .filter((userName): userName is string => typeof userName === "string"),
154 })),
155 // The group may be deleted between listGroups and getGroup.
156 Effect.catchTag("NoSuchEntityException", () => Effect.undefined),
157 ),
158 { concurrency: 10 },
159 );
160 return rows.filter(
161 (row): row is { groupName: string; userNames: string[] } => row !== undefined,
162 );
163 }),
164 delete: Effect.fn(function* ({ output }) {
165 for (const userName of output.userNames) {
166 yield* iam
167 .removeUserFromGroup({
168 GroupName: output.groupName,
169 UserName: userName,
170 })
171 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void));
172 }
173 }),
174 });