/
operation.ts
134 lines (122 loc) · 4.36 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 {
DeleteCertificateOptions,
DeletedCertificate,
GetDeletedCertificateOptions
} from "../../certificatesModels";
import {
KeyVaultCertificatePollOperation,
KeyVaultCertificatePollOperationState
} from "../keyVaultCertificatePoller";
import { KeyVaultClient } from "../../generated/keyVaultClient";
import { getDeletedCertificateFromDeletedCertificateBundle } from "../../transformations";
import { createTraceFunction } from "../../../../keyvault-common/src";
/**
* @internal
*/
const withTrace = createTraceFunction("Azure.KeyVault.Certificates.DeleteCertificatePoller");
/**
* The public representation of the DeleteCertificatePoller operation state.
*/
export type DeleteCertificateState = KeyVaultCertificatePollOperationState<DeletedCertificate>;
/**
* An interface representing the state of a delete certificate's poll operation
*/
export interface DeleteCertificatePollOperationState
extends KeyVaultCertificatePollOperationState<DeletedCertificate> {}
/**
* An interface representing a delete certificate's poll operation
*/
export class DeleteCertificatePollOperation extends KeyVaultCertificatePollOperation<
DeleteCertificatePollOperationState,
DeletedCertificate
> {
constructor(
public state: DeleteCertificatePollOperationState,
private vaultUrl: string,
private client: KeyVaultClient,
private operationOptions: OperationOptions = {}
) {
super(state, { cancelMessage: "Canceling the deletion of a certificate is not supported." });
}
/**
* The DELETE operation applies to any certificate stored in Azure Key Vault. DELETE cannot be applied
* to an individual version of a certificate. This operation requires the certificates/delete permission.
*/
private deleteCertificate(
certificateName: string,
options: DeleteCertificateOptions = {}
): Promise<DeletedCertificate> {
return withTrace("deleteCertificate", options, async (updatedOptions) => {
const response = await this.client.deleteCertificate(
this.vaultUrl,
certificateName,
updatedOptions
);
return getDeletedCertificateFromDeletedCertificateBundle(response);
});
}
/**
* Retrieves the deleted certificate information plus its attributes, such as retention interval, scheduled permanent deletion and the
* current deletion recovery level. This operation requires the certificates/get permission.
*/
public async getDeletedCertificate(
certificateName: string,
options: GetDeletedCertificateOptions = {}
): Promise<DeletedCertificate> {
return withTrace("getDeletedCertificate", options, async (updatedOptions) => {
const result = await this.client.getDeletedCertificate(
this.vaultUrl,
certificateName,
updatedOptions
);
return getDeletedCertificateFromDeletedCertificateBundle(result._response.parsedBody);
});
}
/**
* Reaches to the service and updates the delete certificate's poll operation.
*/
async update(
this: DeleteCertificatePollOperation,
options: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: DeleteCertificatePollOperationState) => void;
} = {}
): Promise<DeleteCertificatePollOperation> {
const state = this.state;
const { certificateName } = state;
if (options.abortSignal) {
this.operationOptions.abortSignal = options.abortSignal;
}
if (!state.isStarted) {
const deletedCertificate = await this.deleteCertificate(
certificateName,
this.operationOptions
);
state.isStarted = true;
state.result = deletedCertificate;
if (!deletedCertificate.recoveryId) {
state.isCompleted = true;
}
}
if (!state.isCompleted) {
try {
state.result = await this.getDeletedCertificate(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;
}
}