< Summary

Class:Azure.Security.KeyVault.Secrets.DeleteSecretOperation
Assembly:Azure.Security.KeyVault.Secrets
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Secrets\src\DeleteSecretOperation.cs
Covered lines:7
Uncovered lines:41
Coverable lines:48
Total lines:145
Line coverage:14.5% (7 of 48)
Covered branches:2
Total branches:20
Branch coverage:10% (2 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
.ctor(...)-85.71%50%
get_Id()-0%100%
get_Value()-100%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.Secrets\src\DeleteSecretOperation.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.Secrets
 11{
 12    /// <summary>
 13    /// A long-running operation for <see cref="SecretClient.StartDeleteSecret(string, CancellationToken)"/> or <see cre
 14    /// </summary>
 15    public class DeleteSecretOperation : Operation<DeletedSecret>
 16    {
 017        private static readonly TimeSpan s_defaultPollingInterval = TimeSpan.FromSeconds(2);
 18
 19        private readonly KeyVaultPipeline _pipeline;
 20        private readonly DeletedSecret _value;
 21        private Response _response;
 22        private bool _completed;
 23
 21624        internal DeleteSecretOperation(KeyVaultPipeline pipeline, Response<DeletedSecret> response)
 25        {
 21626            _pipeline = pipeline;
 21627            _value = response.Value ?? throw new InvalidOperationException("The response does not contain a value.");
 21628            _response = response.GetRawResponse();
 29
 30            // The recoveryId is only returned if soft-delete is enabled.
 21631            if (_value.RecoveryId is null)
 32            {
 033                _completed = true;
 34            }
 21635        }
 36
 37        /// <inheritdoc/>
 038        public override string Id => _value.Id.ToString();
 39
 40        /// <summary>
 41        /// Gets the <see cref="DeletedSecret"/>.
 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="DeletedSecret"/> immediately but may take time to actually delete t
 46        /// </remarks>
 1647        public override DeletedSecret Value => _value;
 48
 49        /// <inheritdoc/>
 050        public override bool HasCompleted => _completed;
 51
 52        /// <inheritdoc/>
 053        public override bool HasValue => true;
 54
 55        /// <inheritdoc/>
 056        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(DeleteSecretOperation)}.{nameof(UpdateStat
 064                scope.AddAttribute("secret", _value.Name);
 065                scope.Start();
 66
 67                try
 68                {
 069                    _response = _pipeline.GetResponse(RequestMethod.Get, cancellationToken, SecretClient.DeletedSecretsP
 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        {
 085            if (!_completed)
 86            {
 087                using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(DeleteSecretOperation)}.{nameof(UpdateStat
 088                scope.AddAttribute("secret", _value.Name);
 089                scope.Start();
 90
 91                try
 92                {
 093                    _response = await _pipeline.GetResponseAsync(RequestMethod.Get, cancellationToken, SecretClient.Dele
 094                    _completed = await CheckCompletedAsync(_response).ConfigureAwait(false);
 095                }
 096                catch (Exception e)
 97                {
 098                    scope.Failed(e);
 099                    throw;
 100                }
 0101            }
 102
 0103            return GetRawResponse();
 0104        }
 105
 106        /// <inheritdoc />
 107        public override ValueTask<Response<DeletedSecret>> WaitForCompletionAsync(CancellationToken cancellationToken = 
 0108            this.DefaultWaitForCompletionAsync(s_defaultPollingInterval, cancellationToken);
 109
 110        /// <inheritdoc />
 111        public override ValueTask<Response<DeletedSecret>> WaitForCompletionAsync(TimeSpan pollingInterval, Cancellation
 0112            this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
 113
 114        private async ValueTask<bool> CheckCompletedAsync(Response response)
 115        {
 0116            switch (response.Status)
 117            {
 118                case 200:
 119                case 403: // Access denied but proof the secret was deleted.
 0120                    return true;
 121
 122                case 404:
 0123                    return false;
 124
 125                default:
 0126                    throw await _pipeline.Diagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false);
 127            }
 0128        }
 129        private bool CheckCompleted(Response response)
 130        {
 0131            switch (response.Status)
 132            {
 133                case 200:
 134                case 403: // Access denied but proof the secret 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}