/
operation.ts
162 lines (147 loc) · 4.84 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AbortSignalLike } from "@azure/abort-controller";
import { KeyVaultClient } from "../../generated/keyVaultClient";
import {
KeyVaultClientRestoreStatusResponse,
KeyVaultClientSelectiveKeyRestoreOperationOptionalParams,
KeyVaultClientSelectiveKeyRestoreOperationResponse,
RestoreOperation
} from "../../generated/models";
import {
KeyVaultAdminPollOperation,
KeyVaultAdminPollOperationState
} from "../keyVaultAdminPoller";
import {
KeyVaultBeginSelectiveKeyRestoreOptions,
KeyVaultSelectiveKeyRestoreResult
} from "../../backupClientModels";
import { OperationOptions } from "@azure/core-client";
import { createTraceFunction } from "../../tracingHelpers";
/**
* @internal
*/
const withTrace = createTraceFunction("Azure.KeyVault.Admin.KeyVaultSelectiveKeyRestorePoller");
/**
* An interface representing the publicly available properties of the state of a restore Key Vault's poll operation.
*/
export interface KeyVaultSelectiveKeyRestoreOperationState
extends KeyVaultAdminPollOperationState<KeyVaultSelectiveKeyRestoreResult> {}
/**
* An internal interface representing the state of a restore Key Vault's poll operation.
*/
export interface KeyVaultSelectiveKeyRestorePollOperationState
extends KeyVaultAdminPollOperationState<KeyVaultSelectiveKeyRestoreResult> {
/**
* 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 where the previous successful full backup was stored.
*/
folderUri: string;
/**
* The SAS token.
*/
sasToken: string;
}
/**
* The selective restore Key Vault's poll operation.
*/
export class KeyVaultSelectiveKeyRestorePollOperation extends KeyVaultAdminPollOperation<
KeyVaultSelectiveKeyRestorePollOperationState,
string
> {
constructor(
public state: KeyVaultSelectiveKeyRestorePollOperationState,
private vaultUrl: string,
private client: KeyVaultClient,
private requestOptions: KeyVaultBeginSelectiveKeyRestoreOptions = {}
) {
super(state, { cancelMessage: "Cancelling a selective Key Vault restore is not supported." });
}
/**
* Tracing the selectiveRestore operation
*/
private selectiveRestore(
keyName: string,
options: KeyVaultClientSelectiveKeyRestoreOperationOptionalParams
): Promise<KeyVaultClientSelectiveKeyRestoreOperationResponse> {
return withTrace("selectiveRestore", options, (updatedOptions) =>
this.client.selectiveKeyRestoreOperation(this.vaultUrl, keyName, updatedOptions)
);
}
/**
* Tracing the restoreStatus operation.
*/
private restoreStatus(
jobId: string,
options: OperationOptions
): Promise<KeyVaultClientRestoreStatusResponse> {
return withTrace("restoreStatus", options, (updatedOptions) =>
this.client.restoreStatus(this.vaultUrl, jobId, updatedOptions)
);
}
/**
* Reaches to the service and updates the selective restore poll operation.
*/
async update(
options: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: KeyVaultSelectiveKeyRestorePollOperationState) => void;
} = {}
): Promise<KeyVaultSelectiveKeyRestorePollOperation> {
const state = this.state;
const { keyName, folderUri, 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: folderUri,
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;
state.isCompleted = !!endTime;
if (state.isCompleted && error?.code) {
throw new Error(error?.message || statusDetails);
}
if (state.isCompleted) {
state.result = {
startTime,
endTime
};
}
}
}