The best way to control Claude Code, Codex and more from any computer or phone

main
ts 547 lines 19.1 KB
Raw
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 expandParent,
14 fieldMask,
15 gibOf,
16 listAtLocation,
17 listLabeledPages,
18 networkName,
19 normalizeLocation,
20 parentOf,
21 parseName,
22 replaceOnIdentity,
23 ResourceNotResolved,
24 toPhysicalId,
25 userLabels,
26 waitForDeleteOperation,
27 waitForOperation,
28 waitUntilExists,
29 waitUntilGone,
30 waitUntilReady,
31 } from "./internal.ts";
32
33 const DEFAULT_SERVICE_LEVEL: netapp.StoragePoolServiceLevelEnum = "STANDARD";
34 const DEFAULT_CAPACITY_GIB = 2048;
35
36 export type StoragePoolProps = {
37 /**
38 * Storage pool id (the `{storagePool}` segment of
39 * `projects/{project}/locations/{location}/storagePools/{storagePool}`).
40 * If omitted, a unique RFC1035 name is generated from the stack, stage,
41 * and logical id. Immutable — changing it replaces the pool.
42 */
43 storagePoolId?: string;
44 /**
45 * Region (`us-central1`, `us-east1`, …). Immutable — changing it
46 * replaces the pool. `US-CENTRAL1` is accepted and normalized to
47 * `us-central1`.
48 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
49 */
50 location?: string;
51 /**
52 * VPC network name (`default`) or path
53 * `projects/{project}/global/networks/{network}`. Immutable — changing
54 * it replaces the pool.
55 * @default "default"
56 */
57 network?: string;
58 /**
59 * Service level. Immutable — changing it replaces the pool.
60 * @default "STANDARD"
61 */
62 serviceLevel?: netapp.StoragePoolServiceLevelEnum | (string & {});
63 /**
64 * Pool capacity in GiB. STANDARD/PREMIUM/EXTREME minimum is 2048.
65 * @default 2048
66 */
67 capacityGib?: number | string;
68 /**
69 * Active zone for a regional Flex pool. Immutable — changing it
70 * replaces the pool.
71 */
72 zone?: string;
73 /**
74 * Replica zone for a regional Flex pool. Immutable — changing it
75 * replaces the pool.
76 */
77 replicaZone?: string;
78 /**
79 * KMS config used to encrypt volumes. Full name or id. Immutable —
80 * changing it replaces the pool.
81 */
82 kmsConfig?: string;
83 /**
84 * Active Directory used for SMB volumes. Full name or id.
85 */
86 activeDirectory?: string;
87 /**
88 * Enable NFS LDAP. Immutable — changing it replaces the pool.
89 */
90 ldapEnabled?: boolean;
91 /**
92 * Allow auto-tiering on volumes. Can be enabled after create but not
93 * disabled.
94 */
95 allowAutoTiering?: boolean;
96 /**
97 * Pool type. Immutable — changing it replaces the pool.
98 */
99 type?: netapp.StoragePoolTypeEnum | (string & {});
100 /**
101 * Pool mode. Immutable — changing it replaces the pool.
102 */
103 mode?: netapp.StoragePoolModeEnum | (string & {});
104 /**
105 * QoS type.
106 */
107 qosType?: netapp.StoragePoolQosTypeEnum | (string & {});
108 /**
109 * Enable independent scaling of capacity and performance. Immutable —
110 * changing it replaces the pool.
111 */
112 customPerformanceEnabled?: boolean;
113 /**
114 * Custom total throughput in MiB/s.
115 */
116 totalThroughputMibps?: number | string;
117 /**
118 * Custom total IOPS.
119 */
120 totalIops?: number | string;
121 /**
122 * Hot-tier size in GiB (Flex only).
123 */
124 hotTierSizeGib?: number | string;
125 /**
126 * Auto-increase hot-tier by 10% when it hits 100%.
127 */
128 enableHotTierAutoResize?: boolean;
129 /**
130 * Scale type. Immutable — changing it replaces the pool.
131 */
132 scaleType?: netapp.StoragePoolScaleTypeEnum | (string & {});
133 /**
134 * Human-readable description.
135 */
136 description?: string;
137 /**
138 * User labels. Alchemy ownership labels are merged in automatically.
139 */
140 labels?: Record<string, string>;
141 };
142
143 export type StoragePool = Resource<
144 "GCP.NetApp.StoragePool",
145 StoragePoolProps,
146 {
147 /** Full resource name. */
148 name: string;
149 /** Storage pool id (last path segment). */
150 storagePoolId: string;
151 /** Project id. */
152 project: string;
153 /** Location id. */
154 location: string;
155 /** VPC network path. */
156 network: string | undefined;
157 /** Service level. */
158 serviceLevel: string | undefined;
159 /** Capacity in GiB. */
160 capacityGib: string | undefined;
161 /** Allocated volume capacity in GiB. */
162 volumeCapacityGib: string | undefined;
163 /** Number of volumes. */
164 volumeCount: number | undefined;
165 /** Active zone. */
166 zone: string | undefined;
167 /** Replica zone. */
168 replicaZone: string | undefined;
169 /** KMS config name. */
170 kmsConfig: string | undefined;
171 /** Active Directory name. */
172 activeDirectory: string | undefined;
173 /** Whether NFS LDAP is enabled. */
174 ldapEnabled: boolean | undefined;
175 /** Whether auto-tiering is allowed. */
176 allowAutoTiering: boolean | undefined;
177 /** Pool type. */
178 type: string | undefined;
179 /** Pool mode. */
180 mode: string | undefined;
181 /** QoS type. */
182 qosType: string | undefined;
183 /** Whether custom performance is enabled. */
184 customPerformanceEnabled: boolean | undefined;
185 /** Total throughput in MiB/s. */
186 totalThroughputMibps: string | undefined;
187 /** Total IOPS. */
188 totalIops: string | undefined;
189 /** Hot-tier size in GiB. */
190 hotTierSizeGib: string | undefined;
191 /** Whether hot-tier auto-resize is enabled. */
192 enableHotTierAutoResize: boolean | undefined;
193 /** Available throughput in MiB/s. */
194 availableThroughputMibps: number | undefined;
195 /** Cold-tier usage in GiB. */
196 coldTierSizeUsedGib: string | undefined;
197 /** Hot-tier usage in GiB. */
198 hotTierSizeUsedGib: string | undefined;
199 /** Encryption key source. */
200 encryptionType: string | undefined;
201 /** Scale type. */
202 scaleType: string | undefined;
203 /** Human-readable description. */
204 description: string | undefined;
205 /** User labels (Alchemy ownership labels stripped). */
206 labels: Record<string, string>;
207 /** Server-reported state. */
208 state: string | undefined;
209 /** State details. */
210 stateDetails: string | undefined;
211 /** RFC3339 creation timestamp. */
212 createTime: string | undefined;
213 },
214 never,
215 Providers
216 >;
217
218 /**
219 * A Cloud NetApp Volumes storage pool — billed capacity that hosts
220 * volumes at a service level.
221 *
222 * Changing `storagePoolId`, `location`, `network`, `serviceLevel`,
223 * `zone`, `replicaZone`, `kmsConfig`, `ldapEnabled`, `type`, `mode`,
224 * `customPerformanceEnabled`, or `scaleType` replaces the pool. Capacity,
225 * description, labels, QoS, and auto-tiering update in place.
226 *
227 * ### Creating a Storage Pool
228 * **Example:** Generated name
229 * ```typescript
230 * const pool = yield* GCP.NetApp.StoragePool("Pool", {
231 * network: "default",
232 * serviceLevel: "STANDARD",
233 * capacityGib: 2048,
234 * });
235 * ```
236 *
237 * **Example:** Explicit id and labels
238 * ```typescript
239 * const pool = yield* GCP.NetApp.StoragePool("Pool", {
240 * storagePoolId: "app-pool",
241 * network: "default",
242 * serviceLevel: "PREMIUM",
243 * capacityGib: 4096,
244 * description: "app volumes",
245 * labels: { env: "prod" },
246 * });
247 * ```
248 *
249 * ### Updating a Storage Pool
250 * **Example:** Capacity and labels
251 * ```typescript
252 * const pool = yield* GCP.NetApp.StoragePool("Pool", {
253 * network: "default",
254 * serviceLevel: "STANDARD",
255 * capacityGib: 4096,
256 * description: "app volumes v2",
257 * labels: { env: "prod", team: "storage" },
258 * });
259 * ```
260 *
261 * @resource
262 * @category NetApp
263 */
264 export const StoragePool = Resource<StoragePool>("GCP.NetApp.StoragePool");
265
266 const resourceName = (project: string, location: string, storagePoolId: string) =>
267 `projects/${project}/locations/${location}/storagePools/${storagePoolId}`;
268
269 const toAttrs = (item: netapp.StoragePool, project: string, region: string) => {
270 const name = item.name ?? "";
271 const parsed = parseName(name, "storagePools", region);
272 return {
273 name,
274 storagePoolId: parsed.id,
275 project: parsed.project || project,
276 location: parsed.location,
277 network: item.network,
278 serviceLevel: item.serviceLevel,
279 capacityGib: item.capacityGib,
280 volumeCapacityGib: item.volumeCapacityGib,
281 volumeCount: item.volumeCount,
282 zone: item.zone,
283 replicaZone: item.replicaZone,
284 kmsConfig: item.kmsConfig,
285 activeDirectory: item.activeDirectory,
286 ldapEnabled: item.ldapEnabled,
287 allowAutoTiering: item.allowAutoTiering,
288 type: item.type,
289 mode: item.mode,
290 qosType: item.qosType,
291 customPerformanceEnabled: item.customPerformanceEnabled,
292 totalThroughputMibps: item.totalThroughputMibps,
293 totalIops: item.totalIops,
294 hotTierSizeGib: item.hotTierSizeGib,
295 enableHotTierAutoResize: item.enableHotTierAutoResize,
296 availableThroughputMibps: item.availableThroughputMibps,
297 coldTierSizeUsedGib: item.coldTierSizeUsedGib,
298 hotTierSizeUsedGib: item.hotTierSizeUsedGib,
299 encryptionType: item.encryptionType,
300 scaleType: item.scaleType,
301 description: item.description,
302 labels: userLabels(item.labels),
303 state: item.state,
304 stateDetails: item.stateDetails,
305 createTime: item.createTime,
306 };
307 };
308
309 const getByName = (name: string) =>
310 netapp
311 .getProjectsLocationsStoragePools({ name })
312 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
313
314 const listOwned = (project: string) =>
315 listAtLocation(project, (parent) =>
316 listLabeledPages(
317 netapp.listProjectsLocationsStoragePools.pages({
318 parent,
319 pageSize: 1000,
320 }),
321 (page) => page.storagePools,
322 (item) => item.labels,
323 ),
324 );
325
326 export const StoragePoolProvider = () =>
327 Provider.succeed(StoragePool, {
328 stables: ["name", "storagePoolId", "project", "location", "createTime"],
329
330 diff: Effect.fn(function* ({ news, olds, output }) {
331 if (!isResolved(news)) return undefined;
332 const env = yield* GcpEnvironment.current;
333 const previousNetwork = olds?.network ?? output?.network;
334 const previousLevel = olds?.serviceLevel ?? output?.serviceLevel;
335 const previousZone = olds?.zone ?? output?.zone;
336 const previousReplica = olds?.replicaZone ?? output?.replicaZone;
337 const previousKms = olds?.kmsConfig ?? output?.kmsConfig;
338 const previousLdap = olds?.ldapEnabled ?? output?.ldapEnabled;
339 const previousType = olds?.type ?? output?.type;
340 const previousMode = olds?.mode ?? output?.mode;
341 const previousCustom = olds?.customPerformanceEnabled ?? output?.customPerformanceEnabled;
342 const previousScale = olds?.scaleType ?? output?.scaleType;
343 return replaceOnIdentity({
344 previousId: olds?.storagePoolId ?? output?.storagePoolId,
345 nextId: news.storagePoolId ?? olds?.storagePoolId ?? output?.storagePoolId,
346 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
347 nextLocation: normalizeLocation(
348 news.location ?? olds?.location ?? output?.location,
349 env.region,
350 ),
351 extra:
352 (previousNetwork !== undefined &&
353 news.network !== undefined &&
354 news.network !== previousNetwork &&
355 !previousNetwork.endsWith(`/${news.network}`)) ||
356 (previousLevel !== undefined &&
357 news.serviceLevel !== undefined &&
358 news.serviceLevel !== previousLevel) ||
359 (previousZone !== undefined && news.zone !== undefined && news.zone !== previousZone) ||
360 (previousReplica !== undefined &&
361 news.replicaZone !== undefined &&
362 news.replicaZone !== previousReplica) ||
363 (previousKms !== undefined &&
364 news.kmsConfig !== undefined &&
365 news.kmsConfig !== previousKms) ||
366 (previousLdap !== undefined &&
367 news.ldapEnabled !== undefined &&
368 news.ldapEnabled !== previousLdap) ||
369 (previousType !== undefined && news.type !== undefined && news.type !== previousType) ||
370 (previousMode !== undefined && news.mode !== undefined && news.mode !== previousMode) ||
371 (previousCustom !== undefined &&
372 news.customPerformanceEnabled !== undefined &&
373 news.customPerformanceEnabled !== previousCustom) ||
374 (previousScale !== undefined &&
375 news.scaleType !== undefined &&
376 news.scaleType !== previousScale),
377 });
378 }),
379
380 read: Effect.fn(function* ({ id, olds, output }) {
381 const env = yield* GcpEnvironment.current;
382 const storagePoolId = yield* toPhysicalId(
383 id,
384 olds?.storagePoolId,
385 output?.storagePoolId,
386 "storagepool",
387 );
388 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
389 const name = output?.name ?? resourceName(env.project, location, storagePoolId);
390 const existing = yield* getByName(name);
391 if (existing === undefined) return undefined;
392 const attrs = toAttrs(existing, env.project, env.region);
393 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
394 }),
395
396 list: () =>
397 Effect.gen(function* () {
398 const env = yield* GcpEnvironment.current;
399 const items = yield* listOwned(env.project);
400 return items.map((item) => toAttrs(item, env.project, env.region));
401 }),
402
403 reconcile: Effect.fn(function* ({ id, news, output }) {
404 const env = yield* GcpEnvironment.current;
405 const storagePoolId = yield* toPhysicalId(
406 id,
407 news.storagePoolId,
408 output?.storagePoolId,
409 "storagepool",
410 );
411 const location = normalizeLocation(news.location ?? output?.location, env.region);
412 const name = resourceName(env.project, location, storagePoolId);
413 const network = networkName(env.project, news.network);
414 const serviceLevel = news.serviceLevel ?? DEFAULT_SERVICE_LEVEL;
415 const capacityGib = gibOf(news.capacityGib ?? DEFAULT_CAPACITY_GIB);
416 const kmsConfig =
417 news.kmsConfig === undefined
418 ? undefined
419 : expandParent(news.kmsConfig, env.project, location, "kmsConfigs");
420 const activeDirectory =
421 news.activeDirectory === undefined
422 ? undefined
423 : expandParent(news.activeDirectory, env.project, location, "activeDirectories");
424 const desiredLabels = {
425 ...toLabels(news.labels),
426 ...(yield* createInternalLabels(id)),
427 };
428
429 let current = yield* getByName(output?.name ?? name);
430
431 if (current === undefined) {
432 const created = yield* netapp
433 .createProjectsLocationsStoragePools({
434 parent: parentOf(env.project, location),
435 storagePoolId,
436 body: {
437 network,
438 serviceLevel,
439 capacityGib,
440 zone: news.zone,
441 replicaZone: news.replicaZone,
442 kmsConfig,
443 activeDirectory,
444 ldapEnabled: news.ldapEnabled,
445 allowAutoTiering: news.allowAutoTiering,
446 type: news.type,
447 mode: news.mode,
448 qosType: news.qosType,
449 customPerformanceEnabled: news.customPerformanceEnabled,
450 totalThroughputMibps: gibOf(news.totalThroughputMibps),
451 totalIops: gibOf(news.totalIops),
452 hotTierSizeGib: gibOf(news.hotTierSizeGib),
453 enableHotTierAutoResize: news.enableHotTierAutoResize,
454 scaleType: news.scaleType,
455 description: news.description,
456 labels: desiredLabels,
457 },
458 })
459 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
460 if (created !== undefined) {
461 yield* waitForOperation(created);
462 }
463 current = yield* waitUntilExists(getByName(name), name);
464 }
465
466 if (current === undefined) {
467 return yield* new ResourceNotResolved({ resourceName: name });
468 }
469
470 current = yield* waitUntilReady(
471 getByName(current.name ?? name),
472 current.name ?? name,
473 (item) => item.state,
474 (item) => item.stateDetails,
475 );
476
477 const observedLabels = tagRecord(current.labels);
478 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
479 const desiredAd = activeDirectory ?? current.activeDirectory;
480 const mask = fieldMask([
481 (upsert.length > 0 || removed.length > 0) && "labels",
482 (current.description ?? "") !== (news.description ?? "") && "description",
483 (current.capacityGib ?? "") !== (capacityGib ?? "") && "capacityGib",
484 (current.activeDirectory ?? "") !== (desiredAd ?? "") && "activeDirectory",
485 (current.allowAutoTiering ?? false) !==
486 (news.allowAutoTiering ?? current.allowAutoTiering ?? false) && "allowAutoTiering",
487 (current.qosType ?? "") !== (news.qosType ?? current.qosType ?? "") && "qosType",
488 (current.totalThroughputMibps ?? "") !==
489 (gibOf(news.totalThroughputMibps) ?? current.totalThroughputMibps ?? "") &&
490 "totalThroughputMibps",
491 (current.totalIops ?? "") !== (gibOf(news.totalIops) ?? current.totalIops ?? "") &&
492 "totalIops",
493 (current.hotTierSizeGib ?? "") !==
494 (gibOf(news.hotTierSizeGib) ?? current.hotTierSizeGib ?? "") && "hotTierSizeGib",
495 (current.enableHotTierAutoResize ?? true) !==
496 (news.enableHotTierAutoResize ?? current.enableHotTierAutoResize ?? true) &&
497 "enableHotTierAutoResize",
498 ]);
499
500 if (mask.length > 0) {
501 const operation = yield* netapp.patchProjectsLocationsStoragePools({
502 name: current.name ?? name,
503 updateMask: mask,
504 body: {
505 name: current.name ?? name,
506 labels: desiredLabels,
507 description: news.description,
508 capacityGib,
509 activeDirectory: desiredAd,
510 allowAutoTiering: news.allowAutoTiering ?? current.allowAutoTiering,
511 qosType: news.qosType ?? current.qosType,
512 totalThroughputMibps: gibOf(news.totalThroughputMibps) ?? current.totalThroughputMibps,
513 totalIops: gibOf(news.totalIops) ?? current.totalIops,
514 hotTierSizeGib: gibOf(news.hotTierSizeGib) ?? current.hotTierSizeGib,
515 enableHotTierAutoResize:
516 news.enableHotTierAutoResize ?? current.enableHotTierAutoResize,
517 },
518 });
519 yield* waitForOperation(operation);
520 current = yield* waitUntilReady(
521 getByName(current.name ?? name),
522 current.name ?? name,
523 (item) => item.state,
524 (item) => item.stateDetails,
525 );
526 }
527
528 return toAttrs(current, env.project, env.region);
529 }),
530
531 delete: Effect.fn(function* ({ output }) {
532 const operation = yield* netapp
533 .deleteProjectsLocationsStoragePools({ name: output.name })
534 .pipe(
535 Effect.retry({
536 while: (error) => error._tag === "Conflict",
537 times: 8,
538 schedule: Schedule.spaced("2 seconds"),
539 }),
540 Effect.catchTag("NotFound", () => Effect.undefined),
541 );
542 if (operation !== undefined) {
543 yield* waitForDeleteOperation(operation);
544 }
545 yield* waitUntilGone(getByName(output.name), output.name);
546 }),
547 });