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

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