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

main
ts 62 lines 1.99 KB
Raw
1 import * as r53r from "@distilled.cloud/aws/route53resolver";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import * as Stream from "effect/Stream";
5
6 /**
7 * Fetch the observed tags on a Route 53 Resolver resource as a plain record.
8 * Any failure (e.g. the resource vanished between observation and the tag
9 * read, or the resource is an AWS-managed rule we cannot read tags for)
10 * degrades to an empty record so callers can still converge.
11 *
12 * @internal
13 */
14 export const fetchResolverTags = (arn: string) =>
15 r53r.listTagsForResource.items({ ResourceArn: arn }).pipe(
16 Stream.runCollect,
17 Effect.map(
18 (chunk) =>
19 Object.fromEntries(Array.from(chunk).map((tag) => [tag.Key, tag.Value])) as Record<
20 string,
21 string
22 >,
23 ),
24 Effect.orElseSucceed(() => ({}) as Record<string, string>),
25 );
26
27 /**
28 * Converge the tags on a Route 53 Resolver resource to `desired`, diffing
29 * against the OBSERVED cloud tags (never olds/output) so adoption converges.
30 *
31 * @internal
32 */
33 export const syncResolverTags = Effect.fn(function* (arn: string, desired: Record<string, string>) {
34 const observed = yield* fetchResolverTags(arn);
35 const { upsert, removed } = diffTags(observed, desired);
36 if (upsert.length > 0) {
37 yield* r53r.tagResource({ ResourceArn: arn, Tags: upsert });
38 }
39 if (removed.length > 0) {
40 yield* r53r.untagResource({ ResourceArn: arn, TagKeys: removed });
41 }
42 });
43
44 /**
45 * Convert a tag record into the wire `Tag[]` shape for create calls.
46 *
47 * @internal
48 */
49 export const toResolverTagList = (tags: Record<string, string>): r53r.Tag[] =>
50 Object.entries(tags).map(([Key, Value]) => ({ Key, Value }));
51
52 /**
53 * Order-insensitive string set equality.
54 *
55 * @internal
56 */
57 export const sameStringSet = (a: readonly string[], b: readonly string[]): boolean => {
58 if (a.length !== b.length) return false;
59 const sortedA = [...a].sort();
60 const sortedB = [...b].sort();
61 return sortedA.every((value, i) => value === sortedB[i]);
62 };