< Summary

Class:Azure.Security.KeyVault.Certificates.CertificateOperation
Assembly:Azure.Security.KeyVault.Certificates
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\CertificateOperation.cs
Covered lines:66
Uncovered lines:11
Coverable lines:77
Total lines:265
Line coverage:85.7% (66 of 77)
Covered branches:25
Total branches:26
Branch coverage:96.1% (25 of 26)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
.ctor(...)-100%100%
get_Properties()-100%100%
get_HasCompleted()-100%100%
get_HasValue()-100%100%
get_Id()-0%100%
get_Value()-100%100%
GetRawResponse()-100%100%
WaitForCompletionAsync(...)-0%100%
WaitForCompletionAsync(...)-100%100%
UpdateStatus(...)-94.12%90%
UpdateStatusAsync()-100%100%
Cancel(...)-0%100%
CancelAsync()-100%100%
Delete(...)-0%100%
DeleteAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\CertificateOperation.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;
 8
 9namespace Azure.Security.KeyVault.Certificates
 10{
 11    /// <summary>
 12    /// A long running poller operation which can be used to track the status of a pending key vault certificate operati
 13    /// </summary>
 14    public class CertificateOperation : Operation<KeyVaultCertificateWithPolicy>
 15    {
 16        private readonly CertificateClient _client;
 17
 18        private bool _completed;
 19        private Response _response;
 20        private KeyVaultCertificateWithPolicy _value;
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="CertificateOperation"/> class.
 24        /// You must call <see cref="UpdateStatus(CancellationToken)"/> or <see cref="UpdateStatusAsync(CancellationToke
 25        /// </summary>
 26        /// <param name="client">A <see cref="CertificateClient"/> for the Key Vault where the operation was started.</p
 27        /// <param name="name">The name of the certificate being created.</param>
 428        public CertificateOperation(CertificateClient client, string name)
 29        {
 430            Argument.AssertNotNull(client, nameof(client));
 431            Argument.AssertNotNullOrEmpty(name, nameof(name));
 32
 433            Properties = new CertificateOperationProperties(client.VaultUri, name);
 34
 435            Id = Properties.Id.ToString();
 436            _client = client;
 437        }
 38
 6039        internal CertificateOperation(Response<CertificateOperationProperties> properties, CertificateClient client)
 40        {
 6041            Properties = properties;
 42
 6043            Id = Properties.Id.ToString();
 6044            _client = client;
 6045        }
 46
 47        /// <summary>
 48        /// Gets the properties of the pending certificate operation.
 49        /// </summary>
 430850        public CertificateOperationProperties Properties { get; private set; }
 51
 52        /// <summary>
 53        /// Gets a value indicating whether the operation has reached a terminal state.
 54        /// </summary>
 72655        public override bool HasCompleted => _completed;
 56
 57        /// <summary>
 58        /// Gets a value indicating whether the Value property can be safely accessed.
 59        /// </summary>
 2460        public override bool HasValue => _value != null;
 61
 62        /// <inheritdoc />
 063        public override string Id { get; }
 64
 65        /// <inheritdoc />
 66        public override KeyVaultCertificateWithPolicy Value
 67        {
 68#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
 69            get
 70            {
 6471                if (Properties is null)
 72                {
 873                    throw new InvalidOperationException("The operation was deleted so no value is available.");
 74                }
 75
 5676                if (Properties.Status == "cancelled")
 77                {
 878                    throw new OperationCanceledException("The operation was canceled so no value is available.");
 79                }
 80
 4881                if (Properties.Error != null)
 82                {
 483                    throw new InvalidOperationException($"The certificate operation failed: {Properties.Error.Message}")
 84                }
 85
 4486                return OperationHelpers.GetValue(ref _value);
 87            }
 88#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
 89        }
 90
 91        /// <inheritdoc />
 70292        public override Response GetRawResponse() => _response;
 93
 94        /// <inheritdoc />
 95        public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(CancellationToken canc
 096            this.DefaultWaitForCompletionAsync(cancellationToken);
 97
 98        /// <inheritdoc />
 99        public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(TimeSpan pollingInterv
 24100            this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
 101
 102        /// <summary>
 103        /// Updates the status of the certificate operation.
 104        /// This operation requires the certificates/get permission.
 105        /// </summary>
 106        /// <remarks>
 107        /// This operation requires the certificates/get permission.
 108        /// </remarks>
 109        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 110        /// <returns>The raw response of the poll operation.</returns>
 111        public override Response UpdateStatus(CancellationToken cancellationToken = default)
 112        {
 272113            if (!_completed)
 114            {
 272115                Response<CertificateOperationProperties> pollResponse = _client.GetPendingCertificate(Properties.Name, c
 116
 272117                _response = pollResponse.GetRawResponse();
 118
 272119                Properties = pollResponse;
 120
 121                // Properties will be null if deleted.
 272122                if (Properties is null)
 123                {
 4124                    _completed = true;
 4125                    return _response;
 126                }
 127            }
 128
 268129            if (Properties.Status == "completed")
 130            {
 12131                Response<KeyVaultCertificateWithPolicy> getResponse = _client.GetCertificate(Properties.Name, cancellati
 132
 12133                _response = getResponse.GetRawResponse();
 134
 12135                _value = getResponse.Value;
 136
 12137                _completed = true;
 138            }
 256139            else if (Properties.Status == "cancelled")
 140            {
 4141                _completed = true;
 142            }
 252143            else if (Properties.Error != null)
 144            {
 0145                _completed = true;
 146            }
 147
 268148            return GetRawResponse();
 149        }
 150
 151        /// <summary>
 152        /// Updates the status of the certificate operation.
 153        /// </summary>
 154        /// <remarks>
 155        /// This operation requires the certificates/get permission.
 156        /// </remarks>
 157        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 158        /// <returns>The raw response of the poll operation.</returns>
 159        public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
 160        {
 402161            if (!_completed)
 162            {
 402163                Response<CertificateOperationProperties> pollResponse = await _client.GetPendingCertificateAsync(Propert
 164
 402165                _response = pollResponse.GetRawResponse();
 166
 402167                Properties = pollResponse;
 168
 169                // Properties will be null if deleted.
 402170                if (Properties is null)
 171                {
 4172                    _completed = true;
 4173                    return _response;
 174                }
 175            }
 176
 398177            if (Properties.Status == "completed")
 178            {
 16179                Response<KeyVaultCertificateWithPolicy> getResponse = await _client.GetCertificateAsync(Properties.Name,
 180
 16181                _response = getResponse.GetRawResponse();
 182
 16183                _value = getResponse.Value;
 184
 16185                _completed = true;
 186            }
 382187            else if (Properties.Status == "cancelled")
 188            {
 4189                _completed = true;
 190            }
 378191            else if (Properties.Error != null)
 192            {
 4193                _completed = true;
 194            }
 195
 398196            return GetRawResponse();
 402197        }
 198
 199        /// <summary>
 200        /// Cancels a pending <see cref="CertificateOperation"/> in the key vault. This operation requires the certifica
 201        /// </summary>
 202        /// <remarks>
 203        /// This operation requires the certificates/update permission.
 204        /// </remarks>
 205        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 206        public virtual void Cancel(CancellationToken cancellationToken = default)
 207        {
 0208            Response<CertificateOperationProperties> response = _client.CancelCertificateOperation(Properties.Name, canc
 209
 0210            _response = response.GetRawResponse();
 211
 0212            Properties = response;
 0213        }
 214
 215        /// <summary>
 216        /// Cancels a pending <see cref="CertificateOperation"/> in the key vault. This operation requires the certifica
 217        /// </summary>
 218        /// <remarks>
 219        /// This operation requires the certificates/update permission.
 220        /// </remarks>
 221        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 222        /// <returns>A <see cref="Task"/> to track the service request.</returns>
 223        public virtual async Task CancelAsync(CancellationToken cancellationToken = default)
 224        {
 4225            Response<CertificateOperationProperties> response = await _client.CancelCertificateOperationAsync(Properties
 226
 4227            _response = response.GetRawResponse();
 228
 4229            Properties = response;
 4230        }
 231
 232        /// <summary>
 233        /// Deletes a pending <see cref="CertificateOperation"/> in the key vault. This operation requires the certifica
 234        /// </summary>
 235        /// <remarks>
 236        /// This operation requires the certificates/update permission.
 237        /// </remarks>
 238        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 239        public virtual void Delete(CancellationToken cancellationToken = default)
 240        {
 0241            Response<CertificateOperationProperties> response = _client.DeleteCertificateOperation(Properties.Name, canc
 242
 0243            _response = response.GetRawResponse();
 244
 0245            Properties = response;
 0246        }
 247
 248        /// <summary>
 249        /// Deletes a pending <see cref="CertificateOperation"/> in the key vault. This operation requires the certifica
 250        /// </summary>
 251        /// <remarks>
 252        /// This operation requires the certificates/update permission.
 253        /// </remarks>
 254        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 255        /// <returns>A <see cref="Task"/> to track the service request.</returns>
 256        public virtual async Task DeleteAsync(CancellationToken cancellationToken = default)
 257        {
 4258            Response<CertificateOperationProperties> response = await _client.DeleteCertificateOperationAsync(Properties
 259
 4260            _response = response.GetRawResponse();
 261
 4262            Properties = response;
 4263        }
 264    }
 265}