/
operation.ts
134 lines (123 loc) · 4.24 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AbortSignalLike } from "@azure/abort-controller";
import { OperationOptions } from "@azure/core-http";
import { createTraceFunction } from "../../../../keyvault-common/src";
import {
GetCertificateOptions,
KeyVaultCertificateWithPolicy,
RecoverDeletedCertificateOptions
} from "../../certificatesModels";
import { KeyVaultClient } from "../../generated/keyVaultClient";
import { getCertificateWithPolicyFromCertificateBundle } from "../../transformations";
import {
KeyVaultCertificatePollOperation,
KeyVaultCertificatePollOperationState
} from "../keyVaultCertificatePoller";
/**
* @internal
*/
const withTrace = createTraceFunction(
"Azure.KeyVault.Certificates.RecoverDeletedCertificatePoller"
);
/**
* Deprecated: Public representation of the recovery of a deleted certificate poll operation
*/
export type RecoverDeletedCertificateState = KeyVaultCertificatePollOperationState<
KeyVaultCertificateWithPolicy
>;
/**
* An interface representing the recovery of a deleted certificate's poll operation
*/
export class RecoverDeletedCertificatePollOperation extends KeyVaultCertificatePollOperation<
RecoverDeletedCertificateState,
KeyVaultCertificateWithPolicy
> {
constructor(
public state: RecoverDeletedCertificateState,
private vaultUrl: string,
private client: KeyVaultClient,
private operationOptions: OperationOptions = {}
) {
super(state, {
cancelMessage: "Canceling the recovery of a deleted certificate is not supported."
});
}
/**
* Gets the latest information available from a specific certificate, including the certificate's policy. This operation requires the certificates/get permission.
*/
private getCertificate(
certificateName: string,
options: GetCertificateOptions = {}
): Promise<KeyVaultCertificateWithPolicy> {
return withTrace("getCertificate", options, async (updatedOptions) => {
const result = await this.client.getCertificate(
this.vaultUrl,
certificateName,
"",
updatedOptions
);
return getCertificateWithPolicyFromCertificateBundle(result);
});
}
/**
* Recovers the deleted certificate in the specified vault. This operation can only be performed on a soft-delete enabled vault. This operation
* requires the certificate/recover permission.
*/
private recoverDeletedCertificate(
certificateName: string,
options: RecoverDeletedCertificateOptions = {}
): Promise<KeyVaultCertificateWithPolicy> {
return withTrace("recoverDeletedCertificate", options, async (updatedOptions) => {
const result = await this.client.recoverDeletedCertificate(
this.vaultUrl,
certificateName,
updatedOptions
);
return getCertificateWithPolicyFromCertificateBundle(result._response.parsedBody);
});
}
/**
* Reaches to the service and updates the poll operation.
*/
async update(
options: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: RecoverDeletedCertificateState) => void;
} = {}
): Promise<RecoverDeletedCertificatePollOperation> {
const state = this.state;
const { certificateName } = state;
if (options.abortSignal) {
this.operationOptions.abortSignal = options.abortSignal;
}
if (!state.isStarted) {
try {
state.result = await this.getCertificate(certificateName, this.operationOptions);
state.isCompleted = true;
} catch (e) {
// getCertificate will only work once the LRO is completed.
}
if (!state.isCompleted) {
state.result = await this.recoverDeletedCertificate(certificateName, this.operationOptions);
state.isStarted = true;
}
}
if (!state.isCompleted) {
try {
state.result = await this.getCertificate(certificateName, this.operationOptions);
state.isCompleted = true;
} catch (error) {
if (error.statusCode === 403) {
// At this point, the resource exists but the user doesn't have access to it.
state.isCompleted = true;
} else if (error.statusCode !== 404) {
state.error = error;
state.isCompleted = true;
throw error;
}
}
}
return this;
}
}