1import * as omics from "@distilled.cloud/aws/omics";
2import * as Effect from "effect/Effect";
3import { diffTags, tagRecord } from "../../Tags.ts";
4
5/**
6 * Reads the observed tags for a HealthOmics resource by ARN. Tolerates a
7 * not-found race (the resource can vanish between observation and this call)
8 * by returning an empty map.
9 *
10 * NOT exported from the service barrel — shared scaffolding for the Omics
11 * resource providers only.
12 */
13export const fetchOmicsTags = Effect.fn(function* (resourceArn: string) {
14 return yield* omics.listTagsForResource({ resourceArn }).pipe(
15 Effect.map((r) => tagRecord(r.tags)),
16 Effect.catchTag("ResourceNotFoundException", () =>
17 Effect.succeed({} as Record<string, string>),
18 ),
19 );
20});
21
22/**
23 * Converges the tags on a HealthOmics resource to `desired`, diffing against
24 * the OBSERVED cloud tags (not olds/output) so adoption converges. Applies
25 * only the delta: `tagResource` for upserts, `untagResource` for removals.
26 */
27export const syncOmicsTags = Effect.fn(function* (
28 resourceArn: string,
29 desired: Record<string, string>,
30) {
31 const current = yield* fetchOmicsTags(resourceArn);
32 const { upsert, removed } = diffTags(current, desired);
33 if (upsert.length > 0) {
34 yield* omics.tagResource({
35 resourceArn,
36 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
37 });
38 }
39 if (removed.length > 0) {
40 yield* omics.untagResource({ resourceArn, tagKeys: removed });
41 }
42});