/
keyVaultAdminPoller.ts
131 lines (118 loc) · 3.42 KB
/
keyVaultAdminPoller.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { OperationOptions } from "@azure/core-client";
import { Poller, PollOperation, PollOperationState } from "@azure/core-lro";
import { KeyVaultClient } from "../generated/keyVaultClient";
/**
* Common parameters to a Key Vault Admin Poller.
*/
export interface KeyVaultAdminPollerOptions {
vaultUrl: string;
client: KeyVaultClient;
requestOptions?: OperationOptions;
intervalInMs?: number;
resumeFrom?: string;
}
/**
* An interface representing the state of a Key Vault Admin Poller's operation.
*/
export interface KeyVaultAdminPollOperationState<TResult> extends PollOperationState<TResult> {
/**
* Identifier for the full restore operation.
*/
jobId?: string;
/**
* Status of the restore operation.
*/
status?: string;
/**
* The status details of restore operation.
*/
statusDetails?: string;
/**
* The start time of the restore operation in UTC
*/
startTime?: Date;
/**
* The end time of the restore operation in UTC
*/
endTime?: Date;
}
/**
* Generates a version of the state with only public properties. At least those common for all of the Key Vault Admin pollers.
*/
export function cleanState<TState extends KeyVaultAdminPollOperationState<TResult>, TResult>(
state: TState
): KeyVaultAdminPollOperationState<TResult> {
return {
jobId: state.jobId,
status: state.status,
statusDetails: state.statusDetails,
startTime: state.startTime,
endTime: state.endTime,
isStarted: state.isStarted,
isCancelled: state.isCancelled,
isCompleted: state.isCompleted,
error: state.error,
result: state.result
};
}
/**
* Common properties and methods of the Key Vault Admin Pollers.
*/
export abstract class KeyVaultAdminPoller<
TState extends KeyVaultAdminPollOperationState<TResult>,
TResult
> extends Poller<TState, TResult> {
/**
* Defines how much time the poller is going to wait before making a new request to the service.
*/
public intervalInMs: number = 2000;
/**
* The method used by the poller to wait before attempting to update its operation.
*/
async delay(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, this.intervalInMs));
}
/**
* Gets the public state of the polling operation
*/
public getOperationState(): TState {
return cleanState(this.operation.state) as TState;
}
}
/**
* Optional parameters to the KeyVaultAdminPollOperation
*/
export interface KeyVaultAdminPollOperationOptions {
cancelMessage: string;
}
/**
* Common properties and methods of the Key Vault Admin Poller operations.
*/
export class KeyVaultAdminPollOperation<TState, TResult> implements PollOperation<TState, TResult> {
private cancelMessage: string;
constructor(public state: TState, options: KeyVaultAdminPollOperationOptions) {
this.cancelMessage = options.cancelMessage;
}
/**
* Meant to reach to the service and update the Poller operation.
*/
public async update(): Promise<PollOperation<TState, TResult>> {
throw new Error("Operation not supported.");
}
/**
* Meant to reach to the service and cancel the Poller operation.
*/
public async cancel(): Promise<PollOperation<TState, TResult>> {
throw new Error(this.cancelMessage);
}
/**
* Serializes the Poller operation.
*/
public toString(): string {
return JSON.stringify({
state: cleanState(this.state)
});
}
}