| | 1 | | // <auto-generated> |
| | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 3 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 4 | | // license information. |
| | 5 | | // |
| | 6 | | // Code generated by Microsoft (R) AutoRest Code Generator. |
| | 7 | | // Changes may cause incorrect behavior and will be lost if the code is |
| | 8 | | // regenerated. |
| | 9 | | // </auto-generated> |
| | 10 | |
|
| | 11 | | namespace Microsoft.Azure.Attestation |
| | 12 | | { |
| | 13 | | using Microsoft.Rest; |
| | 14 | | using Microsoft.Rest.Azure; |
| | 15 | | using Models; |
| | 16 | | using Newtonsoft.Json; |
| | 17 | | using System.Collections; |
| | 18 | | using System.Collections.Generic; |
| | 19 | | using System.Linq; |
| | 20 | | using System.Net; |
| | 21 | | using System.Net.Http; |
| | 22 | | using System.Threading; |
| | 23 | | using System.Threading.Tasks; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// PolicyCertificatesOperations operations. |
| | 27 | | /// </summary> |
| | 28 | | internal partial class PolicyCertificatesOperations : IServiceOperations<AttestationClient>, IPolicyCertificatesOper |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// Initializes a new instance of the PolicyCertificatesOperations class. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name='client'> |
| | 34 | | /// Reference to the service client. |
| | 35 | | /// </param> |
| | 36 | | /// <exception cref="System.ArgumentNullException"> |
| | 37 | | /// Thrown when a required parameter is null |
| | 38 | | /// </exception> |
| 8 | 39 | | internal PolicyCertificatesOperations(AttestationClient client) |
| | 40 | | { |
| 8 | 41 | | if (client == null) |
| | 42 | | { |
| 0 | 43 | | throw new System.ArgumentNullException("client"); |
| | 44 | | } |
| 8 | 45 | | Client = client; |
| 8 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets a reference to the AttestationClient |
| | 50 | | /// </summary> |
| 100 | 51 | | public AttestationClient Client { get; private set; } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Retrieves the set of certificates used to express policy for the current |
| | 55 | | /// tenant. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name='tenantBaseUrl'> |
| | 58 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | 59 | | /// </param> |
| | 60 | | /// <param name='customHeaders'> |
| | 61 | | /// Headers that will be added to request. |
| | 62 | | /// </param> |
| | 63 | | /// <param name='cancellationToken'> |
| | 64 | | /// The cancellation token. |
| | 65 | | /// </param> |
| | 66 | | /// <exception cref="CloudException"> |
| | 67 | | /// Thrown when the operation returned an invalid status code |
| | 68 | | /// </exception> |
| | 69 | | /// <exception cref="SerializationException"> |
| | 70 | | /// Thrown when unable to deserialize the response |
| | 71 | | /// </exception> |
| | 72 | | /// <exception cref="ValidationException"> |
| | 73 | | /// Thrown when a required parameter is null |
| | 74 | | /// </exception> |
| | 75 | | /// <exception cref="System.ArgumentNullException"> |
| | 76 | | /// Thrown when a required parameter is null |
| | 77 | | /// </exception> |
| | 78 | | /// <return> |
| | 79 | | /// A response object containing the response body and response headers. |
| | 80 | | /// </return> |
| | 81 | | public async Task<AzureOperationResponse<object>> GetWithHttpMessagesAsync(string tenantBaseUrl, Dictionary<stri |
| | 82 | | { |
| 4 | 83 | | if (tenantBaseUrl == null) |
| | 84 | | { |
| 0 | 85 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | 86 | | } |
| | 87 | | // Tracing |
| 4 | 88 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 4 | 89 | | string _invocationId = null; |
| 4 | 90 | | if (_shouldTrace) |
| | 91 | | { |
| 0 | 92 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 93 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 94 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| 0 | 95 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 96 | | ServiceClientTracing.Enter(_invocationId, this, "Get", tracingParameters); |
| | 97 | | } |
| | 98 | | // Construct URL |
| 4 | 99 | | var _baseUrl = Client.BaseUri; |
| 4 | 100 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/certificates"; |
| 4 | 101 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| 4 | 102 | | List<string> _queryParameters = new List<string>(); |
| 4 | 103 | | if (Client.ApiVersion != null) |
| | 104 | | { |
| 4 | 105 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 106 | | } |
| 4 | 107 | | if (_queryParameters.Count > 0) |
| | 108 | | { |
| 4 | 109 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 110 | | } |
| | 111 | | // Create HTTP transport objects |
| 4 | 112 | | var _httpRequest = new HttpRequestMessage(); |
| 4 | 113 | | HttpResponseMessage _httpResponse = null; |
| 4 | 114 | | _httpRequest.Method = new HttpMethod("GET"); |
| 4 | 115 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 116 | | // Set Headers |
| 4 | 117 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 118 | | { |
| 4 | 119 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | 120 | | } |
| 4 | 121 | | if (Client.AcceptLanguage != null) |
| | 122 | | { |
| 4 | 123 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 124 | | { |
| 0 | 125 | | _httpRequest.Headers.Remove("accept-language"); |
| | 126 | | } |
| 4 | 127 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 128 | | } |
| | 129 | |
|
| | 130 | |
|
| 4 | 131 | | if (customHeaders != null) |
| | 132 | | { |
| 0 | 133 | | foreach(var _header in customHeaders) |
| | 134 | | { |
| 0 | 135 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 136 | | { |
| 0 | 137 | | _httpRequest.Headers.Remove(_header.Key); |
| | 138 | | } |
| 0 | 139 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 140 | | } |
| | 141 | | } |
| | 142 | |
|
| | 143 | | // Serialize Request |
| 4 | 144 | | string _requestContent = null; |
| | 145 | | // Set Credentials |
| 4 | 146 | | if (Client.Credentials != null) |
| | 147 | | { |
| 4 | 148 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 149 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 150 | | } |
| | 151 | | // Send Request |
| 4 | 152 | | if (_shouldTrace) |
| | 153 | | { |
| 0 | 154 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 155 | | } |
| 4 | 156 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 157 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 4 | 158 | | if (_shouldTrace) |
| | 159 | | { |
| 0 | 160 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 161 | | } |
| 4 | 162 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 4 | 163 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 164 | | string _responseContent = null; |
| 4 | 165 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | 166 | | { |
| 0 | 167 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 168 | | try |
| | 169 | | { |
| 0 | 170 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 171 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 0 | 172 | | if (_errorBody != null) |
| | 173 | | { |
| 0 | 174 | | ex = new CloudException(_errorBody.Message); |
| 0 | 175 | | ex.Body = _errorBody; |
| | 176 | | } |
| 0 | 177 | | } |
| 0 | 178 | | catch (JsonException) |
| | 179 | | { |
| | 180 | | // Ignore the exception |
| 0 | 181 | | } |
| 0 | 182 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 183 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 184 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 185 | | { |
| 0 | 186 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 187 | | } |
| 0 | 188 | | if (_shouldTrace) |
| | 189 | | { |
| 0 | 190 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 191 | | } |
| 0 | 192 | | _httpRequest.Dispose(); |
| 0 | 193 | | if (_httpResponse != null) |
| | 194 | | { |
| 0 | 195 | | _httpResponse.Dispose(); |
| | 196 | | } |
| 0 | 197 | | throw ex; |
| | 198 | | } |
| | 199 | | // Create Result |
| 4 | 200 | | var _result = new AzureOperationResponse<object>(); |
| 4 | 201 | | _result.Request = _httpRequest; |
| 4 | 202 | | _result.Response = _httpResponse; |
| 4 | 203 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 204 | | { |
| 0 | 205 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 206 | | } |
| | 207 | | // Deserialize Response |
| 4 | 208 | | if ((int)_statusCode == 200) |
| | 209 | | { |
| 4 | 210 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 211 | | try |
| | 212 | | { |
| 4 | 213 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| 4 | 214 | | } |
| 0 | 215 | | catch (JsonException ex) |
| | 216 | | { |
| 0 | 217 | | _httpRequest.Dispose(); |
| 0 | 218 | | if (_httpResponse != null) |
| | 219 | | { |
| 0 | 220 | | _httpResponse.Dispose(); |
| | 221 | | } |
| 0 | 222 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 223 | | } |
| | 224 | | } |
| | 225 | | // Deserialize Response |
| 4 | 226 | | if ((int)_statusCode == 400) |
| | 227 | | { |
| 0 | 228 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 229 | | try |
| | 230 | | { |
| 0 | 231 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| 0 | 232 | | } |
| 0 | 233 | | catch (JsonException ex) |
| | 234 | | { |
| 0 | 235 | | _httpRequest.Dispose(); |
| 0 | 236 | | if (_httpResponse != null) |
| | 237 | | { |
| 0 | 238 | | _httpResponse.Dispose(); |
| | 239 | | } |
| 0 | 240 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 241 | | } |
| | 242 | | } |
| | 243 | | // Deserialize Response |
| 4 | 244 | | if ((int)_statusCode == 401) |
| | 245 | | { |
| 0 | 246 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 247 | | try |
| | 248 | | { |
| 0 | 249 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| 0 | 250 | | } |
| 0 | 251 | | catch (JsonException ex) |
| | 252 | | { |
| 0 | 253 | | _httpRequest.Dispose(); |
| 0 | 254 | | if (_httpResponse != null) |
| | 255 | | { |
| 0 | 256 | | _httpResponse.Dispose(); |
| | 257 | | } |
| 0 | 258 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 259 | | } |
| | 260 | | } |
| 4 | 261 | | if (_shouldTrace) |
| | 262 | | { |
| 0 | 263 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 264 | | } |
| 4 | 265 | | return _result; |
| 4 | 266 | | } |
| | 267 | |
|
| | 268 | | /// <summary> |
| | 269 | | /// Adds a new attestation policy certificate to the set of policy management |
| | 270 | | /// certificates. |
| | 271 | | /// </summary> |
| | 272 | | /// <param name='tenantBaseUrl'> |
| | 273 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | 274 | | /// </param> |
| | 275 | | /// <param name='policyCertificateToAdd'> |
| | 276 | | /// An RFC7519 JSON Web Token containing a claim named "aas-policyCertificate" |
| | 277 | | /// whose value is an RFC7517 JSON Web Key which specifies a new key to add. |
| | 278 | | /// The RFC7519 JWT must be signed with one of the existing signing |
| | 279 | | /// certificates |
| | 280 | | /// </param> |
| | 281 | | /// <param name='customHeaders'> |
| | 282 | | /// Headers that will be added to request. |
| | 283 | | /// </param> |
| | 284 | | /// <param name='cancellationToken'> |
| | 285 | | /// The cancellation token. |
| | 286 | | /// </param> |
| | 287 | | /// <exception cref="CloudException"> |
| | 288 | | /// Thrown when the operation returned an invalid status code |
| | 289 | | /// </exception> |
| | 290 | | /// <exception cref="SerializationException"> |
| | 291 | | /// Thrown when unable to deserialize the response |
| | 292 | | /// </exception> |
| | 293 | | /// <exception cref="ValidationException"> |
| | 294 | | /// Thrown when a required parameter is null |
| | 295 | | /// </exception> |
| | 296 | | /// <exception cref="System.ArgumentNullException"> |
| | 297 | | /// Thrown when a required parameter is null |
| | 298 | | /// </exception> |
| | 299 | | /// <return> |
| | 300 | | /// A response object containing the response body and response headers. |
| | 301 | | /// </return> |
| | 302 | | public async Task<AzureOperationResponse<object>> AddWithHttpMessagesAsync(string tenantBaseUrl, string policyCe |
| | 303 | | { |
| 2 | 304 | | if (tenantBaseUrl == null) |
| | 305 | | { |
| 0 | 306 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | 307 | | } |
| 2 | 308 | | if (policyCertificateToAdd == null) |
| | 309 | | { |
| 0 | 310 | | throw new ValidationException(ValidationRules.CannotBeNull, "policyCertificateToAdd"); |
| | 311 | | } |
| | 312 | | // Tracing |
| 2 | 313 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 314 | | string _invocationId = null; |
| 2 | 315 | | if (_shouldTrace) |
| | 316 | | { |
| 0 | 317 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 318 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 319 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| 0 | 320 | | tracingParameters.Add("policyCertificateToAdd", policyCertificateToAdd); |
| 0 | 321 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 322 | | ServiceClientTracing.Enter(_invocationId, this, "Add", tracingParameters); |
| | 323 | | } |
| | 324 | | // Construct URL |
| 2 | 325 | | var _baseUrl = Client.BaseUri; |
| 2 | 326 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/certificates"; |
| 2 | 327 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| 2 | 328 | | List<string> _queryParameters = new List<string>(); |
| 2 | 329 | | if (Client.ApiVersion != null) |
| | 330 | | { |
| 2 | 331 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 332 | | } |
| 2 | 333 | | if (_queryParameters.Count > 0) |
| | 334 | | { |
| 2 | 335 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 336 | | } |
| | 337 | | // Create HTTP transport objects |
| 2 | 338 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 339 | | HttpResponseMessage _httpResponse = null; |
| 2 | 340 | | _httpRequest.Method = new HttpMethod("PUT"); |
| 2 | 341 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 342 | | // Set Headers |
| 2 | 343 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 344 | | { |
| 2 | 345 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | 346 | | } |
| 2 | 347 | | if (Client.AcceptLanguage != null) |
| | 348 | | { |
| 2 | 349 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 350 | | { |
| 0 | 351 | | _httpRequest.Headers.Remove("accept-language"); |
| | 352 | | } |
| 2 | 353 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 354 | | } |
| | 355 | |
|
| | 356 | |
|
| 2 | 357 | | if (customHeaders != null) |
| | 358 | | { |
| 0 | 359 | | foreach(var _header in customHeaders) |
| | 360 | | { |
| 0 | 361 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 362 | | { |
| 0 | 363 | | _httpRequest.Headers.Remove(_header.Key); |
| | 364 | | } |
| 0 | 365 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 366 | | } |
| | 367 | | } |
| | 368 | |
|
| | 369 | | // Serialize Request |
| 2 | 370 | | string _requestContent = null; |
| 2 | 371 | | if(policyCertificateToAdd != null) |
| | 372 | | { |
| 2 | 373 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(policyCertificateToAdd, Client.Seri |
| 2 | 374 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 375 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 376 | | } |
| | 377 | | // Set Credentials |
| 2 | 378 | | if (Client.Credentials != null) |
| | 379 | | { |
| 2 | 380 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 381 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 382 | | } |
| | 383 | | // Send Request |
| 2 | 384 | | if (_shouldTrace) |
| | 385 | | { |
| 0 | 386 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 387 | | } |
| 2 | 388 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 389 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 390 | | if (_shouldTrace) |
| | 391 | | { |
| 0 | 392 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 393 | | } |
| 2 | 394 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 395 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 396 | | string _responseContent = null; |
| 2 | 397 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | 398 | | { |
| 0 | 399 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 400 | | try |
| | 401 | | { |
| 0 | 402 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 403 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 0 | 404 | | if (_errorBody != null) |
| | 405 | | { |
| 0 | 406 | | ex = new CloudException(_errorBody.Message); |
| 0 | 407 | | ex.Body = _errorBody; |
| | 408 | | } |
| 0 | 409 | | } |
| 0 | 410 | | catch (JsonException) |
| | 411 | | { |
| | 412 | | // Ignore the exception |
| 0 | 413 | | } |
| 0 | 414 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 415 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 416 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 417 | | { |
| 0 | 418 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 419 | | } |
| 0 | 420 | | if (_shouldTrace) |
| | 421 | | { |
| 0 | 422 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 423 | | } |
| 0 | 424 | | _httpRequest.Dispose(); |
| 0 | 425 | | if (_httpResponse != null) |
| | 426 | | { |
| 0 | 427 | | _httpResponse.Dispose(); |
| | 428 | | } |
| 0 | 429 | | throw ex; |
| | 430 | | } |
| | 431 | | // Create Result |
| 2 | 432 | | var _result = new AzureOperationResponse<object>(); |
| 2 | 433 | | _result.Request = _httpRequest; |
| 2 | 434 | | _result.Response = _httpResponse; |
| 2 | 435 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 436 | | { |
| 0 | 437 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 438 | | } |
| | 439 | | // Deserialize Response |
| 2 | 440 | | if ((int)_statusCode == 200) |
| | 441 | | { |
| 2 | 442 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 443 | | try |
| | 444 | | { |
| 2 | 445 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| 2 | 446 | | } |
| 0 | 447 | | catch (JsonException ex) |
| | 448 | | { |
| 0 | 449 | | _httpRequest.Dispose(); |
| 0 | 450 | | if (_httpResponse != null) |
| | 451 | | { |
| 0 | 452 | | _httpResponse.Dispose(); |
| | 453 | | } |
| 0 | 454 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 455 | | } |
| | 456 | | } |
| | 457 | | // Deserialize Response |
| 2 | 458 | | if ((int)_statusCode == 400) |
| | 459 | | { |
| 0 | 460 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 461 | | try |
| | 462 | | { |
| 0 | 463 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| 0 | 464 | | } |
| 0 | 465 | | catch (JsonException ex) |
| | 466 | | { |
| 0 | 467 | | _httpRequest.Dispose(); |
| 0 | 468 | | if (_httpResponse != null) |
| | 469 | | { |
| 0 | 470 | | _httpResponse.Dispose(); |
| | 471 | | } |
| 0 | 472 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 473 | | } |
| | 474 | | } |
| | 475 | | // Deserialize Response |
| 2 | 476 | | if ((int)_statusCode == 401) |
| | 477 | | { |
| 0 | 478 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 479 | | try |
| | 480 | | { |
| 0 | 481 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| 0 | 482 | | } |
| 0 | 483 | | catch (JsonException ex) |
| | 484 | | { |
| 0 | 485 | | _httpRequest.Dispose(); |
| 0 | 486 | | if (_httpResponse != null) |
| | 487 | | { |
| 0 | 488 | | _httpResponse.Dispose(); |
| | 489 | | } |
| 0 | 490 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 491 | | } |
| | 492 | | } |
| 2 | 493 | | if (_shouldTrace) |
| | 494 | | { |
| 0 | 495 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 496 | | } |
| 2 | 497 | | return _result; |
| 2 | 498 | | } |
| | 499 | |
|
| | 500 | | /// <summary> |
| | 501 | | /// Removes the specified policy management certificate. Note that the final |
| | 502 | | /// policy management certificate cannot be removed. |
| | 503 | | /// </summary> |
| | 504 | | /// <param name='tenantBaseUrl'> |
| | 505 | | /// The tenant name, for example https://mytenant.attest.azure.net. |
| | 506 | | /// </param> |
| | 507 | | /// <param name='policyCertificateToRemove'> |
| | 508 | | /// An RFC7519 JSON Web Token containing a claim named "aas-policyCertificate" |
| | 509 | | /// whose value is an RFC7517 JSON Web Key which specifies a new key to update. |
| | 510 | | /// The RFC7519 JWT must be signed with one of the existing signing |
| | 511 | | /// certificates |
| | 512 | | /// </param> |
| | 513 | | /// <param name='customHeaders'> |
| | 514 | | /// Headers that will be added to request. |
| | 515 | | /// </param> |
| | 516 | | /// <param name='cancellationToken'> |
| | 517 | | /// The cancellation token. |
| | 518 | | /// </param> |
| | 519 | | /// <exception cref="CloudException"> |
| | 520 | | /// Thrown when the operation returned an invalid status code |
| | 521 | | /// </exception> |
| | 522 | | /// <exception cref="SerializationException"> |
| | 523 | | /// Thrown when unable to deserialize the response |
| | 524 | | /// </exception> |
| | 525 | | /// <exception cref="ValidationException"> |
| | 526 | | /// Thrown when a required parameter is null |
| | 527 | | /// </exception> |
| | 528 | | /// <exception cref="System.ArgumentNullException"> |
| | 529 | | /// Thrown when a required parameter is null |
| | 530 | | /// </exception> |
| | 531 | | /// <return> |
| | 532 | | /// A response object containing the response body and response headers. |
| | 533 | | /// </return> |
| | 534 | | public async Task<AzureOperationResponse<object>> RemoveWithHttpMessagesAsync(string tenantBaseUrl, string polic |
| | 535 | | { |
| 2 | 536 | | if (tenantBaseUrl == null) |
| | 537 | | { |
| 0 | 538 | | throw new ValidationException(ValidationRules.CannotBeNull, "tenantBaseUrl"); |
| | 539 | | } |
| 2 | 540 | | if (policyCertificateToRemove == null) |
| | 541 | | { |
| 0 | 542 | | throw new ValidationException(ValidationRules.CannotBeNull, "policyCertificateToRemove"); |
| | 543 | | } |
| | 544 | | // Tracing |
| 2 | 545 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 546 | | string _invocationId = null; |
| 2 | 547 | | if (_shouldTrace) |
| | 548 | | { |
| 0 | 549 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 550 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 551 | | tracingParameters.Add("tenantBaseUrl", tenantBaseUrl); |
| 0 | 552 | | tracingParameters.Add("policyCertificateToRemove", policyCertificateToRemove); |
| 0 | 553 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 554 | | ServiceClientTracing.Enter(_invocationId, this, "Remove", tracingParameters); |
| | 555 | | } |
| | 556 | | // Construct URL |
| 2 | 557 | | var _baseUrl = Client.BaseUri; |
| 2 | 558 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "operations/policy/certificates"; |
| 2 | 559 | | _url = _url.Replace("{tenantBaseUrl}", tenantBaseUrl); |
| 2 | 560 | | List<string> _queryParameters = new List<string>(); |
| 2 | 561 | | if (Client.ApiVersion != null) |
| | 562 | | { |
| 2 | 563 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(Client.ApiVersion))); |
| | 564 | | } |
| 2 | 565 | | if (_queryParameters.Count > 0) |
| | 566 | | { |
| 2 | 567 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 568 | | } |
| | 569 | | // Create HTTP transport objects |
| 2 | 570 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 571 | | HttpResponseMessage _httpResponse = null; |
| 2 | 572 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 573 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 574 | | // Set Headers |
| 2 | 575 | | if (Client.GenerateClientRequestId != null && Client.GenerateClientRequestId.Value) |
| | 576 | | { |
| 2 | 577 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | 578 | | } |
| 2 | 579 | | if (Client.AcceptLanguage != null) |
| | 580 | | { |
| 2 | 581 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 582 | | { |
| 0 | 583 | | _httpRequest.Headers.Remove("accept-language"); |
| | 584 | | } |
| 2 | 585 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", Client.AcceptLanguage); |
| | 586 | | } |
| | 587 | |
|
| | 588 | |
|
| 2 | 589 | | if (customHeaders != null) |
| | 590 | | { |
| 0 | 591 | | foreach(var _header in customHeaders) |
| | 592 | | { |
| 0 | 593 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 594 | | { |
| 0 | 595 | | _httpRequest.Headers.Remove(_header.Key); |
| | 596 | | } |
| 0 | 597 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 598 | | } |
| | 599 | | } |
| | 600 | |
|
| | 601 | | // Serialize Request |
| 2 | 602 | | string _requestContent = null; |
| 2 | 603 | | if(policyCertificateToRemove != null) |
| | 604 | | { |
| 2 | 605 | | _requestContent = Rest.Serialization.SafeJsonConvert.SerializeObject(policyCertificateToRemove, Client.S |
| 2 | 606 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 607 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 608 | | } |
| | 609 | | // Set Credentials |
| 2 | 610 | | if (Client.Credentials != null) |
| | 611 | | { |
| 2 | 612 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 613 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 614 | | } |
| | 615 | | // Send Request |
| 2 | 616 | | if (_shouldTrace) |
| | 617 | | { |
| 0 | 618 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 619 | | } |
| 2 | 620 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 621 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 622 | | if (_shouldTrace) |
| | 623 | | { |
| 0 | 624 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 625 | | } |
| 2 | 626 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 627 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 628 | | string _responseContent = null; |
| 2 | 629 | | if ((int)_statusCode != 200 && (int)_statusCode != 400 && (int)_statusCode != 401) |
| | 630 | | { |
| 0 | 631 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 632 | | try |
| | 633 | | { |
| 0 | 634 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 635 | | CloudError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseC |
| 0 | 636 | | if (_errorBody != null) |
| | 637 | | { |
| 0 | 638 | | ex = new CloudException(_errorBody.Message); |
| 0 | 639 | | ex.Body = _errorBody; |
| | 640 | | } |
| 0 | 641 | | } |
| 0 | 642 | | catch (JsonException) |
| | 643 | | { |
| | 644 | | // Ignore the exception |
| 0 | 645 | | } |
| 0 | 646 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 647 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 648 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 649 | | { |
| 0 | 650 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 651 | | } |
| 0 | 652 | | if (_shouldTrace) |
| | 653 | | { |
| 0 | 654 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 655 | | } |
| 0 | 656 | | _httpRequest.Dispose(); |
| 0 | 657 | | if (_httpResponse != null) |
| | 658 | | { |
| 0 | 659 | | _httpResponse.Dispose(); |
| | 660 | | } |
| 0 | 661 | | throw ex; |
| | 662 | | } |
| | 663 | | // Create Result |
| 2 | 664 | | var _result = new AzureOperationResponse<object>(); |
| 2 | 665 | | _result.Request = _httpRequest; |
| 2 | 666 | | _result.Response = _httpResponse; |
| 2 | 667 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 668 | | { |
| 0 | 669 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 670 | | } |
| | 671 | | // Deserialize Response |
| 2 | 672 | | if ((int)_statusCode == 200) |
| | 673 | | { |
| 2 | 674 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 675 | | try |
| | 676 | | { |
| 2 | 677 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| 2 | 678 | | } |
| 0 | 679 | | catch (JsonException ex) |
| | 680 | | { |
| 0 | 681 | | _httpRequest.Dispose(); |
| 0 | 682 | | if (_httpResponse != null) |
| | 683 | | { |
| 0 | 684 | | _httpResponse.Dispose(); |
| | 685 | | } |
| 0 | 686 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 687 | | } |
| | 688 | | } |
| | 689 | | // Deserialize Response |
| 2 | 690 | | if ((int)_statusCode == 400) |
| | 691 | | { |
| 0 | 692 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 693 | | try |
| | 694 | | { |
| 0 | 695 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Cl |
| 0 | 696 | | } |
| 0 | 697 | | catch (JsonException ex) |
| | 698 | | { |
| 0 | 699 | | _httpRequest.Dispose(); |
| 0 | 700 | | if (_httpResponse != null) |
| | 701 | | { |
| 0 | 702 | | _httpResponse.Dispose(); |
| | 703 | | } |
| 0 | 704 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 705 | | } |
| | 706 | | } |
| | 707 | | // Deserialize Response |
| 2 | 708 | | if ((int)_statusCode == 401) |
| | 709 | | { |
| 0 | 710 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 711 | | try |
| | 712 | | { |
| 0 | 713 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<string>(_responseContent, Client |
| 0 | 714 | | } |
| 0 | 715 | | catch (JsonException ex) |
| | 716 | | { |
| 0 | 717 | | _httpRequest.Dispose(); |
| 0 | 718 | | if (_httpResponse != null) |
| | 719 | | { |
| 0 | 720 | | _httpResponse.Dispose(); |
| | 721 | | } |
| 0 | 722 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 723 | | } |
| | 724 | | } |
| 2 | 725 | | if (_shouldTrace) |
| | 726 | | { |
| 0 | 727 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 728 | | } |
| 2 | 729 | | return _result; |
| 2 | 730 | | } |
| | 731 | |
|
| | 732 | | } |
| | 733 | | } |