| | | 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 | | |
| | | 9 | | namespace 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> |
| | 4 | 28 | | public CertificateOperation(CertificateClient client, string name) |
| | | 29 | | { |
| | 4 | 30 | | Argument.AssertNotNull(client, nameof(client)); |
| | 4 | 31 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | | 32 | | |
| | 4 | 33 | | Properties = new CertificateOperationProperties(client.VaultUri, name); |
| | | 34 | | |
| | 4 | 35 | | Id = Properties.Id.ToString(); |
| | 4 | 36 | | _client = client; |
| | 4 | 37 | | } |
| | | 38 | | |
| | 60 | 39 | | internal CertificateOperation(Response<CertificateOperationProperties> properties, CertificateClient client) |
| | | 40 | | { |
| | 60 | 41 | | Properties = properties; |
| | | 42 | | |
| | 60 | 43 | | Id = Properties.Id.ToString(); |
| | 60 | 44 | | _client = client; |
| | 60 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the properties of the pending certificate operation. |
| | | 49 | | /// </summary> |
| | 4308 | 50 | | public CertificateOperationProperties Properties { get; private set; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a value indicating whether the operation has reached a terminal state. |
| | | 54 | | /// </summary> |
| | 726 | 55 | | public override bool HasCompleted => _completed; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets a value indicating whether the Value property can be safely accessed. |
| | | 59 | | /// </summary> |
| | 24 | 60 | | public override bool HasValue => _value != null; |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | 0 | 63 | | 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 | | { |
| | 64 | 71 | | if (Properties is null) |
| | | 72 | | { |
| | 8 | 73 | | throw new InvalidOperationException("The operation was deleted so no value is available."); |
| | | 74 | | } |
| | | 75 | | |
| | 56 | 76 | | if (Properties.Status == "cancelled") |
| | | 77 | | { |
| | 8 | 78 | | throw new OperationCanceledException("The operation was canceled so no value is available."); |
| | | 79 | | } |
| | | 80 | | |
| | 48 | 81 | | if (Properties.Error != null) |
| | | 82 | | { |
| | 4 | 83 | | throw new InvalidOperationException($"The certificate operation failed: {Properties.Error.Message}") |
| | | 84 | | } |
| | | 85 | | |
| | 44 | 86 | | return OperationHelpers.GetValue(ref _value); |
| | | 87 | | } |
| | | 88 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | 702 | 92 | | public override Response GetRawResponse() => _response; |
| | | 93 | | |
| | | 94 | | /// <inheritdoc /> |
| | | 95 | | public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(CancellationToken canc |
| | 0 | 96 | | this.DefaultWaitForCompletionAsync(cancellationToken); |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | public override ValueTask<Response<KeyVaultCertificateWithPolicy>> WaitForCompletionAsync(TimeSpan pollingInterv |
| | 24 | 100 | | 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 | | { |
| | 272 | 113 | | if (!_completed) |
| | | 114 | | { |
| | 272 | 115 | | Response<CertificateOperationProperties> pollResponse = _client.GetPendingCertificate(Properties.Name, c |
| | | 116 | | |
| | 272 | 117 | | _response = pollResponse.GetRawResponse(); |
| | | 118 | | |
| | 272 | 119 | | Properties = pollResponse; |
| | | 120 | | |
| | | 121 | | // Properties will be null if deleted. |
| | 272 | 122 | | if (Properties is null) |
| | | 123 | | { |
| | 4 | 124 | | _completed = true; |
| | 4 | 125 | | return _response; |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 268 | 129 | | if (Properties.Status == "completed") |
| | | 130 | | { |
| | 12 | 131 | | Response<KeyVaultCertificateWithPolicy> getResponse = _client.GetCertificate(Properties.Name, cancellati |
| | | 132 | | |
| | 12 | 133 | | _response = getResponse.GetRawResponse(); |
| | | 134 | | |
| | 12 | 135 | | _value = getResponse.Value; |
| | | 136 | | |
| | 12 | 137 | | _completed = true; |
| | | 138 | | } |
| | 256 | 139 | | else if (Properties.Status == "cancelled") |
| | | 140 | | { |
| | 4 | 141 | | _completed = true; |
| | | 142 | | } |
| | 252 | 143 | | else if (Properties.Error != null) |
| | | 144 | | { |
| | 0 | 145 | | _completed = true; |
| | | 146 | | } |
| | | 147 | | |
| | 268 | 148 | | 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 | | { |
| | 402 | 161 | | if (!_completed) |
| | | 162 | | { |
| | 402 | 163 | | Response<CertificateOperationProperties> pollResponse = await _client.GetPendingCertificateAsync(Propert |
| | | 164 | | |
| | 402 | 165 | | _response = pollResponse.GetRawResponse(); |
| | | 166 | | |
| | 402 | 167 | | Properties = pollResponse; |
| | | 168 | | |
| | | 169 | | // Properties will be null if deleted. |
| | 402 | 170 | | if (Properties is null) |
| | | 171 | | { |
| | 4 | 172 | | _completed = true; |
| | 4 | 173 | | return _response; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 398 | 177 | | if (Properties.Status == "completed") |
| | | 178 | | { |
| | 16 | 179 | | Response<KeyVaultCertificateWithPolicy> getResponse = await _client.GetCertificateAsync(Properties.Name, |
| | | 180 | | |
| | 16 | 181 | | _response = getResponse.GetRawResponse(); |
| | | 182 | | |
| | 16 | 183 | | _value = getResponse.Value; |
| | | 184 | | |
| | 16 | 185 | | _completed = true; |
| | | 186 | | } |
| | 382 | 187 | | else if (Properties.Status == "cancelled") |
| | | 188 | | { |
| | 4 | 189 | | _completed = true; |
| | | 190 | | } |
| | 378 | 191 | | else if (Properties.Error != null) |
| | | 192 | | { |
| | 4 | 193 | | _completed = true; |
| | | 194 | | } |
| | | 195 | | |
| | 398 | 196 | | return GetRawResponse(); |
| | 402 | 197 | | } |
| | | 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 | | { |
| | 0 | 208 | | Response<CertificateOperationProperties> response = _client.CancelCertificateOperation(Properties.Name, canc |
| | | 209 | | |
| | 0 | 210 | | _response = response.GetRawResponse(); |
| | | 211 | | |
| | 0 | 212 | | Properties = response; |
| | 0 | 213 | | } |
| | | 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 | | { |
| | 4 | 225 | | Response<CertificateOperationProperties> response = await _client.CancelCertificateOperationAsync(Properties |
| | | 226 | | |
| | 4 | 227 | | _response = response.GetRawResponse(); |
| | | 228 | | |
| | 4 | 229 | | Properties = response; |
| | 4 | 230 | | } |
| | | 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 | | { |
| | 0 | 241 | | Response<CertificateOperationProperties> response = _client.DeleteCertificateOperation(Properties.Name, canc |
| | | 242 | | |
| | 0 | 243 | | _response = response.GetRawResponse(); |
| | | 244 | | |
| | 0 | 245 | | Properties = response; |
| | 0 | 246 | | } |
| | | 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 | | { |
| | 4 | 258 | | Response<CertificateOperationProperties> response = await _client.DeleteCertificateOperationAsync(Properties |
| | | 259 | | |
| | 4 | 260 | | _response = response.GetRawResponse(); |
| | | 261 | | |
| | 4 | 262 | | Properties = response; |
| | 4 | 263 | | } |
| | | 264 | | } |
| | | 265 | | } |