< Summary

Class:Azure.Security.KeyVault.Certificates.RecoverDeletedCertificateOperation
Assembly:Azure.Security.KeyVault.Certificates
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\RecoverDeletedCertificateOperation.cs
Covered lines:5
Uncovered lines:41
Coverable lines:46
Total lines:139
Line coverage:10.8% (5 of 46)
Covered branches:1
Total branches:18
Branch coverage:5.5% (1 of 18)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
.ctor(...)-100%50%
get_Id()-0%100%
get_Value()-0%100%
get_HasCompleted()-0%100%
get_HasValue()-0%100%
GetRawResponse()-0%100%
UpdateStatus(...)-0%0%
UpdateStatusAsync()-0%0%
WaitForCompletionAsync(...)-0%100%
WaitForCompletionAsync(...)-0%100%
CheckCompletedAsync()-0%0%
CheckCompleted(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\RecoverDeletedCertificateOperation.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Azure.Core;
 8using Azure.Core.Pipeline;
 9
 10namespace Azure.Security.KeyVault.Certificates
 11{
 12    /// <summary>
 13    /// A long-running operation for <see cref="CertificateClient.StartRecoverDeletedCertificate(string, CancellationTok
 14    /// </summary>
 15    public class RecoverDeletedCertificateOperation : Operation<KeyVaultCertificateWithPolicy>
 16    {
 017        private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(2);
 18
 19        private readonly KeyVaultPipeline _pipeline;
 20        private readonly KeyVaultCertificateWithPolicy _value;
 21        private Response _response;
 22        private bool _completed;
 23
 424        internal RecoverDeletedCertificateOperation(KeyVaultPipeline pipeline, Response<KeyVaultCertificateWithPolicy> r
 25        {
 426            _pipeline = pipeline;
 427            _value = response.Value ?? throw new InvalidOperationException("The response does not contain a value.");
 428            _response = response.GetRawResponse();
 429        }
 30
 31        /// <inheritdoc/>
 032        public override string Id => _value.Id.ToString();
 33
 34        /// <summary>
 35        /// Gets the <see cref="KeyVaultCertificate"/> of the certificate being recovered.
 36        /// You should await <see cref="WaitForCompletionAsync(CancellationToken)"/> before attempting to use a certific
 37        /// </summary>
 38        /// <remarks>
 39        /// Azure Key Vault will return a <see cref="KeyVaultCertificate"/> immediately but may take time to actually re
 40        /// </remarks>
 041        public override KeyVaultCertificateWithPolicy Value => _value;
 42
 43        /// <inheritdoc/>
 044        public override bool HasCompleted => _completed;
 45
 46        /// <inheritdoc/>
 047        public override bool HasValue => true;
 48
 49        /// <inheritdoc/>
 050        public override Response GetRawResponse() => _response;
 51
 52        /// <inheritdoc/>
 53        public override Response UpdateStatus(CancellationToken cancellationToken = default)
 54        {
 055            if (!_completed)
 56            {
 057                using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(RecoverDeletedCertificateOperation)}.{name
 058                scope.AddAttribute("secret", _value.Name);
 059                scope.Start();
 60
 61                try
 62                {
 063                    _response = _pipeline.GetResponse(RequestMethod.Get, cancellationToken, CertificateClient.Certificat
 064                    _completed = CheckCompleted(_response);
 065                }
 066                catch (Exception e)
 67                {
 068                    scope.Failed(e);
 069                    throw;
 70                }
 71            }
 72
 073            return GetRawResponse();
 74        }
 75
 76        /// <inheritdoc/>
 77        public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
 78        {
 079            if (!_completed)
 80            {
 081                using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(RecoverDeletedCertificateOperation)}.{name
 082                scope.AddAttribute("secret", _value.Name);
 083                scope.Start();
 84
 85                try
 86                {
 087                    _response = await _pipeline.GetResponseAsync(RequestMethod.Get, cancellationToken, CertificateClient
 088                    _completed = await CheckCompletedAsync(_response).ConfigureAwait(false);
 089                }
 090                catch (Exception e)
 91                {
 092                    scope.Failed(e);
 093                    throw;
 94                }
 095            }
 96
 097            return GetRawResponse();
 098        }
 99
 100        /// <inheritdoc />
 101        public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(CancellationToken canc
 0102            this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken);
 103
 104        /// <inheritdoc />
 105        public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(TimeSpan pollingInterv
 0106            this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
 107
 108        private async ValueTask<bool> CheckCompletedAsync(Response response)
 109        {
 0110            switch (response.Status)
 111            {
 112                case 200:
 113                case 403: // Access denied but proof the certificate was recovered.
 0114                    return true;
 115
 116                case 404:
 0117                    return false;
 118
 119                default:
 0120                    throw await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false);
 121            }
 0122        }
 123        private bool CheckCompleted(Response response)
 124        {
 0125            switch (response.Status)
 126            {
 127                case 200:
 128                case 403: // Access denied but proof the certificate was recovered.
 0129                    return true;
 130
 131                case 404:
 0132                    return false;
 133
 134                default:
 0135                    throw _pipeline.Diagnostics.CreateRequestFailedException(response);
 136            }
 137        }
 138    }
 139}