< Summary

Class:Azure.Security.KeyVault.Keys.DeleteKeyOperation
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\DeleteKeyOperation.cs
Covered lines:25
Uncovered lines:23
Coverable lines:48
Total lines:145
Line coverage:52% (25 of 48)
Covered branches:9
Total branches:20
Branch coverage:45% (9 of 20)

Metrics

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

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\DeleteKeyOperation.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.Keys
 11{
 12    /// <summary>
 13    /// A long-running operation for <see cref="KeyClient.StartDeleteKey(string, CancellationToken)"/> or <see cref="Key
 14    /// </summary>
 15    public class DeleteKeyOperation : Operation<DeletedKey>
 16    {
 217        private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(2);
 18
 19        private readonly KeyVaultPipeline _pipeline;
 20        private readonly DeletedKey _value;
 21        private Response _response;
 22        private bool _completed;
 23
 4424        internal DeleteKeyOperation(KeyVaultPipeline pipeline, Response<DeletedKey> response)
 25        {
 4426            _pipeline = pipeline;
 4427            _value = response.Value ?? throw new InvalidOperationException("The response does not contain a value.");
 4428            _response = response.GetRawResponse();
 29
 30            // The recoveryId is only returned if soft-delete is enabled.
 4431            if (_value.RecoveryId is null)
 32            {
 033                _completed = true;
 34            }
 4435        }
 36
 37        /// <inheritdoc/>
 038        public override string Id => _value.Id.ToString();
 39
 40        /// <summary>
 41        /// Gets the <see cref="DeletedKey"/>.
 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="DeletedKey"/> immediately but may take time to actually delete the 
 46        /// </remarks>
 3647        public override DeletedKey Value => _value;
 48
 49        /// <inheritdoc/>
 12650        public override bool HasCompleted => _completed;
 51
 52        /// <inheritdoc/>
 053        public override bool HasValue => true;
 54
 55        /// <inheritdoc/>
 15056        public override Response GetRawResponse() => _response;
 57
 58        /// <inheritdoc/>
 59        public override Response UpdateStatus(CancellationToken cancellationToken = default)
 60        {
 061            if (!_completed)
 62            {
 063                using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteKeyOperation)}.{nameof(UpdateStatus)
 064                scope.AddAttribute("secret", _value.Name);
 065                scope.Start();
 66
 67                try
 68                {
 069                    _response = _pipeline.GetResponse(RequestMethod.Get, cancellationToken, KeyClient.DeletedKeysPath, _
 070                    _completed = CheckCompleted(_response);
 071                }
 072                catch (Exception e)
 73                {
 074                    scope.Failed(e);
 075                    throw;
 76                }
 77            }
 78
 079            return GetRawResponse();
 80        }
 81
 82        /// <inheritdoc/>
 83        public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
 84        {
 12685            if (!_completed)
 86            {
 12687                using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteKeyOperation)}.{nameof(UpdateStatus)
 12688                scope.AddAttribute("secret", _value.Name);
 12689                scope.Start();
 90
 91                try
 92                {
 12693                    _response = await _pipeline.GetResponseAsync(RequestMethod.Get, cancellationToken, KeyClient.Deleted
 12694                    _completed = await CheckCompletedAsync(_response).ConfigureAwait(false);
 12695                }
 096                catch (Exception e)
 97                {
 098                    scope.Failed(e);
 099                    throw;
 100                }
 126101            }
 102
 126103            return GetRawResponse();
 126104        }
 105
 106        /// <inheritdoc />
 107        public override ValueTask<Response<DeletedKey>> WaitForCompletionAsync(CancellationToken cancellationToken = def
 24108            this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken);
 109
 110        /// <inheritdoc />
 111        public override ValueTask<Response<DeletedKey>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationTok
 0112            this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
 113
 114        private async ValueTask<bool> CheckCompletedAsync(Response response)
 115        {
 126116            switch (response.Status)
 117            {
 118                case 200:
 119                case 403: // Access denied but proof the key was deleted.
 24120                    return true;
 121
 122                case 404:
 102123                    return false;
 124
 125                default:
 0126                    throw await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false);
 127            }
 126128        }
 129        private bool CheckCompleted(Response response)
 130        {
 0131            switch (response.Status)
 132            {
 133                case 200:
 134                case 403: // Access denied but proof the key was deleted.
 0135                    return true;
 136
 137                case 404:
 0138                    return false;
 139
 140                default:
 0141                    throw _pipeline.Diagnostics.CreateRequestFailedException(response);
 142            }
 143        }
 144    }
 145}