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, 04:45 UTC
| 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 | type TimeZone, |
| 24 | toPhysicalId, |
| 25 | userLabels, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | waitUntilReady, |
| 29 | } from "./internal.ts"; |
| 30 | import { waitForOperation } from "./operations.ts"; |
| 31 | |
| 32 | const COLLECTION = "dbSystems"; |
| 33 | const FALLBACK_ID = "dbsystem"; |
| 34 | |
| 35 | export type DbHome = { |
| 36 | /** Enable unified auditing. */ |
| 37 | isUnifiedAuditingEnabled?: boolean; |
| 38 | /** Database Home display name. */ |
| 39 | displayName?: string; |
| 40 | /** Oracle Database version. */ |
| 41 | dbVersion?: string; |
| 42 | /** Database resource created in the home. */ |
| 43 | database?: oracle.Database; |
| 44 | }; |
| 45 | |
| 46 | export type DbSystemOptions = { |
| 47 | /** Storage management (`ASM`, `LVM`). */ |
| 48 | storageManagement?: oracle.DbSystemOptionsStorageManagementEnum | (string & {}); |
| 49 | }; |
| 50 | |
| 51 | export type DataCollectionOptionsDbSystem = { |
| 52 | /** Enable incident logs and traces. */ |
| 53 | isIncidentLogsEnabled?: boolean; |
| 54 | /** Enable diagnostics events. */ |
| 55 | isDiagnosticsEventsEnabled?: boolean; |
| 56 | }; |
| 57 | |
| 58 | export type DbSystemPropertiesInput = { |
| 59 | /** Shape. Required on create. */ |
| 60 | shape?: string; |
| 61 | /** SSH public keys. Required on create. */ |
| 62 | sshPublicKeys?: string[]; |
| 63 | /** Initial data storage size in GB. Required on create. */ |
| 64 | initialDataStorageSizeGb?: number; |
| 65 | /** CPU cores. Required on create. */ |
| 66 | computeCount?: number; |
| 67 | /** License model. Required on create. */ |
| 68 | licenseModel?: oracle.DbSystemPropertiesLicenseModelEnum | (string & {}); |
| 69 | /** Database edition. Required on create. */ |
| 70 | databaseEdition?: oracle.DbSystemPropertiesDatabaseEditionEnum | (string & {}); |
| 71 | /** Hostname prefix. */ |
| 72 | hostnamePrefix?: string; |
| 73 | /** Time zone. */ |
| 74 | timeZone?: TimeZone; |
| 75 | /** Database Home. */ |
| 76 | dbHome?: DbHome; |
| 77 | /** Private IP. */ |
| 78 | privateIp?: string; |
| 79 | /** DbSystem options. */ |
| 80 | dbSystemOptions?: DbSystemOptions; |
| 81 | /** Reco/redo storage size in GB. */ |
| 82 | recoStorageSizeGb?: number; |
| 83 | /** Host domain. */ |
| 84 | domain?: string; |
| 85 | /** Memory in GB. */ |
| 86 | memorySizeGb?: number; |
| 87 | /** Diagnostics collection. */ |
| 88 | dataCollectionOptions?: DataCollectionOptionsDbSystem; |
| 89 | /** Node count. */ |
| 90 | nodeCount?: number; |
| 91 | /** Compute model (`ECPU`, `OCPU`). */ |
| 92 | computeModel?: oracle.DbSystemPropertiesComputeModelEnum | (string & {}); |
| 93 | }; |
| 94 | |
| 95 | export type DbSystemProps = { |
| 96 | /** |
| 97 | * DbSystem id. If omitted, a unique RFC1035 name is generated. |
| 98 | * Immutable. |
| 99 | */ |
| 100 | dbSystemId?: string; |
| 101 | /** |
| 102 | * Region. Immutable. |
| 103 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 104 | */ |
| 105 | location?: string; |
| 106 | /** |
| 107 | * Display name. Required on create. |
| 108 | */ |
| 109 | displayName?: string; |
| 110 | /** |
| 111 | * ODB Network. Immutable. |
| 112 | */ |
| 113 | odbNetwork?: string; |
| 114 | /** |
| 115 | * ODB Subnet for IP allocation. Required on create. Immutable. |
| 116 | */ |
| 117 | odbSubnet?: string; |
| 118 | /** |
| 119 | * GCP Oracle zone. Immutable. |
| 120 | */ |
| 121 | gcpOracleZone?: string; |
| 122 | /** |
| 123 | * User labels. Alchemy ownership labels are merged in automatically. |
| 124 | */ |
| 125 | labels?: Record<string, string>; |
| 126 | /** |
| 127 | * DbSystem properties. |
| 128 | */ |
| 129 | properties?: DbSystemPropertiesInput; |
| 130 | /** Shape. Convenience alias for `properties.shape`. */ |
| 131 | shape?: string; |
| 132 | /** SSH public keys. Convenience alias for `properties.sshPublicKeys`. */ |
| 133 | sshPublicKeys?: string[]; |
| 134 | /** CPU cores. Convenience alias for `properties.computeCount`. */ |
| 135 | computeCount?: number; |
| 136 | /** License model. Convenience alias for `properties.licenseModel`. */ |
| 137 | licenseModel?: oracle.DbSystemPropertiesLicenseModelEnum | (string & {}); |
| 138 | /** Database edition. Convenience alias for `properties.databaseEdition`. */ |
| 139 | databaseEdition?: oracle.DbSystemPropertiesDatabaseEditionEnum | (string & {}); |
| 140 | /** Initial data storage in GB. Convenience alias. */ |
| 141 | initialDataStorageSizeGb?: number; |
| 142 | }; |
| 143 | |
| 144 | export type DbSystem = Resource< |
| 145 | "GCP.OracleDatabase.DbSystem", |
| 146 | DbSystemProps, |
| 147 | { |
| 148 | /** Full resource name. */ |
| 149 | name: string; |
| 150 | /** DbSystem id. */ |
| 151 | dbSystemId: string; |
| 152 | /** Project id. */ |
| 153 | project: string; |
| 154 | /** Location id. */ |
| 155 | location: string; |
| 156 | /** Display name. */ |
| 157 | displayName: string | undefined; |
| 158 | /** ODB Network. */ |
| 159 | odbNetwork: string | undefined; |
| 160 | /** ODB Subnet. */ |
| 161 | odbSubnet: string | undefined; |
| 162 | /** GCP Oracle zone. */ |
| 163 | gcpOracleZone: string | undefined; |
| 164 | /** User labels (Alchemy ownership labels stripped). */ |
| 165 | labels: Record<string, string>; |
| 166 | /** Entitlement id. */ |
| 167 | entitlementId: string | undefined; |
| 168 | /** Lifecycle state. */ |
| 169 | lifecycleState: string | undefined; |
| 170 | /** Shape. */ |
| 171 | shape: string | undefined; |
| 172 | /** Hostname. */ |
| 173 | hostname: string | undefined; |
| 174 | /** OCID. */ |
| 175 | ocid: string | undefined; |
| 176 | /** RFC3339 creation timestamp. */ |
| 177 | createTime: string | undefined; |
| 178 | }, |
| 179 | never, |
| 180 | Providers |
| 181 | >; |
| 182 | |
| 183 | /** |
| 184 | * An Oracle BaseDB system on Google Cloud. |
| 185 | * |
| 186 | * Changing `dbSystemId`, `location`, `odbSubnet`, `odbNetwork`, `shape`, |
| 187 | * or `gcpOracleZone` replaces the system. There is no patch API in the |
| 188 | * distilled SDK, so labels are applied at create. |
| 189 | * |
| 190 | * ### Creating a DbSystem |
| 191 | * **Example:** Generated name |
| 192 | * ```typescript |
| 193 | * const db = yield* GCP.OracleDatabase.DbSystem("BaseDb", { |
| 194 | * displayName: "basedb", |
| 195 | * odbSubnet: subnet.name, |
| 196 | * shape: "VM.Standard.E4.Flex", |
| 197 | * sshPublicKeys: [publicKey], |
| 198 | * computeCount: 2, |
| 199 | * initialDataStorageSizeGb: 256, |
| 200 | * licenseModel: "LICENSE_INCLUDED", |
| 201 | * databaseEdition: "ENTERPRISE_EDITION", |
| 202 | * }); |
| 203 | * ``` |
| 204 | * |
| 205 | * @resource |
| 206 | * @category OracleDatabase |
| 207 | */ |
| 208 | export const DbSystem = Resource<DbSystem>("GCP.OracleDatabase.DbSystem"); |
| 209 | |
| 210 | const mergedProperties = (news: DbSystemProps): DbSystemPropertiesInput => ({ |
| 211 | ...news.properties, |
| 212 | shape: news.shape ?? news.properties?.shape, |
| 213 | sshPublicKeys: news.sshPublicKeys ?? news.properties?.sshPublicKeys, |
| 214 | computeCount: news.computeCount ?? news.properties?.computeCount, |
| 215 | licenseModel: news.licenseModel ?? news.properties?.licenseModel, |
| 216 | databaseEdition: news.databaseEdition ?? news.properties?.databaseEdition, |
| 217 | initialDataStorageSizeGb: |
| 218 | news.initialDataStorageSizeGb ?? news.properties?.initialDataStorageSizeGb, |
| 219 | }); |
| 220 | |
| 221 | const toCreateBody = ( |
| 222 | news: DbSystemProps, |
| 223 | desiredLabels: Record<string, string>, |
| 224 | ): oracle.DbSystem => { |
| 225 | const props = mergedProperties(news); |
| 226 | const properties: oracle.DbSystemProperties = {}; |
| 227 | if (props.shape !== undefined) properties.shape = props.shape; |
| 228 | if (props.sshPublicKeys !== undefined) { |
| 229 | properties.sshPublicKeys = props.sshPublicKeys; |
| 230 | } |
| 231 | if (props.initialDataStorageSizeGb !== undefined) { |
| 232 | properties.initialDataStorageSizeGb = props.initialDataStorageSizeGb; |
| 233 | } |
| 234 | if (props.computeCount !== undefined) { |
| 235 | properties.computeCount = props.computeCount; |
| 236 | } |
| 237 | if (props.licenseModel !== undefined) { |
| 238 | properties.licenseModel = props.licenseModel; |
| 239 | } |
| 240 | if (props.databaseEdition !== undefined) { |
| 241 | properties.databaseEdition = props.databaseEdition; |
| 242 | } |
| 243 | if (props.hostnamePrefix !== undefined) { |
| 244 | properties.hostnamePrefix = props.hostnamePrefix; |
| 245 | } |
| 246 | if (props.timeZone !== undefined) properties.timeZone = props.timeZone; |
| 247 | if (props.dbHome !== undefined) properties.dbHome = props.dbHome; |
| 248 | if (props.privateIp !== undefined) properties.privateIp = props.privateIp; |
| 249 | if (props.dbSystemOptions !== undefined) { |
| 250 | properties.dbSystemOptions = props.dbSystemOptions; |
| 251 | } |
| 252 | if (props.recoStorageSizeGb !== undefined) { |
| 253 | properties.recoStorageSizeGb = props.recoStorageSizeGb; |
| 254 | } |
| 255 | if (props.domain !== undefined) properties.domain = props.domain; |
| 256 | if (props.memorySizeGb !== undefined) { |
| 257 | properties.memorySizeGb = props.memorySizeGb; |
| 258 | } |
| 259 | if (props.dataCollectionOptions !== undefined) { |
| 260 | properties.dataCollectionOptions = props.dataCollectionOptions; |
| 261 | } |
| 262 | if (props.nodeCount !== undefined) properties.nodeCount = props.nodeCount; |
| 263 | if (props.computeModel !== undefined) { |
| 264 | properties.computeModel = props.computeModel; |
| 265 | } |
| 266 | const body: oracle.DbSystem = { |
| 267 | labels: desiredLabels, |
| 268 | properties, |
| 269 | }; |
| 270 | if (news.displayName !== undefined) body.displayName = news.displayName; |
| 271 | if (news.odbNetwork !== undefined) body.odbNetwork = news.odbNetwork; |
| 272 | if (news.odbSubnet !== undefined) body.odbSubnet = news.odbSubnet; |
| 273 | if (news.gcpOracleZone !== undefined) body.gcpOracleZone = news.gcpOracleZone; |
| 274 | return body; |
| 275 | }; |
| 276 | |
| 277 | const toAttrs = (system: oracle.DbSystem, project: string) => { |
| 278 | const name = system.name ?? ""; |
| 279 | const parsed = parseName(name, COLLECTION); |
| 280 | return { |
| 281 | name, |
| 282 | dbSystemId: parsed.id, |
| 283 | project: parsed.project || project, |
| 284 | location: parsed.location, |
| 285 | displayName: system.displayName, |
| 286 | odbNetwork: system.odbNetwork, |
| 287 | odbSubnet: system.odbSubnet, |
| 288 | gcpOracleZone: system.gcpOracleZone, |
| 289 | labels: userLabels(system.labels), |
| 290 | entitlementId: system.entitlementId, |
| 291 | lifecycleState: system.properties?.lifecycleState, |
| 292 | shape: system.properties?.shape, |
| 293 | hostname: system.properties?.hostname, |
| 294 | ocid: system.properties?.ocid, |
| 295 | createTime: system.createTime, |
| 296 | }; |
| 297 | }; |
| 298 | |
| 299 | const getByName = (name: string) => |
| 300 | retryQuota(oracle.getProjectsLocationsDbSystems({ name })).pipe( |
| 301 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 302 | ); |
| 303 | |
| 304 | const listSystems = (project: string, region: string) => { |
| 305 | const collect = (parent: string) => |
| 306 | collectPages( |
| 307 | oracle.listProjectsLocationsDbSystems.pages({ |
| 308 | parent, |
| 309 | pageSize: 1000, |
| 310 | }), |
| 311 | (page) => page.dbSystems, |
| 312 | ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))); |
| 313 | return listAtLocation(project, region, collect).pipe( |
| 314 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 315 | ); |
| 316 | }; |
| 317 | |
| 318 | export const DbSystemProvider = () => |
| 319 | Provider.succeed(DbSystem, { |
| 320 | stables: ["name", "dbSystemId", "project", "location", "createTime", "ocid"], |
| 321 | |
| 322 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 323 | if (!isResolved(news)) return undefined; |
| 324 | const env = yield* GcpEnvironment.current; |
| 325 | const previousOdb = olds?.odbSubnet ?? output?.odbSubnet ?? ""; |
| 326 | const nextOdb = news.odbSubnet ?? previousOdb; |
| 327 | const previousShape = olds?.shape ?? olds?.properties?.shape ?? output?.shape ?? ""; |
| 328 | const nextShape = news.shape ?? news.properties?.shape ?? previousShape; |
| 329 | return replaceOnIdentity({ |
| 330 | previousId: olds?.dbSystemId ?? output?.dbSystemId, |
| 331 | nextId: news.dbSystemId ?? olds?.dbSystemId ?? output?.dbSystemId, |
| 332 | previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region), |
| 333 | nextLocation: normalizeLocation( |
| 334 | news.location ?? olds?.location ?? output?.location ?? env.region, |
| 335 | ), |
| 336 | extra: nextOdb !== previousOdb || nextShape !== previousShape, |
| 337 | }); |
| 338 | }), |
| 339 | |
| 340 | read: Effect.fn(function* ({ id, olds, output }) { |
| 341 | const env = yield* GcpEnvironment.current; |
| 342 | const dbSystemId = yield* toPhysicalId(id, olds?.dbSystemId, output?.dbSystemId, FALLBACK_ID); |
| 343 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 344 | const name = output?.name ?? resourceNameOf(env.project, location, COLLECTION, dbSystemId); |
| 345 | const existing = yield* getByName(name); |
| 346 | if (existing === undefined) return undefined; |
| 347 | const attrs = toAttrs(existing, env.project); |
| 348 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 349 | }), |
| 350 | |
| 351 | list: () => |
| 352 | Effect.gen(function* () { |
| 353 | const env = yield* GcpEnvironment.current; |
| 354 | const items = yield* listSystems(env.project, env.region); |
| 355 | return items.map((item) => toAttrs(item, env.project)); |
| 356 | }), |
| 357 | |
| 358 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 359 | const env = yield* GcpEnvironment.current; |
| 360 | const dbSystemId = yield* toPhysicalId(id, news.dbSystemId, output?.dbSystemId, FALLBACK_ID); |
| 361 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 362 | const name = resourceNameOf(env.project, location, COLLECTION, dbSystemId); |
| 363 | const desiredLabels = { |
| 364 | ...toLabels(news.labels), |
| 365 | ...(yield* createInternalLabels(id)), |
| 366 | }; |
| 367 | |
| 368 | let current = yield* getByName(output?.name ?? name); |
| 369 | |
| 370 | if (current === undefined) { |
| 371 | const created = yield* oracle |
| 372 | .createProjectsLocationsDbSystems({ |
| 373 | parent: parentOf(env.project, location), |
| 374 | dbSystemId, |
| 375 | body: toCreateBody(news, desiredLabels), |
| 376 | }) |
| 377 | .pipe( |
| 378 | retryQuota, |
| 379 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 380 | ); |
| 381 | if (created !== undefined) { |
| 382 | yield* waitForOperation(created); |
| 383 | } |
| 384 | current = yield* waitUntilExists(getByName(name), name); |
| 385 | } |
| 386 | |
| 387 | if (current === undefined) { |
| 388 | return yield* new OracleDatabaseNotResolved({ resourceName: name }); |
| 389 | } |
| 390 | |
| 391 | const ready = yield* waitUntilReady( |
| 392 | getByName(current.name ?? name), |
| 393 | current.name ?? name, |
| 394 | (value) => value.properties?.lifecycleState, |
| 395 | ); |
| 396 | |
| 397 | return toAttrs(ready, env.project); |
| 398 | }), |
| 399 | |
| 400 | delete: Effect.fn(function* ({ output }) { |
| 401 | const operation = yield* oracle |
| 402 | .deleteProjectsLocationsDbSystems({ |
| 403 | name: output.name, |
| 404 | }) |
| 405 | .pipe( |
| 406 | retryConflict, |
| 407 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 408 | ); |
| 409 | if (operation !== undefined) { |
| 410 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 411 | } |
| 412 | yield* waitUntilGone(getByName(output.name), output.name); |
| 413 | }), |
| 414 | }); |