예제 #1
0
        public override async Task <Microsoft.Azure.Management.Graph.RBAC.Fluent.IRoleAssignment> CreateResourceAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            if (objectId == null)
            {
                if (userName != null)
                {
                    objectId = (await manager.Users.GetByNameAsync(userName, cancellationToken)).Id;
                }
                else if (servicePrincipalName != null)
                {
                    objectId = (await manager.ServicePrincipals.GetByNameAsync(servicePrincipalName, cancellationToken)).Id;
                }
                else
                {
                    throw new ValidationException("Please pass a non-null value for either object id, user, group, or service principal");
                }
            }

            if (roleDefinitionId == null)
            {
                if (roleName != null)
                {
                    roleDefinitionId = (await manager.RoleDefinitions.GetByScopeAndRoleNameAsync(Scope(), roleName, cancellationToken)).Id;
                }
                else
                {
                    throw new ValidationException("Please pass a non-null value for either role name or role definition ID");
                }
            }

            var propertiesInner = new RoleAssignmentPropertiesInner
            {
                PrincipalId      = objectId,
                RoleDefinitionId = roleDefinitionId
            };
            var inner = await manager.RoleInner.RoleAssignments.CreateAsync(Scope(), Name, propertiesInner, cancellationToken);

            SetInner(inner);
            return(this);
        }
예제 #2
0
        public override async Task <Microsoft.Azure.Management.Graph.RBAC.Fluent.IRoleAssignment> CreateResourceAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            if (objectId == null)
            {
                if (userName != null)
                {
                    objectId = (await manager.Users.GetByNameAsync(userName, cancellationToken)).Id;
                }
                else if (servicePrincipalName != null)
                {
                    objectId = (await manager.ServicePrincipals.GetByNameAsync(servicePrincipalName, cancellationToken)).Id;
                }
                else
                {
                    throw new ValidationException("Please pass a non-null value for either object id, user, group, or service principal");
                }
            }

            if (roleDefinitionId == null)
            {
                if (roleName != null)
                {
                    roleDefinitionId = (await manager.RoleDefinitions.GetByScopeAndRoleNameAsync(Scope(), roleName, cancellationToken)).Id;
                }
                else
                {
                    throw new ValidationException("Please pass a non-null value for either role name or role definition ID");
                }
            }

            var propertiesInner = new RoleAssignmentPropertiesInner
            {
                PrincipalId      = objectId,
                RoleDefinitionId = roleDefinitionId
            };
            int limit = 30;

            while (true)
            {
                try
                {
                    var inner = await manager.RoleInner.RoleAssignments.CreateAsync(Scope(), Name, propertiesInner, cancellationToken);

                    SetInner(inner);
                    break;
                }
                catch (CloudException e)
                {
                    if (--limit < 0)
                    {
                        throw e;
                    }
                    else if (e.Body != null && "PrincipalNotFound".Equals(e.Body.Code, StringComparison.OrdinalIgnoreCase))
                    {
                        await SdkContext.DelayProvider.DelayAsync((30 - limit) * 1000, cancellationToken);
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
            return(this);
        }
 /// <summary>
 /// Creates a role assignment by ID.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='roleAssignmentId'>
 /// The ID of the role assignment to create.
 /// </param>
 /// <param name='properties'>
 /// Role assignment properties.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <RoleAssignmentInner> CreateByIdAsync(this IRoleAssignmentsOperations operations, string roleAssignmentId, RoleAssignmentPropertiesInner properties = default(RoleAssignmentPropertiesInner), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateByIdWithHttpMessagesAsync(roleAssignmentId, properties, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }