| 1 | import * as datazone from "@distilled.cloud/aws/datazone"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Redacted from "effect/Redacted"; |
| 4 | import { diffTags } from "../../Tags.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Unwrap a DataZone `SensitiveString` (decoded as `Redacted`) to its plain |
| 8 | * string value. `String(redacted)` would yield `"<redacted>"`, not the value. |
| 9 | */ |
| 10 | export const unredact = (value: string | Redacted.Redacted<string>): string => |
| 11 | Redacted.isRedacted(value) ? Redacted.value(value) : value; |
| 12 | |
| 13 | /** |
| 14 | * Coerce a DataZone wire tag map (values may be `undefined` on the wire) into |
| 15 | * a plain `Record<string, string>`. |
| 16 | */ |
| 17 | export const toTagRecord = ( |
| 18 | tags: { [key: string]: string | undefined } | undefined, |
| 19 | ): Record<string, string> => |
| 20 | Object.fromEntries( |
| 21 | Object.entries(tags ?? {}).filter( |
| 22 | (entry): entry is [string, string] => entry[1] !== undefined, |
| 23 | ), |
| 24 | ); |
| 25 | |
| 26 | /** |
| 27 | * Sync tags on a DataZone resource by ARN: diff the OBSERVED cloud tags |
| 28 | * against the desired set and apply only the delta. |
| 29 | */ |
| 30 | export const syncDataZoneTags = Effect.fn(function* ( |
| 31 | resourceArn: string, |
| 32 | observedTags: Record<string, string>, |
| 33 | desiredTags: Record<string, string>, |
| 34 | ) { |
| 35 | const { upsert, removed } = diffTags(observedTags, desiredTags); |
| 36 | if (upsert.length > 0) { |
| 37 | yield* datazone.tagResource({ |
| 38 | resourceArn, |
| 39 | tags: Object.fromEntries(upsert.map(({ Key, Value }) => [Key, Value])), |
| 40 | }); |
| 41 | } |
| 42 | if (removed.length > 0) { |
| 43 | yield* datazone.untagResource({ resourceArn, tagKeys: removed }); |
| 44 | } |
| 45 | }); |