@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, 04:45 UTC

main
ts 317 lines 12.3 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as LicenseManager from "@alchemy.run/aws/LicenseManager";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 // Well-formed-but-nonexistent identifiers — drive the typed error paths,
13 // proving the account-level grants reach the API (an IAM gap would surface
14 // AccessDeniedException and 500 the route instead).
15 const BOGUS_LICENSE_ARN =
16 "arn:aws:license-manager::111111111111:license:l-00000000000000000000000000000000";
17 const BOGUS_GRANT_ARN =
18 "arn:aws:license-manager::111111111111:grant:g-00000000000000000000000000000000";
19 const BOGUS_RESOURCE_ARN = "arn:aws:ec2:us-east-1::image/ami-00000000000000000";
20
21 export class LicenseManagerTestFunction extends Lambda.Function<Lambda.Function>()(
22 "LicenseManagerTestFunction",
23 ) {}
24
25 export default LicenseManagerTestFunction.make(
26 {
27 main,
28 functionUrl: true,
29 timeout: Duration.seconds(30),
30 },
31 Effect.gen(function* () {
32 const licenses = yield* LicenseManager.LicenseConfiguration("BindingsLicenses", {
33 licenseCountingType: "vCPU",
34 licenseCount: 5,
35 description: "alchemy license-manager bindings fixture",
36 });
37
38 // --- configuration-scoped bindings ---
39 const getConfiguration = yield* LicenseManager.GetLicenseConfiguration(licenses);
40 const listAssociations =
41 yield* LicenseManager.ListAssociationsForLicenseConfiguration(licenses);
42 const listUsage = yield* LicenseManager.ListUsageForLicenseConfiguration(licenses);
43 const listFailures =
44 yield* LicenseManager.ListFailuresForLicenseConfigurationOperations(licenses);
45
46 // --- checkout data plane (account-level) ---
47 const checkoutLicense = yield* LicenseManager.CheckoutLicense();
48 const checkInLicense = yield* LicenseManager.CheckInLicense();
49 const checkoutBorrowLicense = yield* LicenseManager.CheckoutBorrowLicense();
50 const extendLicenseConsumption = yield* LicenseManager.ExtendLicenseConsumption();
51 const getAccessToken = yield* LicenseManager.GetAccessToken();
52
53 // --- license/grant reads (account-level) ---
54 const getLicense = yield* LicenseManager.GetLicense();
55 const getLicenseUsage = yield* LicenseManager.GetLicenseUsage();
56 const listLicenses = yield* LicenseManager.ListLicenses();
57 const listLicenseVersions = yield* LicenseManager.ListLicenseVersions();
58 const listReceivedLicenses = yield* LicenseManager.ListReceivedLicenses();
59 const getGrant = yield* LicenseManager.GetGrant();
60 const acceptGrant = yield* LicenseManager.AcceptGrant();
61 const rejectGrant = yield* LicenseManager.RejectGrant();
62 const listReceivedGrants = yield* LicenseManager.ListReceivedGrants();
63 const listDistributedGrants = yield* LicenseManager.ListDistributedGrants();
64
65 // --- inventory + specifications + settings (account-level) ---
66 const listResourceInventory = yield* LicenseManager.ListResourceInventory();
67 const listLicenseSpecificationsForResource =
68 yield* LicenseManager.ListLicenseSpecificationsForResource();
69 const updateLicenseSpecificationsForResource =
70 yield* LicenseManager.UpdateLicenseSpecificationsForResource();
71 const getServiceSettings = yield* LicenseManager.GetServiceSettings();
72
73 const bound = {
74 getConfiguration,
75 listAssociations,
76 listUsage,
77 listFailures,
78 checkoutLicense,
79 checkInLicense,
80 checkoutBorrowLicense,
81 extendLicenseConsumption,
82 getAccessToken,
83 getLicense,
84 getLicenseUsage,
85 listLicenses,
86 listLicenseVersions,
87 listReceivedLicenses,
88 getGrant,
89 acceptGrant,
90 rejectGrant,
91 listReceivedGrants,
92 listDistributedGrants,
93 listResourceInventory,
94 listLicenseSpecificationsForResource,
95 updateLicenseSpecificationsForResource,
96 getServiceSettings,
97 };
98
99 return {
100 fetch: Effect.gen(function* () {
101 const request = yield* HttpServerRequest;
102 const url = new URL(request.originalUrl);
103 const pathname = url.pathname;
104
105 if (request.method === "GET" && pathname === "/bindings") {
106 return yield* HttpServerResponse.json({
107 bound: Object.keys(bound),
108 });
109 }
110
111 if (request.method === "GET" && pathname === "/configuration") {
112 const config = yield* getConfiguration();
113 return yield* HttpServerResponse.json({
114 name: config.Name ?? null,
115 countingType: config.LicenseCountingType ?? null,
116 licenseCount: config.LicenseCount ?? null,
117 });
118 }
119
120 if (request.method === "GET" && pathname === "/associations") {
121 const result = yield* listAssociations();
122 return yield* HttpServerResponse.json({
123 count: (result.LicenseConfigurationAssociations ?? []).length,
124 });
125 }
126
127 if (request.method === "GET" && pathname === "/usage") {
128 const result = yield* listUsage();
129 return yield* HttpServerResponse.json({
130 count: (result.LicenseConfigurationUsageList ?? []).length,
131 });
132 }
133
134 if (request.method === "GET" && pathname === "/failures") {
135 const result = yield* listFailures();
136 return yield* HttpServerResponse.json({
137 count: (result.LicenseOperationFailureList ?? []).length,
138 });
139 }
140
141 if (request.method === "GET" && pathname === "/licenses") {
142 const result = yield* listLicenses();
143 return yield* HttpServerResponse.json({
144 count: (result.Licenses ?? []).length,
145 });
146 }
147
148 if (request.method === "GET" && pathname === "/received-licenses") {
149 const result = yield* listReceivedLicenses();
150 return yield* HttpServerResponse.json({
151 count: (result.Licenses ?? []).length,
152 });
153 }
154
155 if (request.method === "GET" && pathname === "/received-grants") {
156 const result = yield* listReceivedGrants();
157 return yield* HttpServerResponse.json({
158 count: (result.Grants ?? []).length,
159 });
160 }
161
162 if (request.method === "GET" && pathname === "/distributed-grants") {
163 const result = yield* listDistributedGrants();
164 return yield* HttpServerResponse.json({
165 count: (result.Grants ?? []).length,
166 });
167 }
168
169 if (request.method === "GET" && pathname === "/inventory") {
170 // Resource inventory requires Systems Manager inventory; an account
171 // without it rejects with the typed FailedDependencyException —
172 // either outcome proves the binding + grant.
173 const result = yield* listResourceInventory().pipe(
174 Effect.map((r) => ({
175 tag: "Ok",
176 count: (r.ResourceInventoryList ?? []).length,
177 })),
178 Effect.catchTag("FailedDependencyException", (e) =>
179 Effect.succeed({ tag: e._tag, count: 0 }),
180 ),
181 );
182 return yield* HttpServerResponse.json(result);
183 }
184
185 if (request.method === "GET" && pathname === "/service-settings") {
186 const result = yield* getServiceSettings();
187 return yield* HttpServerResponse.json({
188 tag: "Ok",
189 hasSnsTopic: result.SnsTopicArn !== undefined,
190 });
191 }
192
193 if (request.method === "GET" && pathname === "/checkout-invalid") {
194 // A nonexistent product SKU must surface a typed rejection — a
195 // genuine IAM gap surfaces AccessDeniedException and 500s instead.
196 const result = yield* checkoutLicense({
197 ProductSKU: "00000000-0000-0000-0000-000000000000",
198 CheckoutType: "PROVISIONAL",
199 KeyFingerprint: "aws:294406891311:AWS/KeyManagement:v1",
200 Entitlements: [{ Name: "seats", Value: "1", Unit: "Count" }],
201 ClientToken: "00000000-0000-4000-8000-000000000000",
202 }).pipe(
203 Effect.map(() => "Ok"),
204 Effect.catchTag(
205 [
206 "ResourceNotFoundException",
207 "NoEntitlementsAllowedException",
208 "InvalidParameterValueException",
209 "ValidationException",
210 ],
211 (e) => Effect.succeed(e._tag),
212 ),
213 );
214 return yield* HttpServerResponse.json({ tag: result });
215 }
216
217 if (request.method === "GET" && pathname === "/access-token-invalid") {
218 // A malformed refresh token must surface a typed rejection —
219 // "Invalid token." arrives as InvalidParameterValueException
220 // (verified by probe; patched into distilled's GetAccessToken
221 // union, which the Smithy model omits).
222 const result = yield* getAccessToken({
223 Token: "not-a-valid-refresh-token",
224 }).pipe(
225 Effect.map(() => "Ok"),
226 Effect.catchTag(
227 ["InvalidParameterValueException", "ValidationException", "AuthorizationException"],
228 (e) => Effect.succeed(e._tag),
229 ),
230 );
231 return yield* HttpServerResponse.json({ tag: result });
232 }
233
234 if (request.method === "GET" && pathname === "/license-invalid") {
235 const result = yield* getLicense({
236 LicenseArn: BOGUS_LICENSE_ARN,
237 }).pipe(
238 Effect.map(() => "Ok"),
239 Effect.catchTag(
240 [
241 "ResourceNotFoundException",
242 "InvalidParameterValueException",
243 "ValidationException",
244 ],
245 (e) => Effect.succeed(e._tag),
246 ),
247 );
248 return yield* HttpServerResponse.json({ tag: result });
249 }
250
251 if (request.method === "GET" && pathname === "/grant-invalid") {
252 const result = yield* getGrant({ GrantArn: BOGUS_GRANT_ARN }).pipe(
253 Effect.map(() => "Ok"),
254 Effect.catchTag(
255 [
256 "ResourceNotFoundException",
257 "InvalidParameterValueException",
258 "ValidationException",
259 ],
260 (e) => Effect.succeed(e._tag),
261 ),
262 );
263 return yield* HttpServerResponse.json({ tag: result });
264 }
265
266 if (request.method === "GET" && pathname === "/specifications-invalid") {
267 const result = yield* listLicenseSpecificationsForResource({
268 ResourceArn: BOGUS_RESOURCE_ARN,
269 }).pipe(
270 Effect.map((r) => ({
271 tag: "Ok",
272 count: (r.LicenseSpecifications ?? []).length,
273 })),
274 Effect.catchTag("InvalidParameterValueException", (e) =>
275 Effect.succeed({ tag: e._tag, count: 0 }),
276 ),
277 );
278 return yield* HttpServerResponse.json(result);
279 }
280
281 return yield* HttpServerResponse.json(
282 { error: "Not found", method: request.method, pathname },
283 { status: 404 },
284 );
285 }).pipe(Effect.orDie),
286 };
287 }).pipe(
288 Effect.provide(
289 Layer.mergeAll(
290 Lambda.EventSource,
291 LicenseManager.GetLicenseConfigurationHttp,
292 LicenseManager.ListAssociationsForLicenseConfigurationHttp,
293 LicenseManager.ListUsageForLicenseConfigurationHttp,
294 LicenseManager.ListFailuresForLicenseConfigurationOperationsHttp,
295 LicenseManager.CheckoutLicenseHttp,
296 LicenseManager.CheckInLicenseHttp,
297 LicenseManager.CheckoutBorrowLicenseHttp,
298 LicenseManager.ExtendLicenseConsumptionHttp,
299 LicenseManager.GetAccessTokenHttp,
300 LicenseManager.GetLicenseHttp,
301 LicenseManager.GetLicenseUsageHttp,
302 LicenseManager.ListLicensesHttp,
303 LicenseManager.ListLicenseVersionsHttp,
304 LicenseManager.ListReceivedLicensesHttp,
305 LicenseManager.GetGrantHttp,
306 LicenseManager.AcceptGrantHttp,
307 LicenseManager.RejectGrantHttp,
308 LicenseManager.ListReceivedGrantsHttp,
309 LicenseManager.ListDistributedGrantsHttp,
310 LicenseManager.ListResourceInventoryHttp,
311 LicenseManager.ListLicenseSpecificationsForResourceHttp,
312 LicenseManager.UpdateLicenseSpecificationsForResourceHttp,
313 LicenseManager.GetServiceSettingsHttp,
314 ),
315 ),
316 ),
317 );