private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken = default)
        {
            if (!HasCompleted)
            {
                try
                {
                    Response <FullBackupDetailsInternal> response = async ?
                                                                    await _client.GetBackupDetailsAsync(Id, cancellationToken).ConfigureAwait(false)
                        : _client.GetBackupDetails(Id, cancellationToken);

                    _value    = response.Value;
                    _response = response.GetRawResponse();
                }
                catch (RequestFailedException ex)
                {
                    _requestFailedException = ex;
                    throw;
                }
                catch (Exception ex)
                {
                    _requestFailedException = new RequestFailedException("Unexpected failure", ex);
                    throw _requestFailedException;
                }
                if (_value != null && _value.EndTime.HasValue && _value.Error != null)
                {
                    _requestFailedException = new RequestFailedException($"{_value.Error.Message}\nInnerError: {_value.Error.InnerError}\nCode: {_value.Error.Code}");
                    throw _requestFailedException;
                }
            }

            return(GetRawResponse());
        }
Exemplo n.º 2
0
        public async Task <Response <FullBackupDetailsInternal> > FullBackupStatusAsync(string vaultBaseUrl, string jobId, CancellationToken cancellationToken = default)
        {
            if (vaultBaseUrl == null)
            {
                throw new ArgumentNullException(nameof(vaultBaseUrl));
            }
            if (jobId == null)
            {
                throw new ArgumentNullException(nameof(jobId));
            }

            using var message = CreateFullBackupStatusRequest(vaultBaseUrl, jobId);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                FullBackupDetailsInternal value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                if (document.RootElement.ValueKind == JsonValueKind.Null)
                {
                    value = null;
                }
                else
                {
                    value = FullBackupDetailsInternal.DeserializeFullBackupDetailsInternal(document.RootElement);
                }
                return(Response.FromValue(value, message.Response));
            }
 /// <summary>
 /// Initializes a new instance of a BackupOperation.
 /// </summary>
 /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 /// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVaultBackupClient.StartBackup(Uri, string, CancellationToken)"/> or <see cref="KeyVaultBackupClient.StartBackupAsync(Uri, string, CancellationToken)"/>.</param>
 internal BackupOperation(KeyVaultBackupClient client, ResponseWithHeaders <FullBackupDetailsInternal, ServiceFullBackupHeaders> response)
 {
     _client            = client;
     _response          = response;
     _retryAfterSeconds = response.Headers.RetryAfter;
     _value             = response.Value ?? throw new InvalidOperationException("The response does not contain a value.");
 }
        public async Task <ResponseWithHeaders <FullBackupDetailsInternal, ServiceFullBackupHeaders> > FullBackupAsync(string vaultBaseUrl, SASTokenParameter azureStorageBlobContainerUri = null, CancellationToken cancellationToken = default)
        {
            if (vaultBaseUrl == null)
            {
                throw new ArgumentNullException(nameof(vaultBaseUrl));
            }

            using var message = CreateFullBackupRequest(vaultBaseUrl, azureStorageBlobContainerUri);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            var headers = new ServiceFullBackupHeaders(message.Response);

            switch (message.Response.Status)
            {
            case 202:
            {
                FullBackupDetailsInternal value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                if (document.RootElement.ValueKind == JsonValueKind.Null)
                {
                    value = null;
                }
                else
                {
                    value = FullBackupDetailsInternal.DeserializeFullBackupDetailsInternal(document.RootElement);
                }
                return(ResponseWithHeaders.FromValue(value, headers, message.Response));
            }
        /// <summary>
        /// Creates an instance of a BackupOperation from a previously started operation. <see cref="UpdateStatus(CancellationToken)"/>, <see cref="UpdateStatusAsync(CancellationToken)"/>,
        ///  <see cref="WaitForCompletionAsync(CancellationToken)"/>, or <see cref="WaitForCompletionAsync(TimeSpan, CancellationToken)"/> must be called
        /// to re-populate the details of this operation.
        /// </summary>
        /// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
        /// <exception cref="ArgumentNullException"><paramref name="id"/> or <paramref name="client"/> is null.</exception>
        public BackupOperation(string id, KeyVaultBackupClient client)
        {
            Argument.AssertNotNull(id, nameof(id));
            Argument.AssertNotNull(client, nameof(client));

            _client = client;
            _value  = new FullBackupDetailsInternal(string.Empty, string.Empty, null, null, null, id, string.Empty);
        }
        /// <summary>
        /// Initializes a new instance of a BackupOperation for mocking purposes.
        /// </summary>
        /// <param name="value">The <see cref="FullBackupDetailsInternal" /> that will be returned from <see cref="Value" />.</param>
        /// <param name="response">The <see cref="Response" /> that will be returned from <see cref="GetRawResponse" />.</param>
        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
        internal BackupOperation(FullBackupDetailsInternal value, Response response, KeyVaultBackupClient client)
        {
            Argument.AssertNotNull(value, nameof(value));
            Argument.AssertNotNull(response, nameof(response));
            Argument.AssertNotNull(client, nameof(client));

            _response = response;
            _value    = value;
            _client   = client;
        }
        /// <inheritdoc/>
        public override Response UpdateStatus(CancellationToken cancellationToken = default)
        {
            if (!HasCompleted)
            {
                Response <FullBackupDetailsInternal> response = _client.GetBackupDetails(Id, cancellationToken);
                _value    = response.Value;
                _response = response.GetRawResponse();
            }

            return(GetRawResponse());
        }
        /// <inheritdoc/>
        public override async ValueTask <Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
        {
            if (!HasCompleted)
            {
                Response <FullBackupDetailsInternal> response = await _client.GetBackupDetailsAsync(Id, cancellationToken).ConfigureAwait(false);

                _value    = response.Value;
                _response = response.GetRawResponse();
            }

            return(GetRawResponse());
        }
Exemplo n.º 9
0
        public void Setup()
        {
            DateTimeOffset now = DateTimeOffset.Now;

            failedBackup = new FullBackupDetailsInternal(
                "failed",
                "failure details",
                new KeyVaultServiceError("500", "failed backup", null),
                DateTimeOffset.Now.AddMinutes(-5),
                now, JobId, BackupLocation);

            incompleteBackup = new FullBackupDetailsInternal(
                "in progress",
                "",
                null,
                DateTimeOffset.Now.AddMinutes(-5),
                null, JobId, BackupLocation);

            failedResponse = new Mock <Response <FullBackupDetailsInternal> >();
            failedResponse.SetupGet(m => m.Value).Returns(failedBackup);
        }