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 | listAtLocation, |
| 15 | listLabeledPages, |
| 16 | normalizeLocation, |
| 17 | parentOf, |
| 18 | parseName, |
| 19 | replaceOnIdentity, |
| 20 | ResourceNotResolved, |
| 21 | sameStringList, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForDeleteOperation, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | waitUntilReady, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | const DEFAULT_TYPE: netapp.HostGroupTypeEnum = "ISCSI_INITIATOR"; |
| 32 | const DEFAULT_OS: netapp.HostGroupOsTypeEnum = "LINUX"; |
| 33 | |
| 34 | export type HostGroupProps = { |
| 35 | /** |
| 36 | * Host group id (the `{hostGroup}` segment of |
| 37 | * `projects/{project}/locations/{location}/hostGroups/{hostGroup}`). |
| 38 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 39 | * and logical id. Immutable — changing it replaces the group. |
| 40 | */ |
| 41 | hostGroupId?: string; |
| 42 | /** |
| 43 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 44 | * replaces the group. `US-CENTRAL1` is accepted and normalized to |
| 45 | * `us-central1`. |
| 46 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 47 | */ |
| 48 | location?: string; |
| 49 | /** |
| 50 | * IQN (iSCSI) or NQN (NVMe) host identifiers in the group. |
| 51 | */ |
| 52 | hosts: string[]; |
| 53 | /** |
| 54 | * Host group type. Immutable — changing it replaces the group. |
| 55 | * @default "ISCSI_INITIATOR" |
| 56 | */ |
| 57 | type?: netapp.HostGroupTypeEnum | (string & {}); |
| 58 | /** |
| 59 | * OS type shared by every host. Immutable — changing it replaces the |
| 60 | * group. |
| 61 | * @default "LINUX" |
| 62 | */ |
| 63 | osType?: netapp.HostGroupOsTypeEnum | (string & {}); |
| 64 | /** |
| 65 | * Human-readable description. |
| 66 | */ |
| 67 | description?: string; |
| 68 | /** |
| 69 | * User labels. Alchemy ownership labels are merged in automatically. |
| 70 | */ |
| 71 | labels?: Record<string, string>; |
| 72 | }; |
| 73 | |
| 74 | export type HostGroup = Resource< |
| 75 | "GCP.NetApp.HostGroup", |
| 76 | HostGroupProps, |
| 77 | { |
| 78 | /** Full resource name. */ |
| 79 | name: string; |
| 80 | /** Host group id (last path segment). */ |
| 81 | hostGroupId: string; |
| 82 | /** Project id. */ |
| 83 | project: string; |
| 84 | /** Location id. */ |
| 85 | location: string; |
| 86 | /** Host identifiers. */ |
| 87 | hosts: string[]; |
| 88 | /** Host group type. */ |
| 89 | type: string | undefined; |
| 90 | /** OS type. */ |
| 91 | osType: string | undefined; |
| 92 | /** Human-readable description. */ |
| 93 | description: string | undefined; |
| 94 | /** User labels (Alchemy ownership labels stripped). */ |
| 95 | labels: Record<string, string>; |
| 96 | /** Server-reported state. */ |
| 97 | state: string | undefined; |
| 98 | /** RFC3339 creation timestamp. */ |
| 99 | createTime: string | undefined; |
| 100 | }, |
| 101 | never, |
| 102 | Providers |
| 103 | >; |
| 104 | |
| 105 | /** |
| 106 | * A Cloud NetApp Volumes host group — a set of iSCSI initiators that can |
| 107 | * mount a block volume. |
| 108 | * |
| 109 | * Changing `hostGroupId`, `location`, `type`, or `osType` replaces the |
| 110 | * group. Hosts, description, and labels update in place. |
| 111 | * |
| 112 | * ### Creating a Host Group |
| 113 | * **Example:** Generated name |
| 114 | * ```typescript |
| 115 | * const group = yield* GCP.NetApp.HostGroup("Initiators", { |
| 116 | * hosts: ["iqn.1993-08.org.debian:01:db"], |
| 117 | * }); |
| 118 | * ``` |
| 119 | * |
| 120 | * **Example:** Explicit id and OS |
| 121 | * ```typescript |
| 122 | * const group = yield* GCP.NetApp.HostGroup("Initiators", { |
| 123 | * hostGroupId: "db-hosts", |
| 124 | * hosts: ["iqn.1993-08.org.debian:01:db"], |
| 125 | * osType: "LINUX", |
| 126 | * description: "database initiators", |
| 127 | * labels: { env: "prod" }, |
| 128 | * }); |
| 129 | * ``` |
| 130 | * |
| 131 | * ### Updating a Host Group |
| 132 | * **Example:** Hosts and labels |
| 133 | * ```typescript |
| 134 | * const group = yield* GCP.NetApp.HostGroup("Initiators", { |
| 135 | * hosts: [ |
| 136 | * "iqn.1993-08.org.debian:01:db", |
| 137 | * "iqn.1993-08.org.debian:01:db2", |
| 138 | * ], |
| 139 | * labels: { env: "prod", team: "storage" }, |
| 140 | * }); |
| 141 | * ``` |
| 142 | * |
| 143 | * @resource |
| 144 | * @category NetApp |
| 145 | */ |
| 146 | export const HostGroup = Resource<HostGroup>("GCP.NetApp.HostGroup"); |
| 147 | |
| 148 | const resourceName = (project: string, location: string, hostGroupId: string) => |
| 149 | `projects/${project}/locations/${location}/hostGroups/${hostGroupId}`; |
| 150 | |
| 151 | const toAttrs = (item: netapp.HostGroup, project: string, region: string) => { |
| 152 | const name = item.name ?? ""; |
| 153 | const parsed = parseName(name, "hostGroups", region); |
| 154 | return { |
| 155 | name, |
| 156 | hostGroupId: parsed.id, |
| 157 | project: parsed.project || project, |
| 158 | location: parsed.location, |
| 159 | hosts: item.hosts ?? [], |
| 160 | type: item.type, |
| 161 | osType: item.osType, |
| 162 | description: item.description, |
| 163 | labels: userLabels(item.labels), |
| 164 | state: item.state, |
| 165 | createTime: item.createTime, |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | const getByName = (name: string) => |
| 170 | netapp |
| 171 | .getProjectsLocationsHostGroups({ name }) |
| 172 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 173 | |
| 174 | const listOwned = (project: string) => |
| 175 | listAtLocation(project, (parent) => |
| 176 | listLabeledPages( |
| 177 | netapp.listProjectsLocationsHostGroups.pages({ |
| 178 | parent, |
| 179 | pageSize: 1000, |
| 180 | }), |
| 181 | (page) => page.hostGroups, |
| 182 | (item) => item.labels, |
| 183 | ), |
| 184 | ); |
| 185 | |
| 186 | export const HostGroupProvider = () => |
| 187 | Provider.succeed(HostGroup, { |
| 188 | stables: ["name", "hostGroupId", "project", "location", "createTime"], |
| 189 | |
| 190 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 191 | if (!isResolved(news)) return undefined; |
| 192 | const env = yield* GcpEnvironment.current; |
| 193 | const previousType = olds?.type ?? output?.type; |
| 194 | const previousOs = olds?.osType ?? output?.osType; |
| 195 | const nextType = news.type ?? DEFAULT_TYPE; |
| 196 | const nextOs = news.osType ?? DEFAULT_OS; |
| 197 | return replaceOnIdentity({ |
| 198 | previousId: olds?.hostGroupId ?? output?.hostGroupId, |
| 199 | nextId: news.hostGroupId ?? olds?.hostGroupId ?? output?.hostGroupId, |
| 200 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 201 | nextLocation: normalizeLocation( |
| 202 | news.location ?? olds?.location ?? output?.location, |
| 203 | env.region, |
| 204 | ), |
| 205 | extra: |
| 206 | (previousType !== undefined && nextType !== previousType) || |
| 207 | (previousOs !== undefined && nextOs !== previousOs), |
| 208 | }); |
| 209 | }), |
| 210 | |
| 211 | read: Effect.fn(function* ({ id, olds, output }) { |
| 212 | const env = yield* GcpEnvironment.current; |
| 213 | const hostGroupId = yield* toPhysicalId( |
| 214 | id, |
| 215 | olds?.hostGroupId, |
| 216 | output?.hostGroupId, |
| 217 | "hostgroup", |
| 218 | ); |
| 219 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 220 | const name = output?.name ?? resourceName(env.project, location, hostGroupId); |
| 221 | const existing = yield* getByName(name); |
| 222 | if (existing === undefined) return undefined; |
| 223 | const attrs = toAttrs(existing, env.project, env.region); |
| 224 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 225 | }), |
| 226 | |
| 227 | list: () => |
| 228 | Effect.gen(function* () { |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | const items = yield* listOwned(env.project); |
| 231 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 232 | }), |
| 233 | |
| 234 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 235 | const env = yield* GcpEnvironment.current; |
| 236 | const hostGroupId = yield* toPhysicalId( |
| 237 | id, |
| 238 | news.hostGroupId, |
| 239 | output?.hostGroupId, |
| 240 | "hostgroup", |
| 241 | ); |
| 242 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 243 | const name = resourceName(env.project, location, hostGroupId); |
| 244 | const type = news.type ?? DEFAULT_TYPE; |
| 245 | const osType = news.osType ?? DEFAULT_OS; |
| 246 | const desiredLabels = { |
| 247 | ...toLabels(news.labels), |
| 248 | ...(yield* createInternalLabels(id)), |
| 249 | }; |
| 250 | |
| 251 | let current = yield* getByName(output?.name ?? name); |
| 252 | |
| 253 | if (current === undefined) { |
| 254 | const created = yield* netapp |
| 255 | .createProjectsLocationsHostGroups({ |
| 256 | parent: parentOf(env.project, location), |
| 257 | hostGroupId, |
| 258 | body: { |
| 259 | hosts: news.hosts, |
| 260 | type, |
| 261 | osType, |
| 262 | description: news.description, |
| 263 | labels: desiredLabels, |
| 264 | }, |
| 265 | }) |
| 266 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 267 | if (created !== undefined) { |
| 268 | yield* waitForOperation(created); |
| 269 | } |
| 270 | current = yield* waitUntilExists(getByName(name), name); |
| 271 | } |
| 272 | |
| 273 | if (current === undefined) { |
| 274 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 275 | } |
| 276 | |
| 277 | current = yield* waitUntilReady( |
| 278 | getByName(current.name ?? name), |
| 279 | current.name ?? name, |
| 280 | (item) => item.state, |
| 281 | ); |
| 282 | |
| 283 | const observedLabels = tagRecord(current.labels); |
| 284 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 285 | const mask = fieldMask([ |
| 286 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 287 | (current.description ?? "") !== (news.description ?? "") && "description", |
| 288 | !sameStringList(current.hosts, news.hosts) && "hosts", |
| 289 | ]); |
| 290 | |
| 291 | if (mask.length > 0) { |
| 292 | const operation = yield* netapp.patchProjectsLocationsHostGroups({ |
| 293 | name: current.name ?? name, |
| 294 | updateMask: mask, |
| 295 | body: { |
| 296 | name: current.name ?? name, |
| 297 | labels: desiredLabels, |
| 298 | description: news.description, |
| 299 | hosts: news.hosts, |
| 300 | }, |
| 301 | }); |
| 302 | yield* waitForOperation(operation); |
| 303 | current = yield* waitUntilReady( |
| 304 | getByName(current.name ?? name), |
| 305 | current.name ?? name, |
| 306 | (item) => item.state, |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | return toAttrs(current, env.project, env.region); |
| 311 | }), |
| 312 | |
| 313 | delete: Effect.fn(function* ({ output }) { |
| 314 | const operation = yield* netapp.deleteProjectsLocationsHostGroups({ name: output.name }).pipe( |
| 315 | Effect.retry({ |
| 316 | while: (error) => error._tag === "Conflict", |
| 317 | times: 8, |
| 318 | schedule: Schedule.spaced("2 seconds"), |
| 319 | }), |
| 320 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 321 | ); |
| 322 | if (operation !== undefined) { |
| 323 | yield* waitForDeleteOperation(operation); |
| 324 | } |
| 325 | yield* waitUntilGone(getByName(output.name), output.name); |
| 326 | }), |
| 327 | }); |