import * as file from "@distilled.cloud/gcp/file_v1";
import type { GcpOpContext } from "@distilled.cloud/gcp/Protocol";
import { createPhysicalName } from "alchemy/PhysicalName";
import { tagRecord } from "alchemy/Tags";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { stripInternalLabels } from "../Labels.ts";
import { waitForOperation as waitForLongRunningOperation } from "../Operation.ts";
export const DEFAULT_ZONE = "us-central1-a";
export const DEFAULT_SHARE_NAME = "share1";
export const MAX_NAME_LENGTH = 63;
export class ResourceNotResolved extends Data.TaggedError("GCP.Filestore.ResourceNotResolved")<{
resourceName: string;
}> {}
export class ResourceStillExists extends Data.TaggedError("GCP.Filestore.ResourceStillExists")<{
resourceName: string;
}> {}
export class ResourceNotReady extends Data.TaggedError("GCP.Filestore.ResourceNotReady")<{
resourceName: string;
state: string;
}> {}
export class ResourceFailed extends Data.TaggedError("GCP.Filestore.ResourceFailed")<{
resourceName: string;
state: string;
}> {}
export const lastSegment = (value: string | undefined) => {
if (value === undefined || value.length === 0) return "";
const trimmed = value.replace(/\/+$/, "");
const parts = trimmed.split("/");
return parts[parts.length - 1] || trimmed;
};
export const rfc1035 = (name: string, fallback = "filestore"): string => {
let next = name
.toLowerCase()
.replace(/[^a-z0-9-]/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "");
if (!/^[a-z]/.test(next)) next = `f${next}`;
next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
if (next.length === 0) return fallback;
if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
return next.slice(0, MAX_NAME_LENGTH);
};
export const normalizeLocation = (location: string | undefined, fallback: string) =>
lastSegment(location ?? fallback).toLowerCase();
export const regionOf = (location: string) => {
const parts = location.split("-");
const last = parts[parts.length - 1];
if (last !== undefined && last.length === 1) {
return parts.slice(0, -1).join("-");
}
return location;
};
export const toPhysicalId = (
id: string,
explicit: string | undefined,
existing: string | undefined,
fallback = "filestore",
) =>
Effect.gen(function* () {
if (explicit !== undefined) return rfc1035(explicit, fallback);
if (existing !== undefined) return existing;
return rfc1035(
yield* createPhysicalName({
id,
maxLength: MAX_NAME_LENGTH,
lowercase: true,
}),
fallback,
);
});
export const parseName = (name: string, collection: string, fallbackLocation: string) => {
const parts = name.split("/").filter((part) => part.length > 0);
const collectionAt = parts.lastIndexOf(collection);
const locationsAt = parts.lastIndexOf("locations");
const projectsAt = parts.lastIndexOf("projects");
return {
project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
location:
locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation,
id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
parent:
collectionAt > 0
? parts.slice(0, collectionAt).join("/")
: parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
};
};
export const expandParent = (
value: string,
project: string,
location: string,
collection: string,
) => {
if (value.includes("/")) return value.replace(/\/+$/, "");
return `projects/${project}/locations/${location}/${collection}/${value}`;
};
export const userLabels = (
labels: Record<string, string | undefined> | null | undefined,
): Record<string, string> => stripInternalLabels(tagRecord(labels));
export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
export const fieldMask = (fields: Array<string | false | undefined>) =>
fields.filter((field): field is string => typeof field === "string").join(",");
export const replaceOnIdentity = (input: {
previousId: string | undefined;
nextId: string | undefined;
previousLocation: string;
nextLocation: string;
extra?: boolean;
previousParent?: string;
nextParent?: string;
}) => {
const parentChanged =
(input.previousParent ?? "") !== "" &&
(input.nextParent ?? "") !== "" &&
(input.previousParent ?? "") !== (input.nextParent ?? "");
const replace =
(input.extra ?? false) ||
parentChanged ||
(input.previousId !== undefined &&
input.nextId !== undefined &&
input.nextId !== input.previousId) ||
input.previousLocation !== input.nextLocation;
if (!replace) return undefined;
const samePhysical =
input.previousLocation === input.nextLocation &&
!parentChanged &&
input.previousId !== undefined &&
input.nextId === input.previousId;
return {
action: "replace" as const,
deleteFirst: samePhysical,
};
};
const READY_STATES = new Set(["READY"]);
const FAILED_STATES = new Set(["INVALID", "ERROR"]);
export const isReadyState = (state: string | undefined) =>
READY_STATES.has((state ?? "").toUpperCase());
export const isFailedState = (state: string | undefined) =>
FAILED_STATES.has((state ?? "").toUpperCase());
export const isDeletingState = (state: string | undefined) =>
(state ?? "").toUpperCase() === "DELETING";
/**
* Wait on a Filestore long-running operation. Instances, backups, and snapshots take 5–20 minutes.
* `ALREADY_EXISTS` (a create race) counts as success; so does `NOT_FOUND`
* when `notFoundOk` (deletes). Returns the final operation.
*/
export const waitForOperation = (operation: file.Operation, options?: { notFoundOk?: boolean }) =>
Effect.suspend(() => {
let latest = operation;
return waitForLongRunningOperation(
operation,
(name) => {
const get = file.getProjectsLocationsOperations({ name }).pipe(
Effect.tap((current) =>
Effect.sync(() => {
latest = current;
}),
),
);
const observe: Effect.Effect<
file.Operation,
file.GetProjectsLocationsOperationsError,
GcpOpContext
> =
options?.notFoundOk === true
? get.pipe(
Effect.catchTag("NotFound", () =>
Effect.succeed<file.Operation>({ name, done: true }),
),
)
: get.pipe(
// A just-returned operation can briefly 404 on read.
Effect.retry({
while: (error) => error._tag === "NotFound",
times: 5,
schedule: Schedule.exponential("250 millis"),
}),
);
return observe;
},
{ budget: "30 minutes", interval: "10 seconds" },
).pipe(
Effect.map(() => latest),
// google.rpc.Code ALREADY_EXISTS = 6, NOT_FOUND = 5.
Effect.catchTag("GCP.OperationFailed", (error) =>
error.code === 6 || (options?.notFoundOk === true && error.code === 5)
? Effect.succeed(latest)
: Effect.fail(error),
),
);
});
export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
get: Effect.Effect<A | undefined, E, R>,
name: string,
): Effect.Effect<NonNullable<A>, E | ResourceNotResolved, R> =>
get.pipe(
Effect.filterOrFail(
(value): value is NonNullable<A> => value != null,
() => new ResourceNotResolved({ resourceName: name }),
),
Effect.retry({
while: (error) => error._tag === "GCP.Filestore.ResourceNotResolved",
times: 8,
schedule: Schedule.spaced("1 second"),
}),
);
export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
get: Effect.Effect<A | undefined, E, R>,
name: string,
): Effect.Effect<void, E | ResourceStillExists, R> =>
get.pipe(
Effect.filterOrFail(
(value) => value === undefined,
() => new ResourceStillExists({ resourceName: name }),
),
Effect.retry({
while: (error) => error._tag === "GCP.Filestore.ResourceStillExists",
// Backup and snapshot deletion takes several minutes.
times: 120,
schedule: Schedule.spaced("10 seconds"),
}),
Effect.asVoid,
);
export const waitUntilReady = <A, E extends { readonly _tag: string }, R>(
get: Effect.Effect<A | undefined, E, R>,
name: string,
stateOf: (value: NonNullable<A>) => string | undefined,
): Effect.Effect<NonNullable<A>, E | ResourceNotResolved | ResourceFailed | ResourceNotReady, R> =>
get.pipe(
Effect.filterOrFail(
(value): value is NonNullable<A> => value != null,
() => new ResourceNotResolved({ resourceName: name }),
),
Effect.filterOrFail(
(value) => !isFailedState(stateOf(value)),
(value) =>
new ResourceFailed({
resourceName: name,
state: stateOf(value) ?? "",
}),
),
Effect.filterOrFail(
(value) => {
const state = stateOf(value) ?? "";
return isReadyState(state) || state.length === 0;
},
(value) => new ResourceNotReady({ resourceName: name, state: stateOf(value) ?? "" }),
),
Effect.retry({
while: (error) =>
error._tag === "GCP.Filestore.ResourceNotReady" ||
error._tag === "GCP.Filestore.ResourceNotResolved",
// Backups and snapshots take several minutes to become READY.
times: 120,
schedule: Schedule.spaced("10 seconds"),
}),
);
export const listLabeledPages = <Page, A, E, R>(
pages: Stream.Stream<Page, E, R>,
items: (page: Page) => readonly A[] | undefined,
labelsOf: (item: A) => Record<string, string | undefined> | null | undefined,
) =>
pages.pipe(
Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
Stream.filter((item) => hasAlchemyLabelMap(labelsOf(item))),
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
);