The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as assuredworkloads from "@distilled.cloud/gcp/assuredworkloads_v1"; |
| 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 Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | type ApiWorkload, |
| 15 | deleteChildResources, |
| 16 | fieldMask, |
| 17 | fingerprint, |
| 18 | lastSegment, |
| 19 | listOwnedWorkloads, |
| 20 | listWorkloads, |
| 21 | locationParent, |
| 22 | normalizeLocation, |
| 23 | organizationIdOf, |
| 24 | organizationParent, |
| 25 | parseName, |
| 26 | replaceOnIdentity, |
| 27 | resolveOrganization, |
| 28 | resourceNameFromOperation, |
| 29 | sameBool, |
| 30 | sameText, |
| 31 | toDisplayName, |
| 32 | tryResolveOrganization, |
| 33 | userLabels, |
| 34 | waitForOperation, |
| 35 | waitUntilExists, |
| 36 | waitUntilGone, |
| 37 | } from "./internal.ts"; |
| 38 | |
| 39 | export type ComplianceRegime = |
| 40 | | assuredworkloads.GoogleCloudAssuredworkloadsV1WorkloadComplianceRegimeEnum |
| 41 | | (string & {}); |
| 42 | |
| 43 | export type Partner = |
| 44 | | assuredworkloads.GoogleCloudAssuredworkloadsV1WorkloadPartnerEnum |
| 45 | | (string & {}); |
| 46 | |
| 47 | export type ResourceType = |
| 48 | | assuredworkloads.GoogleCloudAssuredworkloadsV1WorkloadResourceSettingsResourceTypeEnum |
| 49 | | (string & {}); |
| 50 | |
| 51 | export type ResourceSettings = { |
| 52 | /** |
| 53 | * Resource identifier. For a project this is `project_id`. For a |
| 54 | * KeyRing this is the keyring id. Folders ignore this — Google assigns |
| 55 | * the folder id. |
| 56 | */ |
| 57 | resourceId?: string; |
| 58 | /** |
| 59 | * Resource kind this setting applies to (`CONSUMER_FOLDER`, |
| 60 | * `CONSUMER_PROJECT`, `ENCRYPTION_KEYS_PROJECT`, `KEYRING`). |
| 61 | */ |
| 62 | resourceType?: ResourceType; |
| 63 | /** |
| 64 | * User-assigned display name used when creating the resource. |
| 65 | */ |
| 66 | displayName?: string; |
| 67 | }; |
| 68 | |
| 69 | export type ResourceInfo = { |
| 70 | /** |
| 71 | * Resource identifier. For a project this is the project number. |
| 72 | */ |
| 73 | resourceId?: string; |
| 74 | /** |
| 75 | * Resource kind (`CONSUMER_FOLDER`, `CONSUMER_PROJECT`, |
| 76 | * `ENCRYPTION_KEYS_PROJECT`, `KEYRING`). |
| 77 | */ |
| 78 | resourceType?: ResourceType; |
| 79 | }; |
| 80 | |
| 81 | export type KmsSettings = { |
| 82 | /** |
| 83 | * Time at which KMS creates a new crypto-key version and marks it |
| 84 | * primary. Input-only and immutable. |
| 85 | */ |
| 86 | nextRotationTime?: string; |
| 87 | /** |
| 88 | * Period by which `nextRotationTime` advances on each rotation. At |
| 89 | * least 24 hours and at most 876,000 hours. Input-only and immutable. |
| 90 | */ |
| 91 | rotationPeriod?: string; |
| 92 | }; |
| 93 | |
| 94 | export type WorkloadOptions = { |
| 95 | /** |
| 96 | * Key Access Justifications enrollment type. |
| 97 | */ |
| 98 | kajEnrollmentType?: string; |
| 99 | }; |
| 100 | |
| 101 | export type PartnerPermissions = { |
| 102 | /** |
| 103 | * Allow the partner to view inspectability logs and monitoring |
| 104 | * violations. |
| 105 | */ |
| 106 | dataLogsViewer?: boolean; |
| 107 | /** |
| 108 | * Allow the partner to view violation alerts. |
| 109 | */ |
| 110 | assuredWorkloadsMonitoring?: boolean; |
| 111 | /** |
| 112 | * Allow the partner to view support-case details for an AXT log. |
| 113 | */ |
| 114 | accessTransparencyLogsSupportCaseViewer?: boolean; |
| 115 | /** |
| 116 | * Allow the partner to view Access Approval logs. |
| 117 | */ |
| 118 | serviceAccessApprover?: boolean; |
| 119 | }; |
| 120 | |
| 121 | export type WorkloadProps = { |
| 122 | /** |
| 123 | * Parent organization (`organizations/{organization}` or the numeric |
| 124 | * id). Defaults to `GOOGLE_ORGANIZATION_ID` or the project's Resource |
| 125 | * Manager ancestor. Immutable — changing it replaces the workload. |
| 126 | */ |
| 127 | organization?: string; |
| 128 | /** |
| 129 | * Location (`us-central1`, `europe-west1`, …). Immutable — changing it |
| 130 | * replaces the workload. `US-CENTRAL1` is accepted and normalized to |
| 131 | * `us-central1`. |
| 132 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 133 | */ |
| 134 | location?: string; |
| 135 | /** |
| 136 | * User-assigned display name (4-30 characters: letters, numbers, |
| 137 | * hyphen, spaces). If omitted, a unique name is generated from the |
| 138 | * stack, stage, and logical id. |
| 139 | */ |
| 140 | displayName?: string; |
| 141 | /** |
| 142 | * Compliance regime. Immutable — changing it replaces the workload. |
| 143 | */ |
| 144 | complianceRegime: ComplianceRegime; |
| 145 | /** |
| 146 | * Billing account for resources created as children of this workload. |
| 147 | * Format `billingAccounts/{billing_account_id}`. Immutable — changing |
| 148 | * it replaces the workload. |
| 149 | */ |
| 150 | billingAccount?: string; |
| 151 | /** |
| 152 | * User labels. Alchemy ownership labels are merged in automatically. |
| 153 | */ |
| 154 | labels?: Record<string, string>; |
| 155 | /** |
| 156 | * When true, email notifications for violations are enabled. Honored |
| 157 | * only on update — create always enables notifications. |
| 158 | * @default true |
| 159 | */ |
| 160 | violationNotificationsEnabled?: boolean; |
| 161 | /** |
| 162 | * Partner regime associated with this workload. Immutable — changing |
| 163 | * it replaces the workload. |
| 164 | */ |
| 165 | partner?: Partner; |
| 166 | /** |
| 167 | * Billing account used to purchase services from sovereign partners. |
| 168 | * Format `billingAccounts/{billing_account_id}`. Immutable — changing |
| 169 | * it replaces the workload. |
| 170 | */ |
| 171 | partnerServicesBillingAccount?: string; |
| 172 | /** |
| 173 | * Permissions granted to the Assured Workloads partner service |
| 174 | * account. Updates use `mutatePartnerPermissions`. |
| 175 | */ |
| 176 | partnerPermissions?: PartnerPermissions; |
| 177 | /** |
| 178 | * Enable sovereignty controls (Europe/Canada customers). Immutable — |
| 179 | * changing it replaces the workload. |
| 180 | */ |
| 181 | enableSovereignControls?: boolean; |
| 182 | /** |
| 183 | * Parent folder for provisioned resources, as `folders/{folder_id}`. |
| 184 | * When omitted, resources are created under the organization. |
| 185 | * Immutable — changing it replaces the workload. |
| 186 | */ |
| 187 | provisionedResourcesParent?: string; |
| 188 | /** |
| 189 | * Custom settings for resources created with the workload. Input-only |
| 190 | * and immutable — changing them replaces the workload. |
| 191 | */ |
| 192 | resourceSettings?: ResourceSettings[]; |
| 193 | /** |
| 194 | * CMEK crypto-key settings. Deprecated — prefer `ENCRYPTION_KEYS_PROJECT` |
| 195 | * or `KEYRING` in `resourceSettings`. Immutable — changing them |
| 196 | * replaces the workload. |
| 197 | */ |
| 198 | kmsSettings?: KmsSettings; |
| 199 | /** |
| 200 | * Options applied at create time. Immutable — changing them replaces |
| 201 | * the workload. |
| 202 | */ |
| 203 | workloadOptions?: WorkloadOptions; |
| 204 | /** |
| 205 | * External id attached as a label on the workload and its projects so |
| 206 | * billing can be broken down. Create-only. |
| 207 | */ |
| 208 | externalId?: string; |
| 209 | }; |
| 210 | |
| 211 | export type Workload = Resource< |
| 212 | "GCP.AssuredWorkloads.Workload", |
| 213 | WorkloadProps, |
| 214 | { |
| 215 | /** Full resource name `organizations/{organization}/locations/{location}/workloads/{workload}`. */ |
| 216 | name: string; |
| 217 | /** Server-assigned workload id (last path segment). */ |
| 218 | workloadId: string; |
| 219 | /** Organization resource name. */ |
| 220 | organization: string; |
| 221 | /** Organization id. */ |
| 222 | organizationId: string; |
| 223 | /** Location id. */ |
| 224 | location: string; |
| 225 | /** Project id of the deploying stack. */ |
| 226 | project: string; |
| 227 | /** User-assigned display name. */ |
| 228 | displayName: string | undefined; |
| 229 | /** Compliance regime. */ |
| 230 | complianceRegime: string | undefined; |
| 231 | /** Billing account resource name. */ |
| 232 | billingAccount: string | undefined; |
| 233 | /** User labels (Alchemy ownership labels stripped). */ |
| 234 | labels: Record<string, string>; |
| 235 | /** Whether violation email notifications are enabled. */ |
| 236 | violationNotificationsEnabled: boolean; |
| 237 | /** Partner regime, if set. */ |
| 238 | partner: string | undefined; |
| 239 | /** Partner services billing account, if set. */ |
| 240 | partnerServicesBillingAccount: string | undefined; |
| 241 | /** Partner permissions. */ |
| 242 | partnerPermissions: PartnerPermissions | undefined; |
| 243 | /** Whether sovereignty controls are enabled. */ |
| 244 | enableSovereignControls: boolean | undefined; |
| 245 | /** Child resources created with the workload. */ |
| 246 | resources: ResourceInfo[]; |
| 247 | /** RFC3339 creation timestamp. */ |
| 248 | createTime: string | undefined; |
| 249 | /** Server etag for optimistic concurrency. */ |
| 250 | etag: string | undefined; |
| 251 | /** KAJ enrollment state. */ |
| 252 | kajEnrollmentState: string | undefined; |
| 253 | /** Whether resource monitoring is enabled. */ |
| 254 | resourceMonitoringEnabled: boolean | undefined; |
| 255 | }, |
| 256 | never, |
| 257 | Providers |
| 258 | >; |
| 259 | |
| 260 | /** |
| 261 | * An organization-scoped Assured Workloads folder that applies a |
| 262 | * compliance regime (FedRAMP, IL4, regional data boundary, …) to the |
| 263 | * projects created under it. |
| 264 | * |
| 265 | * Workload ids are assigned by Google. Alchemy stamps ownership into |
| 266 | * `labels` so `list` / `pnpm nuke:gcp` can find them. `organization`, |
| 267 | * `location`, `complianceRegime`, billing accounts, partner settings, |
| 268 | * and resource settings are identity — changing them replaces the |
| 269 | * workload. `displayName`, `labels`, `violationNotificationsEnabled`, |
| 270 | * and `partnerPermissions` update in place. |
| 271 | * |
| 272 | * Creating a workload provisions a folder (and optionally projects and |
| 273 | * key rings) under the organization. Delete first marks those children |
| 274 | * `DELETE_REQUESTED`, then deletes the workload. |
| 275 | * |
| 276 | * ### Creating a Workload |
| 277 | * **Example:** US regional access with generated display name |
| 278 | * ```typescript |
| 279 | * const workload = yield* GCP.AssuredWorkloads.Workload("Regulated", { |
| 280 | * complianceRegime: "US_REGIONAL_ACCESS", |
| 281 | * billingAccount: "billingAccounts/000000-000000-000000", |
| 282 | * labels: { env: "test" }, |
| 283 | * }); |
| 284 | * ``` |
| 285 | * |
| 286 | * **Example:** Named FedRAMP Moderate workload |
| 287 | * ```typescript |
| 288 | * const workload = yield* GCP.AssuredWorkloads.Workload("Fedramp", { |
| 289 | * organization: "organizations/123456789", |
| 290 | * location: "us-central1", |
| 291 | * displayName: "fedramp moderate", |
| 292 | * complianceRegime: "FEDRAMP_MODERATE", |
| 293 | * billingAccount: "billingAccounts/000000-000000-000000", |
| 294 | * }); |
| 295 | * ``` |
| 296 | * |
| 297 | * ### Updating a Workload |
| 298 | * **Example:** Change display name and labels |
| 299 | * ```typescript |
| 300 | * const workload = yield* GCP.AssuredWorkloads.Workload("Regulated", { |
| 301 | * complianceRegime: "US_REGIONAL_ACCESS", |
| 302 | * displayName: "regulated prod", |
| 303 | * labels: { env: "prod" }, |
| 304 | * violationNotificationsEnabled: false, |
| 305 | * }); |
| 306 | * ``` |
| 307 | * |
| 308 | * @resource |
| 309 | * @category AssuredWorkloads |
| 310 | */ |
| 311 | export const Workload = Resource<Workload>("GCP.AssuredWorkloads.Workload"); |
| 312 | |
| 313 | export class WorkloadNotResolved extends Data.TaggedError( |
| 314 | "GCP.AssuredWorkloads.WorkloadNotResolved", |
| 315 | )<{ |
| 316 | resourceName: string; |
| 317 | }> {} |
| 318 | |
| 319 | const toAttrs = ( |
| 320 | workload: ApiWorkload, |
| 321 | project: string, |
| 322 | region: string, |
| 323 | ): Workload["Attributes"] => { |
| 324 | const name = workload.name ?? ""; |
| 325 | const parsed = parseName(name, region); |
| 326 | const organization = parsed.organization ? organizationParent(parsed.organization) : ""; |
| 327 | return { |
| 328 | name, |
| 329 | workloadId: parsed.id || lastSegment(name), |
| 330 | organization, |
| 331 | organizationId: organizationIdOf(organization), |
| 332 | location: parsed.location || region, |
| 333 | project, |
| 334 | displayName: workload.displayName, |
| 335 | complianceRegime: workload.complianceRegime, |
| 336 | billingAccount: workload.billingAccount, |
| 337 | labels: userLabels(workload.labels), |
| 338 | violationNotificationsEnabled: workload.violationNotificationsEnabled ?? true, |
| 339 | partner: workload.partner, |
| 340 | partnerServicesBillingAccount: workload.partnerServicesBillingAccount, |
| 341 | partnerPermissions: workload.partnerPermissions, |
| 342 | enableSovereignControls: workload.enableSovereignControls, |
| 343 | resources: (workload.resources ?? []).map((resource) => ({ |
| 344 | resourceId: resource.resourceId, |
| 345 | resourceType: resource.resourceType, |
| 346 | })), |
| 347 | createTime: workload.createTime, |
| 348 | etag: workload.etag, |
| 349 | kajEnrollmentState: workload.kajEnrollmentState, |
| 350 | resourceMonitoringEnabled: workload.resourceMonitoringEnabled, |
| 351 | }; |
| 352 | }; |
| 353 | |
| 354 | const getByName = (name: string) => |
| 355 | name.length === 0 |
| 356 | ? Effect.undefined |
| 357 | : assuredworkloads |
| 358 | .getOrganizationsLocationsWorkloads({ name }) |
| 359 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 360 | |
| 361 | const findOwned = (id: string, organization: string, location: string) => |
| 362 | Effect.gen(function* () { |
| 363 | const workloads = yield* listWorkloads(organization, location); |
| 364 | for (const workload of workloads) { |
| 365 | if (yield* hasAlchemyLabels(id, tagRecord(workload.labels))) { |
| 366 | return workload; |
| 367 | } |
| 368 | } |
| 369 | return undefined; |
| 370 | }); |
| 371 | |
| 372 | const observe = (id: string, name: string | undefined, organization: string, location: string) => |
| 373 | Effect.gen(function* () { |
| 374 | if (name !== undefined && name.length > 0) { |
| 375 | const byName = yield* getByName(name); |
| 376 | if (byName !== undefined) return byName; |
| 377 | } |
| 378 | return yield* findOwned(id, organization, location); |
| 379 | }); |
| 380 | |
| 381 | const desiredBody = ( |
| 382 | news: WorkloadProps, |
| 383 | displayName: string, |
| 384 | labels: Record<string, string>, |
| 385 | ): ApiWorkload => ({ |
| 386 | displayName, |
| 387 | complianceRegime: news.complianceRegime, |
| 388 | labels, |
| 389 | billingAccount: news.billingAccount, |
| 390 | partner: news.partner, |
| 391 | partnerServicesBillingAccount: news.partnerServicesBillingAccount, |
| 392 | partnerPermissions: news.partnerPermissions, |
| 393 | enableSovereignControls: news.enableSovereignControls, |
| 394 | provisionedResourcesParent: news.provisionedResourcesParent, |
| 395 | resourceSettings: news.resourceSettings, |
| 396 | kmsSettings: news.kmsSettings, |
| 397 | workloadOptions: news.workloadOptions, |
| 398 | }); |
| 399 | |
| 400 | export const WorkloadProvider = () => |
| 401 | Provider.succeed(Workload, { |
| 402 | stables: [ |
| 403 | "name", |
| 404 | "workloadId", |
| 405 | "organization", |
| 406 | "organizationId", |
| 407 | "location", |
| 408 | "project", |
| 409 | "createTime", |
| 410 | ], |
| 411 | |
| 412 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 413 | if (!isResolved(news)) return undefined; |
| 414 | const previousOrg = olds?.organization ?? output?.organization; |
| 415 | const nextOrg = |
| 416 | news.organization !== undefined ? organizationParent(news.organization) : previousOrg; |
| 417 | const previousLocation = olds?.location ?? output?.location; |
| 418 | const previousRegime = olds?.complianceRegime ?? output?.complianceRegime; |
| 419 | const previousBilling = olds?.billingAccount ?? output?.billingAccount; |
| 420 | const previousPartner = olds?.partner ?? output?.partner; |
| 421 | const previousPartnerBilling = |
| 422 | olds?.partnerServicesBillingAccount ?? output?.partnerServicesBillingAccount; |
| 423 | const previousParent = olds?.provisionedResourcesParent ?? undefined; |
| 424 | const previousSovereign = olds?.enableSovereignControls ?? output?.enableSovereignControls; |
| 425 | return replaceOnIdentity( |
| 426 | (previousOrg !== undefined && |
| 427 | nextOrg !== undefined && |
| 428 | organizationParent(previousOrg) !== organizationParent(nextOrg)) || |
| 429 | (previousLocation !== undefined && |
| 430 | news.location !== undefined && |
| 431 | normalizeLocation(previousLocation) !== normalizeLocation(news.location)) || |
| 432 | (previousRegime !== undefined && previousRegime !== news.complianceRegime) || |
| 433 | !sameText(previousBilling, news.billingAccount) || |
| 434 | !sameText(previousPartner, news.partner) || |
| 435 | !sameText(previousPartnerBilling, news.partnerServicesBillingAccount) || |
| 436 | !sameText(previousParent, news.provisionedResourcesParent) || |
| 437 | (news.enableSovereignControls !== undefined && |
| 438 | previousSovereign !== undefined && |
| 439 | previousSovereign !== news.enableSovereignControls) || |
| 440 | (news.resourceSettings !== undefined && |
| 441 | olds?.resourceSettings !== undefined && |
| 442 | fingerprint(olds.resourceSettings) !== fingerprint(news.resourceSettings)) || |
| 443 | (news.kmsSettings !== undefined && |
| 444 | olds?.kmsSettings !== undefined && |
| 445 | fingerprint(olds.kmsSettings) !== fingerprint(news.kmsSettings)) || |
| 446 | (news.workloadOptions !== undefined && |
| 447 | olds?.workloadOptions !== undefined && |
| 448 | fingerprint(olds.workloadOptions) !== fingerprint(news.workloadOptions)), |
| 449 | ); |
| 450 | }), |
| 451 | |
| 452 | read: Effect.fn(function* ({ id, olds, output }) { |
| 453 | const env = yield* GcpEnvironment.current; |
| 454 | const organization = yield* resolveOrganization( |
| 455 | olds?.organization ?? output?.organization, |
| 456 | output?.organization, |
| 457 | ).pipe( |
| 458 | Effect.catchTag("GCP.AssuredWorkloads.OrganizationNotResolved", () => |
| 459 | Effect.succeed(output?.organization ?? ""), |
| 460 | ), |
| 461 | ); |
| 462 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 463 | const existing = yield* observe(id, output?.name, organization, location); |
| 464 | if (existing === undefined) return undefined; |
| 465 | const attrs = toAttrs(existing, env.project, env.region); |
| 466 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 467 | }), |
| 468 | |
| 469 | list: () => |
| 470 | Effect.gen(function* () { |
| 471 | const env = yield* GcpEnvironment.current; |
| 472 | const organization = yield* tryResolveOrganization(); |
| 473 | if (organization === undefined) return []; |
| 474 | const workloads = yield* listOwnedWorkloads(organization, env.region); |
| 475 | return workloads.map((workload) => toAttrs(workload, env.project, env.region)); |
| 476 | }), |
| 477 | |
| 478 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 479 | const env = yield* GcpEnvironment.current; |
| 480 | const organization = yield* resolveOrganization( |
| 481 | news.organization ?? output?.organization, |
| 482 | output?.organization, |
| 483 | ); |
| 484 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 485 | const parent = locationParent(organization, location); |
| 486 | const desiredLabels = { |
| 487 | ...toLabels(news.labels), |
| 488 | ...(yield* createInternalLabels(id)), |
| 489 | }; |
| 490 | const displayName = yield* toDisplayName(id, news.displayName, output?.displayName); |
| 491 | |
| 492 | let current = yield* observe(id, output?.name, organization, location); |
| 493 | |
| 494 | if (current === undefined) { |
| 495 | const created = yield* assuredworkloads |
| 496 | .createOrganizationsLocationsWorkloads({ |
| 497 | parent, |
| 498 | externalId: news.externalId, |
| 499 | body: desiredBody(news, displayName, desiredLabels), |
| 500 | }) |
| 501 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 502 | if (created !== undefined) { |
| 503 | // ALREADY_EXISTS (code 6) is a create race: find the winner below. |
| 504 | const settled = yield* waitForOperation(created).pipe( |
| 505 | Effect.catchIf( |
| 506 | (error) => error._tag === "GCP.OperationFailed" && error.code === 6, |
| 507 | () => Effect.succeed(created), |
| 508 | ), |
| 509 | ); |
| 510 | const createdName = |
| 511 | resourceNameFromOperation(settled) ?? resourceNameFromOperation(created); |
| 512 | current = |
| 513 | createdName !== undefined |
| 514 | ? yield* waitUntilExists(getByName(createdName), createdName) |
| 515 | : yield* waitUntilExists(findOwned(id, organization, location), parent); |
| 516 | } else { |
| 517 | current = yield* waitUntilExists(findOwned(id, organization, location), parent); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | if (current === undefined || current.name === undefined) { |
| 522 | return yield* new WorkloadNotResolved({ |
| 523 | resourceName: output?.name ?? parent, |
| 524 | }); |
| 525 | } |
| 526 | |
| 527 | const name = current.name; |
| 528 | const observedLabels = tagRecord(current.labels); |
| 529 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 530 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 531 | const displayChanged = !sameText(current.displayName, displayName); |
| 532 | const notifyChanged = |
| 533 | news.violationNotificationsEnabled !== undefined && |
| 534 | !sameBool(current.violationNotificationsEnabled, news.violationNotificationsEnabled); |
| 535 | const mask = fieldMask([ |
| 536 | displayChanged && "display_name", |
| 537 | labelsChanged && "labels", |
| 538 | notifyChanged && "violation_notifications_enabled", |
| 539 | ]); |
| 540 | |
| 541 | if (mask.length > 0) { |
| 542 | current = yield* assuredworkloads.patchOrganizationsLocationsWorkloads({ |
| 543 | name, |
| 544 | updateMask: mask, |
| 545 | body: { |
| 546 | name, |
| 547 | displayName, |
| 548 | labels: desiredLabels, |
| 549 | violationNotificationsEnabled: |
| 550 | news.violationNotificationsEnabled ?? current.violationNotificationsEnabled, |
| 551 | etag: current.etag, |
| 552 | }, |
| 553 | }); |
| 554 | } |
| 555 | |
| 556 | const partnerChanged = |
| 557 | news.partnerPermissions !== undefined && |
| 558 | fingerprint(current.partnerPermissions) !== fingerprint(news.partnerPermissions); |
| 559 | if (partnerChanged) { |
| 560 | current = yield* assuredworkloads.mutatePartnerPermissionsOrganizationsLocationsWorkloads({ |
| 561 | name, |
| 562 | body: { |
| 563 | etag: current.etag, |
| 564 | partnerPermissions: news.partnerPermissions, |
| 565 | updateMask: "partner_permissions", |
| 566 | }, |
| 567 | }); |
| 568 | } |
| 569 | |
| 570 | if (current === undefined) { |
| 571 | return yield* new WorkloadNotResolved({ resourceName: name }); |
| 572 | } |
| 573 | |
| 574 | return toAttrs(current, env.project, env.region); |
| 575 | }), |
| 576 | |
| 577 | delete: Effect.fn(function* ({ output }) { |
| 578 | if (!output.name) return; |
| 579 | yield* Effect.gen(function* () { |
| 580 | const existing = yield* getByName(output.name); |
| 581 | if (existing !== undefined) { |
| 582 | yield* deleteChildResources(existing); |
| 583 | } |
| 584 | yield* assuredworkloads.deleteOrganizationsLocationsWorkloads({ |
| 585 | name: output.name, |
| 586 | etag: output.etag, |
| 587 | }); |
| 588 | }).pipe( |
| 589 | Effect.retry({ |
| 590 | while: (error) => error._tag === "BadRequest" || error._tag === "Conflict", |
| 591 | times: 8, |
| 592 | schedule: Schedule.spaced("3 seconds"), |
| 593 | }), |
| 594 | Effect.catchTag("NotFound", () => Effect.void), |
| 595 | ); |
| 596 | yield* waitUntilGone(getByName(output.name), output.name); |
| 597 | }), |
| 598 | }); |