The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as osconfig from "@distilled.cloud/gcp/osconfig_v2"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | DEFAULT_ACTION, |
| 14 | DEFAULT_STATE, |
| 15 | OsconfigNotResolved, |
| 16 | collectOrchestrators, |
| 17 | defaultOrchestratedResource, |
| 18 | fieldMask, |
| 19 | fingerprint, |
| 20 | globalParent, |
| 21 | hasAlchemyLabelMap, |
| 22 | orchestratedPayload, |
| 23 | organizationParent, |
| 24 | parseName, |
| 25 | replaceOnIdentity, |
| 26 | resolveOrganization, |
| 27 | resourceName, |
| 28 | toPhysicalId, |
| 29 | tryResolveOrganization, |
| 30 | userLabels, |
| 31 | waitForOperation, |
| 32 | waitUntilExists, |
| 33 | waitUntilGone, |
| 34 | type OrchestratedResource, |
| 35 | type OrchestrationScope, |
| 36 | } from "./internal.ts"; |
| 37 | |
| 38 | export type OrganizationsLocationsGlobalPolicyOrchestratorProps = { |
| 39 | /** |
| 40 | * Organization (`organizations/{organization}` or the numeric id). When |
| 41 | * omitted, the stack project's organization is used. Immutable — |
| 42 | * changing it replaces the orchestrator. |
| 43 | */ |
| 44 | organizationId?: string; |
| 45 | /** |
| 46 | * Orchestrator id (the `{policyOrchestrator}` segment of |
| 47 | * `organizations/{organization}/locations/global/policyOrchestrators/{policyOrchestrator}`). |
| 48 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 49 | * it replaces the orchestrator. |
| 50 | */ |
| 51 | policyOrchestratorId?: string; |
| 52 | /** |
| 53 | * Action applied to matching OS policy assignments. |
| 54 | * `UPSERT` creates or updates; `DELETE` removes them. |
| 55 | * @default "UPSERT" |
| 56 | */ |
| 57 | action?: string; |
| 58 | /** |
| 59 | * Orchestrator state. `ACTIVE` applies changes; `STOPPED` is idle. |
| 60 | * Defaults to `STOPPED` so create does not roll out assignments. |
| 61 | * @default "STOPPED" |
| 62 | */ |
| 63 | state?: string; |
| 64 | /** |
| 65 | * Free-form description of the orchestrator. |
| 66 | */ |
| 67 | description?: string; |
| 68 | /** |
| 69 | * User labels. Alchemy ownership labels are merged in automatically. |
| 70 | */ |
| 71 | labels?: Record<string, string>; |
| 72 | /** |
| 73 | * Resource the orchestrator creates, updates, or deletes. When omitted, |
| 74 | * a no-op VALIDATION OS policy assignment is used. |
| 75 | */ |
| 76 | orchestratedResource?: OrchestratedResource; |
| 77 | /** |
| 78 | * Scope of projects, folders, and zones the orchestrator targets. |
| 79 | * When omitted, the parent organization is the entire scope. |
| 80 | */ |
| 81 | orchestrationScope?: OrchestrationScope; |
| 82 | }; |
| 83 | |
| 84 | export type OrganizationsLocationsGlobalPolicyOrchestrator = Resource< |
| 85 | "GCP.OSConfig.OrganizationsLocationsGlobalPolicyOrchestrator", |
| 86 | OrganizationsLocationsGlobalPolicyOrchestratorProps, |
| 87 | { |
| 88 | /** Full resource name. */ |
| 89 | name: string; |
| 90 | /** Orchestrator id (last path segment). */ |
| 91 | policyOrchestratorId: string; |
| 92 | /** Parent `organizations/{organization}/locations/global`. */ |
| 93 | parent: string; |
| 94 | /** Organization id. */ |
| 95 | organizationId: string; |
| 96 | /** Project id of the stack (used for quota). */ |
| 97 | project: string; |
| 98 | /** Orchestration action (`UPSERT` or `DELETE`). */ |
| 99 | action: string | undefined; |
| 100 | /** Orchestrator state (`ACTIVE` or `STOPPED`). */ |
| 101 | state: string | undefined; |
| 102 | /** User description. */ |
| 103 | description: string | undefined; |
| 104 | /** User labels (Alchemy ownership labels stripped). */ |
| 105 | labels: Record<string, string>; |
| 106 | /** Orchestrated OS policy assignment payload. */ |
| 107 | orchestratedResource: OrchestratedResource | undefined; |
| 108 | /** Orchestration scope selectors. */ |
| 109 | orchestrationScope: OrchestrationScope | undefined; |
| 110 | /** Whether the orchestrator is currently applying changes. */ |
| 111 | reconciling: boolean | undefined; |
| 112 | /** Server checksum used on update and delete. */ |
| 113 | etag: string | undefined; |
| 114 | /** RFC3339 creation timestamp. */ |
| 115 | createTime: string | undefined; |
| 116 | /** RFC3339 last-update timestamp. */ |
| 117 | updateTime: string | undefined; |
| 118 | }, |
| 119 | never, |
| 120 | Providers |
| 121 | >; |
| 122 | |
| 123 | /** |
| 124 | * An organization-scoped OS Config policy orchestrator. |
| 125 | * |
| 126 | * Policy orchestrators upsert or delete OS policy assignments across |
| 127 | * descendant projects and zones. `organizationId` and |
| 128 | * `policyOrchestratorId` are identity. Action, state, description, |
| 129 | * labels, the orchestrated payload, and scope update in place. |
| 130 | * |
| 131 | * ### Creating a Policy Orchestrator |
| 132 | * **Example:** Generated name on the project's organization |
| 133 | * ```typescript |
| 134 | * const orch = yield* GCP.OSConfig.OrganizationsLocationsGlobalPolicyOrchestrator( |
| 135 | * "Debian", |
| 136 | * { labels: { env: "test" } }, |
| 137 | * ); |
| 138 | * ``` |
| 139 | * |
| 140 | * **Example:** Explicit organization and id |
| 141 | * ```typescript |
| 142 | * const orch = yield* GCP.OSConfig.OrganizationsLocationsGlobalPolicyOrchestrator( |
| 143 | * "Debian", |
| 144 | * { |
| 145 | * organizationId: "123456", |
| 146 | * policyOrchestratorId: "debian-noop", |
| 147 | * state: "STOPPED", |
| 148 | * }, |
| 149 | * ); |
| 150 | * ``` |
| 151 | * |
| 152 | * @resource |
| 153 | * @category OSConfig |
| 154 | */ |
| 155 | export const OrganizationsLocationsGlobalPolicyOrchestrator = |
| 156 | Resource<OrganizationsLocationsGlobalPolicyOrchestrator>( |
| 157 | "GCP.OSConfig.OrganizationsLocationsGlobalPolicyOrchestrator", |
| 158 | ); |
| 159 | |
| 160 | const toAttrs = (item: osconfig.GoogleCloudOsconfigV2__PolicyOrchestrator, project: string) => { |
| 161 | const name = item.name ?? ""; |
| 162 | const parsed = parseName(name); |
| 163 | return { |
| 164 | name, |
| 165 | policyOrchestratorId: parsed.id, |
| 166 | parent: parsed.parent, |
| 167 | organizationId: parsed.organization, |
| 168 | project, |
| 169 | action: item.action, |
| 170 | state: item.state, |
| 171 | description: item.description, |
| 172 | labels: userLabels(item.labels), |
| 173 | orchestratedResource: item.orchestratedResource, |
| 174 | orchestrationScope: item.orchestrationScope, |
| 175 | reconciling: item.reconciling, |
| 176 | etag: item.etag, |
| 177 | createTime: item.createTime, |
| 178 | updateTime: item.updateTime, |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | const getByName = (name: string) => |
| 183 | name.length === 0 |
| 184 | ? Effect.undefined |
| 185 | : osconfig |
| 186 | .getOrganizationsLocationsGlobalPolicyOrchestrators({ name }) |
| 187 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 188 | |
| 189 | const desiredBody = ( |
| 190 | news: OrganizationsLocationsGlobalPolicyOrchestratorProps, |
| 191 | policyOrchestratorId: string, |
| 192 | desiredLabels: Record<string, string>, |
| 193 | ): osconfig.GoogleCloudOsconfigV2__PolicyOrchestrator => ({ |
| 194 | action: news.action ?? DEFAULT_ACTION, |
| 195 | state: news.state ?? DEFAULT_STATE, |
| 196 | description: news.description, |
| 197 | labels: desiredLabels, |
| 198 | orchestratedResource: orchestratedPayload( |
| 199 | news.orchestratedResource ?? defaultOrchestratedResource(policyOrchestratorId.slice(0, 63)), |
| 200 | ), |
| 201 | orchestrationScope: news.orchestrationScope, |
| 202 | }); |
| 203 | |
| 204 | export const OrganizationsLocationsGlobalPolicyOrchestratorProvider = () => |
| 205 | Provider.succeed(OrganizationsLocationsGlobalPolicyOrchestrator, { |
| 206 | stables: ["name", "policyOrchestratorId", "parent", "organizationId", "project", "createTime"], |
| 207 | |
| 208 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 209 | if (!isResolved(news)) return undefined; |
| 210 | const previousOrg = |
| 211 | olds?.organizationId !== undefined |
| 212 | ? organizationParent(olds.organizationId) |
| 213 | : output?.organizationId |
| 214 | ? organizationParent(output.organizationId) |
| 215 | : ""; |
| 216 | const nextOrg = |
| 217 | news.organizationId !== undefined ? organizationParent(news.organizationId) : previousOrg; |
| 218 | return replaceOnIdentity({ |
| 219 | previousId: olds?.policyOrchestratorId ?? output?.policyOrchestratorId, |
| 220 | nextId: |
| 221 | news.policyOrchestratorId ?? olds?.policyOrchestratorId ?? output?.policyOrchestratorId, |
| 222 | previousParent: previousOrg, |
| 223 | nextParent: nextOrg, |
| 224 | }); |
| 225 | }), |
| 226 | |
| 227 | read: Effect.fn(function* ({ id, olds, output }) { |
| 228 | const env = yield* GcpEnvironment.current; |
| 229 | const policyOrchestratorId = yield* toPhysicalId( |
| 230 | id, |
| 231 | olds?.policyOrchestratorId, |
| 232 | output?.policyOrchestratorId, |
| 233 | ); |
| 234 | let name = output?.name ?? ""; |
| 235 | if (name.length === 0) { |
| 236 | const organization = |
| 237 | olds?.organizationId !== undefined |
| 238 | ? organizationParent(olds.organizationId) |
| 239 | : output?.organizationId |
| 240 | ? organizationParent(output.organizationId) |
| 241 | : yield* tryResolveOrganization(); |
| 242 | if (organization === undefined) return undefined; |
| 243 | name = resourceName(globalParent(organization), policyOrchestratorId); |
| 244 | } |
| 245 | const existing = yield* getByName(name); |
| 246 | if (existing === undefined) return undefined; |
| 247 | const attrs = toAttrs(existing, env.project); |
| 248 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 249 | }), |
| 250 | |
| 251 | list: () => |
| 252 | Effect.gen(function* () { |
| 253 | const env = yield* GcpEnvironment.current; |
| 254 | const organization = yield* tryResolveOrganization(); |
| 255 | if (organization === undefined) return []; |
| 256 | const items = yield* collectOrchestrators( |
| 257 | osconfig.listOrganizationsLocationsGlobalPolicyOrchestrators.pages({ |
| 258 | parent: globalParent(organization), |
| 259 | pageSize: 1000, |
| 260 | }), |
| 261 | ); |
| 262 | return items |
| 263 | .filter((item) => hasAlchemyLabelMap(item.labels)) |
| 264 | .map((item) => toAttrs(item, env.project)); |
| 265 | }), |
| 266 | |
| 267 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 268 | const env = yield* GcpEnvironment.current; |
| 269 | const policyOrchestratorId = yield* toPhysicalId( |
| 270 | id, |
| 271 | news.policyOrchestratorId, |
| 272 | output?.policyOrchestratorId, |
| 273 | ); |
| 274 | const organization = yield* resolveOrganization(news.organizationId, output?.organizationId); |
| 275 | const parent = globalParent(organization); |
| 276 | const name = resourceName(parent, policyOrchestratorId); |
| 277 | const desiredLabels = { |
| 278 | ...toLabels(news.labels), |
| 279 | ...(yield* createInternalLabels(id)), |
| 280 | }; |
| 281 | const desired = desiredBody(news, policyOrchestratorId, desiredLabels); |
| 282 | |
| 283 | let current = yield* getByName(output?.name ?? name); |
| 284 | |
| 285 | if (current === undefined) { |
| 286 | const created = yield* osconfig |
| 287 | .createOrganizationsLocationsGlobalPolicyOrchestrators({ |
| 288 | parent, |
| 289 | policyOrchestratorId, |
| 290 | body: desired, |
| 291 | }) |
| 292 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 293 | if (created !== undefined) { |
| 294 | yield* waitForOperation(created); |
| 295 | } |
| 296 | current = yield* waitUntilExists(getByName(name), name); |
| 297 | } |
| 298 | |
| 299 | if (current === undefined) { |
| 300 | return yield* new OsconfigNotResolved({ resourceName: name }); |
| 301 | } |
| 302 | |
| 303 | const observedLabels = tagRecord(current.labels); |
| 304 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 305 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 306 | const actionChanged = |
| 307 | (current.action ?? DEFAULT_ACTION) !== (desired.action ?? DEFAULT_ACTION); |
| 308 | const stateChanged = (current.state ?? DEFAULT_STATE) !== (desired.state ?? DEFAULT_STATE); |
| 309 | const descriptionChanged = (current.description ?? "") !== (desired.description ?? ""); |
| 310 | const resourceChanged = |
| 311 | fingerprint(orchestratedPayload(current.orchestratedResource)) !== |
| 312 | fingerprint(desired.orchestratedResource); |
| 313 | const scopeChanged = |
| 314 | desired.orchestrationScope !== undefined && |
| 315 | fingerprint(current.orchestrationScope) !== fingerprint(desired.orchestrationScope); |
| 316 | const mask = fieldMask([ |
| 317 | labelsChanged && "labels", |
| 318 | actionChanged && "action", |
| 319 | stateChanged && "state", |
| 320 | descriptionChanged && "description", |
| 321 | resourceChanged && "orchestratedResource", |
| 322 | scopeChanged && "orchestrationScope", |
| 323 | ]); |
| 324 | |
| 325 | if (mask.length > 0) { |
| 326 | const operation = yield* osconfig.patchOrganizationsLocationsGlobalPolicyOrchestrators({ |
| 327 | name: current.name ?? name, |
| 328 | updateMask: mask, |
| 329 | body: { |
| 330 | ...desired, |
| 331 | name: current.name ?? name, |
| 332 | etag: current.etag, |
| 333 | }, |
| 334 | }); |
| 335 | yield* waitForOperation(operation); |
| 336 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 337 | } |
| 338 | |
| 339 | return toAttrs(current, env.project); |
| 340 | }), |
| 341 | |
| 342 | delete: Effect.fn(function* ({ output }) { |
| 343 | if (!output.name) return; |
| 344 | const operation = yield* osconfig |
| 345 | .deleteOrganizationsLocationsGlobalPolicyOrchestrators({ |
| 346 | name: output.name, |
| 347 | etag: output.etag, |
| 348 | }) |
| 349 | .pipe( |
| 350 | Effect.retry({ |
| 351 | while: (error) => error._tag === "Conflict", |
| 352 | times: 8, |
| 353 | schedule: Schedule.spaced("2 seconds"), |
| 354 | }), |
| 355 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 356 | ); |
| 357 | if (operation !== undefined) { |
| 358 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 359 | } |
| 360 | yield* waitUntilGone(getByName(output.name), output.name); |
| 361 | }), |
| 362 | }); |