import * as integrations from "@distilled.cloud/gcp/integrations_v1";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { GcpEnvironment } from "../Environment.ts";
import { createInternalLabels } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import {
encodeOwnership,
hasOwnershipMarker,
lastSegment,
locationOf,
locationParent,
normalizeLocation,
ownedByAlchemy,
parentOf,
parseOwnership,
sameText,
toResourceId,
updateMaskOf,
} from "./ownership.ts";
import type { TriggerConfig } from "./shared.ts";
export type IntegrationsVersionProps = {
/**
* Integration id (the `{integration}` segment of
* `projects/{project}/locations/{location}/integrations/{integration}`).
* If omitted, a unique id is generated. Immutable — changing it
* replaces the version.
*/
integrationId?: string;
/**
* Version id (the `{version}` segment). Server-assigned on create.
* Immutable — changing it replaces the version.
*/
versionId?: string;
/**
* Location (`us-central1`, …). Immutable — changing it replaces the
* version. `US-CENTRAL1` is accepted and normalized to `us-central1`.
* @default the stack's GCP region (`GCP.Region`, else the profile
* region, else `us-central1`)
*/
location?: string;
/**
* Human-readable description. Integration versions have no labels
* field, so Alchemy ownership is stored in a `[alchemy …]` prefix and
* stripped from attributes.
*/
description?: string;
/**
* User-defined label that annotates this version.
*/
userLabel?: string;
/**
* Trigger configurations. A draft with no tasks is valid; add an API
* trigger to make the version executable.
*/
triggerConfigs?: TriggerConfig[];
/**
* When true, create a new integration if `integrationId` does not
* exist. Ignored on update.
* @default true
*/
newIntegration?: boolean;
};
export type IntegrationsVersion = Resource<
"GCP.Integrations.IntegrationsVersion",
IntegrationsVersionProps,
{
/** Full resource name `projects/{project}/locations/{location}/integrations/{integration}/versions/{version}`. */
name: string;
/** Version id (last path segment). */
versionId: string;
/** Parent integration id. */
integrationId: string;
/** Parent integration resource name. */
integration: string;
/** Location id. */
location: string;
/** Project id. */
project: string;
/** User description with the Alchemy ownership prefix stripped. */
description: string | undefined;
/** User-defined version label. */
userLabel: string | undefined;
/** Trigger configurations. */
triggerConfigs: TriggerConfig[] | undefined;
/** Server-reported status (`DRAFT`, `ACTIVE`, …). */
status: string | undefined;
/** Server-reported state. */
state: string | undefined;
/** Snapshot number. */
snapshotNumber: string | undefined;
/** RFC3339 creation timestamp. */
createTime: string | undefined;
/** RFC3339 last-update timestamp. */
updateTime: string | undefined;
},
never,
Providers
>;
/**
* An Application Integration version (a draft or published snapshot of
* an integration).
*
* Versions have no labels field — Alchemy stamps ownership into the
* description. Location, integration id, and version id are immutable.
* Description, user label, and trigger configs update in place. Draft
* versions cannot be deleted via the versions API; Alchemy deletes the
* parent integration instead.
*
* ### Creating a Version
* **Example:** Empty draft with an API trigger
* ```typescript
* const version = yield* GCP.Integrations.IntegrationsVersion("Orders", {
* description: "order workflow",
* triggerConfigs: [
* {
* label: "API Trigger",
* triggerType: "API",
* triggerNumber: "1",
* triggerId: "api_trigger/orders",
* properties: { "Trigger name": "orders" },
* },
* ],
* });
* ```
*
* @resource
* @category Integrations
*/
export const IntegrationsVersion = Resource<IntegrationsVersion>(
"GCP.Integrations.IntegrationsVersion",
);
export class IntegrationsVersionNotResolved extends Data.TaggedError(
"GCP.Integrations.IntegrationsVersionNotResolved",
)<{
resourceName: string;
}> {}
const integrationOf = (name: string) => {
const parts = name.split("/");
const index = parts.indexOf("integrations");
return index >= 0 ? parts.slice(0, index + 2).join("/") : parentOf(name);
};
const integrationIdOf = (name: string) => {
const parts = name.split("/");
const index = parts.indexOf("integrations");
return index >= 0 ? (parts[index + 1] ?? lastSegment(name)) : lastSegment(name);
};
const resourceName = (
project: string,
location: string,
integrationId: string,
versionId: string,
) => `${locationParent(project, location)}/integrations/${integrationId}/versions/${versionId}`;
const triggersOf = (
configs: integrations.GoogleCloudIntegrationsV1alphaTriggerConfigList | undefined,
): TriggerConfig[] | undefined => {
if (configs === undefined) return undefined;
return configs.map((config) => ({
label: config.label,
triggerType: config.triggerType,
triggerNumber: config.triggerNumber,
triggerId: config.triggerId,
properties: config.properties,
description: config.description,
}));
};
const toAttrs = (
version: integrations.GoogleCloudIntegrationsV1alphaIntegrationVersion,
project: string,
region: string,
) => {
const name = version.name ?? "";
const parsed = parseOwnership(version.description);
return {
name,
versionId: lastSegment(name),
integrationId: integrationIdOf(name),
integration: integrationOf(name),
location: locationOf(name, region),
project,
description: parsed.text,
userLabel: version.userLabel,
triggerConfigs: triggersOf(version.triggerConfigs),
status: version.status,
state: version.state,
snapshotNumber: version.snapshotNumber,
createTime: version.createTime,
updateTime: version.updateTime,
};
};
const getByName = (name: string) =>
name.length === 0
? Effect.undefined
: integrations
.getProjectsLocationsIntegrationsVersions({ name })
.pipe(Effect.catchTag("NotFound", () => Effect.undefined));
const listAt = (parent: string, project: string, region: string) =>
integrations.listProjectsLocationsIntegrationsVersions.pages({ parent, pageSize: 100 }).pipe(
Stream.flatMap((page) => Stream.fromIterable(page.integrationVersions ?? [])),
Stream.filter((version) => hasOwnershipMarker(version.description)),
Stream.map((version) => toAttrs(version, project, region)),
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
Effect.catchTag("NotFound", () => Effect.succeed([])),
);
const findOwned = (parent: string, id: string) =>
integrations.listProjectsLocationsIntegrationsVersions.pages({ parent, pageSize: 100 }).pipe(
Stream.flatMap((page) => Stream.fromIterable(page.integrationVersions ?? [])),
Stream.filterEffect((version) => ownedByAlchemy(id, version.description)),
Stream.runHead,
Effect.map((option) => (option._tag === "Some" ? option.value : undefined)),
Effect.catchTag("NotFound", () => Effect.undefined),
);
const deleteIntegration = (name: string) =>
name.length === 0
? Effect.void
: integrations.deleteProjectsLocationsIntegrations({ name }).pipe(
Effect.catchTags({
NotFound: () => Effect.void,
BadRequest: () => Effect.void,
Conflict: () => Effect.void,
}),
);
export const IntegrationsVersionProvider = () =>
Provider.succeed(IntegrationsVersion, {
stables: [
"name",
"versionId",
"integrationId",
"integration",
"location",
"project",
"createTime",
],
diff: Effect.fn(function* ({ news, olds, output }) {
if (!isResolved(news)) return undefined;
const env = yield* GcpEnvironment.current;
const previousLocation = olds?.location ?? output?.location;
const nextLocation = normalizeLocation(news.location, previousLocation ?? env.region);
if (
previousLocation !== undefined &&
normalizeLocation(previousLocation, env.region) !== nextLocation
) {
return { action: "replace" as const, deleteFirst: false };
}
const previousIntegration = olds?.integrationId ?? output?.integrationId;
if (
previousIntegration !== undefined &&
news.integrationId !== undefined &&
news.integrationId !== previousIntegration
) {
return { action: "replace" as const, deleteFirst: false };
}
const previousId = olds?.versionId ?? output?.versionId;
if (
previousId !== undefined &&
news.versionId !== undefined &&
news.versionId !== previousId
) {
return { action: "replace" as const, deleteFirst: false };
}
return undefined;
}),
read: Effect.fn(function* ({ id, olds, output }) {
const env = yield* GcpEnvironment.current;
const integrationId = yield* toResourceId(id, olds?.integrationId, output?.integrationId);
const versionId = olds?.versionId ?? output?.versionId ?? "";
const location = normalizeLocation(olds?.location ?? output?.location, env.region);
const name =
output?.name ??
(versionId.length > 0 ? resourceName(env.project, location, integrationId, versionId) : "");
let existing = yield* getByName(name);
if (existing === undefined) {
existing = yield* findOwned(
`${locationParent(env.project, location)}/integrations/${integrationId}`,
id,
);
}
if (existing === undefined) return undefined;
const attrs = toAttrs(existing, env.project, env.region);
return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs);
}),
list: () =>
Effect.gen(function* () {
const env = yield* GcpEnvironment.current;
return yield* listAt(
`${locationParent(env.project, env.region)}/integrations/-`,
env.project,
env.region,
);
}),
reconcile: Effect.fn(function* ({ id, news, output }) {
const env = yield* GcpEnvironment.current;
const location = normalizeLocation(news.location ?? output?.location, env.region);
const integrationId = yield* toResourceId(id, news.integrationId, output?.integrationId);
const parent = `${locationParent(env.project, location)}/integrations/${integrationId}`;
const versionId = news.versionId ?? output?.versionId ?? "";
const name =
output?.name ??
(versionId.length > 0
? resourceName(env.project, location, integrationId, versionId)
: parent);
const ownership = yield* createInternalLabels(id);
const description = encodeOwnership(ownership, news.description);
const triggerConfigs = news.triggerConfigs;
const newIntegration = news.newIntegration !== false;
let current = yield* getByName(output?.name ?? (versionId ? name : ""));
if (current === undefined) {
current = yield* findOwned(parent, id);
}
if (current === undefined) {
const tryCreate = (createNew: boolean) =>
integrations.createProjectsLocationsIntegrationsVersions({
parent,
newIntegration: createNew,
body: {
description,
userLabel: news.userLabel,
triggerConfigs,
},
});
const created = yield* tryCreate(newIntegration).pipe(
Effect.catchTag(["Conflict", "BadRequest"], () =>
tryCreate(false).pipe(
Effect.catchTag(["Conflict", "BadRequest"], () => findOwned(parent, id)),
),
),
);
current = created ?? (yield* findOwned(parent, id));
}
if (current === undefined) {
return yield* new IntegrationsVersionNotResolved({ resourceName: name });
}
const currentName = current.name ?? name;
const descriptionChanged = (current.description ?? "") !== description;
const labelChanged = !sameText(current.userLabel, news.userLabel);
const triggersChanged = !jsonTriggersEqual(
triggersOf(current.triggerConfigs),
triggerConfigs,
);
if (descriptionChanged || labelChanged || triggersChanged) {
current = yield* integrations.patchProjectsLocationsIntegrationsVersions({
name: currentName,
updateMask: updateMaskOf(
descriptionChanged ? "description" : undefined,
labelChanged ? "user_label" : undefined,
triggersChanged ? "trigger_configs" : undefined,
),
body: {
name: currentName,
description,
userLabel: news.userLabel,
triggerConfigs,
},
});
}
return toAttrs(current, env.project, env.region);
}),
delete: Effect.fn(function* ({ output }) {
yield* integrations.deleteProjectsLocationsIntegrationsVersions({ name: output.name }).pipe(
Effect.catchTags({
NotFound: () => Effect.void,
BadRequest: () => Effect.void,
Conflict: () => Effect.void,
}),
);
yield* deleteIntegration(output.integration);
}),
});
const jsonTriggersEqual = (left: TriggerConfig[] | undefined, right: TriggerConfig[] | undefined) =>
JSON.stringify(left ?? null) === JSON.stringify(right ?? null);