| 1 | import * as redshift from "@distilled.cloud/aws/redshift"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | |
| 4 | /** |
| 5 | * Convert Redshift's `[{ Key, Value }]` tag list into a plain record, |
| 6 | * dropping any entry missing a key or value. |
| 7 | */ |
| 8 | export const toTagRecord = ( |
| 9 | tags: Array<{ Key?: string; Value?: string }> | undefined, |
| 10 | ): Record<string, string> => |
| 11 | Object.fromEntries( |
| 12 | (tags ?? []) |
| 13 | .filter( |
| 14 | (tag): tag is { Key: string; Value: string } => |
| 15 | typeof tag.Key === "string" && typeof tag.Value === "string", |
| 16 | ) |
| 17 | .map((tag) => [tag.Key, tag.Value]), |
| 18 | ); |
| 19 | |
| 20 | /** True when two string sets are equal ignoring order and duplicates. */ |
| 21 | export const sameStringSet = ( |
| 22 | a: readonly string[] | undefined, |
| 23 | b: readonly string[] | undefined, |
| 24 | ): boolean => { |
| 25 | const left = [...new Set(a ?? [])].sort(); |
| 26 | const right = [...new Set(b ?? [])].sort(); |
| 27 | return left.length === right.length && left.every((v, i) => v === right[i]); |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * Build the ARN of a provisioned-Redshift resource. Redshift describe |
| 32 | * responses do not surface resource ARNs (only the namespace ARN on |
| 33 | * clusters), so tag operations construct them from the ambient |
| 34 | * account/region. |
| 35 | */ |
| 36 | export const redshiftArn = ( |
| 37 | region: string, |
| 38 | accountId: string, |
| 39 | resourceType: |
| 40 | | "cluster" |
| 41 | | "subnetgroup" |
| 42 | | "parametergroup" |
| 43 | | "eventsubscription", |
| 44 | name: string, |
| 45 | ): string => `arn:aws:redshift:${region}:${accountId}:${resourceType}:${name}`; |
| 46 | |
| 47 | /** |
| 48 | * Apply a tag delta to a provisioned-Redshift resource via the shared |
| 49 | * CreateTags/DeleteTags operations. `upsert`/`removed` come from `diffTags` |
| 50 | * against OBSERVED cloud tags (Redshift describe responses carry tags |
| 51 | * inline). |
| 52 | */ |
| 53 | export const applyRedshiftTagDelta = Effect.fn(function* (input: { |
| 54 | arn: string; |
| 55 | upsert: Array<{ Key: string; Value: string }>; |
| 56 | removed: string[]; |
| 57 | }) { |
| 58 | if (input.upsert.length > 0) { |
| 59 | yield* redshift.createTags({ |
| 60 | ResourceName: input.arn, |
| 61 | Tags: input.upsert, |
| 62 | }); |
| 63 | } |
| 64 | if (input.removed.length > 0) { |
| 65 | yield* redshift.deleteTags({ |
| 66 | ResourceName: input.arn, |
| 67 | TagKeys: input.removed, |
| 68 | }); |
| 69 | } |
| 70 | }); |