/
BlobLeaseClient.ts
340 lines (325 loc) · 10.3 KB
/
BlobLeaseClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { HttpResponse, generateUuid } from "@azure/core-http";
import { CanonicalCode } from "@azure/core-tracing";
import { ModifiedAccessConditions, ContainerBreakLeaseOptionalParams } from "./generatedModels";
import { AbortSignalLike } from "@azure/abort-controller";
import { ContainerClient } from "./ContainerClient";
import { Blob, Container } from "./generated/src/operations";
import { StorageClientContext } from "./generated/src/storageClient";
import { createSpan } from "./utils/tracing";
import { CommonOptions } from "./StorageClient";
import { BlobClient } from "./BlobClient";
/**
* The details for a specific lease.
*/
export interface Lease {
/**
* The ETag contains a value that you can use to
* perform operations conditionally. If the request version is 2011-08-18 or
* newer, the ETag value will be in quotes.
*/
etag?: string;
/**
* Returns the date and time the container was
* last modified. Any operation that modifies the blob, including an update
* of the blob's metadata or properties, changes the last-modified time of
* the blob.
*/
lastModified?: Date;
/**
* Uniquely identifies a container's lease
*/
leaseId?: string;
/**
* Approximate time remaining in the lease
* period, in seconds.
*/
leaseTime?: number;
/**
* This header uniquely identifies the request
* that was made and can be used for troubleshooting the request.
*/
requestId?: string;
/**
* Indicates the version of the Blob service used
* to execute the request. This header is returned for requests made against
* version 2009-09-19 and above.
*/
version?: string;
/**
* UTC date/time value generated by the service that
* indicates the time at which the response was initiated
*/
date?: Date;
/**
* Error code if any associated with the response that returned
* the Lease information.
*/
errorCode?: string;
}
/**
* Contains the response data for operations that create, modify, or delete a lease.
*
* See {@link BlobLeaseClient}.
*/
export type LeaseOperationResponse = Lease & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: Lease;
};
};
/**
* Configures lease operations.
*
* @export
* @interface LeaseOperationOptions
*/
export interface LeaseOperationOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof LeaseOperationOptions
*/
abortSignal?: AbortSignalLike;
/**
* Conditions to meet when changing the lease.
*
* @type {ModifiedAccessConditions}
* @memberof LeaseOperationOptions
*/
conditions?: ModifiedAccessConditions;
}
/**
* A client that manages leases for a {@link ContainerClient} or a {@link BlobClient}.
*
* @export
* @class BlobLeaseClient
*/
export class BlobLeaseClient {
private _leaseId: string;
private _url: string;
private _containerOrBlobOperation: Container | Blob;
/**
* Gets the lease Id.
*
* @readonly
* @memberof BlobLeaseClient
* @type {string}
*/
public get leaseId(): string {
return this._leaseId;
}
/**
* Gets the url.
*
* @readonly
* @memberof BlobLeaseClient
* @type {string}
*/
public get url(): string {
return this._url;
}
/**
* Creates an instance of BlobLeaseClient.
* @param {(ContainerClient | BlobClient)} client The client to make the lease operation requests.
* @param {string} leaseId Initial proposed lease id.
* @memberof BlobLeaseClient
*/
constructor(client: ContainerClient | BlobClient, leaseId?: string) {
const clientContext = new StorageClientContext(
client.url,
(client as any).pipeline.toServiceClientOptions()
);
this._url = client.url;
if (client instanceof ContainerClient) {
this._containerOrBlobOperation = new Container(clientContext);
} else {
this._containerOrBlobOperation = new Blob(clientContext);
}
if (!leaseId) {
leaseId = generateUuid();
}
this._leaseId = leaseId;
}
/**
* Establishes and manages a lock on a container for delete operations, or on a blob
* for write and delete operations.
* The lock duration can be 15 to 60 seconds, or can be infinite.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-container
* and
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-blob
*
* @param {number} duration Must be between 15 to 60 seconds, or infinite (-1)
* @param {LeaseOperationOptions} [options={}] option to configure lease management operations.
* @returns {Promise<LeaseOperationResponse>} Response data for acquire lease operation.
* @memberof BlobLeaseClient
*/
public async acquireLease(
duration: number,
options: LeaseOperationOptions = {}
): Promise<LeaseOperationResponse> {
const { span, spanOptions } = createSpan(
"BlobLeaseClient-acquireLease",
options.tracingOptions
);
try {
return await this._containerOrBlobOperation.acquireLease({
abortSignal: options.abortSignal,
duration,
modifiedAccessConditions: options.conditions,
proposedLeaseId: this._leaseId,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* To change the ID of the lease.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-container
* and
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-blob
*
* @param {string} proposedLeaseId the proposed new lease Id.
* @param {LeaseOperationOptions} [options={}] option to configure lease management operations.
* @returns {Promise<LeaseOperationResponse>} Response data for change lease operation.
* @memberof BlobLeaseClient
*/
public async changeLease(
proposedLeaseId: string,
options: LeaseOperationOptions = {}
): Promise<LeaseOperationResponse> {
const { span, spanOptions } = createSpan("BlobLeaseClient-changeLease", options.tracingOptions);
try {
const response = await this._containerOrBlobOperation.changeLease(
this._leaseId,
proposedLeaseId,
{
abortSignal: options.abortSignal,
modifiedAccessConditions: options.conditions,
spanOptions
}
);
this._leaseId = proposedLeaseId;
return response;
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* To free the lease if it is no longer needed so that another client may
* immediately acquire a lease against the container or the blob.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-container
* and
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-blob
*
* @param {LeaseOperationOptions} [options={}] option to configure lease management operations.
* @returns {Promise<LeaseOperationResponse>} Response data for release lease operation.
* @memberof BlobLeaseClient
*/
public async releaseLease(options: LeaseOperationOptions = {}): Promise<LeaseOperationResponse> {
const { span, spanOptions } = createSpan(
"BlobLeaseClient-releaseLease",
options.tracingOptions
);
try {
return await this._containerOrBlobOperation.releaseLease(this._leaseId, {
abortSignal: options.abortSignal,
modifiedAccessConditions: options.conditions,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* To renew the lease.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-container
* and
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-blob
*
* @param {LeaseOperationOptions} [options={}] Optional option to configure lease management operations.
* @returns {Promise<LeaseOperationResponse>} Response data for renew lease operation.
* @memberof BlobLeaseClient
*/
public async renewLease(options: LeaseOperationOptions = {}): Promise<Lease> {
const { span, spanOptions } = createSpan("BlobLeaseClient-renewLease", options.tracingOptions);
try {
return await this._containerOrBlobOperation.renewLease(this._leaseId, {
abortSignal: options.abortSignal,
modifiedAccessConditions: options.conditions,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* To end the lease but ensure that another client cannot acquire a new lease
* until the current lease period has expired.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-container
* and
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/lease-blob
*
* @static
* @param {number} breakPeriod Break period
* @param {LeaseOperationOptions} [options={}] Optional options to configure lease management operations.
* @returns {Promise<LeaseOperationResponse>} Response data for break lease operation.
* @memberof BlobLeaseClient
*/
public async breakLease(
breakPeriod: number,
options: LeaseOperationOptions = {}
): Promise<LeaseOperationResponse> {
const { span, spanOptions } = createSpan("BlobLeaseClient-breakLease", options.tracingOptions);
try {
const operationOptions: ContainerBreakLeaseOptionalParams = {
abortSignal: options.abortSignal,
breakPeriod,
modifiedAccessConditions: options.conditions,
spanOptions
};
return await this._containerOrBlobOperation.breakLease(operationOptions);
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
}