| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | |
|
| | 10 | | namespace Azure.Security.KeyVault.Certificates |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A long-running operation for <see cref="CertificateClient.StartDeleteCertificate(string, CancellationToken)"/> o |
| | 14 | | /// </summary> |
| | 15 | | public class DeleteCertificateOperation : Operation<DeletedCertificate> |
| | 16 | | { |
| 0 | 17 | | private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(2); |
| | 18 | |
|
| | 19 | | private readonly KeyVaultPipeline _pipeline; |
| | 20 | | private readonly DeletedCertificate _value; |
| | 21 | | private Response _response; |
| | 22 | | private bool _completed; |
| | 23 | |
|
| 8 | 24 | | internal DeleteCertificateOperation(KeyVaultPipeline pipeline, Response<DeletedCertificate> response) |
| | 25 | | { |
| 8 | 26 | | _pipeline = pipeline; |
| 8 | 27 | | _value = response.Value ?? throw new InvalidOperationException("The response does not contain a value."); |
| 8 | 28 | | _response = response.GetRawResponse(); |
| | 29 | |
|
| | 30 | | // The recoveryId is only returned if soft-delete is enabled. |
| 8 | 31 | | if (_value.RecoveryId is null) |
| | 32 | | { |
| 0 | 33 | | _completed = true; |
| | 34 | | } |
| 8 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <inheritdoc/> |
| 0 | 38 | | public override string Id => _value.Id.ToString(); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets the <see cref="DeletedCertificate"/>. |
| | 42 | | /// You should await <see cref="WaitForCompletionAsync(CancellationToken)"/> before attempting to purge or recov |
| | 43 | | /// </summary> |
| | 44 | | /// <remarks> |
| | 45 | | /// Azure Key Vault will return a <see cref="DeletedCertificate"/> immediately but may take time to actually del |
| | 46 | | /// </remarks> |
| 8 | 47 | | public override DeletedCertificate Value => _value; |
| | 48 | |
|
| | 49 | | /// <inheritdoc/> |
| 0 | 50 | | public override bool HasCompleted => _completed; |
| | 51 | |
|
| | 52 | | /// <inheritdoc/> |
| 0 | 53 | | public override bool HasValue => true; |
| | 54 | |
|
| | 55 | | /// <inheritdoc/> |
| 0 | 56 | | public override Response GetRawResponse() => _response; |
| | 57 | |
|
| | 58 | | /// <inheritdoc/> |
| | 59 | | public override Response UpdateStatus(CancellationToken cancellationToken = default) |
| | 60 | | { |
| 0 | 61 | | if (!_completed) |
| | 62 | | { |
| 0 | 63 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteCertificateOperation)}.{nameof(Updat |
| 0 | 64 | | scope.AddAttribute("secret", _value.Name); |
| 0 | 65 | | scope.Start(); |
| | 66 | |
|
| | 67 | | try |
| | 68 | | { |
| 0 | 69 | | _response = _pipeline.GetResponse(RequestMethod.Get, cancellationToken, CertificateClient.DeletedCer |
| 0 | 70 | | _completed = CheckCompleted(_response); |
| 0 | 71 | | } |
| 0 | 72 | | catch (Exception e) |
| | 73 | | { |
| 0 | 74 | | scope.Failed(e); |
| 0 | 75 | | throw; |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | return GetRawResponse(); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <inheritdoc/> |
| | 83 | | public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) |
| | 84 | | { |
| 0 | 85 | | if (!_completed) |
| | 86 | | { |
| 0 | 87 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteCertificateOperation)}.{nameof(Updat |
| 0 | 88 | | scope.AddAttribute("secret", _value.Name); |
| 0 | 89 | | scope.Start(); |
| | 90 | |
|
| | 91 | | try |
| | 92 | | { |
| 0 | 93 | | _response = await _pipeline.GetResponseAsync(RequestMethod.Get, cancellationToken, CertificateClient |
| 0 | 94 | | _completed = await CheckCompletedAsync(_response).ConfigureAwait(false); |
| 0 | 95 | | } |
| 0 | 96 | | catch (Exception e) |
| | 97 | | { |
| 0 | 98 | | scope.Failed(e); |
| 0 | 99 | | throw; |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| 0 | 103 | | return GetRawResponse(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <inheritdoc /> |
| | 107 | | public override ValueTask<Response<DeletedCertificate>> WaitForCompletionAsync(CancellationToken cancellationTok |
| 0 | 108 | | this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken); |
| | 109 | |
|
| | 110 | | /// <inheritdoc /> |
| | 111 | | public override ValueTask<Response<DeletedCertificate>> WaitForCompletionAsync(TimeSpan pollingInterval, Cancell |
| 0 | 112 | | this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken); |
| | 113 | |
|
| | 114 | | private async ValueTask<bool> CheckCompletedAsync(Response response) |
| | 115 | | { |
| 0 | 116 | | switch (response.Status) |
| | 117 | | { |
| | 118 | | case 200: |
| | 119 | | case 403: // Access denied but proof the certificate was deleted. |
| 0 | 120 | | return true; |
| | 121 | |
|
| | 122 | | case 404: |
| 0 | 123 | | return false; |
| | 124 | |
|
| | 125 | | default: |
| 0 | 126 | | throw await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false); |
| | 127 | | } |
| 0 | 128 | | } |
| | 129 | | private bool CheckCompleted(Response response) |
| | 130 | | { |
| 0 | 131 | | switch (response.Status) |
| | 132 | | { |
| | 133 | | case 200: |
| | 134 | | case 403: // Access denied but proof the certificate was deleted. |
| 0 | 135 | | return true; |
| | 136 | |
|
| | 137 | | case 404: |
| 0 | 138 | | return false; |
| | 139 | |
|
| | 140 | | default: |
| 0 | 141 | | throw _pipeline.Diagnostics.CreateRequestFailedException(response); |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |
| | 145 | | } |