/
operation.ts
172 lines (156 loc) · 4.87 KB
/
operation.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AbortSignalLike } from "@azure/abort-controller";
import {
OperationOptions,
operationOptionsToRequestOptionsBase,
RequestOptionsBase
} from "@azure/core-http";
import { KeyVaultClient } from "../../generated/keyVaultClient";
import {
KeyVaultClientRestoreStatusResponse,
KeyVaultClientSelectiveKeyRestoreOperationOptionalParams,
KeyVaultClientSelectiveKeyRestoreOperationResponse,
RestoreOperation
} from "../../generated/models";
import { createSpan, setParentSpan } from "../../../../keyvault-common/src";
import {
KeyVaultAdminPollOperation,
KeyVaultAdminPollOperationState
} from "../keyVaultAdminPoller";
import { RestoreResult } from "../../backupClientModels";
/**
* An interface representing the publicly available properties of the state of a restore Key Vault's poll operation.
*/
export interface SelectiveRestoreOperationState
extends KeyVaultAdminPollOperationState<RestoreResult> {}
/**
* An internal interface representing the state of a restore Key Vault's poll operation.
*/
export interface SelectiveRestorePollOperationState
extends KeyVaultAdminPollOperationState<RestoreResult> {
/**
* The name of a Key Vault Key.
*/
keyName: string;
/**
* The Folder name of the blob where the previous successful full backup was stored
*/
folderName: string;
/**
* The URI of the blob storage account.
*/
blobStorageUri: string;
/**
* The SAS token.
*/
sasToken: string;
}
/**
* The selective restore Key Vault's poll operation.
*/
export class SelectiveRestorePollOperation extends KeyVaultAdminPollOperation<
SelectiveRestorePollOperationState,
string
> {
constructor(
public state: SelectiveRestorePollOperationState,
private vaultUrl: string,
private client: KeyVaultClient,
private requestOptions: RequestOptionsBase = {}
) {
super(state, { cancelMessage: "Cancelling a selective Key Vault restore is not supported." });
}
/**
* Tracing the selectiveRestore operation
*/
private async selectiveRestore(
keyName: string,
options: KeyVaultClientSelectiveKeyRestoreOperationOptionalParams
): Promise<KeyVaultClientSelectiveKeyRestoreOperationResponse> {
const requestOptions = operationOptionsToRequestOptionsBase(options);
const span = createSpan("generatedClient.selectiveRestore", requestOptions);
try {
return await this.client.selectiveKeyRestoreOperation(
this.vaultUrl,
keyName,
setParentSpan(span, requestOptions)
);
} finally {
span.end();
}
}
/**
* Tracing the restoreStatus operation.
*/
private async restoreStatus(
jobId: string,
options: OperationOptions
): Promise<KeyVaultClientRestoreStatusResponse> {
const requestOptions = operationOptionsToRequestOptionsBase(options);
const span = createSpan("generatedClient.restoreStatus", requestOptions);
try {
return await this.client.restoreStatus(this.vaultUrl, jobId, options);
} finally {
span.end();
}
}
/**
* Reaches to the service and updates the selective restore poll operation.
*/
async update(
options: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: SelectiveRestorePollOperationState) => void;
} = {}
): Promise<SelectiveRestorePollOperation> {
const state = this.state;
const { keyName, blobStorageUri, sasToken, folderName } = state;
if (options.abortSignal) {
this.requestOptions.abortSignal = options.abortSignal;
}
if (!state.isStarted) {
const selectiveRestoreOperation = await this.selectiveRestore(keyName, {
...this.requestOptions,
restoreBlobDetails: {
folder: folderName,
sasTokenParameters: {
storageResourceUri: blobStorageUri,
token: sasToken
}
}
});
this.mapState(selectiveRestoreOperation);
} else if (!state.isCompleted) {
if (!state.jobId) {
throw new Error(`Missing "jobId" from the full restore operation.`);
}
const serviceOperation = await this.restoreStatus(state.jobId, this.requestOptions);
this.mapState(serviceOperation);
}
return this;
}
private mapState(serviceOperation: RestoreOperation): void {
const state = this.state;
const { startTime, jobId, endTime, error, status, statusDetails } = serviceOperation;
if (!startTime) {
throw new Error(`Missing "startTime" from the selective restore operation.`);
}
state.isStarted = true;
state.jobId = jobId;
state.endTime = endTime;
state.startTime = startTime;
state.status = status;
state.statusDetails = statusDetails;
if (error?.message) {
throw new Error(error?.message);
}
state.isCompleted = !!endTime;
if (state.isCompleted) {
state.result = {
startTime,
endTime
};
}
}
}