The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as oracle from "@distilled.cloud/gcp/oracledatabase_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 { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | OracleDatabaseNotResolved, |
| 13 | collectPages, |
| 14 | hasAlchemyLabelMap, |
| 15 | listAtLocation, |
| 16 | normalizeLocation, |
| 17 | parentOf, |
| 18 | parseName, |
| 19 | replaceOnIdentity, |
| 20 | resourceNameOf, |
| 21 | retryConflict, |
| 22 | retryQuota, |
| 23 | toPhysicalId, |
| 24 | userLabels, |
| 25 | waitUntilExists, |
| 26 | waitUntilGone, |
| 27 | waitUntilReady, |
| 28 | } from "./internal.ts"; |
| 29 | import { waitForOperation } from "./operations.ts"; |
| 30 | |
| 31 | const COLLECTION = "exascaleDbStorageVaults"; |
| 32 | const FALLBACK_ID = "exavault"; |
| 33 | |
| 34 | export type ExascaleDbStorageDetails = { |
| 35 | /** Total storage allocation in GB. Required on create. */ |
| 36 | totalSizeGbs?: number; |
| 37 | }; |
| 38 | |
| 39 | export type ExascaleDbStorageVaultPropertiesInput = { |
| 40 | /** Additional flash cache as a percent of high-capacity storage. */ |
| 41 | additionalFlashCachePercent?: number; |
| 42 | /** Storage details. Required on create. */ |
| 43 | exascaleDbStorageDetails?: ExascaleDbStorageDetails; |
| 44 | /** Vault description. */ |
| 45 | description?: string; |
| 46 | }; |
| 47 | |
| 48 | export type ExascaleDbStorageVaultProps = { |
| 49 | /** |
| 50 | * Vault id. If omitted, a unique RFC1035 name is generated. Immutable. |
| 51 | */ |
| 52 | exascaleDbStorageVaultId?: string; |
| 53 | /** |
| 54 | * Region. Immutable. |
| 55 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 56 | */ |
| 57 | location?: string; |
| 58 | /** |
| 59 | * Display name. Required on create. |
| 60 | */ |
| 61 | displayName?: string; |
| 62 | /** |
| 63 | * GCP Oracle zone. Immutable. |
| 64 | */ |
| 65 | gcpOracleZone?: string; |
| 66 | /** |
| 67 | * Exadata Infrastructure this vault is created on. Immutable. |
| 68 | */ |
| 69 | exadataInfrastructure?: string; |
| 70 | /** |
| 71 | * User labels. Alchemy ownership labels are merged in automatically. |
| 72 | */ |
| 73 | labels?: Record<string, string>; |
| 74 | /** |
| 75 | * Vault properties. |
| 76 | */ |
| 77 | properties?: ExascaleDbStorageVaultPropertiesInput; |
| 78 | /** Total size in GB. Convenience alias. */ |
| 79 | totalSizeGbs?: number; |
| 80 | /** Vault description. Convenience alias. */ |
| 81 | description?: string; |
| 82 | }; |
| 83 | |
| 84 | export type ExascaleDbStorageVault = Resource< |
| 85 | "GCP.OracleDatabase.ExascaleDbStorageVault", |
| 86 | ExascaleDbStorageVaultProps, |
| 87 | { |
| 88 | /** Full resource name. */ |
| 89 | name: string; |
| 90 | /** Vault id. */ |
| 91 | exascaleDbStorageVaultId: string; |
| 92 | /** Project id. */ |
| 93 | project: string; |
| 94 | /** Location id. */ |
| 95 | location: string; |
| 96 | /** Display name. */ |
| 97 | displayName: string | undefined; |
| 98 | /** GCP Oracle zone. */ |
| 99 | gcpOracleZone: string | undefined; |
| 100 | /** Exadata Infrastructure. */ |
| 101 | exadataInfrastructure: string | undefined; |
| 102 | /** User labels (Alchemy ownership labels stripped). */ |
| 103 | labels: Record<string, string>; |
| 104 | /** Entitlement id. */ |
| 105 | entitlementId: string | undefined; |
| 106 | /** Lifecycle state. */ |
| 107 | state: string | undefined; |
| 108 | /** Total size in GB. */ |
| 109 | totalSizeGbs: number | undefined; |
| 110 | /** Available size in GB. */ |
| 111 | availableSizeGbs: number | undefined; |
| 112 | /** Description. */ |
| 113 | description: string | undefined; |
| 114 | /** OCID. */ |
| 115 | ocid: string | undefined; |
| 116 | /** RFC3339 creation timestamp. */ |
| 117 | createTime: string | undefined; |
| 118 | }, |
| 119 | never, |
| 120 | Providers |
| 121 | >; |
| 122 | |
| 123 | /** |
| 124 | * An Oracle Exascale DB storage vault on Google Cloud. |
| 125 | * |
| 126 | * Changing `exascaleDbStorageVaultId`, `location`, |
| 127 | * `exadataInfrastructure`, or `gcpOracleZone` replaces the vault. There |
| 128 | * is no patch API in the distilled SDK, so labels are applied at create. |
| 129 | * |
| 130 | * ### Creating a storage vault |
| 131 | * **Example:** Generated name |
| 132 | * ```typescript |
| 133 | * const vault = yield* GCP.OracleDatabase.ExascaleDbStorageVault("Vault", { |
| 134 | * displayName: "vault", |
| 135 | * totalSizeGbs: 300, |
| 136 | * }); |
| 137 | * ``` |
| 138 | * |
| 139 | * @resource |
| 140 | * @category OracleDatabase |
| 141 | */ |
| 142 | export const ExascaleDbStorageVault = Resource<ExascaleDbStorageVault>( |
| 143 | "GCP.OracleDatabase.ExascaleDbStorageVault", |
| 144 | ); |
| 145 | |
| 146 | const mergedProperties = ( |
| 147 | news: ExascaleDbStorageVaultProps, |
| 148 | ): ExascaleDbStorageVaultPropertiesInput => ({ |
| 149 | ...news.properties, |
| 150 | description: news.description ?? news.properties?.description, |
| 151 | exascaleDbStorageDetails: { |
| 152 | ...news.properties?.exascaleDbStorageDetails, |
| 153 | totalSizeGbs: news.totalSizeGbs ?? news.properties?.exascaleDbStorageDetails?.totalSizeGbs, |
| 154 | }, |
| 155 | }); |
| 156 | |
| 157 | const toCreateBody = ( |
| 158 | news: ExascaleDbStorageVaultProps, |
| 159 | desiredLabels: Record<string, string>, |
| 160 | ): oracle.ExascaleDbStorageVault => { |
| 161 | const props = mergedProperties(news); |
| 162 | const properties: oracle.ExascaleDbStorageVaultProperties = {}; |
| 163 | if (props.additionalFlashCachePercent !== undefined) { |
| 164 | properties.additionalFlashCachePercent = props.additionalFlashCachePercent; |
| 165 | } |
| 166 | if (props.exascaleDbStorageDetails !== undefined) { |
| 167 | properties.exascaleDbStorageDetails = props.exascaleDbStorageDetails; |
| 168 | } |
| 169 | if (props.description !== undefined) properties.description = props.description; |
| 170 | const body: oracle.ExascaleDbStorageVault = { |
| 171 | labels: desiredLabels, |
| 172 | properties, |
| 173 | }; |
| 174 | if (news.displayName !== undefined) body.displayName = news.displayName; |
| 175 | if (news.gcpOracleZone !== undefined) body.gcpOracleZone = news.gcpOracleZone; |
| 176 | if (news.exadataInfrastructure !== undefined) { |
| 177 | body.exadataInfrastructure = news.exadataInfrastructure; |
| 178 | } |
| 179 | return body; |
| 180 | }; |
| 181 | |
| 182 | const toAttrs = (vault: oracle.ExascaleDbStorageVault, project: string) => { |
| 183 | const name = vault.name ?? ""; |
| 184 | const parsed = parseName(name, COLLECTION); |
| 185 | return { |
| 186 | name, |
| 187 | exascaleDbStorageVaultId: parsed.id, |
| 188 | project: parsed.project || project, |
| 189 | location: parsed.location, |
| 190 | displayName: vault.displayName, |
| 191 | gcpOracleZone: vault.gcpOracleZone, |
| 192 | exadataInfrastructure: vault.exadataInfrastructure, |
| 193 | labels: userLabels(vault.labels), |
| 194 | entitlementId: vault.entitlementId, |
| 195 | state: vault.properties?.state, |
| 196 | totalSizeGbs: vault.properties?.exascaleDbStorageDetails?.totalSizeGbs, |
| 197 | availableSizeGbs: vault.properties?.exascaleDbStorageDetails?.availableSizeGbs, |
| 198 | description: vault.properties?.description, |
| 199 | ocid: vault.properties?.ocid, |
| 200 | createTime: vault.createTime, |
| 201 | }; |
| 202 | }; |
| 203 | |
| 204 | const getByName = (name: string) => |
| 205 | retryQuota(oracle.getProjectsLocationsExascaleDbStorageVaults({ name })).pipe( |
| 206 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 207 | ); |
| 208 | |
| 209 | const listVaults = (project: string, region: string) => { |
| 210 | const collect = (parent: string) => |
| 211 | collectPages( |
| 212 | oracle.listProjectsLocationsExascaleDbStorageVaults.pages({ |
| 213 | parent, |
| 214 | pageSize: 1000, |
| 215 | }), |
| 216 | (page) => page.exascaleDbStorageVaults, |
| 217 | ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))); |
| 218 | return listAtLocation(project, region, collect).pipe( |
| 219 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 220 | ); |
| 221 | }; |
| 222 | |
| 223 | export const ExascaleDbStorageVaultProvider = () => |
| 224 | Provider.succeed(ExascaleDbStorageVault, { |
| 225 | stables: ["name", "exascaleDbStorageVaultId", "project", "location", "createTime", "ocid"], |
| 226 | |
| 227 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 228 | if (!isResolved(news)) return undefined; |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | const previousInfra = olds?.exadataInfrastructure ?? output?.exadataInfrastructure ?? ""; |
| 231 | const nextInfra = news.exadataInfrastructure ?? previousInfra; |
| 232 | const previousZone = olds?.gcpOracleZone ?? output?.gcpOracleZone ?? ""; |
| 233 | const nextZone = news.gcpOracleZone ?? previousZone; |
| 234 | return replaceOnIdentity({ |
| 235 | previousId: olds?.exascaleDbStorageVaultId ?? output?.exascaleDbStorageVaultId, |
| 236 | nextId: |
| 237 | news.exascaleDbStorageVaultId ?? |
| 238 | olds?.exascaleDbStorageVaultId ?? |
| 239 | output?.exascaleDbStorageVaultId, |
| 240 | previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region), |
| 241 | nextLocation: normalizeLocation( |
| 242 | news.location ?? olds?.location ?? output?.location ?? env.region, |
| 243 | ), |
| 244 | extra: nextInfra !== previousInfra || nextZone !== previousZone, |
| 245 | }); |
| 246 | }), |
| 247 | |
| 248 | read: Effect.fn(function* ({ id, olds, output }) { |
| 249 | const env = yield* GcpEnvironment.current; |
| 250 | const exascaleDbStorageVaultId = yield* toPhysicalId( |
| 251 | id, |
| 252 | olds?.exascaleDbStorageVaultId, |
| 253 | output?.exascaleDbStorageVaultId, |
| 254 | FALLBACK_ID, |
| 255 | ); |
| 256 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 257 | const name = |
| 258 | output?.name ?? resourceNameOf(env.project, location, COLLECTION, exascaleDbStorageVaultId); |
| 259 | const existing = yield* getByName(name); |
| 260 | if (existing === undefined) return undefined; |
| 261 | const attrs = toAttrs(existing, env.project); |
| 262 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 263 | }), |
| 264 | |
| 265 | list: () => |
| 266 | Effect.gen(function* () { |
| 267 | const env = yield* GcpEnvironment.current; |
| 268 | const items = yield* listVaults(env.project, env.region); |
| 269 | return items.map((item) => toAttrs(item, env.project)); |
| 270 | }), |
| 271 | |
| 272 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 273 | const env = yield* GcpEnvironment.current; |
| 274 | const exascaleDbStorageVaultId = yield* toPhysicalId( |
| 275 | id, |
| 276 | news.exascaleDbStorageVaultId, |
| 277 | output?.exascaleDbStorageVaultId, |
| 278 | FALLBACK_ID, |
| 279 | ); |
| 280 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 281 | const name = resourceNameOf(env.project, location, COLLECTION, exascaleDbStorageVaultId); |
| 282 | const desiredLabels = { |
| 283 | ...toLabels(news.labels), |
| 284 | ...(yield* createInternalLabels(id)), |
| 285 | }; |
| 286 | |
| 287 | let current = yield* getByName(output?.name ?? name); |
| 288 | |
| 289 | if (current === undefined) { |
| 290 | const created = yield* oracle |
| 291 | .createProjectsLocationsExascaleDbStorageVaults({ |
| 292 | parent: parentOf(env.project, location), |
| 293 | exascaleDbStorageVaultId, |
| 294 | body: toCreateBody(news, desiredLabels), |
| 295 | }) |
| 296 | .pipe( |
| 297 | retryQuota, |
| 298 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 299 | ); |
| 300 | if (created !== undefined) { |
| 301 | yield* waitForOperation(created); |
| 302 | } |
| 303 | current = yield* waitUntilExists(getByName(name), name); |
| 304 | } |
| 305 | |
| 306 | if (current === undefined) { |
| 307 | return yield* new OracleDatabaseNotResolved({ resourceName: name }); |
| 308 | } |
| 309 | |
| 310 | const ready = yield* waitUntilReady( |
| 311 | getByName(current.name ?? name), |
| 312 | current.name ?? name, |
| 313 | (value) => value.properties?.state, |
| 314 | ); |
| 315 | |
| 316 | return toAttrs(ready, env.project); |
| 317 | }), |
| 318 | |
| 319 | delete: Effect.fn(function* ({ output }) { |
| 320 | const operation = yield* oracle |
| 321 | .deleteProjectsLocationsExascaleDbStorageVaults({ |
| 322 | name: output.name, |
| 323 | }) |
| 324 | .pipe( |
| 325 | retryConflict, |
| 326 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 327 | ); |
| 328 | if (operation !== undefined) { |
| 329 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 330 | } |
| 331 | yield* waitUntilGone(getByName(output.name), output.name); |
| 332 | }), |
| 333 | }); |