sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ ManagedIdentities/ Peering.ts

import * as managedidentities from "@distilled.cloud/gcp/managedidentities_v1";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import { tagRecord } from "alchemy/Tags";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import { GcpEnvironment } from "../Environment.ts";
import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import {
  domainResourceOf,
  fieldMask,
  globalParent,
  listPeerings,
  networkOf,
  parseName,
  replaceOnIdentity,
  ResourceNotResolved,
  toPhysicalId,
  userLabels,
  waitForOperation,
  waitUntilExists,
  waitUntilGone,
  waitUntilReady,
} from "./internal.ts";

export type PeeringProps = {
  /**
   * Peering id (the `{peering}` segment of
   * `projects/{project}/locations/global/peerings/{peering}`). If omitted,
   * a unique RFC1035 name is generated from the stack, stage, and logical
   * id. Must match `^[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/gcp/src/ManagedIdentities/Peering.ts at main · sailfork/t3code-github · Sailfork
	
		
. Immutable — * changing it replaces the peering. */ peeringId?: string; /** * Managed Microsoft AD domain to peer with. Must live in a different * project than `authorizedNetwork`. Full name * `projects/{project}/locations/global/domains/{domain}` or the domain * FQDN. Immutable — changing it replaces the peering. */ domainResource: string; /** * VPC network the domain is peered into, as an id (`default`) or full * name (`projects/{project}/global/networks/{network}`). Caller must * ensure CIDR subnets do not overlap the domain. Immutable — changing * it replaces the peering. */ authorizedNetwork: string; /** * User labels. Alchemy ownership labels are merged in automatically. * Labels are the only mutable field. */ labels?: Record<string, string>; }; export type Peering = Resource< "GCP.ManagedIdentities.Peering", PeeringProps, { /** Full resource name `projects/{project}/locations/global/peerings/{peering}`. */ name: string; /** Peering id (last path segment). */ peeringId: string; /** Project id. */ project: string; /** Resource location (`global`). */ location: string; /** Peered domain resource name. */ domainResource: string | undefined; /** Authorized VPC network. */ authorizedNetwork: string | undefined; /** User labels (Alchemy ownership labels stripped). */ labels: Record<string, string>; /** Server-reported state (`CONNECTED`, `CREATING`, …). */ state: string | undefined; /** Additional status information, if available. */ statusMessage: string | undefined; /** RFC3339 creation timestamp. */ createTime: string | undefined; /** RFC3339 last-update timestamp. */ updateTime: string | undefined; }, never, Providers >; /** * A Managed Microsoft AD domain peering — attaches a VPC in this * project to a Managed AD domain that lives in a **different** project * so VMs on that network can join the domain. Same-project access uses * `Domain.authorizedNetworks` instead; creating a peering when domain * and network share a project is rejected with `BadRequest`. * * Changing `peeringId`, `domainResource`, or `authorizedNetwork` * replaces the peering. Labels update in place. * * ### Creating a Peering * **Example:** Generated name * ```typescript * const peering = yield* GCP.ManagedIdentities.Peering("Spoke", { * domainResource: domain.name, * authorizedNetwork: "default", * }); * ``` * * **Example:** Explicit id and labels * ```typescript * const peering = yield* GCP.ManagedIdentities.Peering("Spoke", { * peeringId: "app-spoke", * domainResource: domain.name, * authorizedNetwork: "projects/my-project/global/networks/default", * labels: { env: "prod" }, * }); * ``` * * ### Updating a Peering * Re-declare the same logical id with changed props; the engine keeps the * physical resource and updates it in place. * * **Example:** Labels * ```typescript * const peering = yield* GCP.ManagedIdentities.Peering("Spoke", { * domainResource: domain.name, * authorizedNetwork: "default", * labels: { env: "prod", team: "identity" }, * }); * ``` * * @resource * @category ManagedIdentities */ export const Peering = Resource<Peering>("GCP.ManagedIdentities.Peering"); const resourceName = (project: string, peeringId: string) => `${globalParent(project)}/peerings/${peeringId}`; const toAttrs = (item: managedidentities.Peering, project: string) => { const name = item.name ?? ""; const parsed = parseName(name, "peerings"); return { name, peeringId: parsed.id, project: parsed.project || project, location: parsed.location, domainResource: item.domainResource, authorizedNetwork: item.authorizedNetwork, labels: userLabels(item.labels), state: item.state, statusMessage: item.statusMessage, createTime: item.createTime, updateTime: item.updateTime, }; }; const getByName = (name: string) => name.length === 0 ? Effect.undefined : managedidentities .getProjectsLocationsGlobalPeerings({ name }) .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); export const PeeringProvider = () => Provider.succeed(Peering, { stables: ["name", "peeringId", "project", "location", "createTime"], diff: Effect.fn(function* ({ news, olds, output }) { if (!isResolved(news)) return undefined; const env = yield* GcpEnvironment.current; const previousDomain = domainResourceOf( olds?.domainResource ?? output?.domainResource ?? "", env.project, ); const nextDomain = domainResourceOf( news.domainResource ?? olds?.domainResource ?? output?.domainResource ?? "", env.project, ); const previousNetwork = networkOf( olds?.authorizedNetwork ?? output?.authorizedNetwork ?? "", env.project, ); const nextNetwork = networkOf( news.authorizedNetwork ?? olds?.authorizedNetwork ?? output?.authorizedNetwork ?? "", env.project, ); return replaceOnIdentity({ previousId: olds?.peeringId ?? output?.peeringId, nextId: news.peeringId ?? olds?.peeringId ?? output?.peeringId, extra: (previousDomain.length > 0 && nextDomain.length > 0 && previousDomain !== nextDomain) || (previousNetwork.length > 0 && nextNetwork.length > 0 && previousNetwork !== nextNetwork), }); }), read: Effect.fn(function* ({ id, olds, output }) { const env = yield* GcpEnvironment.current; const peeringId = yield* toPhysicalId(id, olds?.peeringId, output?.peeringId, "peering"); const name = output?.name ?? resourceName(env.project, peeringId); const existing = yield* getByName(name); if (existing === undefined) return undefined; const attrs = toAttrs(existing, env.project); return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); }), list: () => Effect.gen(function* () { const env = yield* GcpEnvironment.current; const items = yield* listPeerings(env.project); return items.map((item) => toAttrs(item, env.project)); }), reconcile: Effect.fn(function* ({ id, news, output }) { const env = yield* GcpEnvironment.current; const peeringId = yield* toPhysicalId(id, news.peeringId, output?.peeringId, "peering"); const name = resourceName(env.project, peeringId); const domainResource = domainResourceOf(news.domainResource, env.project); const authorizedNetwork = networkOf(news.authorizedNetwork, env.project); const desiredLabels = { ...toLabels(news.labels), ...(yield* createInternalLabels(id)), }; let current = yield* getByName(output?.name ?? name); if (current === undefined) { const created = yield* managedidentities .createProjectsLocationsGlobalPeerings({ parent: globalParent(env.project), peeringId, body: { domainResource, authorizedNetwork, labels: desiredLabels, }, }) .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); if (created !== undefined) { yield* waitForOperation(created); } current = yield* waitUntilExists(getByName(name), name); } if (current === undefined) { return yield* new ResourceNotResolved({ resourceName: name }); } current = yield* waitUntilReady( getByName(current.name ?? name), current.name ?? name, (item: managedidentities.Peering) => item.state, (item: managedidentities.Peering) => item.statusMessage, ); const observedLabels = tagRecord(current.labels); const { upsert, removed } = diffLabels(observedLabels, desiredLabels); const mask = fieldMask([(upsert.length > 0 || removed.length > 0) && "labels"]); if (mask.length > 0) { const operation = yield* managedidentities.patchProjectsLocationsGlobalPeerings({ name: current.name ?? name, updateMask: mask, body: { name: current.name ?? name, labels: desiredLabels, }, }); yield* waitForOperation(operation); current = yield* waitUntilReady( getByName(current.name ?? name), current.name ?? name, (item: managedidentities.Peering) => item.state, (item: managedidentities.Peering) => item.statusMessage, ); } return toAttrs(current, env.project); }), delete: Effect.fn(function* ({ output }) { if (!output.name) return; const operation = yield* managedidentities .deleteProjectsLocationsGlobalPeerings({ name: output.name }) .pipe( Effect.retry({ while: (error) => error._tag === "Conflict", times: 8, schedule: Schedule.spaced("2 seconds"), }), Effect.catchTag("NotFound", () => Effect.undefined), ); if (operation !== undefined) { yield* waitForOperation(operation, { notFoundOk: true }); } yield* waitUntilGone(getByName(output.name), output.name); }), });
.repos/alchemy-effect/packages/gcp/src/ManagedIdentities/Peering.ts at main · sailfork/t3code-github · Sailfork
. Immutable —\n * changing it replaces the peering.\n */\n peeringId?: string;\n /**\n * Managed Microsoft AD domain to peer with. Must live in a different\n * project than `authorizedNetwork`. Full name\n * `projects/{project}/locations/global/domains/{domain}` or the domain\n * FQDN. Immutable — changing it replaces the peering.\n */\n domainResource: string;\n /**\n * VPC network the domain is peered into, as an id (`default`) or full\n * name (`projects/{project}/global/networks/{network}`). Caller must\n * ensure CIDR subnets do not overlap the domain. Immutable — changing\n * it replaces the peering.\n */\n authorizedNetwork: string;\n /**\n * User labels. Alchemy ownership labels are merged in automatically.\n * Labels are the only mutable field.\n */\n labels?: Record\u003cstring, string>;\n};\n\nexport type Peering = Resource\u003c\n \"GCP.ManagedIdentities.Peering\",\n PeeringProps,\n {\n /** Full resource name `projects/{project}/locations/global/peerings/{peering}`. */\n name: string;\n /** Peering id (last path segment). */\n peeringId: string;\n /** Project id. */\n project: string;\n /** Resource location (`global`). */\n location: string;\n /** Peered domain resource name. */\n domainResource: string | undefined;\n /** Authorized VPC network. */\n authorizedNetwork: string | undefined;\n /** User labels (Alchemy ownership labels stripped). */\n labels: Record\u003cstring, string>;\n /** Server-reported state (`CONNECTED`, `CREATING`, …). */\n state: string | undefined;\n /** Additional status information, if available. */\n statusMessage: string | undefined;\n /** RFC3339 creation timestamp. */\n createTime: string | undefined;\n /** RFC3339 last-update timestamp. */\n updateTime: string | undefined;\n },\n never,\n Providers\n>;\n\n/**\n * A Managed Microsoft AD domain peering — attaches a VPC in this\n * project to a Managed AD domain that lives in a **different** project\n * so VMs on that network can join the domain. Same-project access uses\n * `Domain.authorizedNetworks` instead; creating a peering when domain\n * and network share a project is rejected with `BadRequest`.\n *\n * Changing `peeringId`, `domainResource`, or `authorizedNetwork`\n * replaces the peering. Labels update in place.\n *\n * ### Creating a Peering\n * **Example:** Generated name\n * ```typescript\n * const peering = yield* GCP.ManagedIdentities.Peering(\"Spoke\", {\n * domainResource: domain.name,\n * authorizedNetwork: \"default\",\n * });\n * ```\n *\n * **Example:** Explicit id and labels\n * ```typescript\n * const peering = yield* GCP.ManagedIdentities.Peering(\"Spoke\", {\n * peeringId: \"app-spoke\",\n * domainResource: domain.name,\n * authorizedNetwork: \"projects/my-project/global/networks/default\",\n * labels: { env: \"prod\" },\n * });\n * ```\n *\n * ### Updating a Peering\n * Re-declare the same logical id with changed props; the engine keeps the\n * physical resource and updates it in place.\n *\n * **Example:** Labels\n * ```typescript\n * const peering = yield* GCP.ManagedIdentities.Peering(\"Spoke\", {\n * domainResource: domain.name,\n * authorizedNetwork: \"default\",\n * labels: { env: \"prod\", team: \"identity\" },\n * });\n * ```\n *\n * @resource\n * @category ManagedIdentities\n */\nexport const Peering = Resource\u003cPeering>(\"GCP.ManagedIdentities.Peering\");\n\nconst resourceName = (project: string, peeringId: string) =>\n `${globalParent(project)}/peerings/${peeringId}`;\n\nconst toAttrs = (item: managedidentities.Peering, project: string) => {\n const name = item.name ?? \"\";\n const parsed = parseName(name, \"peerings\");\n return {\n name,\n peeringId: parsed.id,\n project: parsed.project || project,\n location: parsed.location,\n domainResource: item.domainResource,\n authorizedNetwork: item.authorizedNetwork,\n labels: userLabels(item.labels),\n state: item.state,\n statusMessage: item.statusMessage,\n createTime: item.createTime,\n updateTime: item.updateTime,\n };\n};\n\nconst getByName = (name: string) =>\n name.length === 0\n ? Effect.undefined\n : managedidentities\n .getProjectsLocationsGlobalPeerings({ name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.undefined));\n\nexport const PeeringProvider = () =>\n Provider.succeed(Peering, {\n stables: [\"name\", \"peeringId\", \"project\", \"location\", \"createTime\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n const env = yield* GcpEnvironment.current;\n const previousDomain = domainResourceOf(\n olds?.domainResource ?? output?.domainResource ?? \"\",\n env.project,\n );\n const nextDomain = domainResourceOf(\n news.domainResource ?? olds?.domainResource ?? output?.domainResource ?? \"\",\n env.project,\n );\n const previousNetwork = networkOf(\n olds?.authorizedNetwork ?? output?.authorizedNetwork ?? \"\",\n env.project,\n );\n const nextNetwork = networkOf(\n news.authorizedNetwork ?? olds?.authorizedNetwork ?? output?.authorizedNetwork ?? \"\",\n env.project,\n );\n return replaceOnIdentity({\n previousId: olds?.peeringId ?? output?.peeringId,\n nextId: news.peeringId ?? olds?.peeringId ?? output?.peeringId,\n extra:\n (previousDomain.length > 0 && nextDomain.length > 0 && previousDomain !== nextDomain) ||\n (previousNetwork.length > 0 && nextNetwork.length > 0 && previousNetwork !== nextNetwork),\n });\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const env = yield* GcpEnvironment.current;\n const peeringId = yield* toPhysicalId(id, olds?.peeringId, output?.peeringId, \"peering\");\n const name = output?.name ?? resourceName(env.project, peeringId);\n const existing = yield* getByName(name);\n if (existing === undefined) return undefined;\n const attrs = toAttrs(existing, env.project);\n return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);\n }),\n\n list: () =>\n Effect.gen(function* () {\n const env = yield* GcpEnvironment.current;\n const items = yield* listPeerings(env.project);\n return items.map((item) => toAttrs(item, env.project));\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output }) {\n const env = yield* GcpEnvironment.current;\n const peeringId = yield* toPhysicalId(id, news.peeringId, output?.peeringId, \"peering\");\n const name = resourceName(env.project, peeringId);\n const domainResource = domainResourceOf(news.domainResource, env.project);\n const authorizedNetwork = networkOf(news.authorizedNetwork, env.project);\n const desiredLabels = {\n ...toLabels(news.labels),\n ...(yield* createInternalLabels(id)),\n };\n\n let current = yield* getByName(output?.name ?? name);\n\n if (current === undefined) {\n const created = yield* managedidentities\n .createProjectsLocationsGlobalPeerings({\n parent: globalParent(env.project),\n peeringId,\n body: {\n domainResource,\n authorizedNetwork,\n labels: desiredLabels,\n },\n })\n .pipe(Effect.catchTag(\"Conflict\", () => Effect.undefined));\n if (created !== undefined) {\n yield* waitForOperation(created);\n }\n current = yield* waitUntilExists(getByName(name), name);\n }\n\n if (current === undefined) {\n return yield* new ResourceNotResolved({ resourceName: name });\n }\n\n current = yield* waitUntilReady(\n getByName(current.name ?? name),\n current.name ?? name,\n (item: managedidentities.Peering) => item.state,\n (item: managedidentities.Peering) => item.statusMessage,\n );\n\n const observedLabels = tagRecord(current.labels);\n const { upsert, removed } = diffLabels(observedLabels, desiredLabels);\n const mask = fieldMask([(upsert.length > 0 || removed.length > 0) && \"labels\"]);\n\n if (mask.length > 0) {\n const operation = yield* managedidentities.patchProjectsLocationsGlobalPeerings({\n name: current.name ?? name,\n updateMask: mask,\n body: {\n name: current.name ?? name,\n labels: desiredLabels,\n },\n });\n yield* waitForOperation(operation);\n current = yield* waitUntilReady(\n getByName(current.name ?? name),\n current.name ?? name,\n (item: managedidentities.Peering) => item.state,\n (item: managedidentities.Peering) => item.statusMessage,\n );\n }\n\n return toAttrs(current, env.project);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n if (!output.name) return;\n const operation = yield* managedidentities\n .deleteProjectsLocationsGlobalPeerings({ name: output.name })\n .pipe(\n Effect.retry({\n while: (error) => error._tag === \"Conflict\",\n times: 8,\n schedule: Schedule.spaced(\"2 seconds\"),\n }),\n Effect.catchTag(\"NotFound\", () => Effect.undefined),\n );\n if (operation !== undefined) {\n yield* waitForOperation(operation, { notFoundOk: true });\n }\n yield* waitUntilGone(getByName(output.name), output.name);\n }),\n });\n","preview":null,"history":null,"version":"63f1b833aea9149313e4683d544f553a17f968c8"},"/_session?shell=1":null}