The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as managedidentities from "@distilled.cloud/gcp/managedidentities_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 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 | domainResourceOf, |
| 14 | fieldMask, |
| 15 | listBackups, |
| 16 | parseName, |
| 17 | replaceOnIdentity, |
| 18 | ResourceNotResolved, |
| 19 | toPhysicalId, |
| 20 | userLabels, |
| 21 | waitForOperation, |
| 22 | waitUntilExists, |
| 23 | waitUntilGone, |
| 24 | waitUntilReady, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | export type DomainsBackupProps = { |
| 28 | /** |
| 29 | * Parent Managed Microsoft AD domain. Full name |
| 30 | * `projects/{project}/locations/global/domains/{domain}` or the domain |
| 31 | * FQDN. Immutable — changing it replaces the backup. |
| 32 | */ |
| 33 | domain: string; |
| 34 | /** |
| 35 | * Backup id (the `{backup}` segment of |
| 36 | * `.../domains/{domain}/backups/{backup}`). If omitted, a unique |
| 37 | * RFC1035 name is generated from the stack, stage, and logical id. |
| 38 | * 1-63 characters, lowercase letters, numbers, and hyphens. Immutable |
| 39 | * — changing it replaces the backup. |
| 40 | */ |
| 41 | backupId?: string; |
| 42 | /** |
| 43 | * User labels. Alchemy ownership labels are merged in automatically. |
| 44 | * Labels are the only mutable field. |
| 45 | */ |
| 46 | labels?: Record<string, string>; |
| 47 | }; |
| 48 | |
| 49 | export type DomainsBackup = Resource< |
| 50 | "GCP.ManagedIdentities.DomainsBackup", |
| 51 | DomainsBackupProps, |
| 52 | { |
| 53 | /** Full resource name. */ |
| 54 | name: string; |
| 55 | /** Backup id (last path segment). */ |
| 56 | backupId: string; |
| 57 | /** Parent domain resource name. */ |
| 58 | domain: string; |
| 59 | /** Project id. */ |
| 60 | project: string; |
| 61 | /** Resource location (`global`). */ |
| 62 | location: string; |
| 63 | /** User labels (Alchemy ownership labels stripped). */ |
| 64 | labels: Record<string, string>; |
| 65 | /** Backup type (`ON_DEMAND` or `SCHEDULED`). */ |
| 66 | type: string | undefined; |
| 67 | /** Server-reported state (`ACTIVE`, `CREATING`, …). */ |
| 68 | state: string | undefined; |
| 69 | /** Additional status information, if available. */ |
| 70 | statusMessage: string | undefined; |
| 71 | /** RFC3339 creation timestamp. */ |
| 72 | createTime: string | undefined; |
| 73 | /** RFC3339 last-update timestamp. */ |
| 74 | updateTime: string | undefined; |
| 75 | }, |
| 76 | never, |
| 77 | Providers |
| 78 | >; |
| 79 | |
| 80 | /** |
| 81 | * An on-demand backup of a Managed Microsoft AD domain. |
| 82 | * |
| 83 | * Changing `backupId` or `domain` replaces the backup. Labels update in |
| 84 | * place. Create is on-demand; scheduled backups are produced by the |
| 85 | * domain and are not managed here. |
| 86 | * |
| 87 | * ### Creating a Backup |
| 88 | * **Example:** Generated name |
| 89 | * ```typescript |
| 90 | * const backup = yield* GCP.ManagedIdentities.DomainsBackup("Nightly", { |
| 91 | * domain: domain.name, |
| 92 | * }); |
| 93 | * ``` |
| 94 | * |
| 95 | * **Example:** Explicit id and labels |
| 96 | * ```typescript |
| 97 | * const backup = yield* GCP.ManagedIdentities.DomainsBackup("Nightly", { |
| 98 | * domain: domain.name, |
| 99 | * backupId: "app-nightly", |
| 100 | * labels: { env: "prod" }, |
| 101 | * }); |
| 102 | * ``` |
| 103 | * |
| 104 | * ### Updating a Backup |
| 105 | * Re-declare the same logical id with changed props; the engine keeps the |
| 106 | * physical resource and updates it in place. |
| 107 | * |
| 108 | * **Example:** Labels |
| 109 | * ```typescript |
| 110 | * const backup = yield* GCP.ManagedIdentities.DomainsBackup("Nightly", { |
| 111 | * domain: domain.name, |
| 112 | * labels: { env: "prod", team: "identity" }, |
| 113 | * }); |
| 114 | * ``` |
| 115 | * |
| 116 | * @resource |
| 117 | * @category ManagedIdentities |
| 118 | */ |
| 119 | export const DomainsBackup = Resource<DomainsBackup>("GCP.ManagedIdentities.DomainsBackup"); |
| 120 | |
| 121 | const resourceName = (domain: string, backupId: string) => `${domain}/backups/${backupId}`; |
| 122 | |
| 123 | const toAttrs = (item: managedidentities.Backup, project: string) => { |
| 124 | const name = item.name ?? ""; |
| 125 | const parsed = parseName(name, "backups"); |
| 126 | return { |
| 127 | name, |
| 128 | backupId: parsed.id, |
| 129 | domain: parsed.parent, |
| 130 | project: parsed.project || project, |
| 131 | location: parsed.location, |
| 132 | labels: userLabels(item.labels), |
| 133 | type: item.type, |
| 134 | state: item.state, |
| 135 | statusMessage: item.statusMessage, |
| 136 | createTime: item.createTime, |
| 137 | updateTime: item.updateTime, |
| 138 | }; |
| 139 | }; |
| 140 | |
| 141 | const getByName = (name: string) => |
| 142 | name.length === 0 || name.includes("//") |
| 143 | ? Effect.undefined |
| 144 | : managedidentities |
| 145 | .getProjectsLocationsGlobalDomainsBackups({ name }) |
| 146 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 147 | |
| 148 | export const DomainsBackupProvider = () => |
| 149 | Provider.succeed(DomainsBackup, { |
| 150 | stables: ["name", "backupId", "domain", "project", "location", "createTime"], |
| 151 | |
| 152 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 153 | if (!isResolved(news)) return undefined; |
| 154 | const env = yield* GcpEnvironment.current; |
| 155 | const previousDomain = domainResourceOf(olds?.domain ?? output?.domain ?? "", env.project); |
| 156 | const nextDomain = domainResourceOf( |
| 157 | news.domain ?? olds?.domain ?? output?.domain ?? "", |
| 158 | env.project, |
| 159 | ); |
| 160 | return replaceOnIdentity({ |
| 161 | previousId: olds?.backupId ?? output?.backupId, |
| 162 | nextId: news.backupId ?? olds?.backupId ?? output?.backupId, |
| 163 | previousParent: previousDomain, |
| 164 | nextParent: nextDomain, |
| 165 | }); |
| 166 | }), |
| 167 | |
| 168 | read: Effect.fn(function* ({ id, olds, output }) { |
| 169 | const env = yield* GcpEnvironment.current; |
| 170 | const domain = domainResourceOf(olds?.domain ?? output?.domain ?? "", env.project); |
| 171 | const backupId = yield* toPhysicalId(id, olds?.backupId, output?.backupId, "backup"); |
| 172 | const name = output?.name ?? resourceName(domain, backupId); |
| 173 | const existing = yield* getByName(name); |
| 174 | if (existing === undefined) return undefined; |
| 175 | const attrs = toAttrs(existing, env.project); |
| 176 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 177 | }), |
| 178 | |
| 179 | list: () => |
| 180 | Effect.gen(function* () { |
| 181 | const env = yield* GcpEnvironment.current; |
| 182 | const items = yield* listBackups(env.project); |
| 183 | return items.map((item) => toAttrs(item, env.project)); |
| 184 | }), |
| 185 | |
| 186 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 187 | const env = yield* GcpEnvironment.current; |
| 188 | const domain = domainResourceOf(news.domain, env.project); |
| 189 | const backupId = yield* toPhysicalId(id, news.backupId, output?.backupId, "backup"); |
| 190 | const name = output?.name ?? resourceName(domain, backupId); |
| 191 | const desiredLabels = { |
| 192 | ...toLabels(news.labels), |
| 193 | ...(yield* createInternalLabels(id)), |
| 194 | }; |
| 195 | |
| 196 | let current = yield* getByName(output?.name ?? name); |
| 197 | |
| 198 | if (current === undefined) { |
| 199 | const created = yield* managedidentities |
| 200 | .createProjectsLocationsGlobalDomainsBackups({ |
| 201 | parent: domain, |
| 202 | backupId, |
| 203 | body: { |
| 204 | labels: desiredLabels, |
| 205 | }, |
| 206 | }) |
| 207 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 208 | if (created !== undefined) { |
| 209 | yield* waitForOperation(created); |
| 210 | } |
| 211 | current = yield* waitUntilExists(getByName(name), name); |
| 212 | } |
| 213 | |
| 214 | if (current === undefined) { |
| 215 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 216 | } |
| 217 | |
| 218 | current = yield* waitUntilReady( |
| 219 | getByName(current.name ?? name), |
| 220 | current.name ?? name, |
| 221 | (item: managedidentities.Backup) => item.state, |
| 222 | (item: managedidentities.Backup) => item.statusMessage, |
| 223 | ); |
| 224 | |
| 225 | const observedLabels = tagRecord(current.labels); |
| 226 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 227 | const mask = fieldMask([(upsert.length > 0 || removed.length > 0) && "labels"]); |
| 228 | |
| 229 | if (mask.length > 0) { |
| 230 | const operation = yield* managedidentities.patchProjectsLocationsGlobalDomainsBackups({ |
| 231 | name: current.name ?? name, |
| 232 | updateMask: mask, |
| 233 | body: { |
| 234 | name: current.name ?? name, |
| 235 | labels: desiredLabels, |
| 236 | }, |
| 237 | }); |
| 238 | yield* waitForOperation(operation); |
| 239 | current = yield* waitUntilReady( |
| 240 | getByName(current.name ?? name), |
| 241 | current.name ?? name, |
| 242 | (item: managedidentities.Backup) => item.state, |
| 243 | (item: managedidentities.Backup) => item.statusMessage, |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | return toAttrs(current, env.project); |
| 248 | }), |
| 249 | |
| 250 | delete: Effect.fn(function* ({ output }) { |
| 251 | if (!output.name || output.name.includes("//")) return; |
| 252 | const operation = yield* managedidentities |
| 253 | .deleteProjectsLocationsGlobalDomainsBackups({ name: output.name }) |
| 254 | .pipe( |
| 255 | Effect.retry({ |
| 256 | while: (error) => error._tag === "Conflict", |
| 257 | times: 8, |
| 258 | schedule: Schedule.spaced("2 seconds"), |
| 259 | }), |
| 260 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 261 | ); |
| 262 | if (operation !== undefined) { |
| 263 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 264 | } |
| 265 | yield* waitUntilGone(getByName(output.name), output.name); |
| 266 | }), |
| 267 | }); |