The best way to control Claude Code, Codex and more from any computer or phone

main
ts 689 lines 25.9 KB
Raw
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, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 OracleDatabaseNotResolved,
13 collectPages,
14 type CustomerContact,
15 expandNetwork,
16 hasAlchemyLabelMap,
17 listAtLocation,
18 normalizeLocation,
19 parentOf,
20 parseName,
21 replaceOnIdentity,
22 resourceNameOf,
23 retryQuota,
24 specifiedEquals,
25 toPhysicalId,
26 userLabels,
27 waitUntilExists,
28 waitUntilGone,
29 retryConflict,
30 waitUntilReady,
31 } from "./internal.ts";
32 import { waitForOperation } from "./operations.ts";
33
34 const COLLECTION = "autonomousDatabases";
35 const FALLBACK_ID = "adb";
36 const DEFAULT_LICENSE: oracle.AutonomousDatabasePropertiesLicenseTypeEnum = "LICENSE_INCLUDED";
37 const DEFAULT_WORKLOAD: oracle.AutonomousDatabasePropertiesDbWorkloadEnum = "OLTP";
38
39 export type EncryptionKey = {
40 /** Key provider (`GOOGLE_MANAGED` or `ORACLE_MANAGED`). */
41 provider?: oracle.EncryptionKeyProviderEnum | (string & {});
42 /**
43 * KMS key
44 * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
45 */
46 kmsKeyId?: string;
47 };
48
49 export type SourceConfig = {
50 /** Primary Autonomous Database used to create a peer. */
51 autonomousDatabase?: string;
52 /** Replicate automatic backups when creating Data Guard. */
53 automaticBackupsReplicationEnabled?: boolean;
54 };
55
56 export type AutonomousDatabasePropertiesInput = {
57 /** License type. Immutable. */
58 licenseType?: oracle.AutonomousDatabasePropertiesLicenseTypeEnum | (string & {});
59 /** Workload type (`OLTP`, `DW`, `AJD`, `APEX`). Immutable. */
60 dbWorkload?: oracle.AutonomousDatabasePropertiesDbWorkloadEnum | (string & {});
61 /** Database edition. Immutable. */
62 dbEdition?: oracle.AutonomousDatabasePropertiesDbEditionEnum | (string & {});
63 /** Oracle Database version. Immutable. */
64 dbVersion?: string;
65 /** CPU cores. */
66 cpuCoreCount?: number;
67 /** Compute count (ECPU). */
68 computeCount?: number;
69 /** Data storage in GB. */
70 dataStorageSizeGb?: number;
71 /** Data storage in TB. */
72 dataStorageSizeTb?: number;
73 /** Enable compute auto scaling. */
74 isAutoScalingEnabled?: boolean;
75 /** Enable storage auto scaling. */
76 isStorageAutoScalingEnabled?: boolean;
77 /** Require mTLS. Immutable. */
78 mtlsConnectionRequired?: boolean;
79 /** Backup retention in days (1-60). Immutable. */
80 backupRetentionPeriodDays?: number;
81 /** Character set. Immutable. */
82 characterSet?: string;
83 /** National character set. Immutable. */
84 nCharacterSet?: string;
85 /** Maintenance schedule (`EARLY`, `REGULAR`). Immutable. */
86 maintenanceScheduleType?:
87 | oracle.AutonomousDatabasePropertiesMaintenanceScheduleTypeEnum
88 | (string & {});
89 /** Allowlisted IP addresses. Immutable. */
90 allowlistedIps?: string[];
91 /** Customer contacts. Immutable. */
92 customerContacts?: CustomerContact[];
93 /** Encryption key. Updating appends a history entry. */
94 encryptionKey?: EncryptionKey;
95 /** Enable in-region Data Guard. */
96 localDataGuardEnabled?: boolean;
97 /** Private endpoint label. Immutable. */
98 privateEndpointLabel?: string;
99 /** Private endpoint IP. Immutable. */
100 privateEndpointIp?: string;
101 /** Maximum Data Guard failover data-loss limit, in seconds. */
102 localAdgAutoFailoverMaxDataLossLimitDuration?: number;
103 };
104
105 export type AutonomousDatabaseProps = {
106 /**
107 * Autonomous Database id (the `{autonomous_database}` segment of
108 * `projects/{project}/locations/{location}/autonomousDatabases/{autonomous_database}`).
109 * If omitted, a unique RFC1035 name is generated from the stack, stage,
110 * and logical id. Immutable — changing it replaces the database.
111 */
112 autonomousDatabaseId?: string;
113 /**
114 * Region (`us-central1`, `us-east4`, …). Immutable — changing it
115 * replaces the database. `US-CENTRAL1` is accepted and normalized to
116 * `us-central1`.
117 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
118 */
119 location?: string;
120 /**
121 * Oracle database name (1-30 alphanumeric, unique in the project).
122 * Immutable.
123 */
124 database?: string;
125 /**
126 * Display name. Immutable.
127 */
128 displayName?: string;
129 /**
130 * VPC network (`projects/{project}/global/networks/{network}` or a
131 * bare network id). Immutable. Required unless `odbSubnet` is set.
132 */
133 network?: string;
134 /**
135 * Subnet CIDR for the database. Immutable. Required unless
136 * `odbSubnet` is set.
137 */
138 cidr?: string;
139 /**
140 * ODB Network
141 * (`projects/{project}/locations/{location}/odbNetworks/{odb_network}`).
142 * Immutable.
143 */
144 odbNetwork?: string;
145 /**
146 * ODB Subnet
147 * (`projects/{project}/locations/{location}/odbNetworks/{odb_network}/odbSubnets/{odb_subnet}`).
148 * Immutable.
149 */
150 odbSubnet?: string;
151 /**
152 * ADMIN user password. Create-only. Mutually exclusive with
153 * `adminPasswordSecretVersion`.
154 */
155 adminPassword?: string;
156 /**
157 * Secret Manager version holding the ADMIN password
158 * (`projects/{project}/secrets/{secret}/versions/{version}`).
159 * Create-only.
160 */
161 adminPasswordSecretVersion?: string;
162 /**
163 * Source Autonomous Database when creating a Data Guard peer.
164 * Immutable.
165 */
166 sourceConfig?: SourceConfig;
167 /**
168 * User labels. Alchemy ownership labels are merged in automatically.
169 */
170 labels?: Record<string, string>;
171 /**
172 * Database properties. Flattened aliases (`licenseType`,
173 * `dbWorkload`, …) are also accepted on the resource and win over
174 * this nested object when both are set.
175 */
176 properties?: AutonomousDatabasePropertiesInput;
177 /** License type. Immutable. Convenience alias for `properties.licenseType`. */
178 licenseType?: oracle.AutonomousDatabasePropertiesLicenseTypeEnum | (string & {});
179 /** Workload type. Immutable. Convenience alias for `properties.dbWorkload`. */
180 dbWorkload?: oracle.AutonomousDatabasePropertiesDbWorkloadEnum | (string & {});
181 /** CPU cores. Convenience alias for `properties.cpuCoreCount`. */
182 cpuCoreCount?: number;
183 /** Compute count. Convenience alias for `properties.computeCount`. */
184 computeCount?: number;
185 /** Data storage in GB. Convenience alias for `properties.dataStorageSizeGb`. */
186 dataStorageSizeGb?: number;
187 /** Data storage in TB. Convenience alias for `properties.dataStorageSizeTb`. */
188 dataStorageSizeTb?: number;
189 /** Compute auto scaling. Convenience alias for `properties.isAutoScalingEnabled`. */
190 isAutoScalingEnabled?: boolean;
191 /** Storage auto scaling. Convenience alias for `properties.isStorageAutoScalingEnabled`. */
192 isStorageAutoScalingEnabled?: boolean;
193 /** Oracle Database version. Immutable. */
194 dbVersion?: string;
195 /** Database edition. Immutable. */
196 dbEdition?: oracle.AutonomousDatabasePropertiesDbEditionEnum | (string & {});
197 };
198
199 export type AutonomousDatabase = Resource<
200 "GCP.OracleDatabase.AutonomousDatabase",
201 AutonomousDatabaseProps,
202 {
203 /** Full resource name. */
204 name: string;
205 /** Autonomous Database id (last path segment). */
206 autonomousDatabaseId: string;
207 /** Project id. */
208 project: string;
209 /** Location id. */
210 location: string;
211 /** Oracle database name. */
212 database: string | undefined;
213 /** Display name. */
214 displayName: string | undefined;
215 /** VPC network. */
216 network: string | undefined;
217 /** Subnet CIDR. */
218 cidr: string | undefined;
219 /** ODB Network. */
220 odbNetwork: string | undefined;
221 /** ODB Subnet. */
222 odbSubnet: string | undefined;
223 /** User labels (Alchemy ownership labels stripped). */
224 labels: Record<string, string>;
225 /** Entitlement id. */
226 entitlementId: string | undefined;
227 /** Lifecycle state. */
228 state: string | undefined;
229 /** License type. */
230 licenseType: string | undefined;
231 /** Workload type. */
232 dbWorkload: string | undefined;
233 /** Database edition. */
234 dbEdition: string | undefined;
235 /** Oracle Database version. */
236 dbVersion: string | undefined;
237 /** CPU cores. */
238 cpuCoreCount: number | undefined;
239 /** Compute count. */
240 computeCount: number | undefined;
241 /** Data storage in GB. */
242 dataStorageSizeGb: number | undefined;
243 /** Data storage in TB. */
244 dataStorageSizeTb: number | undefined;
245 /** Compute auto scaling. */
246 isAutoScalingEnabled: boolean | undefined;
247 /** Storage auto scaling. */
248 isStorageAutoScalingEnabled: boolean | undefined;
249 /** OCID. */
250 ocid: string | undefined;
251 /** Connection strings currently reported. */
252 connectionStrings: oracle.AutonomousDatabaseConnectionStrings | undefined;
253 /** RFC3339 creation timestamp. */
254 createTime: string | undefined;
255 },
256 never,
257 Providers
258 >;
259
260 /**
261 * An Oracle Autonomous Database on Google Cloud.
262 *
263 * Changing `autonomousDatabaseId`, `location`, `database`, `network`,
264 * `cidr`, `odbNetwork`, `odbSubnet`, `sourceConfig`, `licenseType`,
265 * `dbWorkload`, `dbEdition`, or `dbVersion` replaces the database.
266 * Labels, compute, storage, and auto-scaling patch in place.
267 *
268 * Provisioning typically takes tens of minutes and requires an Oracle
269 * Database@Google Cloud entitlement.
270 *
271 * ### Creating an Autonomous Database
272 * **Example:** Generated name on a VPC
273 * ```typescript
274 * const db = yield* GCP.OracleDatabase.AutonomousDatabase("AppDb", {
275 * network: "default",
276 * cidr: "10.10.0.0/24",
277 * adminPassword: "AlchemyTest1!",
278 * licenseType: "LICENSE_INCLUDED",
279 * dbWorkload: "OLTP",
280 * cpuCoreCount: 2,
281 * dataStorageSizeGb: 20,
282 * });
283 * ```
284 *
285 * **Example:** Explicit id, ODB subnet, and labels
286 * ```typescript
287 * const db = yield* GCP.OracleDatabase.AutonomousDatabase("AppDb", {
288 * autonomousDatabaseId: "app-db",
289 * odbSubnet: subnet.name,
290 * adminPassword: "AlchemyTest1!",
291 * displayName: "app-db",
292 * labels: { env: "prod" },
293 * properties: {
294 * licenseType: "LICENSE_INCLUDED",
295 * dbWorkload: "OLTP",
296 * computeCount: 2,
297 * dataStorageSizeGb: 20,
298 * },
299 * });
300 * ```
301 *
302 * @resource
303 * @category OracleDatabase
304 */
305 export const AutonomousDatabase = Resource<AutonomousDatabase>(
306 "GCP.OracleDatabase.AutonomousDatabase",
307 );
308
309 const mergedProperties = (news: AutonomousDatabaseProps): AutonomousDatabasePropertiesInput => ({
310 ...news.properties,
311 licenseType: news.licenseType ?? news.properties?.licenseType,
312 dbWorkload: news.dbWorkload ?? news.properties?.dbWorkload,
313 cpuCoreCount: news.cpuCoreCount ?? news.properties?.cpuCoreCount,
314 computeCount: news.computeCount ?? news.properties?.computeCount,
315 dataStorageSizeGb: news.dataStorageSizeGb ?? news.properties?.dataStorageSizeGb,
316 dataStorageSizeTb: news.dataStorageSizeTb ?? news.properties?.dataStorageSizeTb,
317 isAutoScalingEnabled: news.isAutoScalingEnabled ?? news.properties?.isAutoScalingEnabled,
318 isStorageAutoScalingEnabled:
319 news.isStorageAutoScalingEnabled ?? news.properties?.isStorageAutoScalingEnabled,
320 dbVersion: news.dbVersion ?? news.properties?.dbVersion,
321 dbEdition: news.dbEdition ?? news.properties?.dbEdition,
322 });
323
324 const toCreateProperties = (news: AutonomousDatabaseProps): oracle.AutonomousDatabaseProperties => {
325 const props = mergedProperties(news);
326 const body: oracle.AutonomousDatabaseProperties = {
327 licenseType: props.licenseType ?? DEFAULT_LICENSE,
328 dbWorkload: props.dbWorkload ?? DEFAULT_WORKLOAD,
329 };
330 if (props.dbEdition !== undefined) body.dbEdition = props.dbEdition;
331 if (props.dbVersion !== undefined) body.dbVersion = props.dbVersion;
332 if (props.cpuCoreCount !== undefined) body.cpuCoreCount = props.cpuCoreCount;
333 if (props.computeCount !== undefined) body.computeCount = props.computeCount;
334 if (props.dataStorageSizeGb !== undefined) {
335 body.dataStorageSizeGb = props.dataStorageSizeGb;
336 }
337 if (props.dataStorageSizeTb !== undefined) {
338 body.dataStorageSizeTb = props.dataStorageSizeTb;
339 }
340 if (props.isAutoScalingEnabled !== undefined) {
341 body.isAutoScalingEnabled = props.isAutoScalingEnabled;
342 }
343 if (props.isStorageAutoScalingEnabled !== undefined) {
344 body.isStorageAutoScalingEnabled = props.isStorageAutoScalingEnabled;
345 }
346 if (props.mtlsConnectionRequired !== undefined) {
347 body.mtlsConnectionRequired = props.mtlsConnectionRequired;
348 }
349 if (props.backupRetentionPeriodDays !== undefined) {
350 body.backupRetentionPeriodDays = props.backupRetentionPeriodDays;
351 }
352 if (props.characterSet !== undefined) body.characterSet = props.characterSet;
353 if (props.nCharacterSet !== undefined) {
354 body.nCharacterSet = props.nCharacterSet;
355 }
356 if (props.maintenanceScheduleType !== undefined) {
357 body.maintenanceScheduleType = props.maintenanceScheduleType;
358 }
359 if (props.allowlistedIps !== undefined) {
360 body.allowlistedIps = props.allowlistedIps;
361 }
362 if (props.customerContacts !== undefined) {
363 body.customerContacts = props.customerContacts;
364 }
365 if (props.encryptionKey !== undefined) {
366 body.encryptionKey = props.encryptionKey;
367 }
368 if (props.localDataGuardEnabled !== undefined) {
369 body.localDataGuardEnabled = props.localDataGuardEnabled;
370 }
371 if (props.privateEndpointLabel !== undefined) {
372 body.privateEndpointLabel = props.privateEndpointLabel;
373 }
374 if (props.privateEndpointIp !== undefined) {
375 body.privateEndpointIp = props.privateEndpointIp;
376 }
377 if (props.localAdgAutoFailoverMaxDataLossLimitDuration !== undefined) {
378 body.localAdgAutoFailoverMaxDataLossLimitDuration =
379 props.localAdgAutoFailoverMaxDataLossLimitDuration;
380 }
381 return body;
382 };
383
384 const toCreateBody = (
385 news: AutonomousDatabaseProps,
386 desiredLabels: Record<string, string>,
387 project: string,
388 ): oracle.AutonomousDatabase => {
389 const body: oracle.AutonomousDatabase = {
390 labels: desiredLabels,
391 properties: toCreateProperties(news),
392 };
393 if (news.database !== undefined) body.database = news.database;
394 if (news.displayName !== undefined) body.displayName = news.displayName;
395 const network = expandNetwork(project, news.network);
396 if (network !== undefined) body.network = network;
397 if (news.cidr !== undefined) body.cidr = news.cidr;
398 if (news.odbNetwork !== undefined) body.odbNetwork = news.odbNetwork;
399 if (news.odbSubnet !== undefined) body.odbSubnet = news.odbSubnet;
400 if (news.adminPassword !== undefined) body.adminPassword = news.adminPassword;
401 if (news.adminPasswordSecretVersion !== undefined) {
402 body.adminPasswordSecretVersion = news.adminPasswordSecretVersion;
403 }
404 if (news.sourceConfig !== undefined) body.sourceConfig = news.sourceConfig;
405 return body;
406 };
407
408 const toAttrs = (database: oracle.AutonomousDatabase, project: string) => {
409 const name = database.name ?? "";
410 const parsed = parseName(name, COLLECTION);
411 const properties = database.properties;
412 return {
413 name,
414 autonomousDatabaseId: parsed.id,
415 project: parsed.project || project,
416 location: parsed.location,
417 database: database.database,
418 displayName: database.displayName,
419 network: database.network,
420 cidr: database.cidr,
421 odbNetwork: database.odbNetwork,
422 odbSubnet: database.odbSubnet,
423 labels: userLabels(database.labels),
424 entitlementId: database.entitlementId,
425 state: properties?.state,
426 licenseType: properties?.licenseType,
427 dbWorkload: properties?.dbWorkload,
428 dbEdition: properties?.dbEdition,
429 dbVersion: properties?.dbVersion,
430 cpuCoreCount: properties?.cpuCoreCount,
431 computeCount: properties?.computeCount,
432 dataStorageSizeGb: properties?.dataStorageSizeGb,
433 dataStorageSizeTb: properties?.dataStorageSizeTb,
434 isAutoScalingEnabled: properties?.isAutoScalingEnabled,
435 isStorageAutoScalingEnabled: properties?.isStorageAutoScalingEnabled,
436 ocid: properties?.ocid,
437 connectionStrings: properties?.connectionStrings,
438 createTime: database.createTime,
439 };
440 };
441
442 const getByName = (name: string) =>
443 retryQuota(oracle.getProjectsLocationsAutonomousDatabases({ name })).pipe(
444 Effect.catchTag("NotFound", () => Effect.undefined),
445 );
446
447 const listDatabases = (project: string, region: string) => {
448 const collect = (parent: string) =>
449 collectPages(
450 oracle.listProjectsLocationsAutonomousDatabases.pages({
451 parent,
452 pageSize: 1000,
453 }),
454 (page) => page.autonomousDatabases,
455 ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels))));
456 return listAtLocation(project, region, collect).pipe(
457 Effect.catchTag("NotFound", () => Effect.succeed([])),
458 );
459 };
460
461 const identityChanged = (
462 news: AutonomousDatabaseProps,
463 olds: AutonomousDatabaseProps | undefined,
464 output: AutonomousDatabase["Attributes"] | undefined,
465 ) => {
466 const props = mergedProperties(news);
467 const previousNetwork = olds?.network ?? output?.network ?? "";
468 const nextNetwork = news.network ?? previousNetwork;
469 const previousCidr = olds?.cidr ?? output?.cidr ?? "";
470 const nextCidr = news.cidr ?? previousCidr;
471 const previousOdb = olds?.odbSubnet ?? output?.odbSubnet ?? "";
472 const nextOdb = news.odbSubnet ?? previousOdb;
473 const previousOdbNet = olds?.odbNetwork ?? output?.odbNetwork ?? "";
474 const nextOdbNet = news.odbNetwork ?? previousOdbNet;
475 const previousDatabase = olds?.database ?? output?.database ?? "";
476 const nextDatabase = news.database ?? previousDatabase;
477 const previousLicense =
478 olds?.licenseType ?? olds?.properties?.licenseType ?? output?.licenseType ?? "";
479 const nextLicense = props.licenseType ?? previousLicense;
480 const previousWorkload =
481 olds?.dbWorkload ?? olds?.properties?.dbWorkload ?? output?.dbWorkload ?? "";
482 const nextWorkload = props.dbWorkload ?? previousWorkload;
483 const previousEdition = olds?.dbEdition ?? olds?.properties?.dbEdition ?? output?.dbEdition ?? "";
484 const nextEdition = props.dbEdition ?? previousEdition;
485 const previousVersion = olds?.dbVersion ?? olds?.properties?.dbVersion ?? output?.dbVersion ?? "";
486 const nextVersion = props.dbVersion ?? previousVersion;
487 return (
488 nextNetwork !== previousNetwork ||
489 nextCidr !== previousCidr ||
490 nextOdb !== previousOdb ||
491 nextOdbNet !== previousOdbNet ||
492 nextDatabase !== previousDatabase ||
493 nextLicense !== previousLicense ||
494 nextWorkload !== previousWorkload ||
495 nextEdition !== previousEdition ||
496 nextVersion !== previousVersion
497 );
498 };
499
500 export const AutonomousDatabaseProvider = () =>
501 Provider.succeed(AutonomousDatabase, {
502 stables: ["name", "autonomousDatabaseId", "project", "location", "createTime", "ocid"],
503
504 diff: Effect.fn(function* ({ news, olds, output }) {
505 if (!isResolved(news)) return undefined;
506 const env = yield* GcpEnvironment.current;
507 return replaceOnIdentity({
508 previousId: olds?.autonomousDatabaseId ?? output?.autonomousDatabaseId,
509 nextId:
510 news.autonomousDatabaseId ?? olds?.autonomousDatabaseId ?? output?.autonomousDatabaseId,
511 previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region),
512 nextLocation: normalizeLocation(
513 news.location ?? olds?.location ?? output?.location ?? env.region,
514 ),
515 extra: identityChanged(news, olds, output),
516 });
517 }),
518
519 read: Effect.fn(function* ({ id, olds, output }) {
520 const env = yield* GcpEnvironment.current;
521 const autonomousDatabaseId = yield* toPhysicalId(
522 id,
523 olds?.autonomousDatabaseId,
524 output?.autonomousDatabaseId,
525 FALLBACK_ID,
526 );
527 const location = normalizeLocation(olds?.location ?? output?.location ?? env.region);
528 const name =
529 output?.name ?? resourceNameOf(env.project, location, COLLECTION, autonomousDatabaseId);
530 const existing = yield* getByName(name);
531 if (existing === undefined) return undefined;
532 const attrs = toAttrs(existing, env.project);
533 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
534 }),
535
536 list: () =>
537 Effect.gen(function* () {
538 const env = yield* GcpEnvironment.current;
539 const items = yield* listDatabases(env.project, env.region);
540 return items.map((item) => toAttrs(item, env.project));
541 }),
542
543 reconcile: Effect.fn(function* ({ id, news, output }) {
544 const env = yield* GcpEnvironment.current;
545 const autonomousDatabaseId = yield* toPhysicalId(
546 id,
547 news.autonomousDatabaseId,
548 output?.autonomousDatabaseId,
549 FALLBACK_ID,
550 );
551 const location = normalizeLocation(news.location ?? output?.location ?? env.region);
552 const name = resourceNameOf(env.project, location, COLLECTION, autonomousDatabaseId);
553 const desiredLabels = {
554 ...toLabels(news.labels),
555 ...(yield* createInternalLabels(id)),
556 };
557
558 let current = yield* getByName(output?.name ?? name);
559
560 if (current === undefined) {
561 const created = yield* oracle
562 .createProjectsLocationsAutonomousDatabases({
563 parent: parentOf(env.project, location),
564 autonomousDatabaseId,
565 body: toCreateBody(news, desiredLabels, env.project),
566 })
567 .pipe(
568 retryQuota,
569 Effect.catchTag("Conflict", () => Effect.undefined),
570 );
571 if (created !== undefined) {
572 yield* waitForOperation(created);
573 }
574 current = yield* waitUntilExists(getByName(name), name);
575 }
576
577 if (current === undefined) {
578 return yield* new OracleDatabaseNotResolved({ resourceName: name });
579 }
580
581 const ready = yield* waitUntilReady(
582 getByName(current.name ?? name),
583 current.name ?? name,
584 (value) => value.properties?.state,
585 );
586
587 const props = mergedProperties(news);
588 const observedLabels = tagRecord(ready.labels);
589 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
590 const labelsChanged = upsert.length > 0 || removed.length > 0;
591 const computeChanged =
592 (props.cpuCoreCount !== undefined &&
593 !specifiedEquals(props.cpuCoreCount, ready.properties?.cpuCoreCount)) ||
594 (props.computeCount !== undefined &&
595 !specifiedEquals(props.computeCount, ready.properties?.computeCount));
596 const storageChanged =
597 (props.dataStorageSizeGb !== undefined &&
598 !specifiedEquals(props.dataStorageSizeGb, ready.properties?.dataStorageSizeGb)) ||
599 (props.dataStorageSizeTb !== undefined &&
600 !specifiedEquals(props.dataStorageSizeTb, ready.properties?.dataStorageSizeTb));
601 const scalingChanged =
602 (props.isAutoScalingEnabled !== undefined &&
603 props.isAutoScalingEnabled !== ready.properties?.isAutoScalingEnabled) ||
604 (props.isStorageAutoScalingEnabled !== undefined &&
605 props.isStorageAutoScalingEnabled !== ready.properties?.isStorageAutoScalingEnabled);
606 const encryptionChanged =
607 props.encryptionKey !== undefined &&
608 !specifiedEquals(props.encryptionKey, ready.properties?.encryptionKey);
609 const dataGuardChanged =
610 props.localDataGuardEnabled !== undefined &&
611 props.localDataGuardEnabled !== ready.properties?.localDataGuardEnabled;
612 const failoverLimitChanged =
613 props.localAdgAutoFailoverMaxDataLossLimitDuration !== undefined &&
614 !specifiedEquals(
615 props.localAdgAutoFailoverMaxDataLossLimitDuration,
616 ready.properties?.localAdgAutoFailoverMaxDataLossLimitDuration,
617 );
618
619 if (
620 labelsChanged ||
621 computeChanged ||
622 storageChanged ||
623 scalingChanged ||
624 encryptionChanged ||
625 dataGuardChanged ||
626 failoverLimitChanged
627 ) {
628 const mask = [
629 labelsChanged ? "labels" : undefined,
630 computeChanged ? "properties.cpu_core_count" : undefined,
631 computeChanged ? "properties.compute_count" : undefined,
632 storageChanged ? "properties.data_storage_size_gb" : undefined,
633 storageChanged ? "properties.data_storage_size_tb" : undefined,
634 scalingChanged ? "properties.is_auto_scaling_enabled" : undefined,
635 scalingChanged ? "properties.is_storage_auto_scaling_enabled" : undefined,
636 encryptionChanged ? "properties.encryption_key" : undefined,
637 dataGuardChanged ? "properties.local_data_guard_enabled" : undefined,
638 failoverLimitChanged
639 ? "properties.local_adg_auto_failover_max_data_loss_limit_duration"
640 : undefined,
641 ].filter((field): field is string => field !== undefined);
642
643 const operation = yield* retryQuota(
644 oracle.patchProjectsLocationsAutonomousDatabases({
645 name: ready.name ?? name,
646 updateMask: mask.join(","),
647 body: {
648 name: ready.name ?? name,
649 labels: desiredLabels,
650 properties: {
651 cpuCoreCount: props.cpuCoreCount,
652 computeCount: props.computeCount,
653 dataStorageSizeGb: props.dataStorageSizeGb,
654 dataStorageSizeTb: props.dataStorageSizeTb,
655 isAutoScalingEnabled: props.isAutoScalingEnabled,
656 isStorageAutoScalingEnabled: props.isStorageAutoScalingEnabled,
657 encryptionKey: props.encryptionKey,
658 localDataGuardEnabled: props.localDataGuardEnabled,
659 localAdgAutoFailoverMaxDataLossLimitDuration:
660 props.localAdgAutoFailoverMaxDataLossLimitDuration,
661 },
662 },
663 }),
664 );
665 yield* waitForOperation(operation);
666 return toAttrs(
667 yield* waitUntilExists(getByName(ready.name ?? name), ready.name ?? name),
668 env.project,
669 );
670 }
671
672 return toAttrs(ready, env.project);
673 }),
674
675 delete: Effect.fn(function* ({ output }) {
676 const operation = yield* oracle
677 .deleteProjectsLocationsAutonomousDatabases({
678 name: output.name,
679 })
680 .pipe(
681 retryConflict,
682 Effect.catchTag("NotFound", () => Effect.undefined),
683 );
684 if (operation !== undefined) {
685 yield* waitForOperation(operation, { notFoundOk: true });
686 }
687 yield* waitUntilGone(getByName(output.name), output.name);
688 }),
689 });