< Summary

Class:Azure.Security.KeyVault.Administration.BackupOperation
Assembly:Azure.Security.KeyVault.Administration
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Administration\src\BackupOperation.cs
Covered lines:22
Uncovered lines:24
Coverable lines:46
Total lines:153
Line coverage:47.8% (22 of 46)
Covered branches:13
Total branches:24
Branch coverage:54.1% (13 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
.ctor(...)-100%50%
.ctor(...)-0%100%
get_StartTime()-0%0%
get_EndTime()-100%100%
get_Id()-100%100%
get_Value()-60%66.67%
get_HasCompleted()-100%100%
get_HasValue()-100%50%
GetRawResponse()-100%100%
UpdateStatus(...)-0%0%
UpdateStatusAsync()-100%100%
WaitForCompletionAsync(...)-100%50%
WaitForCompletionAsync(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Administration\src\BackupOperation.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.Security.KeyVault.Administration.Models;
 9
 10namespace Azure.Security.KeyVault.Administration
 11{
 12    /// <summary>
 13    /// A long-running operation for <see cref="KeyVaultBackupClient.StartBackup(Uri, string, CancellationToken)"/> or <
 14    /// </summary>
 15    public class BackupOperation : Operation<Uri>
 16    {
 17        /// <summary>
 18        /// The number of seconds recommended by the service to delay before checking on completion status.
 19        /// </summary>
 20        private readonly int? _retryAfterSeconds;
 21        private readonly KeyVaultBackupClient _client;
 22        private Response _response;
 23        private FullBackupDetailsInternal _value;
 24        private readonly string _id;
 25
 26        /// <summary>
 27        /// Creates an instance of a BackupOperation from a previously started operation. <see cref="UpdateStatus(Cancel
 28        ///  <see cref="WaitForCompletionAsync(CancellationToken)"/>, or <see cref="WaitForCompletionAsync(TimeSpan, Can
 29        /// to re-populate the details of this operation.
 30        /// </summary>
 31        /// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
 32        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 33        /// <exception cref="ArgumentNullException"><paramref name="id"/> or <paramref name="client"/> is null.</excepti
 034        public BackupOperation(string id, KeyVaultBackupClient client)
 35        {
 036            Argument.AssertNotNull(id, nameof(id));
 037            Argument.AssertNotNull(client, nameof(client));
 38
 039            _client = client;
 040            _id = id;
 041        }
 42
 43        /// <summary>
 44        /// Initializes a new instance of a BackupOperation.
 45        /// </summary>
 46        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 47        /// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVault
 448        internal BackupOperation(KeyVaultBackupClient client, ResponseWithHeaders<ServiceFullBackupHeaders> response)
 49        {
 450            _client = client;
 451            _response = response;
 452            _retryAfterSeconds = response.Headers.RetryAfter;
 453            _id = response.Headers.JobId() ?? throw new InvalidOperationException("The response does not contain an Id")
 454        }
 55
 56        /// <summary>
 57        /// Initializes a new instance of a BackupOperation for mocking purposes.
 58        /// </summary>
 59        /// <param name="value">The <see cref="FullBackupDetailsInternal" /> that will be returned from <see cref="Value
 60        /// <param name="response">The <see cref="Response" /> that will be returned from <see cref="GetRawResponse" />.
 61        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 062        internal BackupOperation(FullBackupDetailsInternal value, Response response, KeyVaultBackupClient client)
 63        {
 064            Argument.AssertNotNull(value, nameof(value));
 065            Argument.AssertNotNull(response, nameof(response));
 066            Argument.AssertNotNull(client, nameof(client));
 67
 068            _response = response;
 069            _value = value;
 070            _id = value.JobId;
 071            _client = client;
 072        }
 73
 74        /// <summary>
 75        /// The start time of the restore operation.
 76        /// </summary>
 077        public DateTimeOffset? StartTime => _value?.StartTime;
 78
 79        /// <summary>
 80        /// The end time of the restore operation.
 81        /// </summary>
 3282        public DateTimeOffset? EndTime => _value?.EndTime;
 83
 84        /// <inheritdoc/>
 1085        public override string Id => _id;
 86
 87        /// <summary>
 88        /// Gets the <see cref="FullBackupDetailsInternal"/> of the backup operation.
 89        /// You should await <see cref="WaitForCompletionAsync(CancellationToken)"/> before attempting to use a key in t
 90        /// </summary>
 91        public override Uri Value
 92        {
 93            get
 94            {
 95#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
 496                if (!HasCompleted)
 97                {
 098                    throw new InvalidOperationException("The operation is not complete.");
 99                }
 4100                if (EndTime.HasValue && _value.Error != null)
 101                {
 0102                    throw new RequestFailedException($"{_value.Error.Message}\nInnerError: {_value.Error.InnerError}\nCo
 103                }
 104#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
 4105                return new Uri(_value.AzureStorageBlobContainerUri);
 106            }
 107        }
 108
 109        /// <inheritdoc/>
 28110        public override bool HasCompleted => EndTime.HasValue;
 111
 112        /// <inheritdoc/>
 4113        public override bool HasValue => _response != null && _value?.Error == null && HasCompleted;
 114
 115        /// <inheritdoc/>
 14116        public override Response GetRawResponse() => _response;
 117
 118        /// <inheritdoc/>
 119        public override Response UpdateStatus(CancellationToken cancellationToken = default)
 120        {
 0121            if (!HasCompleted)
 122            {
 0123                Response<FullBackupDetailsInternal> response = _client.GetBackupDetails(Id, cancellationToken);
 0124                _value = response.Value;
 0125                _response = response.GetRawResponse();
 126            }
 127
 0128            return GetRawResponse();
 129        }
 130
 131        /// <inheritdoc/>
 132        public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
 133        {
 10134            if (!HasCompleted)
 135            {
 10136                Response<FullBackupDetailsInternal> response = await _client.GetBackupDetailsAsync(Id, cancellationToken
 10137                _value = response.Value;
 10138                _response = response.GetRawResponse();
 139            }
 140
 10141            return GetRawResponse();
 10142        }
 143
 144        /// <inheritdoc/>
 145        public override ValueTask<Response<Uri>> WaitForCompletionAsync(CancellationToken cancellationToken = default) =
 4146            _retryAfterSeconds.HasValue ? this.DefaultWaitForCompletionAsync(TimeSpan.FromSeconds(_retryAfterSeconds.Val
 4147                this.DefaultWaitForCompletionAsync(cancellationToken);
 148
 149        /// <inheritdoc/>
 150        public override ValueTask<Response<Uri>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken canc
 0151                this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
 152    }
 153}