/
operation.ts
159 lines (143 loc) · 4.34 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AbortSignalLike } from "@azure/abort-controller";
import { RequestOptionsBase } from "@azure/core-http";
import { KeyVaultClient } from "../../generated/keyVaultClient";
import {
FullBackupOperation,
KeyVaultClientFullBackupOptionalParams,
KeyVaultClientFullBackupResponse,
KeyVaultClientFullBackupStatusResponse
} from "../../generated/models";
import { createSpan, setParentSpan } from "../../../../keyvault-common/src";
import { BackupResult, BeginBackupOptions } from "../../backupClientModels";
import {
KeyVaultAdminPollOperation,
KeyVaultAdminPollOperationState
} from "../keyVaultAdminPoller";
/**
* An interface representing the publicly available properties of the state of a backup Key Vault's poll operation.
*/
export type BackupOperationState = KeyVaultAdminPollOperationState<BackupResult>;
/**
* An internal interface representing the state of a backup Key Vault's poll operation.
*/
export interface BackupPollOperationState extends KeyVaultAdminPollOperationState<BackupResult> {
/**
* The URI of the blob storage account.
*/
blobStorageUri: string;
/**
* The SAS token.
*/
sasToken: string;
}
/**
* The backup Key Vault's poll operation.
*/
export class BackupPollOperation extends KeyVaultAdminPollOperation<
BackupPollOperationState,
string
> {
constructor(
public state: BackupPollOperationState,
private vaultUrl: string,
private client: KeyVaultClient,
private requestOptions: RequestOptionsBase = {}
) {
super(state, { cancelMessage: "Cancelling a full Key Vault backup is not supported." });
}
/**
* Tracing the fullBackup operation
*/
private async fullBackup(
options: KeyVaultClientFullBackupOptionalParams
): Promise<KeyVaultClientFullBackupResponse> {
const span = createSpan("generatedClient.fullBackup", options);
try {
return await this.client.fullBackup(this.vaultUrl, setParentSpan(span, options));
} finally {
span.end();
}
}
/**
* Tracing the fullBackupStatus operation
*/
private async fullBackupStatus(
jobId: string,
options: BeginBackupOptions
): Promise<KeyVaultClientFullBackupStatusResponse> {
const span = createSpan("generatedClient.fullBackupStatus", options);
try {
return await this.client.fullBackupStatus(this.vaultUrl, jobId, setParentSpan(span, options));
} finally {
span.end();
}
}
/**
* Reaches to the service and updates the backup's poll operation.
*/
async update(
options: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: BackupPollOperationState) => void;
} = {}
): Promise<BackupPollOperation> {
const state = this.state;
const { blobStorageUri, sasToken } = state;
if (options.abortSignal) {
this.requestOptions.abortSignal = options.abortSignal;
}
if (!state.isStarted) {
const serviceOperation = await this.fullBackup({
...this.requestOptions,
azureStorageBlobContainerUri: {
storageResourceUri: blobStorageUri!,
token: sasToken!
}
});
this.mapState(serviceOperation);
} else if (!state.isCompleted) {
if (!state.jobId) {
throw new Error(`Missing "jobId" from the full backup operation.`);
}
const serviceOperation = await this.fullBackupStatus(state.jobId, this.requestOptions);
this.mapState(serviceOperation);
}
return this;
}
private mapState(serviceOperation: FullBackupOperation): void {
const state = this.state;
const {
startTime,
jobId,
azureStorageBlobContainerUri,
endTime,
error,
status,
statusDetails
} = serviceOperation;
if (!startTime) {
throw new Error(
`Missing "startTime" from the full backup operation. Full backup did not start successfully.`
);
}
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 && azureStorageBlobContainerUri) {
state.result = {
backupFolderUri: azureStorageBlobContainerUri,
startTime,
endTime
};
}
}
}