/// <summary>
 /// Create role assignment.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Authorization.IRoleAssignmentOperations.
 /// </param>
 /// <param name='scope'>
 /// Required. Scope.
 /// </param>
 /// <param name='roleAssignmentName'>
 /// Required. Role assignment name.
 /// </param>
 /// <param name='parameters'>
 /// Required. Role assignment.
 /// </param>
 /// <returns>
 /// Role assignments creation results
 /// </returns>
 public static RoleAssignmentCreateResult Create(this IRoleAssignmentOperations operations, string scope, Guid roleAssignmentName, RoleAssignmentCreateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IRoleAssignmentOperations)s).CreateAsync(scope, roleAssignmentName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// Creates new role assignment.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created role assignment object</returns>
        public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parameters)
        {
            Guid principalId = ActiveDirectoryClient.GetObjectId(parameters.ADObjectFilter);
            Guid roleAssignmentId = RoleAssignmentNames.Count == 0 ? Guid.NewGuid() : RoleAssignmentNames.Dequeue();
            string roleDefinitionId = GetRoleRoleDefinition(parameters.RoleDefinition).Id;

            RoleAssignmentCreateParameters createParameters = new RoleAssignmentCreateParameters
            {
                PrincipalId = principalId,
                RoleDefinitionId = roleDefinitionId
            };

            AuthorizationManagementClient.RoleAssignments.Create(parameters.Scope, roleAssignmentId, createParameters);
            return AuthorizationManagementClient.RoleAssignments.Get(parameters.Scope, roleAssignmentId).RoleAssignment.ToPSRoleAssignment(this, ActiveDirectoryClient);
        }
        /// <summary>
        /// Creates new role assignment.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created role assignment object</returns>
        public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parameters)
        {
            Guid principalId = ActiveDirectoryClient.GetObjectId(parameters.ADObjectFilter);
            Guid roleAssignmentId = RoleAssignmentNames.Count == 0 ? Guid.NewGuid() : RoleAssignmentNames.Dequeue();
            string roleDefinitionId = !string.IsNullOrEmpty(parameters.RoleDefinitionName)
                ? AuthorizationHelper.ConstructFullyQualifiedRoleDefinitionIdFromScopeAndIdAsGuid(parameters.Scope, GetSingleRoleDefinitionByName(parameters.RoleDefinitionName, parameters.Scope).Id)
                : AuthorizationHelper.ConstructFullyQualifiedRoleDefinitionIdFromScopeAndIdAsGuid(parameters.Scope, parameters.RoleDefinitionId);

            RoleAssignmentCreateParameters createParameters = new RoleAssignmentCreateParameters
            {
                Properties = new RoleAssignmentProperties
                {
                    PrincipalId = principalId,
                    RoleDefinitionId = roleDefinitionId
                }
            };

            RoleAssignment assignment = AuthorizationManagementClient.RoleAssignments.Create(parameters.Scope, roleAssignmentId, createParameters).RoleAssignment;

            return assignment.ToPSRoleAssignment(this, ActiveDirectoryClient);
        }
 /// <summary>
 /// Create role assignment.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Authorization.IRoleAssignmentOperations.
 /// </param>
 /// <param name='scope'>
 /// Required. Scope.
 /// </param>
 /// <param name='roleAssignmentName'>
 /// Required. Role assignment name.
 /// </param>
 /// <param name='parameters'>
 /// Required. Role assignment.
 /// </param>
 /// <returns>
 /// Role assignments creation results
 /// </returns>
 public static Task<RoleAssignmentCreateResult> CreateAsync(this IRoleAssignmentOperations operations, string scope, Guid roleAssignmentName, RoleAssignmentCreateParameters parameters)
 {
     return operations.CreateAsync(scope, roleAssignmentName, parameters, CancellationToken.None);
 }
 /// <summary>
 /// Create role assignment by Id.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Authorization.IRoleAssignmentOperations.
 /// </param>
 /// <param name='roleAssignmentId'>
 /// Required. Role assignment Id
 /// </param>
 /// <param name='parameters'>
 /// Required. Role assignment.
 /// </param>
 /// <returns>
 /// Role assignments creation results
 /// </returns>
 public static Task<RoleAssignmentCreateResult> CreateByIdAsync(this IRoleAssignmentOperations operations, string roleAssignmentId, RoleAssignmentCreateParameters parameters)
 {
     return operations.CreateByIdAsync(roleAssignmentId, parameters, CancellationToken.None);
 }
        /// <summary>
        /// Creates new role assignment.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created role assignment object</returns>
        public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parameters, string subscriptionId)
        {
            Guid principalId = ActiveDirectoryClient.GetObjectId(parameters.ADObjectFilter);
            Guid roleAssignmentId = RoleAssignmentNames.Count == 0 ? Guid.NewGuid() : RoleAssignmentNames.Dequeue();
            string roleDefinitionId = !string.IsNullOrEmpty(parameters.RoleDefinitionName)
                ? AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(subscriptionId, GetRoleRoleDefinition(parameters.RoleDefinitionName).Id)
                : AuthorizationHelper.GetRoleDefinitionFullyQualifiedId(subscriptionId, parameters.RoleDefinitionId);

            RoleAssignmentCreateParameters createParameters = new RoleAssignmentCreateParameters
            {
                Properties = new RoleAssignmentProperties
                {
                    PrincipalId = principalId,
                    RoleDefinitionId = roleDefinitionId
                }
            };

            RoleAssignment assignment = AuthorizationManagementClient.RoleAssignments.Create(parameters.Scope, roleAssignmentId, createParameters).RoleAssignment;

            IEnumerable<RoleAssignment> assignments = new List<RoleAssignment>() { assignment };

            return assignments.ToPSRoleAssignments(this, ActiveDirectoryClient).FirstOrDefault();
        }
        /// <summary>
        /// Creates new role assignment.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created role assignment object</returns>
        public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parameters)
        {
            Guid principalId = ActiveDirectoryClient.GetObjectId(parameters.ADObjectFilter);
            Guid roleAssignmentId = RoleAssignmentNames.Count == 0 ? Guid.NewGuid() : RoleAssignmentNames.Dequeue();
            string roleDefinitionId = GetRoleRoleDefinition(parameters.RoleDefinition).Id;

            RoleAssignmentCreateParameters createParameters = new RoleAssignmentCreateParameters
            {
                Properties = new RoleAssignmentProperties {
                    PrincipalId = principalId,
                    RoleDefinitionId = roleDefinitionId
                }
            };

            AuthorizationManagementClient.RoleAssignments.Create(parameters.Scope, roleAssignmentId, createParameters);

            RoleAssignment assignment = AuthorizationManagementClient.RoleAssignments.Get(parameters.Scope, roleAssignmentId).RoleAssignment;
            IEnumerable<RoleAssignment> assignments = new List<RoleAssignment>() { assignment };

            return assignments.ToPSRoleAssignments(this, ActiveDirectoryClient).FirstOrDefault();
        }