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 { alchemyLabelKeys, stripInternalLabels } from "../Labels.ts";
export const DEFAULT_GLOBAL = "global";
export const DEFAULT_ZONE = "us-central1-a";
export const MAX_NAME_LENGTH = 63;
export class NetworksecurityNotResolved extends Data.TaggedError(
"GCP.NetworkSecurity.NotResolved",
)<{
resourceName: string;
}> {}
export class NetworksecurityStillExists extends Data.TaggedError(
"GCP.NetworkSecurity.StillExists",
)<{
resourceName: string;
}> {}
export class NetworksecurityFailed extends Data.TaggedError("GCP.NetworkSecurity.Failed")<{
resourceName: string;
state: string | undefined;
}> {}
export const lastSegment = (value: string) => {
const trimmed = value.replace(/\/+$/, "");
const parts = trimmed.split("/");
return parts[parts.length - 1] || trimmed;
};
export const rfc1035 = (name: string, fallback = "resource"): string => {
let next = name
.toLowerCase()
.replace(/[^a-z0-9-]/g, "-")
.replace(/-+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "");
if (!/^[a-z]/.test(next)) {
next = `n${next}`;
}
next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, "");
return next.length > 0 ? next : fallback;
};
export const normalizeLocation = (location: string | undefined, fallback: string) =>
lastSegment(location ?? fallback).toLowerCase();
export const parentOf = (project: string, location: string) =>
`projects/${project}/locations/${location}`;
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),
};
};
export const toPhysicalId = (
id: string,
explicit: string | undefined,
existing: string | undefined,
fallback = "resource",
) =>
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 userLabels = (
labels: Record<string, string | undefined> | null | undefined,
): Record<string, string> => stripInternalLabels(tagRecord(labels));
export const hasAlchemyLabelKeys = (
labels: Record<string, string | undefined> | null | undefined,
) => Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
export const encodeOwnership = (
labels: Record<string, string>,
text: string | undefined,
): string => {
const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
const trimmed = text?.trim();
return trimmed && trimmed.length > 0 ? `${marker}\n${trimmed}` : marker;
};
export const parseOwnership = (
text: string | undefined,
): {
labels: Record<string, string>;
text: string | undefined;
} => {
if (!text?.startsWith("[alchemy ")) {
return { labels: {}, text };
}
const end = text.indexOf("]");
if (end < 0) return { labels: {}, text };
const labels: Record<string, string> = {};
for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
const eq = part.indexOf("=");
if (eq > 0) {
labels[part.slice(0, eq)] = part.slice(eq + 1);
}
}
const rest = text.slice(end + 1).replace(/^\s+/, "");
return { labels, text: rest.length > 0 ? rest : undefined };
};
export const hasOwnershipMarker = (text: string | undefined) =>
Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));
export const sameJson = (left: unknown, right: unknown) =>
JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
export const sameStringList = (
left: readonly string[] | undefined,
right: readonly string[] | undefined,
) =>
JSON.stringify([...(left ?? [])].map(canonicalizeLink).sort()) ===
JSON.stringify([...(right ?? [])].map(canonicalizeLink).sort());
export const canonicalizeLink = (value: string | undefined) => {
if (value === undefined || value.length === 0) return "";
return value
.replace(/^https?:\/\/[^/]+\//, "")
.replace(/^compute\/v1\//, "")
.replace(/\/+$/, "");
};
export const linkKey = (value: string | undefined) =>
lastSegment(canonicalizeLink(value)).toLowerCase();
export const changedFields = (pairs: ReadonlyArray<readonly [string, boolean]>) =>
pairs.filter(([, changed]) => changed).map(([field]) => field);
export const waitUntilPresent = <A, E, R>(get: Effect.Effect<A | undefined, E, R>, name: string) =>
get.pipe(
Effect.flatMap((value) =>
value
? Effect.succeed(value)
: Effect.fail(new NetworksecurityNotResolved({ resourceName: name })),
),
Effect.retry({
while: (error) => error instanceof NetworksecurityNotResolved,
times: 8,
schedule: Schedule.spaced("1 second"),
}),
);
export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>, name: string) =>
get.pipe(
Effect.flatMap((value) =>
value === undefined
? Effect.void
: Effect.fail(new NetworksecurityStillExists({ resourceName: name })),
),
Effect.retry({
while: (error) => error instanceof NetworksecurityStillExists,
times: 10,
schedule: Schedule.spaced("2 seconds"),
}),
);
const PENDING_STATES = new Set([
"STATE_UNSPECIFIED",
"CREATING",
"UPDATING",
"DELETING",
"ACCEPTING",
"REJECTING",
]);
const FAILED_STATES = new Set(["FAILED", "DELETE_FAILED", "OBSOLETE"]);
export const waitUntilReady = <A extends { state?: string }, E, R>(
get: Effect.Effect<A | undefined, E, R>,
name: string,
) =>
get.pipe(
Effect.filterOrFail(
(value): value is A => value !== undefined,
() => new NetworksecurityNotResolved({ resourceName: name }),
),
Effect.filterOrFail(
(value) => !FAILED_STATES.has(value.state ?? ""),
(value) => new NetworksecurityFailed({ resourceName: name, state: value.state }),
),
Effect.filterOrFail(
(value) => !PENDING_STATES.has(value.state ?? ""),
() => new NetworksecurityNotResolved({ resourceName: name }),
),
Effect.retry({
while: (error) => error instanceof NetworksecurityNotResolved,
times: 10,
schedule: Schedule.spaced("4 seconds"),
}),
);
/** Collect every page; a missing parent (`NotFound`) lists as empty. */
export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>(
stream: Stream.Stream<Page, E, R>,
pick: (page: Page) => readonly Item[] | undefined,
) =>
stream.pipe(
Stream.flatMap((page) => Stream.fromIterable(pick(page) ?? [])),
Stream.runCollect,
Effect.map((chunk): Item[] => Array.from(chunk)),
Effect.catchIf(
(error) => error._tag === "NotFound",
() => Effect.succeed<Item[]>([]),
),
);
export { waitForOperation } from "./operations.ts";