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

main
ts 203 lines 8.05 KB
Raw
1 import * as appregistry from "@distilled.cloud/aws/service-catalog-appregistry";
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 * as Stream from "effect/Stream";
7 import { AWSEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9
10 export interface AttributeGroupAssociationProps {
11 /**
12 * The application to associate the attribute group with: its ID, name, or
13 * ARN. Changing it replaces the association.
14 */
15 application: string;
16 /**
17 * The attribute group to associate: its ID, name, or ARN. Changing it
18 * replaces the association.
19 */
20 attributeGroup: string;
21 }
22
23 export interface AttributeGroupAssociation extends Resource<
24 "AWS.AppRegistry.AttributeGroupAssociation",
25 AttributeGroupAssociationProps,
26 {
27 /** The ID of the associated application. */
28 applicationId: string;
29 /** The ARN of the associated application. */
30 applicationArn: string;
31 /** The ID of the associated attribute group. */
32 attributeGroupId: string;
33 /** The ARN of the associated attribute group. */
34 attributeGroupArn: string;
35 },
36 never,
37 Providers
38 > {}
39
40 /**
41 * Associates an AppRegistry {@link AttributeGroup} with an
42 * {@link Application} so the group's user-defined JSON metadata augments the
43 * application's machine-readable description.
44 *
45 * ### Associating an Attribute Group
46 * **Example:** Attach an Attribute Group to an Application
47 * ```typescript
48 * import * as AppRegistry from "@alchemy.run/aws/AppRegistry";
49 *
50 * const app = yield* AppRegistry.Application("Storefront", {});
51 * const group = yield* AppRegistry.AttributeGroup("Ownership", {
52 * attributes: { owner: "commerce-team" },
53 * });
54 *
55 * const association = yield* AppRegistry.AttributeGroupAssociation("Assoc", {
56 * application: app.applicationId,
57 * attributeGroup: group.attributeGroupId,
58 * });
59 * ```
60 *
61 * @resource
62 */
63 export const AttributeGroupAssociation = Resource<AttributeGroupAssociation>(
64 "AWS.AppRegistry.AttributeGroupAssociation",
65 );
66
67 export const AttributeGroupAssociationProvider = () =>
68 Provider.effect(
69 AttributeGroupAssociation,
70 Effect.gen(function* () {
71 // getApplication / getAttributeGroup both accept a name, ID, or ARN.
72 const observeApplication = Effect.fn(function* (specifier: string) {
73 return yield* appregistry
74 .getApplication({ application: specifier })
75 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
76 });
77 const observeAttributeGroup = Effect.fn(function* (specifier: string) {
78 return yield* appregistry
79 .getAttributeGroup({ attributeGroup: specifier })
80 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
81 });
82
83 const isAssociated = Effect.fn(function* (applicationId: string, attributeGroupId: string) {
84 const groupIds = yield* appregistry.listAssociatedAttributeGroups
85 .pages({ application: applicationId })
86 .pipe(
87 Stream.runCollect,
88 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.attributeGroups ?? [])),
89 );
90 return groupIds.includes(attributeGroupId);
91 });
92
93 return AttributeGroupAssociation.Provider.of({
94 stables: ["applicationId", "applicationArn", "attributeGroupId", "attributeGroupArn"],
95 // The application/attribute-group pair IS the association's identity —
96 // changing either side replaces the association.
97 diff: Effect.fn(function* ({ olds, news }) {
98 if (!isResolved(news)) return undefined;
99 if (
100 olds !== undefined &&
101 (olds.application !== news.application || olds.attributeGroup !== news.attributeGroup)
102 ) {
103 return { action: "replace" } as const;
104 }
105 }),
106 read: Effect.fn(function* ({ olds, output }) {
107 const applicationSpecifier = output?.applicationId ?? olds?.application;
108 const attributeGroupSpecifier = output?.attributeGroupId ?? olds?.attributeGroup;
109 if (applicationSpecifier === undefined || attributeGroupSpecifier === undefined) {
110 return undefined;
111 }
112 const application = yield* observeApplication(applicationSpecifier);
113 if (application?.id === undefined) return undefined;
114 const attributeGroup = yield* observeAttributeGroup(attributeGroupSpecifier);
115 if (attributeGroup?.id === undefined) return undefined;
116 if (!(yield* isAssociated(application.id, attributeGroup.id))) {
117 return undefined;
118 }
119 // Associations cannot carry tags; existence between our two parents
120 // is the ownership signal.
121 return {
122 applicationId: application.id,
123 applicationArn: application.arn!,
124 attributeGroupId: attributeGroup.id,
125 attributeGroupArn: attributeGroup.arn!,
126 };
127 }),
128 reconcile: Effect.fn(function* ({ news, session }) {
129 // 1. OBSERVE — resolve both sides to canonical IDs; a missing parent
130 // surfaces its typed ResourceNotFoundException.
131 const application = yield* appregistry.getApplication({
132 application: news.application,
133 });
134 const attributeGroup = yield* appregistry.getAttributeGroup({
135 attributeGroup: news.attributeGroup,
136 });
137 const applicationId = application.id!;
138 const attributeGroupId = attributeGroup.id!;
139
140 // 2. ENSURE — associate when missing; a ConflictException is a
141 // concurrent-associate race, not a failure.
142 if (!(yield* isAssociated(applicationId, attributeGroupId))) {
143 yield* appregistry
144 .associateAttributeGroup({
145 application: applicationId,
146 attributeGroup: attributeGroupId,
147 })
148 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
149 }
150
151 yield* session.note(`${applicationId}/${attributeGroupId}`);
152 return {
153 applicationId,
154 applicationArn: application.arn!,
155 attributeGroupId,
156 attributeGroupArn: attributeGroup.arn!,
157 };
158 }),
159 delete: Effect.fn(function* ({ output }) {
160 yield* appregistry
161 .disassociateAttributeGroup({
162 application: output.applicationId,
163 attributeGroup: output.attributeGroupId,
164 })
165 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
166 }),
167 list: () =>
168 Effect.gen(function* () {
169 const { accountId, region } = yield* AWSEnvironment.current;
170 const applications = yield* appregistry.listApplications.pages({}).pipe(
171 Stream.runCollect,
172 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.applications ?? [])),
173 );
174 const results: {
175 applicationId: string;
176 applicationArn: string;
177 attributeGroupId: string;
178 attributeGroupArn: string;
179 }[] = [];
180 for (const application of applications) {
181 if (application.id === undefined) continue;
182 const groupIds = yield* appregistry.listAssociatedAttributeGroups
183 .pages({ application: application.id })
184 .pipe(
185 Stream.runCollect,
186 Effect.map((chunk) =>
187 Array.from(chunk).flatMap((page) => page.attributeGroups ?? []),
188 ),
189 );
190 for (const attributeGroupId of groupIds) {
191 results.push({
192 applicationId: application.id,
193 applicationArn: application.arn!,
194 attributeGroupId,
195 attributeGroupArn: `arn:aws:servicecatalog:${region}:${accountId}:/attribute-groups/${attributeGroupId}`,
196 });
197 }
198 }
199 return results;
200 }),
201 });
202 }),
203 );