| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Security.KeyVault.Administration.Models; |
| | 9 | |
|
| | 10 | | namespace Azure.Security.KeyVault.Administration |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A long-running operation for <see cref="KeyVaultBackupClient.StartRestore(Uri, string, string, CancellationToken |
| | 14 | | /// </summary> |
| | 15 | | public class RestoreOperation : Operation<Response> |
| | 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 RestoreDetailsInternal _value; |
| | 24 | | private readonly string _id; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Creates an instance of a RestoreOperation from a previously started operation. <see cref="UpdateStatus(Cance |
| | 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 |
| 0 | 34 | | public RestoreOperation(string id, KeyVaultBackupClient client) |
| | 35 | | { |
| 0 | 36 | | Argument.AssertNotNull(id, nameof(id)); |
| 0 | 37 | | Argument.AssertNotNull(client, nameof(client)); |
| | 38 | |
|
| 0 | 39 | | _client = client; |
| 0 | 40 | | _id = id; |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initializes a new instance of a RestoreOperation. |
| | 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 |
| | 48 | | /// <exception cref="ArgumentNullException"><paramref name="client"/> or <paramref name="response"/> is null.</e |
| 4 | 49 | | internal RestoreOperation(KeyVaultBackupClient client, ResponseWithHeaders<ServiceFullRestoreOperationHeaders> r |
| | 50 | | { |
| 4 | 51 | | Argument.AssertNotNull(client, nameof(client)); |
| 4 | 52 | | Argument.AssertNotNull(response, nameof(response)); |
| | 53 | |
|
| 4 | 54 | | _id = response.Headers.JobId() ?? throw new InvalidOperationException("The response does not contain an Id") |
| 4 | 55 | | _client = client; |
| 4 | 56 | | _response = response.GetRawResponse(); |
| 4 | 57 | | _retryAfterSeconds = response.Headers.RetryAfter; |
| 4 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Initializes a new instance of a RestoreOperation. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param> |
| | 64 | | /// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVault |
| | 65 | | /// <exception cref="ArgumentNullException"><paramref name="client"/> or <paramref name="response"/> is null.</e |
| 0 | 66 | | internal RestoreOperation(KeyVaultBackupClient client, ResponseWithHeaders<ServiceSelectiveKeyRestoreOperationHe |
| | 67 | | { |
| 0 | 68 | | Argument.AssertNotNull(client, nameof(client)); |
| 0 | 69 | | Argument.AssertNotNull(response, nameof(response)); |
| | 70 | |
|
| 0 | 71 | | _id = response.Headers.JobId() ?? throw new InvalidOperationException("The response does not contain an Id") |
| 0 | 72 | | _client = client; |
| 0 | 73 | | _response = response.GetRawResponse(); |
| 0 | 74 | | _retryAfterSeconds = response.Headers.RetryAfter; |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Initializes a new instance of a RestoreOperation for mocking purposes. |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="value">The <see cref="RestoreDetailsInternal" /> that will be used to populate various properti |
| | 81 | | /// <param name="response">The <see cref="Response" /> that will be returned from <see cref="GetRawResponse" />. |
| | 82 | | /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param> |
| | 83 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> or <paramref name="response"/> or <paramref |
| 0 | 84 | | internal RestoreOperation(RestoreDetailsInternal value, Response response, KeyVaultBackupClient client) |
| | 85 | | { |
| 0 | 86 | | Argument.AssertNotNull(value, nameof(value)); |
| 0 | 87 | | Argument.AssertNotNull(response, nameof(response)); |
| 0 | 88 | | Argument.AssertNotNull(client, nameof(client)); |
| | 89 | |
|
| 0 | 90 | | _client = client; |
| 0 | 91 | | _response = response; |
| 0 | 92 | | _value = value; |
| 0 | 93 | | _id = value.JobId; |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// The start time of the restore operation. |
| | 98 | | /// </summary> |
| 0 | 99 | | public DateTimeOffset? StartTime => _value?.StartTime; |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// The end time of the restore operation. |
| | 103 | | /// </summary> |
| 36 | 104 | | public DateTimeOffset? EndTime => _value?.EndTime; |
| | 105 | |
|
| | 106 | | /// <inheritdoc/> |
| 12 | 107 | | public override string Id => _id; |
| | 108 | |
|
| | 109 | | /// <inheritdoc/> |
| | 110 | | public override Response Value |
| | 111 | | { |
| | 112 | | get |
| | 113 | | { |
| | 114 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| 4 | 115 | | if (!HasCompleted) |
| | 116 | | { |
| 0 | 117 | | throw new InvalidOperationException("The operation is not complete."); |
| | 118 | | } |
| 4 | 119 | | if (EndTime.HasValue && _value.Error != null) |
| | 120 | | { |
| 0 | 121 | | throw new RequestFailedException($"{_value.Error.Message}\nInnerError: {_value.Error.InnerError}\nCo |
| | 122 | | } |
| | 123 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| 4 | 124 | | return _response; |
| | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| | 128 | | /// <inheritdoc/> |
| 32 | 129 | | public override bool HasCompleted => EndTime.HasValue; |
| | 130 | |
|
| | 131 | | /// <inheritdoc/> |
| 4 | 132 | | public override bool HasValue => _response != null && _value?.Error == null && HasCompleted; |
| | 133 | |
|
| | 134 | | /// <inheritdoc/> |
| 16 | 135 | | public override Response GetRawResponse() => _response; |
| | 136 | |
|
| | 137 | | /// <inheritdoc/> |
| | 138 | | public override Response UpdateStatus(CancellationToken cancellationToken = default) |
| | 139 | | { |
| 0 | 140 | | if (!HasCompleted) |
| | 141 | | { |
| 0 | 142 | | Response<RestoreDetailsInternal> response = _client.GetRestoreDetails(Id, cancellationToken); |
| 0 | 143 | | _value = response.Value; |
| 0 | 144 | | _response = response.GetRawResponse(); |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | return GetRawResponse(); |
| | 148 | | } |
| | 149 | |
|
| | 150 | | /// <inheritdoc/> |
| | 151 | | public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) |
| | 152 | | { |
| 12 | 153 | | if (!HasCompleted) |
| | 154 | | { |
| 12 | 155 | | Response<RestoreDetailsInternal> response = await _client.GetRestoreDetailsAsync(Id, cancellationToken). |
| 12 | 156 | | _value = response.Value; |
| 12 | 157 | | _response = response.GetRawResponse(); |
| | 158 | | } |
| | 159 | |
|
| 12 | 160 | | return GetRawResponse(); |
| 12 | 161 | | } |
| | 162 | |
|
| | 163 | | /// <inheritdoc/> |
| | 164 | | public override ValueTask<Response<Response>> WaitForCompletionAsync(CancellationToken cancellationToken = defau |
| 4 | 165 | | _retryAfterSeconds.HasValue ? this.DefaultWaitForCompletionAsync(TimeSpan.FromSeconds(_retryAfterSeconds.Val |
| 4 | 166 | | this.DefaultWaitForCompletionAsync(cancellationToken); |
| | 167 | |
|
| | 168 | | /// <inheritdoc/> |
| | 169 | | public override ValueTask<Response<Response>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken |
| 0 | 170 | | this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken); |
| | 171 | | } |
| | 172 | | } |