import * as storagetransfer from "@distilled.cloud/gcp/storagetransfer_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 Schedule from "effect/Schedule";
import { GcpEnvironment } from "../Environment.ts";
import type { Providers } from "../Providers.ts";
import {
agentPoolIdOf,
agentPoolName,
createOwnership,
DEFAULT_AGENT_POOL,
encodeOwnership,
fieldMask,
MAX_AGENT_POOL_DISPLAY_NAME,
getAgentPool,
hasOwnershipMarker,
listAgentPools,
ownedByAlchemy,
parseOwnership,
retryApiDisabled,
sameValue,
sanitizeAgentPoolId,
toAgentPoolId,
waitAgentPoolCreated,
waitAgentPoolGone,
} from "./internal.ts";
export type BandwidthLimit = {
/**
* Bandwidth rate in megabytes per second, distributed across all
* agents in the pool. Omit for no limit.
*/
limitMbps?: string;
};
export type AgentPoolProps = {
/**
* Agent pool id (the `{agent_pool}` segment of
* `projects/{project}/agentPools/{agent_pool}`). If omitted, a unique
* name is generated from the stack, stage, and logical id. Must match
* `^(?!goog)[a-z]([a-z0-9-._~]*[a-z0-9])?
.repos/alchemy-effect/packages/gcp/src/StorageTransfer/AgentPool.ts at main · sailfork/t3code-github · Sailfork
and is at most 128
* characters. Immutable — changing it replaces the pool.
*/
agentPoolId?: string;
/**
* Client-specified description (combined with Alchemy ownership, max
* 127 characters). Agent pools have no labels field, so ownership is
* stored in a `[alchemy …]` prefix and stripped from attributes.
*/
displayName?: string;
/**
* Bandwidth cap for the pool. Unspecified means no limit.
*/
bandwidthLimit?: BandwidthLimit;
};
export type AgentPool = Resource<
"GCP.StorageTransfer.AgentPool",
AgentPoolProps,
{
/** Full resource name `projects/{project}/agentPools/{agent_pool}`. */
name: string;
/** Agent pool id (last path segment). */
agentPoolId: string;
/** Project id. */
project: string;
/** User display name with the Alchemy ownership prefix stripped. */
displayName: string | undefined;
/** Pool state (`CREATING`, `CREATED`, `DELETING`). */
state: string | undefined;
/** Configured bandwidth limit, if any. */
bandwidthLimit: BandwidthLimit | undefined;
},
never,
Providers
>;
/**
* A Storage Transfer Service agent pool for on-premises (POSIX) agents.
*
* Agent pools have no labels field, so Alchemy stamps ownership into
* `displayName` for `list` / nuke. Name is identity — changing
* `agentPoolId` replaces the pool. `displayName` and `bandwidthLimit`
* update in place.
*
* ### Creating an Agent Pool
* **Example:** Generated id
* ```typescript
* const pool = yield* GCP.StorageTransfer.AgentPool("OnPrem", {
* displayName: "warehouse scanners",
* });
* ```
*
* **Example:** Named pool with a bandwidth cap
* ```typescript
* const pool = yield* GCP.StorageTransfer.AgentPool("OnPrem", {
* agentPoolId: "warehouse-scanners",
* displayName: "warehouse scanners",
* bandwidthLimit: { limitMbps: "120" },
* });
* ```
*
* ### Updating an Agent Pool
* Change props on the same logical id; the engine keeps the physical id.
*
* **Example:** Raise the bandwidth cap
* ```typescript
* const pool = yield* GCP.StorageTransfer.AgentPool("OnPrem", {
* displayName: "warehouse scanners",
* bandwidthLimit: { limitMbps: "250" },
* });
* ```
*
* @resource
* @category StorageTransfer
*/
export const AgentPool = Resource<AgentPool>("GCP.StorageTransfer.AgentPool");
export class AgentPoolNotResolved extends Data.TaggedError(
"GCP.StorageTransfer.AgentPoolNotResolved",
)<{
resourceName: string;
}> {}
const toBandwidth = (
limit: storagetransfer.BandwidthLimit | BandwidthLimit | undefined,
): BandwidthLimit | undefined => {
const limitMbps = limit?.limitMbps;
if (limitMbps === undefined || limitMbps.length === 0) return undefined;
return { limitMbps };
};
const toAttrs = (pool: storagetransfer.AgentPool, project: string) => {
const name = pool.name ?? "";
return {
name,
agentPoolId: agentPoolIdOf(name),
project,
displayName: parseOwnership(pool.displayName).text,
state: pool.state,
bandwidthLimit: toBandwidth(pool.bandwidthLimit),
};
};
export const AgentPoolProvider = () =>
Provider.succeed(AgentPool, {
stables: ["name", "agentPoolId", "project"],
diff: Effect.fn(function* ({ news, olds, output }) {
if (!isResolved(news)) return undefined;
const previous = olds?.agentPoolId ?? output?.agentPoolId;
if (
previous !== undefined &&
news.agentPoolId !== undefined &&
sanitizeAgentPoolId(news.agentPoolId) !== previous
) {
return { action: "replace" as const, deleteFirst: false };
}
return undefined;
}),
read: Effect.fn(function* ({ id, olds, output }) {
const env = yield* GcpEnvironment.current;
const agentPoolId = yield* toAgentPoolId(id, olds?.agentPoolId, output?.agentPoolId);
const name = output?.name ?? agentPoolName(env.project, agentPoolId);
const existing = yield* getAgentPool(name);
if (existing === undefined) return undefined;
const attrs = toAttrs(existing, env.project);
return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
}),
list: () =>
Effect.gen(function* () {
const env = yield* GcpEnvironment.current;
const pools = yield* listAgentPools(env.project);
return pools
.filter(
(pool) =>
agentPoolIdOf(pool.name ?? "") !== DEFAULT_AGENT_POOL &&
hasOwnershipMarker(pool.displayName),
)
.map((pool) => toAttrs(pool, env.project));
}),
reconcile: Effect.fn(function* ({ id, news, output }) {
const env = yield* GcpEnvironment.current;
const agentPoolId = yield* toAgentPoolId(id, news.agentPoolId, output?.agentPoolId);
const name = agentPoolName(env.project, agentPoolId);
const ownership = yield* createOwnership(id);
const desiredDisplayName = encodeOwnership(
ownership,
news.displayName,
MAX_AGENT_POOL_DISPLAY_NAME,
);
const desiredLimit = toBandwidth(news.bandwidthLimit);
let current = yield* getAgentPool(output?.name ?? name);
if (current?.state === "DELETING") {
yield* waitAgentPoolGone(name);
current = undefined;
}
if (current === undefined) {
const created = yield* retryApiDisabled(
storagetransfer.createProjectsAgentPools({
projectId: env.project,
agentPoolId,
body: {
displayName: desiredDisplayName,
bandwidthLimit: desiredLimit,
},
}),
).pipe(Effect.catchTag("Conflict", () => getAgentPool(name)));
current = created ?? undefined;
}
if (current !== undefined && current.state === "CREATING") {
current = (yield* waitAgentPoolCreated(name)) ?? current;
}
if (current === undefined) {
return yield* new AgentPoolNotResolved({ resourceName: name });
}
const displayChanged = (current.displayName ?? "") !== desiredDisplayName;
const limitChanged = !sameValue(toBandwidth(current.bandwidthLimit), desiredLimit);
const updateMask = fieldMask([
displayChanged ? "display_name" : undefined,
limitChanged ? "bandwidth_limit" : undefined,
]);
if (updateMask.length > 0) {
current = yield* storagetransfer.patchProjectsAgentPools({
name: current.name ?? name,
updateMask,
body: {
displayName: desiredDisplayName,
bandwidthLimit: desiredLimit,
},
});
}
return toAttrs(current, env.project);
}),
delete: Effect.fn(function* ({ output }) {
if (output.agentPoolId === DEFAULT_AGENT_POOL) return;
yield* storagetransfer.deleteProjectsAgentPools({ name: output.name }).pipe(
Effect.catchTag("NotFound", () => Effect.void),
Effect.retry({
while: (error) => error._tag === "Conflict",
times: 8,
schedule: Schedule.exponential("500 millis"),
}),
);
}),
});
.repos/alchemy-effect/packages/gcp/src/StorageTransfer/AgentPool.ts at main · sailfork/t3code-github · Sailfork
and is at most 128\n * characters. Immutable — changing it replaces the pool.\n */\n agentPoolId?: string;\n /**\n * Client-specified description (combined with Alchemy ownership, max\n * 127 characters). Agent pools have no labels field, so ownership is\n * stored in a `[alchemy …]` prefix and stripped from attributes.\n */\n displayName?: string;\n /**\n * Bandwidth cap for the pool. Unspecified means no limit.\n */\n bandwidthLimit?: BandwidthLimit;\n};\n\nexport type AgentPool = Resource\u003c\n \"GCP.StorageTransfer.AgentPool\",\n AgentPoolProps,\n {\n /** Full resource name `projects/{project}/agentPools/{agent_pool}`. */\n name: string;\n /** Agent pool id (last path segment). */\n agentPoolId: string;\n /** Project id. */\n project: string;\n /** User display name with the Alchemy ownership prefix stripped. */\n displayName: string | undefined;\n /** Pool state (`CREATING`, `CREATED`, `DELETING`). */\n state: string | undefined;\n /** Configured bandwidth limit, if any. */\n bandwidthLimit: BandwidthLimit | undefined;\n },\n never,\n Providers\n>;\n\n/**\n * A Storage Transfer Service agent pool for on-premises (POSIX) agents.\n *\n * Agent pools have no labels field, so Alchemy stamps ownership into\n * `displayName` for `list` / nuke. Name is identity — changing\n * `agentPoolId` replaces the pool. `displayName` and `bandwidthLimit`\n * update in place.\n *\n * ### Creating an Agent Pool\n * **Example:** Generated id\n * ```typescript\n * const pool = yield* GCP.StorageTransfer.AgentPool(\"OnPrem\", {\n * displayName: \"warehouse scanners\",\n * });\n * ```\n *\n * **Example:** Named pool with a bandwidth cap\n * ```typescript\n * const pool = yield* GCP.StorageTransfer.AgentPool(\"OnPrem\", {\n * agentPoolId: \"warehouse-scanners\",\n * displayName: \"warehouse scanners\",\n * bandwidthLimit: { limitMbps: \"120\" },\n * });\n * ```\n *\n * ### Updating an Agent Pool\n * Change props on the same logical id; the engine keeps the physical id.\n *\n * **Example:** Raise the bandwidth cap\n * ```typescript\n * const pool = yield* GCP.StorageTransfer.AgentPool(\"OnPrem\", {\n * displayName: \"warehouse scanners\",\n * bandwidthLimit: { limitMbps: \"250\" },\n * });\n * ```\n *\n * @resource\n * @category StorageTransfer\n */\nexport const AgentPool = Resource\u003cAgentPool>(\"GCP.StorageTransfer.AgentPool\");\n\nexport class AgentPoolNotResolved extends Data.TaggedError(\n \"GCP.StorageTransfer.AgentPoolNotResolved\",\n)\u003c{\n resourceName: string;\n}> {}\n\nconst toBandwidth = (\n limit: storagetransfer.BandwidthLimit | BandwidthLimit | undefined,\n): BandwidthLimit | undefined => {\n const limitMbps = limit?.limitMbps;\n if (limitMbps === undefined || limitMbps.length === 0) return undefined;\n return { limitMbps };\n};\n\nconst toAttrs = (pool: storagetransfer.AgentPool, project: string) => {\n const name = pool.name ?? \"\";\n return {\n name,\n agentPoolId: agentPoolIdOf(name),\n project,\n displayName: parseOwnership(pool.displayName).text,\n state: pool.state,\n bandwidthLimit: toBandwidth(pool.bandwidthLimit),\n };\n};\n\nexport const AgentPoolProvider = () =>\n Provider.succeed(AgentPool, {\n stables: [\"name\", \"agentPoolId\", \"project\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n const previous = olds?.agentPoolId ?? output?.agentPoolId;\n if (\n previous !== undefined &&\n news.agentPoolId !== undefined &&\n sanitizeAgentPoolId(news.agentPoolId) !== previous\n ) {\n return { action: \"replace\" as const, deleteFirst: false };\n }\n return undefined;\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const env = yield* GcpEnvironment.current;\n const agentPoolId = yield* toAgentPoolId(id, olds?.agentPoolId, output?.agentPoolId);\n const name = output?.name ?? agentPoolName(env.project, agentPoolId);\n const existing = yield* getAgentPool(name);\n if (existing === undefined) return undefined;\n const attrs = toAttrs(existing, env.project);\n return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);\n }),\n\n list: () =>\n Effect.gen(function* () {\n const env = yield* GcpEnvironment.current;\n const pools = yield* listAgentPools(env.project);\n return pools\n .filter(\n (pool) =>\n agentPoolIdOf(pool.name ?? \"\") !== DEFAULT_AGENT_POOL &&\n hasOwnershipMarker(pool.displayName),\n )\n .map((pool) => toAttrs(pool, env.project));\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output }) {\n const env = yield* GcpEnvironment.current;\n const agentPoolId = yield* toAgentPoolId(id, news.agentPoolId, output?.agentPoolId);\n const name = agentPoolName(env.project, agentPoolId);\n const ownership = yield* createOwnership(id);\n const desiredDisplayName = encodeOwnership(\n ownership,\n news.displayName,\n MAX_AGENT_POOL_DISPLAY_NAME,\n );\n const desiredLimit = toBandwidth(news.bandwidthLimit);\n\n let current = yield* getAgentPool(output?.name ?? name);\n if (current?.state === \"DELETING\") {\n yield* waitAgentPoolGone(name);\n current = undefined;\n }\n\n if (current === undefined) {\n const created = yield* retryApiDisabled(\n storagetransfer.createProjectsAgentPools({\n projectId: env.project,\n agentPoolId,\n body: {\n displayName: desiredDisplayName,\n bandwidthLimit: desiredLimit,\n },\n }),\n ).pipe(Effect.catchTag(\"Conflict\", () => getAgentPool(name)));\n current = created ?? undefined;\n }\n\n if (current !== undefined && current.state === \"CREATING\") {\n current = (yield* waitAgentPoolCreated(name)) ?? current;\n }\n\n if (current === undefined) {\n return yield* new AgentPoolNotResolved({ resourceName: name });\n }\n\n const displayChanged = (current.displayName ?? \"\") !== desiredDisplayName;\n const limitChanged = !sameValue(toBandwidth(current.bandwidthLimit), desiredLimit);\n const updateMask = fieldMask([\n displayChanged ? \"display_name\" : undefined,\n limitChanged ? \"bandwidth_limit\" : undefined,\n ]);\n\n if (updateMask.length > 0) {\n current = yield* storagetransfer.patchProjectsAgentPools({\n name: current.name ?? name,\n updateMask,\n body: {\n displayName: desiredDisplayName,\n bandwidthLimit: desiredLimit,\n },\n });\n }\n\n return toAttrs(current, env.project);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n if (output.agentPoolId === DEFAULT_AGENT_POOL) return;\n yield* storagetransfer.deleteProjectsAgentPools({ name: output.name }).pipe(\n Effect.catchTag(\"NotFound\", () => Effect.void),\n Effect.retry({\n while: (error) => error._tag === \"Conflict\",\n times: 8,\n schedule: Schedule.exponential(\"500 millis\"),\n }),\n );\n }),\n });\n","preview":null,"history":null,"version":"323e79a8ca4b5aa5061aaa5a835e72aa89513c54"},"/_session?shell=1":null}