The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as netapp from "@distilled.cloud/gcp/netapp_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 * as Stream from "effect/Stream"; |
| 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 | fieldMask, |
| 15 | hasAlchemyLabelMap, |
| 16 | listAtLocation, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitForDeleteOperation, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | waitUntilReady, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | const DEFAULT_DAILY = 2; |
| 32 | const DEFAULT_WEEKLY = 1; |
| 33 | const DEFAULT_MONTHLY = 1; |
| 34 | |
| 35 | export type BackupPolicyProps = { |
| 36 | /** |
| 37 | * Backup policy id (the `{backupPolicy}` segment of |
| 38 | * `projects/{project}/locations/{location}/backupPolicies/{backupPolicy}`). |
| 39 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 40 | * and logical id. Immutable — changing it replaces the policy. |
| 41 | */ |
| 42 | backupPolicyId?: string; |
| 43 | /** |
| 44 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 45 | * replaces the policy. `US-CENTRAL1` is accepted and normalized to |
| 46 | * `us-central1`. |
| 47 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 48 | */ |
| 49 | location?: string; |
| 50 | /** |
| 51 | * Number of daily backups to keep. Minimum is 2. |
| 52 | * @default 2 |
| 53 | */ |
| 54 | dailyBackupLimit?: number; |
| 55 | /** |
| 56 | * Number of weekly backups to keep. |
| 57 | * @default 1 |
| 58 | */ |
| 59 | weeklyBackupLimit?: number; |
| 60 | /** |
| 61 | * Number of monthly backups to keep. |
| 62 | * @default 1 |
| 63 | */ |
| 64 | monthlyBackupLimit?: number; |
| 65 | /** |
| 66 | * Whether scheduled backups are enabled for attached volumes. |
| 67 | * @default true |
| 68 | */ |
| 69 | enabled?: boolean; |
| 70 | /** |
| 71 | * Human-readable description. |
| 72 | */ |
| 73 | description?: string; |
| 74 | /** |
| 75 | * User labels. Alchemy ownership labels are merged in automatically. |
| 76 | */ |
| 77 | labels?: Record<string, string>; |
| 78 | }; |
| 79 | |
| 80 | export type BackupPolicy = Resource< |
| 81 | "GCP.NetApp.BackupPolicy", |
| 82 | BackupPolicyProps, |
| 83 | { |
| 84 | /** Full resource name. */ |
| 85 | name: string; |
| 86 | /** Backup policy id (last path segment). */ |
| 87 | backupPolicyId: string; |
| 88 | /** Project id. */ |
| 89 | project: string; |
| 90 | /** Location id. */ |
| 91 | location: string; |
| 92 | /** Daily backup retention count. */ |
| 93 | dailyBackupLimit: number | undefined; |
| 94 | /** Weekly backup retention count. */ |
| 95 | weeklyBackupLimit: number | undefined; |
| 96 | /** Monthly backup retention count. */ |
| 97 | monthlyBackupLimit: number | undefined; |
| 98 | /** Whether scheduled backups are enabled. */ |
| 99 | enabled: boolean | undefined; |
| 100 | /** Number of volumes using this policy. */ |
| 101 | assignedVolumeCount: number | undefined; |
| 102 | /** Human-readable description. */ |
| 103 | description: string | undefined; |
| 104 | /** User labels (Alchemy ownership labels stripped). */ |
| 105 | labels: Record<string, string>; |
| 106 | /** Server-reported state. */ |
| 107 | state: string | undefined; |
| 108 | /** RFC3339 creation timestamp. */ |
| 109 | createTime: string | undefined; |
| 110 | }, |
| 111 | never, |
| 112 | Providers |
| 113 | >; |
| 114 | |
| 115 | /** |
| 116 | * A Cloud NetApp Volumes backup policy that schedules daily, weekly, and |
| 117 | * monthly volume backups. |
| 118 | * |
| 119 | * Changing `backupPolicyId` or `location` replaces the policy. Retention |
| 120 | * limits, `enabled`, description, and labels update in place. |
| 121 | * |
| 122 | * ### Creating a Backup Policy |
| 123 | * **Example:** Generated name |
| 124 | * ```typescript |
| 125 | * const policy = yield* GCP.NetApp.BackupPolicy("Nightly", {}); |
| 126 | * ``` |
| 127 | * |
| 128 | * **Example:** Explicit id and limits |
| 129 | * ```typescript |
| 130 | * const policy = yield* GCP.NetApp.BackupPolicy("Nightly", { |
| 131 | * backupPolicyId: "app-nightly", |
| 132 | * dailyBackupLimit: 2, |
| 133 | * weeklyBackupLimit: 1, |
| 134 | * monthlyBackupLimit: 1, |
| 135 | * description: "nightly backups", |
| 136 | * labels: { env: "prod" }, |
| 137 | * }); |
| 138 | * ``` |
| 139 | * |
| 140 | * ### Updating a Backup Policy |
| 141 | * **Example:** Description and labels |
| 142 | * ```typescript |
| 143 | * const policy = yield* GCP.NetApp.BackupPolicy("Nightly", { |
| 144 | * description: "nightly backups v2", |
| 145 | * labels: { env: "prod", team: "storage" }, |
| 146 | * }); |
| 147 | * ``` |
| 148 | * |
| 149 | * @resource |
| 150 | * @category NetApp |
| 151 | */ |
| 152 | export const BackupPolicy = Resource<BackupPolicy>("GCP.NetApp.BackupPolicy"); |
| 153 | |
| 154 | const resourceName = (project: string, location: string, backupPolicyId: string) => |
| 155 | `projects/${project}/locations/${location}/backupPolicies/${backupPolicyId}`; |
| 156 | |
| 157 | const toAttrs = (policy: netapp.BackupPolicy, project: string, region: string) => { |
| 158 | const name = policy.name ?? ""; |
| 159 | const parsed = parseName(name, "backupPolicies", region); |
| 160 | return { |
| 161 | name, |
| 162 | backupPolicyId: parsed.id, |
| 163 | project: parsed.project || project, |
| 164 | location: parsed.location, |
| 165 | dailyBackupLimit: policy.dailyBackupLimit, |
| 166 | weeklyBackupLimit: policy.weeklyBackupLimit, |
| 167 | monthlyBackupLimit: policy.monthlyBackupLimit, |
| 168 | enabled: policy.enabled, |
| 169 | assignedVolumeCount: policy.assignedVolumeCount, |
| 170 | description: policy.description, |
| 171 | labels: userLabels(policy.labels), |
| 172 | state: policy.state, |
| 173 | createTime: policy.createTime, |
| 174 | }; |
| 175 | }; |
| 176 | |
| 177 | const getByName = (name: string) => |
| 178 | netapp |
| 179 | .getProjectsLocationsBackupPolicies({ name }) |
| 180 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 181 | |
| 182 | const listOwned = (project: string) => |
| 183 | listAtLocation(project, (parent) => |
| 184 | netapp.listProjectsLocationsBackupPolicies.pages({ parent, pageSize: 1000 }).pipe( |
| 185 | Stream.flatMap((page) => Stream.fromIterable(page.backupPolicies ?? [])), |
| 186 | Stream.filter((item) => hasAlchemyLabelMap(item.labels)), |
| 187 | Stream.runCollect, |
| 188 | Effect.map((chunk) => Array.from(chunk)), |
| 189 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 190 | ), |
| 191 | ); |
| 192 | |
| 193 | export const BackupPolicyProvider = () => |
| 194 | Provider.succeed(BackupPolicy, { |
| 195 | stables: ["name", "backupPolicyId", "project", "location", "createTime"], |
| 196 | |
| 197 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 198 | if (!isResolved(news)) return undefined; |
| 199 | const env = yield* GcpEnvironment.current; |
| 200 | return replaceOnIdentity({ |
| 201 | previousId: olds?.backupPolicyId ?? output?.backupPolicyId, |
| 202 | nextId: news.backupPolicyId ?? olds?.backupPolicyId ?? output?.backupPolicyId, |
| 203 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 204 | nextLocation: normalizeLocation( |
| 205 | news.location ?? olds?.location ?? output?.location, |
| 206 | env.region, |
| 207 | ), |
| 208 | }); |
| 209 | }), |
| 210 | |
| 211 | read: Effect.fn(function* ({ id, olds, output }) { |
| 212 | const env = yield* GcpEnvironment.current; |
| 213 | const backupPolicyId = yield* toPhysicalId( |
| 214 | id, |
| 215 | olds?.backupPolicyId, |
| 216 | output?.backupPolicyId, |
| 217 | "backuppolicy", |
| 218 | ); |
| 219 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 220 | const name = output?.name ?? resourceName(env.project, location, backupPolicyId); |
| 221 | const existing = yield* getByName(name); |
| 222 | if (existing === undefined) return undefined; |
| 223 | const attrs = toAttrs(existing, env.project, env.region); |
| 224 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 225 | }), |
| 226 | |
| 227 | list: () => |
| 228 | Effect.gen(function* () { |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | const items = yield* listOwned(env.project); |
| 231 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 232 | }), |
| 233 | |
| 234 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 235 | const env = yield* GcpEnvironment.current; |
| 236 | const backupPolicyId = yield* toPhysicalId( |
| 237 | id, |
| 238 | news.backupPolicyId, |
| 239 | output?.backupPolicyId, |
| 240 | "backuppolicy", |
| 241 | ); |
| 242 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 243 | const name = resourceName(env.project, location, backupPolicyId); |
| 244 | const dailyBackupLimit = news.dailyBackupLimit ?? DEFAULT_DAILY; |
| 245 | const weeklyBackupLimit = news.weeklyBackupLimit ?? DEFAULT_WEEKLY; |
| 246 | const monthlyBackupLimit = news.monthlyBackupLimit ?? DEFAULT_MONTHLY; |
| 247 | const enabled = news.enabled ?? true; |
| 248 | const desiredLabels = { |
| 249 | ...toLabels(news.labels), |
| 250 | ...(yield* createInternalLabels(id)), |
| 251 | }; |
| 252 | |
| 253 | let current = yield* getByName(output?.name ?? name); |
| 254 | |
| 255 | if (current === undefined) { |
| 256 | const created = yield* netapp |
| 257 | .createProjectsLocationsBackupPolicies({ |
| 258 | parent: parentOf(env.project, location), |
| 259 | backupPolicyId, |
| 260 | body: { |
| 261 | dailyBackupLimit, |
| 262 | weeklyBackupLimit, |
| 263 | monthlyBackupLimit, |
| 264 | enabled, |
| 265 | description: news.description, |
| 266 | labels: desiredLabels, |
| 267 | }, |
| 268 | }) |
| 269 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 270 | if (created !== undefined) { |
| 271 | yield* waitForOperation(created); |
| 272 | } |
| 273 | current = yield* waitUntilExists(getByName(name), name); |
| 274 | } |
| 275 | |
| 276 | if (current === undefined) { |
| 277 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 278 | } |
| 279 | |
| 280 | current = yield* waitUntilReady( |
| 281 | getByName(current.name ?? name), |
| 282 | current.name ?? name, |
| 283 | (item) => item.state, |
| 284 | ); |
| 285 | |
| 286 | const observedLabels = tagRecord(current.labels); |
| 287 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 288 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 289 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 290 | const dailyChanged = (current.dailyBackupLimit ?? DEFAULT_DAILY) !== dailyBackupLimit; |
| 291 | const weeklyChanged = (current.weeklyBackupLimit ?? DEFAULT_WEEKLY) !== weeklyBackupLimit; |
| 292 | const monthlyChanged = (current.monthlyBackupLimit ?? DEFAULT_MONTHLY) !== monthlyBackupLimit; |
| 293 | const enabledChanged = (current.enabled ?? true) !== enabled; |
| 294 | const mask = fieldMask([ |
| 295 | labelsChanged && "labels", |
| 296 | descriptionChanged && "description", |
| 297 | dailyChanged && "dailyBackupLimit", |
| 298 | weeklyChanged && "weeklyBackupLimit", |
| 299 | monthlyChanged && "monthlyBackupLimit", |
| 300 | enabledChanged && "enabled", |
| 301 | ]); |
| 302 | |
| 303 | if (mask.length > 0) { |
| 304 | const operation = yield* netapp.patchProjectsLocationsBackupPolicies({ |
| 305 | name: current.name ?? name, |
| 306 | updateMask: mask, |
| 307 | body: { |
| 308 | name: current.name ?? name, |
| 309 | labels: desiredLabels, |
| 310 | description: news.description, |
| 311 | dailyBackupLimit, |
| 312 | weeklyBackupLimit, |
| 313 | monthlyBackupLimit, |
| 314 | enabled, |
| 315 | }, |
| 316 | }); |
| 317 | yield* waitForOperation(operation); |
| 318 | current = yield* waitUntilReady( |
| 319 | getByName(current.name ?? name), |
| 320 | current.name ?? name, |
| 321 | (item) => item.state, |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | return toAttrs(current, env.project, env.region); |
| 326 | }), |
| 327 | |
| 328 | delete: Effect.fn(function* ({ output }) { |
| 329 | const operation = yield* netapp |
| 330 | .deleteProjectsLocationsBackupPolicies({ name: output.name }) |
| 331 | .pipe( |
| 332 | Effect.retry({ |
| 333 | while: (error) => error._tag === "Conflict", |
| 334 | times: 8, |
| 335 | schedule: Schedule.spaced("2 seconds"), |
| 336 | }), |
| 337 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 338 | ); |
| 339 | if (operation !== undefined) { |
| 340 | yield* waitForDeleteOperation(operation); |
| 341 | } |
| 342 | yield* waitUntilGone(getByName(output.name), output.name); |
| 343 | }), |
| 344 | }); |