@databricks/sdk-environments 0.0.0-dev → 0.1.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,735 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ import {VERSION as AUTH_VERSION} from '@databricks/sdk-auth';
4
+ import {createDefault} from '@databricks/sdk-core/clientinfo';
5
+ import type {Logger} from '@databricks/sdk-core/logger';
6
+ import {NoOpLogger} from '@databricks/sdk-core/logger';
7
+ import type {CallOptions} from '@databricks/sdk-options/call';
8
+ import type {ClientOptions} from '@databricks/sdk-options/client';
9
+ import type {LroOptions} from '@databricks/sdk-options/lro';
10
+ import type {HttpClient} from '@databricks/sdk-core/http';
11
+ import {newHttpClient} from './transport';
12
+ import {
13
+ buildHttpRequest,
14
+ executeCall,
15
+ executeHttpCall,
16
+ marshalRequest,
17
+ parseResponse,
18
+ executeWait,
19
+ StillRunningError,
20
+ } from './utils';
21
+ import pkgJson from '../../package.json' with {type: 'json'};
22
+ import {z} from 'zod';
23
+ import type {
24
+ CreateWorkspaceBaseEnvironmentRequest,
25
+ DefaultWorkspaceBaseEnvironment,
26
+ DeleteWorkspaceBaseEnvironmentRequest,
27
+ GetDefaultWorkspaceBaseEnvironmentRequest,
28
+ GetOperationRequest,
29
+ GetWorkspaceBaseEnvironmentRequest,
30
+ ListWorkspaceBaseEnvironmentsRequest,
31
+ ListWorkspaceBaseEnvironmentsResponse,
32
+ Operation,
33
+ RefreshWorkspaceBaseEnvironmentRequest,
34
+ UpdateDefaultWorkspaceBaseEnvironmentRequest,
35
+ UpdateWorkspaceBaseEnvironmentRequest,
36
+ WorkspaceBaseEnvironment,
37
+ WorkspaceBaseEnvironmentOperationMetadata,
38
+ } from './model';
39
+ import {
40
+ marshalDefaultWorkspaceBaseEnvironmentSchema,
41
+ marshalRefreshWorkspaceBaseEnvironmentRequestSchema,
42
+ marshalWorkspaceBaseEnvironmentSchema,
43
+ unmarshalDefaultWorkspaceBaseEnvironmentSchema,
44
+ unmarshalListWorkspaceBaseEnvironmentsResponseSchema,
45
+ unmarshalOperationSchema,
46
+ unmarshalWorkspaceBaseEnvironmentOperationMetadataSchema,
47
+ unmarshalWorkspaceBaseEnvironmentSchema,
48
+ } from './model';
49
+
50
+ // Package identity segment for this client to be used in the User-Agent header.
51
+ const PACKAGE_SEGMENT = {
52
+ key: 'sdk-js-' + pkgJson.name.replace(/^@[^/]+\/sdk-/, ''),
53
+ value: pkgJson.version,
54
+ };
55
+
56
+ export class EnvironmentsClient {
57
+ private readonly host: string;
58
+ // Workspace ID used to route workspace-level calls on unified hosts (SPOG).
59
+ // When set, workspace-level methods send X-Databricks-Org-Id on every
60
+ // request.
61
+ private readonly workspaceId: string | undefined;
62
+ private readonly httpClient: HttpClient;
63
+ private readonly logger: Logger;
64
+ // User-Agent header value. Composed once at construction from
65
+ // createDefault() merged with this package's identity and the active
66
+ // credential's name.
67
+ private readonly userAgent: string;
68
+
69
+ constructor(options: ClientOptions) {
70
+ if (options.host === undefined) {
71
+ throw new Error('Host is required.');
72
+ }
73
+ this.host = options.host.replace(/\/$/, '');
74
+ this.workspaceId = options.workspaceId;
75
+ this.logger = options.logger ?? new NoOpLogger();
76
+ const info = createDefault()
77
+ .with(PACKAGE_SEGMENT)
78
+ .with({key: 'sdk-js-auth', value: AUTH_VERSION})
79
+ .with({key: 'auth', value: options.credentials?.name() ?? 'default'});
80
+ this.userAgent = info.toString();
81
+ this.httpClient = newHttpClient(options);
82
+ }
83
+
84
+ /**
85
+ * Creates a new WorkspaceBaseEnvironment.
86
+ * This is a long-running operation. The operation will asynchronously generate a materialized environment
87
+ * to optimize dependency resolution and is only marked as done when the materialized environment has been
88
+ * successfully generated or has failed.
89
+ */
90
+ private async createWorkspaceBaseEnvironment(
91
+ req: CreateWorkspaceBaseEnvironmentRequest,
92
+ options?: CallOptions
93
+ ): Promise<Operation> {
94
+ const url = `${this.host}/api/environments/v1/workspace-base-environments`;
95
+ const params = new URLSearchParams();
96
+ if (req.workspaceBaseEnvironmentId !== undefined) {
97
+ params.append(
98
+ 'workspace_base_environment_id',
99
+ req.workspaceBaseEnvironmentId
100
+ );
101
+ }
102
+ if (req.requestId !== undefined) {
103
+ params.append('request_id', req.requestId);
104
+ }
105
+ const query = params.toString();
106
+ const fullUrl = query !== '' ? `${url}?${query}` : url;
107
+ const body = marshalRequest(
108
+ req.workspaceBaseEnvironment,
109
+ marshalWorkspaceBaseEnvironmentSchema
110
+ );
111
+ let resp: Operation | undefined;
112
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
113
+ const headers = new Headers({'Content-Type': 'application/json'});
114
+ if (this.workspaceId !== undefined) {
115
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
116
+ }
117
+ headers.set('User-Agent', this.userAgent);
118
+ const httpReq = buildHttpRequest(
119
+ 'POST',
120
+ fullUrl,
121
+ headers,
122
+ callSignal,
123
+ body
124
+ );
125
+ const respBody = await executeHttpCall({
126
+ request: httpReq,
127
+ httpClient: this.httpClient,
128
+ logger: this.logger,
129
+ });
130
+ resp = parseResponse(respBody, unmarshalOperationSchema);
131
+ };
132
+ await executeCall(call, options);
133
+ if (resp === undefined) {
134
+ throw new Error('operation completed without a result.');
135
+ }
136
+ return resp;
137
+ }
138
+
139
+ async createWorkspaceBaseEnvironmentOperation(
140
+ req: CreateWorkspaceBaseEnvironmentRequest,
141
+ options?: CallOptions
142
+ ): Promise<CreateWorkspaceBaseEnvironmentOperation> {
143
+ const op = await this.createWorkspaceBaseEnvironment(req, options);
144
+ return new CreateWorkspaceBaseEnvironmentOperation(this, op);
145
+ }
146
+
147
+ /**
148
+ * Deletes a WorkspaceBaseEnvironment.
149
+ * Deleting a base environment may impact linked notebooks and jobs.
150
+ * This operation is irreversible and should be performed only when you are certain the environment is no longer needed.
151
+ */
152
+ async deleteWorkspaceBaseEnvironment(
153
+ req: DeleteWorkspaceBaseEnvironmentRequest,
154
+ options?: CallOptions
155
+ ): Promise<void> {
156
+ const url = `${this.host}/api/environments/v1/${req.name ?? ''}`;
157
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
158
+ const headers = new Headers();
159
+ if (this.workspaceId !== undefined) {
160
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
161
+ }
162
+ headers.set('User-Agent', this.userAgent);
163
+ const httpReq = buildHttpRequest('DELETE', url, headers, callSignal);
164
+ await executeHttpCall({
165
+ request: httpReq,
166
+ httpClient: this.httpClient,
167
+ logger: this.logger,
168
+ });
169
+ };
170
+ await executeCall(call, options);
171
+ }
172
+
173
+ /**
174
+ * Gets the default WorkspaceBaseEnvironment configuration for the workspace.
175
+ * Returns the current default base environment settings for both CPU and GPU compute.
176
+ */
177
+ async getDefaultWorkspaceBaseEnvironment(
178
+ req: GetDefaultWorkspaceBaseEnvironmentRequest,
179
+ options?: CallOptions
180
+ ): Promise<DefaultWorkspaceBaseEnvironment> {
181
+ const url = `${this.host}/api/environments/v1/${req.name ?? ''}`;
182
+ let resp: DefaultWorkspaceBaseEnvironment | undefined;
183
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
184
+ const headers = new Headers();
185
+ if (this.workspaceId !== undefined) {
186
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
187
+ }
188
+ headers.set('User-Agent', this.userAgent);
189
+ const httpReq = buildHttpRequest('GET', url, headers, callSignal);
190
+ const respBody = await executeHttpCall({
191
+ request: httpReq,
192
+ httpClient: this.httpClient,
193
+ logger: this.logger,
194
+ });
195
+ resp = parseResponse(
196
+ respBody,
197
+ unmarshalDefaultWorkspaceBaseEnvironmentSchema
198
+ );
199
+ };
200
+ await executeCall(call, options);
201
+ if (resp === undefined) {
202
+ throw new Error('operation completed without a result.');
203
+ }
204
+ return resp;
205
+ }
206
+
207
+ /**
208
+ * Gets the status of a long-running operation.
209
+ * Clients can use this method to poll the operation result.
210
+ */
211
+ async getOperation(
212
+ req: GetOperationRequest,
213
+ options?: CallOptions
214
+ ): Promise<Operation> {
215
+ const url = `${this.host}/api/environments/v1/${req.name ?? ''}`;
216
+ let resp: Operation | undefined;
217
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
218
+ const headers = new Headers();
219
+ if (this.workspaceId !== undefined) {
220
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
221
+ }
222
+ headers.set('User-Agent', this.userAgent);
223
+ const httpReq = buildHttpRequest('GET', url, headers, callSignal);
224
+ const respBody = await executeHttpCall({
225
+ request: httpReq,
226
+ httpClient: this.httpClient,
227
+ logger: this.logger,
228
+ });
229
+ resp = parseResponse(respBody, unmarshalOperationSchema);
230
+ };
231
+ await executeCall(call, options);
232
+ if (resp === undefined) {
233
+ throw new Error('operation completed without a result.');
234
+ }
235
+ return resp;
236
+ }
237
+
238
+ /** Retrieves a WorkspaceBaseEnvironment by its name. */
239
+ async getWorkspaceBaseEnvironment(
240
+ req: GetWorkspaceBaseEnvironmentRequest,
241
+ options?: CallOptions
242
+ ): Promise<WorkspaceBaseEnvironment> {
243
+ const url = `${this.host}/api/environments/v1/${req.name ?? ''}`;
244
+ let resp: WorkspaceBaseEnvironment | undefined;
245
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
246
+ const headers = new Headers();
247
+ if (this.workspaceId !== undefined) {
248
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
249
+ }
250
+ headers.set('User-Agent', this.userAgent);
251
+ const httpReq = buildHttpRequest('GET', url, headers, callSignal);
252
+ const respBody = await executeHttpCall({
253
+ request: httpReq,
254
+ httpClient: this.httpClient,
255
+ logger: this.logger,
256
+ });
257
+ resp = parseResponse(respBody, unmarshalWorkspaceBaseEnvironmentSchema);
258
+ };
259
+ await executeCall(call, options);
260
+ if (resp === undefined) {
261
+ throw new Error('operation completed without a result.');
262
+ }
263
+ return resp;
264
+ }
265
+
266
+ /**
267
+ * Lists all WorkspaceBaseEnvironments in the workspace.
268
+ *
269
+ * <Databricks> provides the following base environments:
270
+ *
271
+ * - `workspace-base-environments/databricks_ai_...`: includes popular AI and deep learning packages for serverless GPU compute.
272
+ *
273
+ *
274
+ *
275
+ * - `workspace-base-environments/databricks_ml_...`: includes popular ML packages for serverless compute.
276
+ *
277
+ *
278
+ *
279
+ *
280
+ * Databricks-provided base environments are versioned. For example, `workspace-base-environments/databricks_ml_v5` corresponds to the ML environment built on environment version 5.
281
+ */
282
+ async listWorkspaceBaseEnvironments(
283
+ req: ListWorkspaceBaseEnvironmentsRequest,
284
+ options?: CallOptions
285
+ ): Promise<ListWorkspaceBaseEnvironmentsResponse> {
286
+ const url = `${this.host}/api/environments/v1/workspace-base-environments`;
287
+ const params = new URLSearchParams();
288
+ if (req.pageSize !== undefined) {
289
+ params.append('page_size', String(req.pageSize));
290
+ }
291
+ if (req.pageToken !== undefined) {
292
+ params.append('page_token', req.pageToken);
293
+ }
294
+ const query = params.toString();
295
+ const fullUrl = query !== '' ? `${url}?${query}` : url;
296
+ let resp: ListWorkspaceBaseEnvironmentsResponse | undefined;
297
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
298
+ const headers = new Headers();
299
+ if (this.workspaceId !== undefined) {
300
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
301
+ }
302
+ headers.set('User-Agent', this.userAgent);
303
+ const httpReq = buildHttpRequest('GET', fullUrl, headers, callSignal);
304
+ const respBody = await executeHttpCall({
305
+ request: httpReq,
306
+ httpClient: this.httpClient,
307
+ logger: this.logger,
308
+ });
309
+ resp = parseResponse(
310
+ respBody,
311
+ unmarshalListWorkspaceBaseEnvironmentsResponseSchema
312
+ );
313
+ };
314
+ await executeCall(call, options);
315
+ if (resp === undefined) {
316
+ throw new Error('operation completed without a result.');
317
+ }
318
+ return resp;
319
+ }
320
+
321
+ async *listWorkspaceBaseEnvironmentsIter(
322
+ req: ListWorkspaceBaseEnvironmentsRequest,
323
+ options?: CallOptions
324
+ ): AsyncGenerator<WorkspaceBaseEnvironment> {
325
+ const pageReq: ListWorkspaceBaseEnvironmentsRequest = {...req};
326
+ for (;;) {
327
+ const resp = await this.listWorkspaceBaseEnvironments(pageReq, options);
328
+ for (const item of resp.workspaceBaseEnvironments ?? []) {
329
+ yield item;
330
+ }
331
+ if (resp.nextPageToken === undefined || resp.nextPageToken === '') {
332
+ return;
333
+ }
334
+ pageReq.pageToken = resp.nextPageToken;
335
+ }
336
+ }
337
+
338
+ /**
339
+ * Refreshes the materialized environment for a WorkspaceBaseEnvironment.
340
+ * This is a long-running operation. The operation will asynchronously regenerate the materialized environment
341
+ * and is only marked as done when the materialized environment has been successfully generated or has failed.
342
+ * The existing materialized environment remains available until it expires.
343
+ */
344
+ private async refreshWorkspaceBaseEnvironment(
345
+ req: RefreshWorkspaceBaseEnvironmentRequest,
346
+ options?: CallOptions
347
+ ): Promise<Operation> {
348
+ const url = `${this.host}/api/environments/v1/${req.name ?? ''}/refresh`;
349
+ const body = marshalRequest(
350
+ req,
351
+ marshalRefreshWorkspaceBaseEnvironmentRequestSchema
352
+ );
353
+ let resp: Operation | undefined;
354
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
355
+ const headers = new Headers({'Content-Type': 'application/json'});
356
+ if (this.workspaceId !== undefined) {
357
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
358
+ }
359
+ headers.set('User-Agent', this.userAgent);
360
+ const httpReq = buildHttpRequest('POST', url, headers, callSignal, body);
361
+ const respBody = await executeHttpCall({
362
+ request: httpReq,
363
+ httpClient: this.httpClient,
364
+ logger: this.logger,
365
+ });
366
+ resp = parseResponse(respBody, unmarshalOperationSchema);
367
+ };
368
+ await executeCall(call, options);
369
+ if (resp === undefined) {
370
+ throw new Error('operation completed without a result.');
371
+ }
372
+ return resp;
373
+ }
374
+
375
+ async refreshWorkspaceBaseEnvironmentOperation(
376
+ req: RefreshWorkspaceBaseEnvironmentRequest,
377
+ options?: CallOptions
378
+ ): Promise<RefreshWorkspaceBaseEnvironmentOperation> {
379
+ const op = await this.refreshWorkspaceBaseEnvironment(req, options);
380
+ return new RefreshWorkspaceBaseEnvironmentOperation(this, op);
381
+ }
382
+
383
+ /**
384
+ * Updates the default WorkspaceBaseEnvironment configuration for the workspace.
385
+ * Sets the specified base environments as the workspace defaults for CPU and/or GPU compute.
386
+ */
387
+ async updateDefaultWorkspaceBaseEnvironment(
388
+ req: UpdateDefaultWorkspaceBaseEnvironmentRequest,
389
+ options?: CallOptions
390
+ ): Promise<DefaultWorkspaceBaseEnvironment> {
391
+ const url = `${this.host}/api/environments/v1/${req.defaultWorkspaceBaseEnvironment?.name ?? ''}`;
392
+ const params = new URLSearchParams();
393
+ if (req.updateMask !== undefined) {
394
+ params.append('update_mask', req.updateMask.toString());
395
+ }
396
+ const query = params.toString();
397
+ const fullUrl = query !== '' ? `${url}?${query}` : url;
398
+ const body = marshalRequest(
399
+ req.defaultWorkspaceBaseEnvironment,
400
+ marshalDefaultWorkspaceBaseEnvironmentSchema
401
+ );
402
+ let resp: DefaultWorkspaceBaseEnvironment | undefined;
403
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
404
+ const headers = new Headers({'Content-Type': 'application/json'});
405
+ if (this.workspaceId !== undefined) {
406
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
407
+ }
408
+ headers.set('User-Agent', this.userAgent);
409
+ const httpReq = buildHttpRequest(
410
+ 'PATCH',
411
+ fullUrl,
412
+ headers,
413
+ callSignal,
414
+ body
415
+ );
416
+ const respBody = await executeHttpCall({
417
+ request: httpReq,
418
+ httpClient: this.httpClient,
419
+ logger: this.logger,
420
+ });
421
+ resp = parseResponse(
422
+ respBody,
423
+ unmarshalDefaultWorkspaceBaseEnvironmentSchema
424
+ );
425
+ };
426
+ await executeCall(call, options);
427
+ if (resp === undefined) {
428
+ throw new Error('operation completed without a result.');
429
+ }
430
+ return resp;
431
+ }
432
+
433
+ /**
434
+ * Updates an existing WorkspaceBaseEnvironment.
435
+ * This is a long-running operation. The operation will asynchronously regenerate the materialized environment
436
+ * and is only marked as done when the materialized environment has been successfully generated or has failed.
437
+ * The existing materialized environment remains available until it expires.
438
+ */
439
+ private async updateWorkspaceBaseEnvironment(
440
+ req: UpdateWorkspaceBaseEnvironmentRequest,
441
+ options?: CallOptions
442
+ ): Promise<Operation> {
443
+ const url = `${this.host}/api/environments/v1/${req.name ?? ''}`;
444
+ const body = marshalRequest(
445
+ req.workspaceBaseEnvironment,
446
+ marshalWorkspaceBaseEnvironmentSchema
447
+ );
448
+ let resp: Operation | undefined;
449
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
450
+ const headers = new Headers({'Content-Type': 'application/json'});
451
+ if (this.workspaceId !== undefined) {
452
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
453
+ }
454
+ headers.set('User-Agent', this.userAgent);
455
+ const httpReq = buildHttpRequest('PATCH', url, headers, callSignal, body);
456
+ const respBody = await executeHttpCall({
457
+ request: httpReq,
458
+ httpClient: this.httpClient,
459
+ logger: this.logger,
460
+ });
461
+ resp = parseResponse(respBody, unmarshalOperationSchema);
462
+ };
463
+ await executeCall(call, options);
464
+ if (resp === undefined) {
465
+ throw new Error('operation completed without a result.');
466
+ }
467
+ return resp;
468
+ }
469
+
470
+ async updateWorkspaceBaseEnvironmentOperation(
471
+ req: UpdateWorkspaceBaseEnvironmentRequest,
472
+ options?: CallOptions
473
+ ): Promise<UpdateWorkspaceBaseEnvironmentOperation> {
474
+ const op = await this.updateWorkspaceBaseEnvironment(req, options);
475
+ return new UpdateWorkspaceBaseEnvironmentOperation(this, op);
476
+ }
477
+ }
478
+
479
+ export class CreateWorkspaceBaseEnvironmentOperation {
480
+ constructor(
481
+ private readonly client: EnvironmentsClient,
482
+ private operation: Operation
483
+ ) {}
484
+
485
+ /** Returns the server-assigned name of the long-running operation. */
486
+ name(): Promise<string | undefined> {
487
+ return Promise.resolve(this.operation.name);
488
+ }
489
+
490
+ /** Returns metadata associated with the long-running operation. */
491
+ metadata(): Promise<WorkspaceBaseEnvironmentOperationMetadata | undefined> {
492
+ if (this.operation.metadata === undefined) {
493
+ return Promise.resolve(undefined);
494
+ }
495
+ return Promise.resolve(
496
+ z
497
+ .lazy(() => unmarshalWorkspaceBaseEnvironmentOperationMetadataSchema)
498
+ .parse(this.operation.metadata)
499
+ );
500
+ }
501
+
502
+ /**
503
+ * Polls the operation until it completes.
504
+ *
505
+ * Throws if the operation failed.
506
+ */
507
+ async wait(options?: LroOptions): Promise<WorkspaceBaseEnvironment> {
508
+ let result: WorkspaceBaseEnvironment | undefined;
509
+
510
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
511
+ const op = await this.client.getOperation(
512
+ {
513
+ name: this.operation.name,
514
+ },
515
+ callSignal !== undefined ? {signal: callSignal} : undefined
516
+ );
517
+ this.operation = op;
518
+ if (op.done === undefined) {
519
+ throw new Error('operation is missing the done field');
520
+ }
521
+ if (!op.done) {
522
+ throw new StillRunningError();
523
+ }
524
+
525
+ if (op.result?.$case === 'error') {
526
+ const err = op.result.error;
527
+ const msg =
528
+ err.message !== undefined && err.message !== ''
529
+ ? err.message
530
+ : 'unknown error';
531
+ const errorMsg =
532
+ err.errorCode !== undefined ? `[${err.errorCode}] ${msg}` : msg;
533
+ throw new Error(`operation failed: ${errorMsg}`, {
534
+ cause: err,
535
+ });
536
+ }
537
+
538
+ if (op.result?.$case !== 'response') {
539
+ throw new Error('operation completed without a response');
540
+ }
541
+
542
+ result = z
543
+ .lazy(() => unmarshalWorkspaceBaseEnvironmentSchema)
544
+ .parse(op.result.response);
545
+ };
546
+
547
+ await executeWait(call, options);
548
+ if (result === undefined) {
549
+ throw new Error('operation completed without a result.');
550
+ }
551
+ return result;
552
+ }
553
+
554
+ /** Checks whether the operation has completed */
555
+ async done(options?: CallOptions): Promise<boolean | undefined> {
556
+ const op = await this.client.getOperation(
557
+ {name: this.operation.name},
558
+ options
559
+ );
560
+ this.operation = op;
561
+ return op.done;
562
+ }
563
+ }
564
+
565
+ export class RefreshWorkspaceBaseEnvironmentOperation {
566
+ constructor(
567
+ private readonly client: EnvironmentsClient,
568
+ private operation: Operation
569
+ ) {}
570
+
571
+ /** Returns the server-assigned name of the long-running operation. */
572
+ name(): Promise<string | undefined> {
573
+ return Promise.resolve(this.operation.name);
574
+ }
575
+
576
+ /** Returns metadata associated with the long-running operation. */
577
+ metadata(): Promise<WorkspaceBaseEnvironmentOperationMetadata | undefined> {
578
+ if (this.operation.metadata === undefined) {
579
+ return Promise.resolve(undefined);
580
+ }
581
+ return Promise.resolve(
582
+ z
583
+ .lazy(() => unmarshalWorkspaceBaseEnvironmentOperationMetadataSchema)
584
+ .parse(this.operation.metadata)
585
+ );
586
+ }
587
+
588
+ /**
589
+ * Polls the operation until it completes.
590
+ *
591
+ * Throws if the operation failed.
592
+ */
593
+ async wait(options?: LroOptions): Promise<WorkspaceBaseEnvironment> {
594
+ let result: WorkspaceBaseEnvironment | undefined;
595
+
596
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
597
+ const op = await this.client.getOperation(
598
+ {
599
+ name: this.operation.name,
600
+ },
601
+ callSignal !== undefined ? {signal: callSignal} : undefined
602
+ );
603
+ this.operation = op;
604
+ if (op.done === undefined) {
605
+ throw new Error('operation is missing the done field');
606
+ }
607
+ if (!op.done) {
608
+ throw new StillRunningError();
609
+ }
610
+
611
+ if (op.result?.$case === 'error') {
612
+ const err = op.result.error;
613
+ const msg =
614
+ err.message !== undefined && err.message !== ''
615
+ ? err.message
616
+ : 'unknown error';
617
+ const errorMsg =
618
+ err.errorCode !== undefined ? `[${err.errorCode}] ${msg}` : msg;
619
+ throw new Error(`operation failed: ${errorMsg}`, {
620
+ cause: err,
621
+ });
622
+ }
623
+
624
+ if (op.result?.$case !== 'response') {
625
+ throw new Error('operation completed without a response');
626
+ }
627
+
628
+ result = z
629
+ .lazy(() => unmarshalWorkspaceBaseEnvironmentSchema)
630
+ .parse(op.result.response);
631
+ };
632
+
633
+ await executeWait(call, options);
634
+ if (result === undefined) {
635
+ throw new Error('operation completed without a result.');
636
+ }
637
+ return result;
638
+ }
639
+
640
+ /** Checks whether the operation has completed */
641
+ async done(options?: CallOptions): Promise<boolean | undefined> {
642
+ const op = await this.client.getOperation(
643
+ {name: this.operation.name},
644
+ options
645
+ );
646
+ this.operation = op;
647
+ return op.done;
648
+ }
649
+ }
650
+
651
+ export class UpdateWorkspaceBaseEnvironmentOperation {
652
+ constructor(
653
+ private readonly client: EnvironmentsClient,
654
+ private operation: Operation
655
+ ) {}
656
+
657
+ /** Returns the server-assigned name of the long-running operation. */
658
+ name(): Promise<string | undefined> {
659
+ return Promise.resolve(this.operation.name);
660
+ }
661
+
662
+ /** Returns metadata associated with the long-running operation. */
663
+ metadata(): Promise<WorkspaceBaseEnvironmentOperationMetadata | undefined> {
664
+ if (this.operation.metadata === undefined) {
665
+ return Promise.resolve(undefined);
666
+ }
667
+ return Promise.resolve(
668
+ z
669
+ .lazy(() => unmarshalWorkspaceBaseEnvironmentOperationMetadataSchema)
670
+ .parse(this.operation.metadata)
671
+ );
672
+ }
673
+
674
+ /**
675
+ * Polls the operation until it completes.
676
+ *
677
+ * Throws if the operation failed.
678
+ */
679
+ async wait(options?: LroOptions): Promise<WorkspaceBaseEnvironment> {
680
+ let result: WorkspaceBaseEnvironment | undefined;
681
+
682
+ const call = async (callSignal?: AbortSignal): Promise<void> => {
683
+ const op = await this.client.getOperation(
684
+ {
685
+ name: this.operation.name,
686
+ },
687
+ callSignal !== undefined ? {signal: callSignal} : undefined
688
+ );
689
+ this.operation = op;
690
+ if (op.done === undefined) {
691
+ throw new Error('operation is missing the done field');
692
+ }
693
+ if (!op.done) {
694
+ throw new StillRunningError();
695
+ }
696
+
697
+ if (op.result?.$case === 'error') {
698
+ const err = op.result.error;
699
+ const msg =
700
+ err.message !== undefined && err.message !== ''
701
+ ? err.message
702
+ : 'unknown error';
703
+ const errorMsg =
704
+ err.errorCode !== undefined ? `[${err.errorCode}] ${msg}` : msg;
705
+ throw new Error(`operation failed: ${errorMsg}`, {
706
+ cause: err,
707
+ });
708
+ }
709
+
710
+ if (op.result?.$case !== 'response') {
711
+ throw new Error('operation completed without a response');
712
+ }
713
+
714
+ result = z
715
+ .lazy(() => unmarshalWorkspaceBaseEnvironmentSchema)
716
+ .parse(op.result.response);
717
+ };
718
+
719
+ await executeWait(call, options);
720
+ if (result === undefined) {
721
+ throw new Error('operation completed without a result.');
722
+ }
723
+ return result;
724
+ }
725
+
726
+ /** Checks whether the operation has completed */
727
+ async done(options?: CallOptions): Promise<boolean | undefined> {
728
+ const op = await this.client.getOperation(
729
+ {name: this.operation.name},
730
+ options
731
+ );
732
+ this.operation = op;
733
+ return op.done;
734
+ }
735
+ }