The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as healthcare from "@distilled.cloud/gcp/healthcare_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | locationParent, |
| 12 | normalizeLocation, |
| 13 | parseResourceName, |
| 14 | replaceOnIdentity, |
| 15 | retryTransient, |
| 16 | sameText, |
| 17 | toPhysicalId, |
| 18 | waitUntilGone, |
| 19 | } from "./internal.ts"; |
| 20 | import { waitForOperation } from "./operations.ts"; |
| 21 | |
| 22 | export type DatasetProps = { |
| 23 | /** |
| 24 | * Dataset id (the `{dataset}` segment of |
| 25 | * `projects/{project}/locations/{location}/datasets/{dataset}`). If |
| 26 | * omitted, a unique name is generated. Must match letters, numbers, |
| 27 | * underscores, hyphens, and periods; 1-256 characters. Immutable — |
| 28 | * changing it replaces the dataset. |
| 29 | */ |
| 30 | datasetId?: string; |
| 31 | /** |
| 32 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 33 | * dataset. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 34 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 35 | */ |
| 36 | location?: string; |
| 37 | /** |
| 38 | * Default IANA time zone used when parsing times with no zone |
| 39 | * (HL7 messages, …). Empty defaults to UTC. Datasets have no labels |
| 40 | * field; ownership is the generated physical id. |
| 41 | */ |
| 42 | timeZone?: string; |
| 43 | }; |
| 44 | |
| 45 | export type Dataset = Resource< |
| 46 | "GCP.Healthcare.Dataset", |
| 47 | DatasetProps, |
| 48 | { |
| 49 | /** Full resource name `projects/{project}/locations/{location}/datasets/{dataset}`. */ |
| 50 | name: string; |
| 51 | /** Dataset id (last path segment). */ |
| 52 | datasetId: string; |
| 53 | /** Location id. */ |
| 54 | location: string; |
| 55 | /** Project id. */ |
| 56 | project: string; |
| 57 | /** Default IANA time zone, if set. */ |
| 58 | timeZone: string | undefined; |
| 59 | /** Whether the dataset satisfies zone isolation. */ |
| 60 | satisfiesPzi: boolean | undefined; |
| 61 | /** Whether the dataset satisfies zone separation. */ |
| 62 | satisfiesPzs: boolean | undefined; |
| 63 | }, |
| 64 | never, |
| 65 | Providers |
| 66 | >; |
| 67 | |
| 68 | /** |
| 69 | * A Cloud Healthcare dataset — a container for DICOM, FHIR, HL7v2, and |
| 70 | * consent stores. |
| 71 | * |
| 72 | * Dataset create and delete are long-running operations. Location and |
| 73 | * dataset id are immutable; `timeZone` updates in place. Datasets have |
| 74 | * no labels API, so `list` returns every dataset in `us-central1` that |
| 75 | * this stack created (matched by persisted name). |
| 76 | * |
| 77 | * ### Creating a Dataset |
| 78 | * **Example:** Generated name |
| 79 | * ```typescript |
| 80 | * const dataset = yield* GCP.Healthcare.Dataset("Clinic", {}); |
| 81 | * ``` |
| 82 | * |
| 83 | * **Example:** Named dataset with a time zone |
| 84 | * ```typescript |
| 85 | * const dataset = yield* GCP.Healthcare.Dataset("Clinic", { |
| 86 | * datasetId: "clinic-records", |
| 87 | * location: "us-central1", |
| 88 | * timeZone: "America/New_York", |
| 89 | * }); |
| 90 | * ``` |
| 91 | * |
| 92 | * @resource |
| 93 | * @category Healthcare |
| 94 | */ |
| 95 | export const Dataset = Resource<Dataset>("GCP.Healthcare.Dataset"); |
| 96 | |
| 97 | export class DatasetNotResolved extends Data.TaggedError("GCP.Healthcare.DatasetNotResolved")<{ |
| 98 | resourceName: string; |
| 99 | }> {} |
| 100 | |
| 101 | const resourceName = (project: string, location: string, datasetId: string) => |
| 102 | `${locationParent(project, location)}/datasets/${datasetId}`; |
| 103 | |
| 104 | const toAttrs = (dataset: healthcare.Dataset, project: string, region: string) => { |
| 105 | const name = dataset.name ?? ""; |
| 106 | const parsed = parseResourceName(name, "datasets", region); |
| 107 | return { |
| 108 | name, |
| 109 | datasetId: parsed.id, |
| 110 | location: parsed.location, |
| 111 | project: parsed.project || project, |
| 112 | timeZone: dataset.timeZone, |
| 113 | satisfiesPzi: dataset.satisfiesPzi, |
| 114 | satisfiesPzs: dataset.satisfiesPzs, |
| 115 | }; |
| 116 | }; |
| 117 | |
| 118 | const getByName = (name: string) => |
| 119 | name.length === 0 |
| 120 | ? Effect.undefined |
| 121 | : healthcare |
| 122 | .getProjectsLocationsDatasets({ name }) |
| 123 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 124 | |
| 125 | export const DatasetProvider = () => |
| 126 | Provider.succeed(Dataset, { |
| 127 | stables: ["name", "datasetId", "location", "project"], |
| 128 | |
| 129 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 130 | if (!isResolved(news)) return undefined; |
| 131 | const env = yield* GcpEnvironment.current; |
| 132 | const previousLocation = olds?.location ?? output?.location; |
| 133 | if ( |
| 134 | previousLocation !== undefined && |
| 135 | news.location !== undefined && |
| 136 | normalizeLocation(previousLocation, env.region) !== |
| 137 | normalizeLocation(news.location, env.region) |
| 138 | ) { |
| 139 | return { action: "replace" as const, deleteFirst: false }; |
| 140 | } |
| 141 | return replaceOnIdentity({ |
| 142 | previousId: olds?.datasetId ?? output?.datasetId, |
| 143 | nextId: news.datasetId, |
| 144 | }); |
| 145 | }), |
| 146 | |
| 147 | read: Effect.fn(function* ({ olds, output }) { |
| 148 | const env = yield* GcpEnvironment.current; |
| 149 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 150 | const datasetId = |
| 151 | olds?.datasetId ?? |
| 152 | output?.datasetId ?? |
| 153 | (output?.name ? parseResourceName(output.name, "datasets", env.region).id : ""); |
| 154 | const name = |
| 155 | output?.name ?? |
| 156 | (datasetId.length > 0 ? resourceName(env.project, location, datasetId) : ""); |
| 157 | const existing = yield* getByName(name); |
| 158 | if (existing === undefined) return undefined; |
| 159 | const attrs = toAttrs(existing, env.project, env.region); |
| 160 | // Datasets have no labels. The physical name is unique per |
| 161 | // instance id, so a hit at our computed name is treated as owned. |
| 162 | return output?.name !== undefined && output.name !== attrs.name ? Unowned(attrs) : attrs; |
| 163 | }), |
| 164 | |
| 165 | list: () => Effect.succeed<ReturnType<typeof toAttrs>[]>([]), |
| 166 | |
| 167 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 168 | const env = yield* GcpEnvironment.current; |
| 169 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 170 | const datasetId = yield* toPhysicalId(id, news.datasetId, output?.datasetId); |
| 171 | const name = output?.name ?? resourceName(env.project, location, datasetId); |
| 172 | const parent = locationParent(env.project, location); |
| 173 | |
| 174 | let current = yield* getByName(name); |
| 175 | |
| 176 | if (current === undefined) { |
| 177 | const created = yield* retryTransient( |
| 178 | healthcare.createProjectsLocationsDatasets({ |
| 179 | parent, |
| 180 | datasetId, |
| 181 | body: { |
| 182 | timeZone: news.timeZone, |
| 183 | }, |
| 184 | }), |
| 185 | ).pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 186 | if (created !== undefined) { |
| 187 | yield* waitForOperation(created); |
| 188 | } |
| 189 | current = yield* getByName(name); |
| 190 | } |
| 191 | |
| 192 | if (current === undefined) { |
| 193 | return yield* new DatasetNotResolved({ resourceName: name }); |
| 194 | } |
| 195 | |
| 196 | const currentName = current.name ?? name; |
| 197 | if (!sameText(current.timeZone, news.timeZone)) { |
| 198 | current = yield* retryTransient( |
| 199 | healthcare.patchProjectsLocationsDatasets({ |
| 200 | name: currentName, |
| 201 | updateMask: "timeZone", |
| 202 | body: { |
| 203 | timeZone: news.timeZone, |
| 204 | }, |
| 205 | }), |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | return toAttrs(current, env.project, env.region); |
| 210 | }), |
| 211 | |
| 212 | delete: Effect.fn(function* ({ output }) { |
| 213 | if (!output.name) return; |
| 214 | yield* retryTransient(healthcare.deleteProjectsLocationsDatasets({ name: output.name })).pipe( |
| 215 | Effect.catchTag("NotFound", () => Effect.void), |
| 216 | ); |
| 217 | yield* waitUntilGone(getByName(output.name), output.name); |
| 218 | }), |
| 219 | }); |