The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as netapp from "@distilled.cloud/gcp/netapp_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | fieldMask, |
| 14 | listAtNested, |
| 15 | listLabeledPages, |
| 16 | normalizeLocation, |
| 17 | parseName, |
| 18 | replaceOnIdentity, |
| 19 | ResourceNotResolved, |
| 20 | toPhysicalId, |
| 21 | userLabels, |
| 22 | volumeOf, |
| 23 | waitForDeleteOperation, |
| 24 | waitForOperation, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | waitUntilReady, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type VolumesSnapshotProps = { |
| 31 | /** |
| 32 | * Parent volume. Full name |
| 33 | * `projects/{project}/locations/{location}/volumes/{volume}` or the |
| 34 | * volume id (combined with `location`). Immutable — changing it |
| 35 | * replaces the snapshot. |
| 36 | */ |
| 37 | volume: string; |
| 38 | /** |
| 39 | * Region used when `volume` is a bare id. Immutable — changing it |
| 40 | * replaces the snapshot. `US-CENTRAL1` is accepted and normalized to |
| 41 | * `us-central1`. |
| 42 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 43 | */ |
| 44 | location?: string; |
| 45 | /** |
| 46 | * Snapshot id (the `{snapshot}` segment of |
| 47 | * `.../volumes/{volume}/snapshots/{snapshot}`). If omitted, a unique |
| 48 | * RFC1035 name is generated from the stack, stage, and logical id. |
| 49 | * Immutable — changing it replaces the snapshot. |
| 50 | */ |
| 51 | snapshotId?: string; |
| 52 | /** |
| 53 | * Human-readable description (2048 characters or less). |
| 54 | */ |
| 55 | description?: string; |
| 56 | /** |
| 57 | * User labels. Alchemy ownership labels are merged in automatically. |
| 58 | */ |
| 59 | labels?: Record<string, string>; |
| 60 | }; |
| 61 | |
| 62 | export type VolumesSnapshot = Resource< |
| 63 | "GCP.NetApp.VolumesSnapshot", |
| 64 | VolumesSnapshotProps, |
| 65 | { |
| 66 | /** Full resource name. */ |
| 67 | name: string; |
| 68 | /** Snapshot id (last path segment). */ |
| 69 | snapshotId: string; |
| 70 | /** Parent volume resource name. */ |
| 71 | volume: string; |
| 72 | /** Project id. */ |
| 73 | project: string; |
| 74 | /** Location id. */ |
| 75 | location: string; |
| 76 | /** Human-readable description. */ |
| 77 | description: string | undefined; |
| 78 | /** User labels (Alchemy ownership labels stripped). */ |
| 79 | labels: Record<string, string>; |
| 80 | /** Storage used by unique snapshot blocks, in bytes. */ |
| 81 | usedBytes: number | undefined; |
| 82 | /** Server-reported state. */ |
| 83 | state: string | undefined; |
| 84 | /** Server-reported state details. */ |
| 85 | stateDetails: string | undefined; |
| 86 | /** RFC3339 creation timestamp. */ |
| 87 | createTime: string | undefined; |
| 88 | }, |
| 89 | never, |
| 90 | Providers |
| 91 | >; |
| 92 | |
| 93 | /** |
| 94 | * A Cloud NetApp Volumes snapshot — a point-in-time copy of a volume. |
| 95 | * |
| 96 | * Changing `snapshotId`, `volume`, or `location` replaces the snapshot. |
| 97 | * Description and labels update in place. |
| 98 | * |
| 99 | * ### Creating a Snapshot |
| 100 | * **Example:** Generated name |
| 101 | * ```typescript |
| 102 | * const snap = yield* GCP.NetApp.VolumesSnapshot("Nightly", { |
| 103 | * volume: volume.name, |
| 104 | * }); |
| 105 | * ``` |
| 106 | * |
| 107 | * **Example:** Explicit id, description, and labels |
| 108 | * ```typescript |
| 109 | * const snap = yield* GCP.NetApp.VolumesSnapshot("Nightly", { |
| 110 | * volume: volume.name, |
| 111 | * snapshotId: "app-nightly", |
| 112 | * description: "nightly snapshot", |
| 113 | * labels: { env: "prod" }, |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * ### Updating a Snapshot |
| 118 | * **Example:** Description and labels |
| 119 | * ```typescript |
| 120 | * const snap = yield* GCP.NetApp.VolumesSnapshot("Nightly", { |
| 121 | * volume: volume.name, |
| 122 | * description: "nightly snapshot v2", |
| 123 | * labels: { env: "prod", team: "storage" }, |
| 124 | * }); |
| 125 | * ``` |
| 126 | * |
| 127 | * @resource |
| 128 | * @category NetApp |
| 129 | */ |
| 130 | export const VolumesSnapshot = Resource<VolumesSnapshot>("GCP.NetApp.VolumesSnapshot"); |
| 131 | |
| 132 | const resourceName = (volume: string, snapshotId: string) => `${volume}/snapshots/${snapshotId}`; |
| 133 | |
| 134 | const toAttrs = (snapshot: netapp.Snapshot, project: string, region: string) => { |
| 135 | const name = snapshot.name ?? ""; |
| 136 | const parsed = parseName(name, "snapshots", region); |
| 137 | return { |
| 138 | name, |
| 139 | snapshotId: parsed.id, |
| 140 | volume: parsed.parent, |
| 141 | project: parsed.project || project, |
| 142 | location: parsed.location, |
| 143 | description: snapshot.description, |
| 144 | labels: userLabels(snapshot.labels), |
| 145 | usedBytes: snapshot.usedBytes, |
| 146 | state: snapshot.state, |
| 147 | stateDetails: snapshot.stateDetails, |
| 148 | createTime: snapshot.createTime, |
| 149 | }; |
| 150 | }; |
| 151 | |
| 152 | const getByName = (name: string) => |
| 153 | name.length === 0 || name.includes("//") |
| 154 | ? Effect.undefined |
| 155 | : netapp |
| 156 | .getProjectsLocationsVolumesSnapshots({ name }) |
| 157 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 158 | |
| 159 | const listOwned = (project: string) => |
| 160 | listAtNested(project, "volumes/-", (parent) => |
| 161 | listLabeledPages( |
| 162 | netapp.listProjectsLocationsVolumesSnapshots.pages({ |
| 163 | parent, |
| 164 | pageSize: 1000, |
| 165 | }), |
| 166 | (page) => page.snapshots, |
| 167 | (item) => item.labels, |
| 168 | ), |
| 169 | ); |
| 170 | |
| 171 | export const VolumesSnapshotProvider = () => |
| 172 | Provider.succeed(VolumesSnapshot, { |
| 173 | stables: ["name", "snapshotId", "volume", "project", "location", "createTime"], |
| 174 | |
| 175 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 176 | if (!isResolved(news)) return undefined; |
| 177 | const env = yield* GcpEnvironment.current; |
| 178 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 179 | const nextLocation = normalizeLocation( |
| 180 | news.location ?? olds?.location ?? output?.location, |
| 181 | env.region, |
| 182 | ); |
| 183 | const previousVolume = volumeOf( |
| 184 | olds?.volume ?? output?.volume ?? "", |
| 185 | env.project, |
| 186 | previousLocation, |
| 187 | ); |
| 188 | const nextVolume = volumeOf( |
| 189 | news.volume ?? olds?.volume ?? output?.volume ?? "", |
| 190 | env.project, |
| 191 | nextLocation, |
| 192 | ); |
| 193 | return replaceOnIdentity({ |
| 194 | previousId: olds?.snapshotId ?? output?.snapshotId, |
| 195 | nextId: news.snapshotId ?? olds?.snapshotId ?? output?.snapshotId, |
| 196 | previousLocation, |
| 197 | nextLocation, |
| 198 | previousParent: previousVolume, |
| 199 | nextParent: nextVolume, |
| 200 | }); |
| 201 | }), |
| 202 | |
| 203 | read: Effect.fn(function* ({ id, olds, output }) { |
| 204 | const env = yield* GcpEnvironment.current; |
| 205 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 206 | const volume = volumeOf(olds?.volume ?? output?.volume ?? "", env.project, location); |
| 207 | const snapshotId = yield* toPhysicalId(id, olds?.snapshotId, output?.snapshotId, "snapshot"); |
| 208 | const name = output?.name ?? resourceName(volume, snapshotId); |
| 209 | const existing = yield* getByName(name); |
| 210 | if (existing === undefined) return undefined; |
| 211 | const attrs = toAttrs(existing, env.project, env.region); |
| 212 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 213 | }), |
| 214 | |
| 215 | list: () => |
| 216 | Effect.gen(function* () { |
| 217 | const env = yield* GcpEnvironment.current; |
| 218 | const items = yield* listOwned(env.project); |
| 219 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 220 | }), |
| 221 | |
| 222 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 223 | const env = yield* GcpEnvironment.current; |
| 224 | const locationHint = normalizeLocation(news.location ?? output?.location, env.region); |
| 225 | const volume = volumeOf(news.volume, env.project, locationHint); |
| 226 | const snapshotId = yield* toPhysicalId(id, news.snapshotId, output?.snapshotId, "snapshot"); |
| 227 | const name = output?.name ?? resourceName(volume, snapshotId); |
| 228 | const desiredLabels = { |
| 229 | ...toLabels(news.labels), |
| 230 | ...(yield* createInternalLabels(id)), |
| 231 | }; |
| 232 | |
| 233 | let current = yield* getByName(output?.name ?? name); |
| 234 | |
| 235 | if (current === undefined) { |
| 236 | const created = yield* netapp |
| 237 | .createProjectsLocationsVolumesSnapshots({ |
| 238 | parent: volume, |
| 239 | snapshotId, |
| 240 | body: { |
| 241 | description: news.description, |
| 242 | labels: desiredLabels, |
| 243 | }, |
| 244 | }) |
| 245 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 246 | if (created !== undefined) { |
| 247 | yield* waitForOperation(created); |
| 248 | } |
| 249 | current = yield* waitUntilExists(getByName(name), name); |
| 250 | } |
| 251 | |
| 252 | if (current === undefined) { |
| 253 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 254 | } |
| 255 | |
| 256 | current = yield* waitUntilReady( |
| 257 | getByName(current.name ?? name), |
| 258 | current.name ?? name, |
| 259 | (item) => item.state, |
| 260 | ); |
| 261 | |
| 262 | const observedLabels = tagRecord(current.labels); |
| 263 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 264 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 265 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 266 | const mask = fieldMask([labelsChanged && "labels", descriptionChanged && "description"]); |
| 267 | |
| 268 | if (mask.length > 0) { |
| 269 | const operation = yield* netapp.patchProjectsLocationsVolumesSnapshots({ |
| 270 | name: current.name ?? name, |
| 271 | updateMask: mask, |
| 272 | body: { |
| 273 | name: current.name ?? name, |
| 274 | labels: desiredLabels, |
| 275 | description: news.description, |
| 276 | }, |
| 277 | }); |
| 278 | yield* waitForOperation(operation); |
| 279 | current = yield* waitUntilReady( |
| 280 | getByName(current.name ?? name), |
| 281 | current.name ?? name, |
| 282 | (item) => item.state, |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | return toAttrs(current, env.project, env.region); |
| 287 | }), |
| 288 | |
| 289 | delete: Effect.fn(function* ({ output }) { |
| 290 | if (!output.name || output.name.includes("//")) return; |
| 291 | const operation = yield* netapp |
| 292 | .deleteProjectsLocationsVolumesSnapshots({ name: output.name }) |
| 293 | .pipe( |
| 294 | Effect.retry({ |
| 295 | while: (error) => error._tag === "Conflict", |
| 296 | times: 8, |
| 297 | schedule: Schedule.spaced("2 seconds"), |
| 298 | }), |
| 299 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 300 | ); |
| 301 | if (operation !== undefined) { |
| 302 | yield* waitForDeleteOperation(operation); |
| 303 | } |
| 304 | yield* waitUntilGone(getByName(output.name), output.name); |
| 305 | }), |
| 306 | }); |