1import * as elasticache from "@distilled.cloud/aws/elasticache";
2import * as Effect from "effect/Effect";
3
4export const toTagRecord = (
5 tags: Array<{ Key?: string; Value?: string }> | undefined,
6): Record<string, string> =>
7 Object.fromEntries(
8 (tags ?? [])
9 .filter(
10 (tag): tag is { Key: string; Value: string } =>
11 tag.Key !== undefined && tag.Value !== undefined,
12 )
13 .map((tag) => [tag.Key, tag.Value]),
14 );
15
16/** Read tags after a resource has reached an observable state. */
17export const readElastiCacheTags = Effect.fn(function* (arn: string) {
18 const response = yield* elasticache
19 .listTagsForResource({ ResourceName: arn })
20 .pipe(Effect.catch(() => Effect.succeed(undefined)));
21 return toTagRecord(response?.TagList);
22});
23
24export const sameStringSet = (
25 a: readonly string[] | undefined,
26 b: readonly string[] | undefined,
27): boolean => {
28 const left = [...new Set(a ?? [])].sort();
29 const right = [...new Set(b ?? [])].sort();
30 return (
31 left.length === right.length && left.every((value, i) => value === right[i])
32 );
33};
34
35export const tagsToWire = (tags: Record<string, string>) =>
36 Object.entries(tags).map(([Key, Value]) => ({ Key, Value }));