// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 

// Warning: This code was generated by a tool.
// 
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Hyak.Common;
using Microsoft.Azure;
using Microsoft.Azure.Management.Authorization;
using Microsoft.Azure.Management.Authorization.Models;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.Management.Authorization
{
    /// <summary>
    /// TBD  (see http://TBD for more information)
    /// </summary>
    internal partial class RoleAssignmentOperations : IServiceOperations<AuthorizationManagementClient>, IRoleAssignmentOperations
    {
        /// <summary>
        /// Initializes a new instance of the RoleAssignmentOperations class.
        /// </summary>
        /// <param name='client'>
        /// Reference to the service client.
        /// </param>
        internal RoleAssignmentOperations(AuthorizationManagementClient client)
        {
            this._client = client;
        }
        
        private AuthorizationManagementClient _client;
        
        /// <summary>
        /// Gets a reference to the
        /// Microsoft.Azure.Management.Authorization.AuthorizationManagementClient.
        /// </summary>
        public AuthorizationManagementClient Client
        {
            get { return this._client; }
        }
        
        /// <summary>
        /// Create role assignment.
        /// </summary>
        /// <param name='scope'>
        /// Required. Scope.
        /// </param>
        /// <param name='roleAssignmentName'>
        /// Required. Role assignment name.
        /// </param>
        /// <param name='parameters'>
        /// Required. Role assignment.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignments creation results
        /// </returns>
        public async Task<RoleAssignmentCreateResult> CreateAsync(string scope, Guid roleAssignmentName, RoleAssignmentCreateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("scope", scope);
                tracingParameters.Add("roleAssignmentName", roleAssignmentName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + scope;
            url = url + "/providers/Microsoft.Authorization/roleAssignments/";
            url = url + Uri.EscapeDataString(roleAssignmentName.ToString());
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Put;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Serialize Request
                string requestContent = null;
                JToken requestDoc = null;
                
                JObject propertiesValue = new JObject();
                requestDoc = propertiesValue;
                
                if (parameters.Properties != null)
                {
                    JObject propertiesValue2 = new JObject();
                    propertiesValue["properties"] = propertiesValue2;
                    
                    if (parameters.Properties.RoleDefinitionId != null)
                    {
                        propertiesValue2["roleDefinitionId"] = parameters.Properties.RoleDefinitionId;
                    }
                    
                    propertiesValue2["principalId"] = parameters.Properties.PrincipalId.ToString();
                }
                
                requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentCreateResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentCreateResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RoleAssignment roleAssignmentInstance = new RoleAssignment();
                            result.RoleAssignment = roleAssignmentInstance;
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                roleAssignmentInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                Guid nameInstance = Guid.Parse(((string)nameValue));
                                roleAssignmentInstance.Name = nameInstance;
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                roleAssignmentInstance.Type = typeInstance;
                            }
                            
                            JToken propertiesValue3 = responseDoc["properties"];
                            if (propertiesValue3 != null && propertiesValue3.Type != JTokenType.Null)
                            {
                                RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                roleAssignmentInstance.Properties = propertiesInstance;
                                
                                JToken scopeValue = propertiesValue3["scope"];
                                if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                {
                                    string scopeInstance = ((string)scopeValue);
                                    propertiesInstance.Scope = scopeInstance;
                                }
                                
                                JToken roleDefinitionIdValue = propertiesValue3["roleDefinitionId"];
                                if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                {
                                    string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                    propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                }
                                
                                JToken principalIdValue = propertiesValue3["principalId"];
                                if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                {
                                    Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                    propertiesInstance.PrincipalId = principalIdInstance;
                                }
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Create role assignment by Id.
        /// </summary>
        /// <param name='roleAssignmentId'>
        /// Required. Role assignment Id
        /// </param>
        /// <param name='parameters'>
        /// Required. Role assignment.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignments creation results
        /// </returns>
        public async Task<RoleAssignmentCreateResult> CreateByIdAsync(string roleAssignmentId, RoleAssignmentCreateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (roleAssignmentId == null)
            {
                throw new ArgumentNullException("roleAssignmentId");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("roleAssignmentId", roleAssignmentId);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateByIdAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + roleAssignmentId;
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Put;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Serialize Request
                string requestContent = null;
                JToken requestDoc = null;
                
                JObject propertiesValue = new JObject();
                requestDoc = propertiesValue;
                
                if (parameters.Properties != null)
                {
                    JObject propertiesValue2 = new JObject();
                    propertiesValue["properties"] = propertiesValue2;
                    
                    if (parameters.Properties.RoleDefinitionId != null)
                    {
                        propertiesValue2["roleDefinitionId"] = parameters.Properties.RoleDefinitionId;
                    }
                    
                    propertiesValue2["principalId"] = parameters.Properties.PrincipalId.ToString();
                }
                
                requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentCreateResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentCreateResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RoleAssignment roleAssignmentInstance = new RoleAssignment();
                            result.RoleAssignment = roleAssignmentInstance;
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                roleAssignmentInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                Guid nameInstance = Guid.Parse(((string)nameValue));
                                roleAssignmentInstance.Name = nameInstance;
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                roleAssignmentInstance.Type = typeInstance;
                            }
                            
                            JToken propertiesValue3 = responseDoc["properties"];
                            if (propertiesValue3 != null && propertiesValue3.Type != JTokenType.Null)
                            {
                                RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                roleAssignmentInstance.Properties = propertiesInstance;
                                
                                JToken scopeValue = propertiesValue3["scope"];
                                if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                {
                                    string scopeInstance = ((string)scopeValue);
                                    propertiesInstance.Scope = scopeInstance;
                                }
                                
                                JToken roleDefinitionIdValue = propertiesValue3["roleDefinitionId"];
                                if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                {
                                    string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                    propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                }
                                
                                JToken principalIdValue = propertiesValue3["principalId"];
                                if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                {
                                    Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                    propertiesInstance.PrincipalId = principalIdInstance;
                                }
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Delete role assignment.
        /// </summary>
        /// <param name='scope'>
        /// Required. Scope.
        /// </param>
        /// <param name='roleAssignmentName'>
        /// Required. Role assignment name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignments delete result
        /// </returns>
        public async Task<RoleAssignmentDeleteResult> DeleteAsync(string scope, Guid roleAssignmentName, CancellationToken cancellationToken)
        {
            // Validate
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("scope", scope);
                tracingParameters.Add("roleAssignmentName", roleAssignmentName);
                TracingAdapter.Enter(invocationId, this, "DeleteAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + scope;
            url = url + "/providers/Microsoft.Authorization/roleAssignments/";
            url = url + Uri.EscapeDataString(roleAssignmentName.ToString());
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Delete;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentDeleteResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentDeleteResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RoleAssignment roleAssignmentInstance = new RoleAssignment();
                            result.RoleAssignment = roleAssignmentInstance;
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                roleAssignmentInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                Guid nameInstance = Guid.Parse(((string)nameValue));
                                roleAssignmentInstance.Name = nameInstance;
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                roleAssignmentInstance.Type = typeInstance;
                            }
                            
                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                roleAssignmentInstance.Properties = propertiesInstance;
                                
                                JToken scopeValue = propertiesValue["scope"];
                                if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                {
                                    string scopeInstance = ((string)scopeValue);
                                    propertiesInstance.Scope = scopeInstance;
                                }
                                
                                JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                {
                                    string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                    propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                }
                                
                                JToken principalIdValue = propertiesValue["principalId"];
                                if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                {
                                    Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                    propertiesInstance.PrincipalId = principalIdInstance;
                                }
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Delete role assignment.
        /// </summary>
        /// <param name='roleAssignmentId'>
        /// Required. Role assignment Id
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignments delete result
        /// </returns>
        public async Task<RoleAssignmentDeleteResult> DeleteByIdAsync(string roleAssignmentId, CancellationToken cancellationToken)
        {
            // Validate
            if (roleAssignmentId == null)
            {
                throw new ArgumentNullException("roleAssignmentId");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("roleAssignmentId", roleAssignmentId);
                TracingAdapter.Enter(invocationId, this, "DeleteByIdAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + roleAssignmentId;
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Delete;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentDeleteResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentDeleteResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RoleAssignment roleAssignmentInstance = new RoleAssignment();
                            result.RoleAssignment = roleAssignmentInstance;
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                roleAssignmentInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                Guid nameInstance = Guid.Parse(((string)nameValue));
                                roleAssignmentInstance.Name = nameInstance;
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                roleAssignmentInstance.Type = typeInstance;
                            }
                            
                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                roleAssignmentInstance.Properties = propertiesInstance;
                                
                                JToken scopeValue = propertiesValue["scope"];
                                if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                {
                                    string scopeInstance = ((string)scopeValue);
                                    propertiesInstance.Scope = scopeInstance;
                                }
                                
                                JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                {
                                    string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                    propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                }
                                
                                JToken principalIdValue = propertiesValue["principalId"];
                                if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                {
                                    Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                    propertiesInstance.PrincipalId = principalIdInstance;
                                }
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Get single role assignment.
        /// </summary>
        /// <param name='scope'>
        /// Required. Scope.
        /// </param>
        /// <param name='roleAssignmentName'>
        /// Required. Role assignment name.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment get operation result.
        /// </returns>
        public async Task<RoleAssignmentGetResult> GetAsync(string scope, Guid roleAssignmentName, CancellationToken cancellationToken)
        {
            // Validate
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("scope", scope);
                tracingParameters.Add("roleAssignmentName", roleAssignmentName);
                TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + scope;
            url = url + "/providers/Microsoft.Authorization/roleAssignments/";
            url = url + Uri.EscapeDataString(roleAssignmentName.ToString());
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentGetResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentGetResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RoleAssignment roleAssignmentInstance = new RoleAssignment();
                            result.RoleAssignment = roleAssignmentInstance;
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                roleAssignmentInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                Guid nameInstance = Guid.Parse(((string)nameValue));
                                roleAssignmentInstance.Name = nameInstance;
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                roleAssignmentInstance.Type = typeInstance;
                            }
                            
                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                roleAssignmentInstance.Properties = propertiesInstance;
                                
                                JToken scopeValue = propertiesValue["scope"];
                                if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                {
                                    string scopeInstance = ((string)scopeValue);
                                    propertiesInstance.Scope = scopeInstance;
                                }
                                
                                JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                {
                                    string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                    propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                }
                                
                                JToken principalIdValue = propertiesValue["principalId"];
                                if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                {
                                    Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                    propertiesInstance.PrincipalId = principalIdInstance;
                                }
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Get single role assignment.
        /// </summary>
        /// <param name='roleAssignmentId'>
        /// Required. Role assignment Id
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment get operation result.
        /// </returns>
        public async Task<RoleAssignmentGetResult> GetByIdAsync(string roleAssignmentId, CancellationToken cancellationToken)
        {
            // Validate
            if (roleAssignmentId == null)
            {
                throw new ArgumentNullException("roleAssignmentId");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("roleAssignmentId", roleAssignmentId);
                TracingAdapter.Enter(invocationId, this, "GetByIdAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + roleAssignmentId;
            List<string> queryParameters = new List<string>();
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentGetResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentGetResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RoleAssignment roleAssignmentInstance = new RoleAssignment();
                            result.RoleAssignment = roleAssignmentInstance;
                            
                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                roleAssignmentInstance.Id = idInstance;
                            }
                            
                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                Guid nameInstance = Guid.Parse(((string)nameValue));
                                roleAssignmentInstance.Name = nameInstance;
                            }
                            
                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                roleAssignmentInstance.Type = typeInstance;
                            }
                            
                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                roleAssignmentInstance.Properties = propertiesInstance;
                                
                                JToken scopeValue = propertiesValue["scope"];
                                if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                {
                                    string scopeInstance = ((string)scopeValue);
                                    propertiesInstance.Scope = scopeInstance;
                                }
                                
                                JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                {
                                    string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                    propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                }
                                
                                JToken principalIdValue = propertiesValue["principalId"];
                                if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                {
                                    Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                    propertiesInstance.PrincipalId = principalIdInstance;
                                }
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the subscription.
        /// </summary>
        /// <param name='parameters'>
        /// Optional. List operation filters. If null will return all role
        /// assignments at, above or below the subscription.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListAsync(ListAssignmentsFilterParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/providers/Microsoft.Authorization/roleAssignments";
            List<string> queryParameters = new List<string>();
            List<string> odataFilter = new List<string>();
            if (parameters != null && parameters.AtScope == true)
            {
                odataFilter.Add("atScope()");
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            List<string> odataFilter2 = new List<string>();
            if (parameters != null && parameters.PrincipalId != null)
            {
                odataFilter2.Add("principalId eq '" + Uri.EscapeDataString(parameters.PrincipalId.Value.ToString()) + "'");
            }
            if (odataFilter2.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter2));
            }
            List<string> odataFilter3 = new List<string>();
            if (parameters != null && parameters.AssignedToPrincipalId != null)
            {
                odataFilter3.Add("assignedTo('" + Uri.EscapeDataString(parameters.AssignedToPrincipalId.Value.ToString()) + "')");
            }
            if (odataFilter3.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter3));
            }
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the resource.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group.
        /// </param>
        /// <param name='identity'>
        /// Required. Resource identity.
        /// </param>
        /// <param name='parameters'>
        /// Optional. List operation filters. If null will return all role
        /// assignments at, above or below the resource.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListForResourceAsync(string resourceGroupName, ResourceIdentity identity, ListAssignmentsFilterParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (identity == null)
            {
                throw new ArgumentNullException("identity");
            }
            if (identity.ResourceName == null)
            {
                throw new ArgumentNullException("identity.");
            }
            if (identity.ResourceProviderNamespace == null)
            {
                throw new ArgumentNullException("identity.");
            }
            if (identity.ResourceType == null)
            {
                throw new ArgumentNullException("identity.");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("identity", identity);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "ListForResourceAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourcegroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            url = url + Uri.EscapeDataString(identity.ResourceProviderNamespace);
            url = url + "/";
            if (identity.ParentResourcePath != null)
            {
                url = url + identity.ParentResourcePath;
            }
            url = url + "/";
            url = url + identity.ResourceType;
            url = url + "/";
            url = url + Uri.EscapeDataString(identity.ResourceName);
            url = url + "providers/Microsoft.Authorization/roleAssignments";
            List<string> queryParameters = new List<string>();
            List<string> odataFilter = new List<string>();
            if (parameters != null && parameters.AtScope == true)
            {
                odataFilter.Add("atScope()");
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            List<string> odataFilter2 = new List<string>();
            if (parameters != null && parameters.PrincipalId != null)
            {
                odataFilter2.Add("principalId eq '" + Uri.EscapeDataString(parameters.PrincipalId.Value.ToString()) + "'");
            }
            if (odataFilter2.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter2));
            }
            List<string> odataFilter3 = new List<string>();
            if (parameters != null && parameters.AssignedToPrincipalId != null)
            {
                odataFilter3.Add("assignedTo('" + Uri.EscapeDataString(parameters.AssignedToPrincipalId.Value.ToString()) + "')");
            }
            if (odataFilter3.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter3));
            }
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the resource group.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Optional. Resource group name.
        /// </param>
        /// <param name='parameters'>
        /// Optional. List operation filters. If null will return all role
        /// assignments at, above or below the resource group.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListForResourceGroupAsync(string resourceGroupName, ListAssignmentsFilterParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "ListForResourceGroupAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            if (resourceGroupName != null)
            {
                url = url + Uri.EscapeDataString(resourceGroupName);
            }
            url = url + "/providers/Microsoft.Authorization/roleAssignments";
            List<string> queryParameters = new List<string>();
            List<string> odataFilter = new List<string>();
            if (parameters != null && parameters.AtScope == true)
            {
                odataFilter.Add("atScope()");
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            List<string> odataFilter2 = new List<string>();
            if (parameters != null && parameters.PrincipalId != null)
            {
                odataFilter2.Add("principalId eq '" + Uri.EscapeDataString(parameters.PrincipalId.Value.ToString()) + "'");
            }
            if (odataFilter2.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter2));
            }
            List<string> odataFilter3 = new List<string>();
            if (parameters != null && parameters.AssignedToPrincipalId != null)
            {
                odataFilter3.Add("assignedTo('" + Uri.EscapeDataString(parameters.AssignedToPrincipalId.Value.ToString()) + "')");
            }
            if (odataFilter3.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter3));
            }
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the resource group.
        /// </summary>
        /// <param name='nextLink'>
        /// Required. NextLink from the previous successful call to List
        /// operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListForResourceGroupNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("nextLink", nextLink);
                TracingAdapter.Enter(invocationId, this, "ListForResourceGroupNextAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + nextLink;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the resource.
        /// </summary>
        /// <param name='nextLink'>
        /// Required. NextLink from the previous successful call to List
        /// operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListForResourceNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("nextLink", nextLink);
                TracingAdapter.Enter(invocationId, this, "ListForResourceNextAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + nextLink;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the scope.
        /// </summary>
        /// <param name='scope'>
        /// Required. Scope.
        /// </param>
        /// <param name='parameters'>
        /// Optional. List operation filters. If null will return all role
        /// assignments at, above or below the subscription.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListForScopeAsync(string scope, ListAssignmentsFilterParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("scope", scope);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "ListForScopeAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + scope;
            url = url + "/providers/Microsoft.Authorization/roleAssignments";
            List<string> queryParameters = new List<string>();
            List<string> odataFilter = new List<string>();
            if (parameters != null && parameters.AtScope == true)
            {
                odataFilter.Add("atScope()");
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            List<string> odataFilter2 = new List<string>();
            if (parameters != null && parameters.PrincipalId != null)
            {
                odataFilter2.Add("principalId eq '" + Uri.EscapeDataString(parameters.PrincipalId.Value.ToString()) + "'");
            }
            if (odataFilter2.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter2));
            }
            List<string> odataFilter3 = new List<string>();
            if (parameters != null && parameters.AssignedToPrincipalId != null)
            {
                odataFilter3.Add("assignedTo('" + Uri.EscapeDataString(parameters.AssignedToPrincipalId.Value.ToString()) + "')");
            }
            if (odataFilter3.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter3));
            }
            queryParameters.Add("api-version=2015-07-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the scope.
        /// </summary>
        /// <param name='nextLink'>
        /// Required. NextLink from the previous successful call to List
        /// operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListForScopeNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("nextLink", nextLink);
                TracingAdapter.Enter(invocationId, this, "ListForScopeNextAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + nextLink;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        
        /// <summary>
        /// Gets role assignments of the subscription.
        /// </summary>
        /// <param name='nextLink'>
        /// Required. NextLink from the previous successful call to List
        /// operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Role assignment list operation result.
        /// </returns>
        public async Task<RoleAssignmentListResult> ListNextAsync(string nextLink, CancellationToken cancellationToken)
        {
            // Validate
            if (nextLink == null)
            {
                throw new ArgumentNullException("nextLink");
            }
            
            // Tracing
            bool shouldTrace = TracingAdapter.IsEnabled;
            string invocationId = null;
            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
                tracingParameters.Add("nextLink", nextLink);
                TracingAdapter.Enter(invocationId, this, "ListNextAsync", tracingParameters);
            }
            
            // Construct URL
            string url = "";
            url = url + nextLink;
            url = url.Replace(" ", "%20");
            
            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest = new HttpRequestMessage();
                httpRequest.Method = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);
                
                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");
                
                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                
                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }
                    
                    // Create Result
                    RoleAssignmentListResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                        result = new RoleAssignmentListResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }
                        
                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RoleAssignment roleAssignmentInstance = new RoleAssignment();
                                    result.RoleAssignments.Add(roleAssignmentInstance);
                                    
                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        roleAssignmentInstance.Id = idInstance;
                                    }
                                    
                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        Guid nameInstance = Guid.Parse(((string)nameValue));
                                        roleAssignmentInstance.Name = nameInstance;
                                    }
                                    
                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        roleAssignmentInstance.Type = typeInstance;
                                    }
                                    
                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        RoleAssignmentPropertiesWithScope propertiesInstance = new RoleAssignmentPropertiesWithScope();
                                        roleAssignmentInstance.Properties = propertiesInstance;
                                        
                                        JToken scopeValue = propertiesValue["scope"];
                                        if (scopeValue != null && scopeValue.Type != JTokenType.Null)
                                        {
                                            string scopeInstance = ((string)scopeValue);
                                            propertiesInstance.Scope = scopeInstance;
                                        }
                                        
                                        JToken roleDefinitionIdValue = propertiesValue["roleDefinitionId"];
                                        if (roleDefinitionIdValue != null && roleDefinitionIdValue.Type != JTokenType.Null)
                                        {
                                            string roleDefinitionIdInstance = ((string)roleDefinitionIdValue);
                                            propertiesInstance.RoleDefinitionId = roleDefinitionIdInstance;
                                        }
                                        
                                        JToken principalIdValue = propertiesValue["principalId"];
                                        if (principalIdValue != null && principalIdValue.Type != JTokenType.Null)
                                        {
                                            Guid principalIdInstance = Guid.Parse(((string)principalIdValue));
                                            propertiesInstance.PrincipalId = principalIdInstance;
                                        }
                                    }
                                }
                            }
                            
                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                result.NextLink = nextLinkInstance;
                            }
                        }
                        
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    
                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return result;
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
    }
}
