2
2
// Licensed under the MIT License.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . Threading ;
6
7
using System . Threading . Tasks ;
7
8
using Azure . Core ;
8
- using Azure . Core . Pipeline ;
9
9
10
10
namespace Azure . Security . KeyVault . Certificates
11
11
{
12
12
/// <summary>
13
13
/// A long-running operation for <see cref="CertificateClient.StartDeleteCertificate(string, CancellationToken)"/> or <see cref="CertificateClient.StartDeleteCertificateAsync(string, CancellationToken)"/>.
14
14
/// </summary>
15
- public class DeleteCertificateOperation : Operation < DeletedCertificate >
15
+ public class DeleteCertificateOperation : Operation < DeletedCertificate > , IOperation
16
16
{
17
17
private static readonly TimeSpan s_defaultPollingInterval = TimeSpan . FromSeconds ( 2 ) ;
18
18
19
19
private readonly KeyVaultPipeline _pipeline ;
20
+ private readonly OperationInternal _operationInternal ;
20
21
private readonly DeletedCertificate _value ;
21
- private Response _response ;
22
- private bool _completed ;
23
22
24
23
internal DeleteCertificateOperation ( KeyVaultPipeline pipeline , Response < DeletedCertificate > response )
25
24
{
26
25
_pipeline = pipeline ;
27
26
_value = response . Value ?? throw new InvalidOperationException ( "The response does not contain a value." ) ;
28
- _response = response . GetRawResponse ( ) ;
27
+ _operationInternal = new ( _pipeline . Diagnostics , this , response . GetRawResponse ( ) , nameof ( DeleteCertificateOperation ) , new [ ]
28
+ {
29
+ new KeyValuePair < string , string > ( "secret" , _value . Name ) , // Retained for backward compatibility.
30
+ new KeyValuePair < string , string > ( "certificate" , _value . Name ) ,
31
+ } ) ;
29
32
30
- // The recoveryId is only returned if soft- delete is enabled.
33
+ // The recoveryId is only returned if soft delete is enabled.
31
34
if ( _value . RecoveryId is null )
32
35
{
33
- _completed = true ;
36
+ // If soft delete is not enabled, deleting is immediate so set success accordingly.
37
+ _operationInternal . SetState ( OperationState . Success ( response . GetRawResponse ( ) ) ) ;
34
38
}
35
39
}
36
40
@@ -50,33 +54,20 @@ protected DeleteCertificateOperation() {}
50
54
public override DeletedCertificate Value => _value ;
51
55
52
56
/// <inheritdoc/>
53
- public override bool HasCompleted => _completed ;
57
+ public override bool HasCompleted => _operationInternal . HasCompleted ;
54
58
55
59
/// <inheritdoc/>
56
60
public override bool HasValue => true ;
57
61
58
62
/// <inheritdoc/>
59
- public override Response GetRawResponse ( ) => _response ;
63
+ public override Response GetRawResponse ( ) => _operationInternal . RawResponse ;
60
64
61
65
/// <inheritdoc/>
62
66
public override Response UpdateStatus ( CancellationToken cancellationToken = default )
63
67
{
64
- if ( ! _completed )
68
+ if ( ! HasCompleted )
65
69
{
66
- using DiagnosticScope scope = _pipeline . CreateScope ( $ "{ nameof ( DeleteCertificateOperation ) } .{ nameof ( UpdateStatus ) } ") ;
67
- scope . AddAttribute ( "secret" , _value . Name ) ;
68
- scope . Start ( ) ;
69
-
70
- try
71
- {
72
- _response = _pipeline . GetResponse ( RequestMethod . Get , cancellationToken , CertificateClient . DeletedCertificatesPath , _value . Name ) ;
73
- _completed = CheckCompleted ( _response ) ;
74
- }
75
- catch ( Exception e )
76
- {
77
- scope . Failed ( e ) ;
78
- throw ;
79
- }
70
+ return _operationInternal . UpdateStatus ( cancellationToken ) ;
80
71
}
81
72
82
73
return GetRawResponse ( ) ;
@@ -85,22 +76,9 @@ public override Response UpdateStatus(CancellationToken cancellationToken = defa
85
76
/// <inheritdoc/>
86
77
public override async ValueTask < Response > UpdateStatusAsync ( CancellationToken cancellationToken = default )
87
78
{
88
- if ( ! _completed )
79
+ if ( ! HasCompleted )
89
80
{
90
- using DiagnosticScope scope = _pipeline . CreateScope ( $ "{ nameof ( DeleteCertificateOperation ) } .{ nameof ( UpdateStatus ) } ") ;
91
- scope . AddAttribute ( "secret" , _value . Name ) ;
92
- scope . Start ( ) ;
93
-
94
- try
95
- {
96
- _response = await _pipeline . GetResponseAsync ( RequestMethod . Get , cancellationToken , CertificateClient . DeletedCertificatesPath , _value . Name ) . ConfigureAwait ( false ) ;
97
- _completed = await CheckCompletedAsync ( _response ) . ConfigureAwait ( false ) ;
98
- }
99
- catch ( Exception e )
100
- {
101
- scope . Failed ( e ) ;
102
- throw ;
103
- }
81
+ return await _operationInternal . UpdateStatusAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
104
82
}
105
83
106
84
return GetRawResponse ( ) ;
@@ -114,34 +92,27 @@ public override ValueTask<Response<DeletedCertificate>> WaitForCompletionAsync(C
114
92
public override ValueTask < Response < DeletedCertificate > > WaitForCompletionAsync ( TimeSpan pollingInterval , CancellationToken cancellationToken ) =>
115
93
this . DefaultWaitForCompletionAsync ( pollingInterval , cancellationToken ) ;
116
94
117
- private async ValueTask < bool > CheckCompletedAsync ( Response response )
95
+ async ValueTask < OperationState > IOperation . UpdateStateAsync ( bool async , CancellationToken cancellationToken )
118
96
{
119
- switch ( response . Status )
120
- {
121
- case 200 :
122
- case 403 : // Access denied but proof the certificate was deleted.
123
- return true ;
124
-
125
- case 404 :
126
- return false ;
97
+ Response response = async
98
+ ? await _pipeline . GetResponseAsync ( RequestMethod . Get , cancellationToken , CertificateClient . DeletedCertificatesPath , _value . Name ) . ConfigureAwait ( false )
99
+ : _pipeline . GetResponse ( RequestMethod . Get , cancellationToken , CertificateClient . DeletedCertificatesPath , _value . Name ) ;
127
100
128
- default :
129
- throw await _pipeline . Diagnostics . CreateRequestFailedExceptionAsync ( response ) . ConfigureAwait ( false ) ;
130
- }
131
- }
132
- private bool CheckCompleted ( Response response )
133
- {
134
101
switch ( response . Status )
135
102
{
136
103
case 200 :
137
104
case 403 : // Access denied but proof the certificate was deleted.
138
- return true ;
105
+ return OperationState . Success ( response ) ;
139
106
140
107
case 404 :
141
- return false ;
108
+ return OperationState . Pending ( response ) ;
142
109
143
110
default :
144
- throw _pipeline . Diagnostics . CreateRequestFailedException ( response ) ;
111
+ RequestFailedException ex = async
112
+ ? await _pipeline . Diagnostics . CreateRequestFailedExceptionAsync ( response ) . ConfigureAwait ( false )
113
+ : _pipeline . Diagnostics . CreateRequestFailedException ( response ) ;
114
+
115
+ return OperationState . Failure ( response , ex ) ;
145
116
}
146
117
}
147
118
}
0 commit comments