/
operation.ts
162 lines (144 loc) · 4.6 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 {
KeyVaultClientFullRestoreOperationOptionalParams,
KeyVaultClientRestoreStatusResponse,
RestoreOperation
} from "../../generated/models";
import { KeyVaultClientFullRestoreOperationResponse } from "../../generated/models";
import {
KeyVaultAdminPollOperation,
KeyVaultAdminPollOperationState
} from "../keyVaultAdminPoller";
import { KeyVaultBeginRestoreOptions, KeyVaultRestoreResult } from "../../backupClientModels";
import { createTraceFunction } from "../../tracingHelpers";
import { OperationOptions } from "@azure/core-client";
/**
* @internal
*/
const withTrace = createTraceFunction("Azure.KeyVault.Admin.KeyVaultRestorePoller");
/**
* An interface representing the publicly available properties of the state of a restore Key Vault's poll operation.
*/
export interface KeyVaultRestoreOperationState
extends KeyVaultAdminPollOperationState<KeyVaultRestoreResult> {}
/**
* An internal interface representing the state of a restore Key Vault's poll operation.
* @internal
*/
export interface KeyVaultRestorePollOperationState
extends KeyVaultAdminPollOperationState<KeyVaultRestoreResult> {
/**
* The URI of the blob storage account.
*/
folderUri: string;
/**
* The SAS token.
*/
sasToken: string;
/**
* The Folder name of the blob where the previous successful full backup was stored
*/
folderName: string;
}
/**
* An interface representing a restore Key Vault's poll operation.
*/
export class KeyVaultRestorePollOperation extends KeyVaultAdminPollOperation<
KeyVaultRestorePollOperationState,
KeyVaultRestoreResult
> {
constructor(
public state: KeyVaultRestorePollOperationState,
private vaultUrl: string,
private client: KeyVaultClient,
private requestOptions: KeyVaultBeginRestoreOptions = {}
) {
super(state, {
cancelMessage: "Cancelling the restoration full Key Vault backup is not supported."
});
}
/**
* Tracing the fullRestore operation
*/
private fullRestore(
options: KeyVaultClientFullRestoreOperationOptionalParams
): Promise<KeyVaultClientFullRestoreOperationResponse> {
return withTrace("fullRestore", options, (updatedOptions) =>
this.client.fullRestoreOperation(this.vaultUrl, updatedOptions)
);
}
/**
* Tracing the restoreStatus operation.
*/
private async 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 restore poll operation.
*/
async update(
options: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: KeyVaultRestorePollOperationState) => void;
} = {}
): Promise<KeyVaultRestorePollOperation> {
const state = this.state;
const { folderUri, sasToken, folderName } = state;
if (options.abortSignal) {
this.requestOptions.abortSignal = options.abortSignal;
}
if (!state.isStarted) {
const serviceOperation = await this.fullRestore({
...this.requestOptions,
restoreBlobDetails: {
folderToRestore: folderName,
sasTokenParameters: {
storageResourceUri: folderUri,
token: sasToken
}
}
});
this.mapState(serviceOperation);
} 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 full restore operation. Restore did not start successfully.`
);
}
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
};
}
}
}